def setUp(self): self.client = APIClient() self.user = sample_user(email='*****@*****.**') self.user_2 = sample_user(email='*****@*****.**') sample_ad_with_images(user=self.user, title='Test_1') sample_ad_with_images(user=self.user_2, title='Test_2') sample_ad_with_images(user=self.user, title='Test_3')
def test_message_str(self): """Test the message string representation""" user1 = sample_user(email='*****@*****.**') user2 = sample_user(email='*****@*****.**') advert = sample_ad(user=user1) conversation = sample_conversation(user1, user2, advert) msg = models.Message.objects.create(sender=user1, conversation=conversation, content='abcd') self.assertEqual(str(msg), f'MSG#{msg.pk}')
def test_check_advert_ownership_fails(self): """Test that advert doesn't belong to the authenticated user""" user_2 = sample_user() advert = sample_ad(user=user_2) res = self.client.head(advert_owner_url(advert.id)) self.assertEqual(res.status_code, status.HTTP_403_FORBIDDEN)
def test_retrieve_conversation_only_own(self): """Test that user can't retrieve conversation of other users""" user_1 = sample_user(email='*****@*****.**') user_2 = sample_user(email='*****@*****.**') ad = sample_ad(user_1) conversation = sample_conversation(user_1, user_2, ad) sample_message(user_1, conversation) sample_message(user_2, conversation) sample_message(user_1, conversation) res = self.client.get(conversation_detail_url(conversation.id)) self.assertEqual(res.status_code, status.HTTP_404_NOT_FOUND) user_2_client = APIClient() user_2_client.force_authenticate(user_2) res_user_2 = user_2_client.get(conversation_detail_url( conversation.id)) self.assertEqual(res_user_2.status_code, status.HTTP_200_OK)
def test_message_encrypted(self): """Test that message content is stored encrypted in the DB""" msg_content = 'Nie oddam Panu Opla' user1 = sample_user(email='*****@*****.**') user2 = sample_user(email='*****@*****.**') advert = sample_ad(user=user1) conversation = sample_conversation(user1, user2, advert) msg = models.Message.objects.create(sender=user1, conversation=conversation, content=msg_content) # raw query to extract msg content from a given message msg_encrypted_db = retrieve_msg_content(msg.id) self.assertFalse(isinstance(msg_encrypted_db, str)) self.assertNotEqual(msg_encrypted_db.tobytes(), msg.content) msg_decrypted_db = models.Message.objects.get(pk=msg.id) self.assertEqual(msg_decrypted_db.content, msg.content)
def test_list_owned_ads(self): """Test listing ads owned by user is succesful""" user_2 = sample_user() sample_ad(user=self.user) sample_ad(user=self.user) sample_ad(user=user_2) res = self.client.get(OWNED_ADVERTS_URL) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data), 2)
def test_new_message_user_not_part_of_conversation(self): """Test that user can't send a message to other user's conversation""" user_3 = sample_user(email='*****@*****.**') advert = sample_ad(user_3) conversation = sample_conversation(user_3, self.user_2, advert) payload = {'conversation': conversation.id, 'content': 'valid message'} res = self.client.post(MESSAGE_URL, payload) self.assertEqual(res.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(res.data['conversation'][0].code, 'message_forbidden_conversation')
def test_list_conversations_only_own(self): """ Test that listing conversations returns only ones where user is involved and not conversations of other users. """ user_1 = sample_user(email='*****@*****.**') user_2 = sample_user(email='*****@*****.**') ad = sample_ad(user_1) conversation = sample_conversation(user_1, user_2, ad) sample_message(user_1, conversation) sample_message(user_2, conversation) sample_message(user_1, conversation) res = self.client.get(CONVERSATIONS_URL) user_conversations = Conversation.objects.filter( Q(user_1=self.user) | Q(user_2=self.user)) serializer = ConversationsSerializer(user_conversations, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data), 1) self.assertEqual(res.data, serializer.data)
def test_list_owned_ads_ordered(self): """Test listing owned ads returns them ordered by refreshed date""" user_2 = sample_user() sample_ad(user=self.user) sample_ad(user=self.user) sample_ad(user=user_2) res = self.client.get(OWNED_ADVERTS_URL) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data), 2) newest = parse_datetime(res.data[0]['date_refreshed']) oldest = parse_datetime(res.data[1]['date_refreshed']) self.assertGreater(newest, oldest)
def setUp(self): self.client = APIClient() self.user = get_user_model().objects.create_user( email='*****@*****.**', password='******', name='Conversation Starter') self.client.force_authenticate(self.user) user_2 = sample_user(email='*****@*****.**') ad = sample_ad(self.user) self.conversation = sample_conversation(self.user, user_2, ad) self.messages = [ sample_message(self.user, self.conversation), sample_message(user_2, self.conversation), sample_message(self.user, self.conversation) ]
def test_list_conversations_sorted_by_newest(self): """ Test that listed conversations returns list that is sorted by newest conversations first """ user_1 = sample_user(email='*****@*****.**') ad = sample_ad(user_1) conversation = sample_conversation(user_1, self.user, ad) sample_message(user_1, conversation) sample_message(self.user, conversation) sample_message(user_1, conversation) res = self.client.get(CONVERSATIONS_URL) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data), 2) newer_date = parse_datetime(res.data[0]['last_updated']) older_date = parse_datetime(res.data[1]['last_updated']) self.assertGreater(newer_date, older_date)
def test_list_conversations_owner_and_customer(self): """ Test that listing conversations returns all conversations where user performs the role of an ad-owner and customer """ user_1 = sample_user(email='*****@*****.**') ad = sample_ad(user_1) conversation = sample_conversation(user_1, self.user, ad) sample_message(user_1, conversation) sample_message(self.user, conversation) sample_message(user_1, conversation) res = self.client.get(CONVERSATIONS_URL) user_conversations = Conversation.objects \ .filter(Q(user_1=self.user) | Q(user_2=self.user)) \ .order_by('-last_updated') serializer = ConversationsSerializer(user_conversations, many=True) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(len(res.data), 2) self.assertEqual(res.data, serializer.data)