def setUp(self): self.client = APIClient() self.location_1 = sample_location(name='New York') self.location_2 = sample_location(name='Chicago') self.location_3 = sample_location(name='New Orlean') sample_ad_with_images(title='Test_1', location=self.location_1) sample_ad_with_images(title='Test_2', location=self.location_2) sample_ad_with_images(title='Test_3', location=self.location_3)
def test_list_locations_format(self): """Test listing all locations returns id, name and url""" sample_location(name='TestLocation_1') sample_location(name='TestLocation_2') res = self.client.get(LOCATIONS_URL) self.assertEqual(res.status_code, status.HTTP_200_OK) for result in res.data: self.assertIn('id', result) self.assertIn('name', result) self.assertIn('url', result)
def test_create_advert_with_images(self): """Test creating advert with images attached""" category = sample_category() location = sample_location() image_1 = upload_valid_test_image(self.client).data image_2 = upload_valid_test_image(self.client).data payload = { 'title': 'AdTitle', 'category': category.id, 'price': 10.50, 'phone': '+447701900774', 'location': location.id, 'content': 'Example Advert Content', 'images': [image_1['id'], image_2['id']] } res = self.client.post(ADVERTS_URL, payload) self.assertEqual(res.status_code, status.HTTP_201_CREATED) new_advert = Advert.objects.get(id=res.data['id']) new_advert_images = AdvertImage.objects.filter( advertimagelink__advert=new_advert ) payload_images = payload.pop('images') payload['category'] = category payload['location'] = location for key in payload.keys(): self.assertEqual(payload[key], getattr(new_advert, key)) for advert_image in new_advert_images: self.assertIn(advert_image.id, payload_images)
def test_create_new_advert(self): """Test creating basic Advert""" category = sample_category() location = sample_location() payload = { 'title': 'AdTitle', 'category': category.id, 'price': 10.50, 'phone': '+447701900774', 'location': location.id, 'content': 'Example Advert Content', } res = self.client.post(ADVERTS_URL, payload) self.assertEqual(res.status_code, status.HTTP_201_CREATED) new_advert = Advert.objects.get(id=res.data['id']) payload['category'] = category payload['location'] = location for key in payload.keys(): self.assertEqual(payload[key], getattr(new_advert, key)) new_advert.title = 'Changed Title' new_advert.save()
def test_create_disallowed(self): """Test that unauthenticated users cant make new ads""" category = sample_category() location = sample_location() payload = { 'title': 'AdTitle', 'category': category.id, 'price': 10.50, 'location': location.id, 'content': 'Example Advert Content', } res = self.client.post(ADVERTS_URL, payload) self.assertEqual(res.status_code, status.HTTP_401_UNAUTHORIZED)
def test_list_all_locations(self): """Test listing all available locations""" sample_location(name='TestLocation_1') sample_location(name='TestLocation_2') res = self.client.get(LOCATIONS_URL) self.assertEqual(res.status_code, status.HTTP_200_OK)