def test_list_offers_by_status(self): self.client.force_login(user=self.user) for _ in range(4): OfferFactory(user=self.user, group=self.group, images=[image_path], status='active') for _ in range(2): OfferFactory(user=self.user, group=self.group, images=[image_path], status='accepted') OfferFactory(user=self.user, group=self.group, images=[image_path], status='archived') response = self.client.get('/api/offers/', {'status': 'active'}) self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) self.assertEqual(len(response.data['results']), 4) response = self.client.get('/api/offers/', {'status': 'accepted'}) self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) self.assertEqual(len(response.data['results']), 2) response = self.client.get('/api/offers/', {'status': 'archived'}) self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) self.assertEqual(len(response.data['results']), 1)
def test_cannot_list_another_users_archived_offers(self): for _ in range(2): OfferFactory(user=self.user, group=self.group, images=[image_path], status='active') for _ in range(3): OfferFactory(user=self.user, group=self.group, images=[image_path], status='archived') self.client.force_login(user=self.another_user) response = self.client.get('/api/offers/') self.assertEqual(len(response.data['results']), 2)
def test_cannot_list_offers_for_another_group(self): self.client.force_login(user=self.user) another_group = GroupFactory() # user is not part of this group for _ in range(2): OfferFactory(user=self.user, group=self.group, images=[image_path]) for _ in range(3): OfferFactory(user=self.user, group=another_group, images=[image_path]) response = self.client.get('/api/offers/') self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) self.assertEqual(len(response.data['results']), 2) response = self.client.get('/api/offers/', {'group': another_group.id}) self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) self.assertEqual(len(response.data['results']), 0)
def setUp(self): self.user = UserFactory() self.another_user = UserFactory() self.group = GroupFactory(members=[self.user, self.another_user]) self.offer = OfferFactory(user=self.user, group=self.group, images=[image_path])
def test_list_conversations_with_related_data_efficiently(self): user = UserFactory() group = GroupFactory(members=[user]) place = PlaceFactory(group=group) pickup = PickupDateFactory(place=place) application = ApplicationFactory(user=UserFactory(), group=group) issue = IssueFactory(group=group) offer = OfferFactory(group=group) conversations = [ t.conversation for t in (group, pickup, application, issue, offer) ] [c.sync_users([user]) for c in conversations] [c.messages.create(content='hey', author=user) for c in conversations] ConversationMeta.objects.get_or_create(user=user) self.client.force_login(user=user) with self.assertNumQueries(15): response = self.client.get('/api/conversations/', {'group': group.id}, format='json') results = response.data['results'] self.assertEqual(len(results['conversations']), len(conversations)) self.assertEqual(results['pickups'][0]['id'], pickup.id) self.assertEqual(results['applications'][0]['id'], application.id) self.assertEqual(results['issues'][0]['id'], issue.id) self.assertEqual(results['offers'][0]['id'], offer.id)
def test_offer_image_url(self): offer = OfferFactory(images=[image_path]) url = offer_image_url(offer) self.assertEqual( url, '{hostname}/api/offers/{id}/image/'.format( hostname=settings.HOSTNAME, id=offer.id, ))
def test_update_offer_as_another_user(self): offer = OfferFactory(user=self.user, group=self.group, images=[image_path]) self.client.force_login(user=self.another_user) data = { 'name': faker.name(), } response = self.client.patch('/api/offers/{}/'.format(offer.id), data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN, response.data)
def test_cannot_fetch_another_users_archived_offer(self): offer = OfferFactory(user=self.user, group=self.group, images=[image_path], status='archived') self.client.force_login(user=self.another_user) response = self.client.get('/api/offers/{}/'.format(offer.id)) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND, response.data)
def test_can_fetch_other_users_archived_offer_if_in_the_conversation(self): offer = OfferFactory(user=self.user, group=self.group, images=[image_path], status='archived') offer.conversation.join(self.another_user) self.client.force_login(user=self.another_user) response = self.client.get('/api/offers/{}/'.format(offer.id)) self.assertEqual(response.status_code, status.HTTP_200_OK, response.data)
def test_does_not_notify_user_about_own_offer(self): mail.outbox = [] with execute_scheduled_tasks_immediately(): with transaction.atomic(): self.offer = OfferFactory(group=self.group, user=self.user, images=[image_path]) email_addresses = [address for m in mail.outbox for address in m.to] self.assertEqual(len(mail.outbox), 1) self.assertEqual(email_addresses, [self.other_user.email])
def get_or_create_offer(): offer = Offer.objects.order_by('?').first() if offer is None: user = VerifiedUserFactory() group = random_group() image_path = os.path.join(os.path.dirname(__file__), './offer.jpg') offer = OfferFactory(group=group, user=user, images=[image_path]) return offer
def test_remove_all_images(self): offer = OfferFactory(user=self.user, group=self.group, images=[image_path, image_path]) self.client.force_login(user=self.user) data = { 'images': [{ 'id': image.id, '_removed': True, } for image in offer.images.all()], } response = self.client.patch('/api/offers/{}/'.format(offer.id), encode_offer_data(data), format='multipart') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.data)
def test_remove_image(self): offer = OfferFactory(user=self.user, group=self.group, images=[image_path, image_path]) self.client.force_login(user=self.user) data = { 'images': [{ 'id': offer.images.first().id, '_removed': True }], } response = self.client.patch('/api/offers/{}/'.format(offer.id), encode_offer_data(data), format='multipart') self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) self.assertEqual(len(response.data['images']), 1)
class OfferReceiverTests(WSTestCase): def setUp(self): super().setUp() self.member = UserFactory() self.other_member = UserFactory() self.group = GroupFactory(members=[self.member, self.other_member]) self.offer = OfferFactory(group=self.group, user=self.member) def test_receive_offer_changes(self): client = self.connect_as(self.member) self.offer.name = faker.name() self.offer.save() response = client.messages_by_topic.get('offers:offer')[0] self.assertEqual(response['payload']['name'], self.offer.name) self.assertEqual(len(client.messages), 1) def test_receiver_offer_deleted(self): client = self.connect_as(self.member) id = self.offer.id self.offer.delete() response = client.messages_by_topic.get('offers:offer_deleted')[0] self.assertEqual(response['payload']['id'], id) self.assertEqual(len(client.messages), 1) def test_receiver_offer_deleted_for_other_user_when_archived(self): client = self.connect_as(self.other_member) id = self.offer.id self.offer.archive() response = client.messages_by_topic.get('offers:offer_deleted')[0] self.assertEqual(response['payload']['id'], id) self.assertEqual(len(client.messages), 1) def test_receiver_offer_updated_for_other_user_when_archived_if_in_conversation( self): client = self.connect_as(self.other_member) self.offer.conversation.join(self.other_member) client.reset_messages( ) # otherwise we have various conversation related messages id = self.offer.id self.offer.archive() response = client.messages_by_topic.get('offers:offer')[0] self.assertEqual(response['payload']['id'], id) self.assertEqual(len(client.messages), 1)
def test_reposition_image(self): offer = OfferFactory(user=self.user, group=self.group, images=[image_path, image_path]) self.client.force_login(user=self.user) image_id = offer.images.first().id new_position = 5 data = { 'images': [{ 'id': image_id, 'position': new_position }], } response = self.client.patch('/api/offers/{}/'.format(offer.id), data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) by_id = {image['id']: image for image in response.data['images']} self.assertEqual(by_id[image_id]['position'], new_position)
def test_add_image(self): offer = OfferFactory(user=self.user, group=self.group, images=[image_path]) self.client.force_login(user=self.user) with open(image_path, 'rb') as image_file: data = { 'images': [{ 'position': 1, 'image': image_file }], } response = self.client.patch( '/api/offers/{}/'.format(offer.id), encode_offer_data(data), format='multipart' ) self.assertEqual(response.status_code, status.HTTP_200_OK, response.data) self.assertEqual(len(response.data['images']), 2)
def test_get_conversation_status_efficiently(self): user = UserFactory() group = GroupFactory(members=[user]) place = PlaceFactory(group=group) activity = ActivityFactory(place=place) application = ApplicationFactory(user=UserFactory(), group=group) issue = IssueFactory(group=group) offer = OfferFactory(group=group) conversations = [ t.conversation for t in (group, activity, application, issue, offer) ] another_user = UserFactory() [c.sync_users([user, another_user]) for c in conversations] [ c.messages.create(content='hey', author=another_user) for c in conversations ] with self.assertNumQueries(2): unread_conversations(user)
def test_offer_image_url_without_images(self): offer = OfferFactory() url = offer_image_url(offer) self.assertEqual(url, None)
def setUp(self): super().setUp() self.member = UserFactory() self.other_member = UserFactory() self.group = GroupFactory(members=[self.member, self.other_member]) self.offer = OfferFactory(group=self.group, user=self.member)