def test_list_all_categories(self): """Test listing all available categories""" sample_category(name='TestCategory_1') sample_category(name='TestCategory_2') res = self.client.get(CATEGORIES_URL) self.assertEqual(res.status_code, status.HTTP_200_OK)
def test_list_categories_format(self): """Test listed categories should have id,name and url""" sample_category(name='TestCategory_1') res = self.client.get(CATEGORIES_URL) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertIn('id', res.data[0]) self.assertIn('name', res.data[0]) self.assertIn('url', res.data[0])
def setUp(self): self.client = APIClient() self.category_1 = sample_category(name='Test_Category_1') self.category_2 = sample_category(name='Test_Category_2') self.category_3 = sample_category( name='Test_Category_3', parent=self.category_2 ) self.category_4 = sample_category( name='Test_Category_4', parent=self.category_3 ) sample_ad_with_images(title='Test_1', category=self.category_1) sample_ad_with_images(title='Test_2', category=self.category_2) sample_ad_with_images(title='Test_3', category=self.category_3) sample_ad_with_images(title='Test_3', category=self.category_4)
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 sample_category_tree(): """Create a category tree with nested categories""" cat_top_1 = sample_category(name='TestCategory_1') cat_top_2 = sample_category(name='TestCategory_2') cat_mid_1_1 = sample_category(name='TestCategory_1_1', parent=cat_top_1) sample_category(name='TestCategory_1_2', parent=cat_top_1) sample_category(name='TestCategory_1_3', parent=cat_top_1) cat_mid_2_1 = sample_category(name='TestCategory_2_1', parent=cat_top_2) sample_category(name='TestCategory_1_1_1', parent=cat_mid_1_1) sample_category(name='TestCategory_2_1_1', parent=cat_mid_2_1)