Esempio n. 1
0
class CategoryAPITests(APITestCase):
    def setUp(self):
        self.category = CategoryFactory()
        self.user = UserFactory()
        self.client = APIClient()

    def test_can_access_category_if_authenticated(self):
        self.client.force_login(self.user)
        self.client.force_authenticate(user=self.user)

        response = self.client.get(reverse_lazy('offer:category-list-api'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, self.category.name)

    def test_can_access_specific_category_if_authenticated(self):
        self.client.force_login(self.user)
        self.client.force_authenticate(user=self.user)

        response = self.client.get(
            reverse_lazy('offer:category-detail-api',
                         kwargs={'pk': self.category.pk}))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, self.category.name)

    def test_can_edit_specific_category_if_authenticated(self):
        self.client.force_login(self.user)
        self.client.force_authenticate(user=self.user)
        name = "New name"

        response = self.client.put(reverse_lazy(
            'offer:category-detail-api', kwargs={'pk': self.category.pk}),
                                   data={'name': name})
        self.category.refresh_from_db()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(self.category.name, name)

    def test_can_delete_category_if_authenticated(self):
        self.assertEqual(Category.objects.count(), 1)
        self.client.force_login(self.user)
        self.client.force_authenticate(user=self.user)

        response = self.client.delete(
            reverse_lazy('offer:category-detail-api',
                         kwargs={'pk': self.category.pk}))
        self.assertEqual(response.status_code, 204)
        self.assertEqual(Category.objects.count(), 0)

    def test_can_post_category_if_authenticated(self):
        self.assertEqual(Category.objects.count(), 1)
        self.client.force_login(self.user)
        self.client.force_authenticate(user=self.user)
        data = {'name': 'New'}

        response = self.client.post(reverse_lazy('offer:category-list-api'),
                                    data=data)
        self.assertEqual(response.status_code, 201)
        self.assertEqual(Category.objects.count(), 2)

    def tearDown(self):
        self.client.logout()
Esempio n. 2
0
 def setUp(self):
     self.category = CategoryFactory()
     self.user = UserFactory()
     self.offer = OfferFactory(category=self.category, author=self.user)
     self.user2 = UserFactory()
     self.url = reverse('offer:offer-update', kwargs={'pk': self.offer.pk})
     self.client = Client()
Esempio n. 3
0
 def setUp(self):
     self.category = CategoryFactory()
     self.client = Client()
     self.user = UserFactory()
     self.url = reverse('offer:category', kwargs={'pk': self.category.pk})
Esempio n. 4
0
 def setUp(self):
     self.client = Client()
     self.category = CategoryFactory()
     self.user = UserFactory()
Esempio n. 5
0
 def setUp(self):
     self.category = CategoryFactory()
     self.offer = OfferFactory(category=self.category)
     self.user = UserFactory()
     self.client = Client()
     self.url = reverse('offer:offer-detail', kwargs={'pk': self.offer.pk})
Esempio n. 6
0
 def setUp(self):
     self.category = CategoryFactory()
     self.client = Client()
     self.url = reverse('offer:statistics')
Esempio n. 7
0
 def setUp(self):
     self.client = APIClient()
     self.offer = OfferFactory()
     self.other_offer = OfferFactory()
     self.category = CategoryFactory()
     self.user = UserFactory()