Пример #1
0
class IndexViewTests(TestCase):
    def setUp(self):
        self.offer = OfferFactory()
        self.client = Client()
        self.url = reverse('offer:index')

    def test_get(self):
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)

    def test_if_an_offer_is_displayed_correctly_if_is_accepted(self):
        Offer.objects.filter(pk=self.offer.pk).update(status='a')
        self.offer.refresh_from_db()
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, self.offer.title)
        self.assertNotContains(response, 'No image to display')

    def test_if_an_offer_is_displayed_correctly_if_is_pending_or_rejected(
            self):
        offer = OfferFactory()
        Offer.objects.filter(pk=self.offer.pk).update(status='r')
        self.offer.refresh_from_db()
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        self.assertQuerysetEqual(response.context['offers'], [])
        self.assertNotContains(response, self.offer.title)
        self.assertNotContains(response, offer.title)
Пример #2
0
 def setUp(self):
     self.user = UserFactory()
     self.accepted_offer = OfferFactory(author=self.user, status='a')
     self.rejected_offer = OfferFactory(author=self.user, status='r')
     self.pending_offer = OfferFactory(author=self.user)
     self.url = reverse('offer:user-offers', kwargs={'pk': self.user.pk})
     self.client = Client()
Пример #3
0
 def setUp(self):
     self.offer = OfferFactory()
     self.accepted_offer = OfferFactory(status='a')
     self.url = reverse('offer:pending')
     self.client = Client()
     self.superuser = UserFactory(is_superuser=True)
     self.user = UserFactory()
Пример #4
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()
Пример #5
0
    def test_number_of_offers_for_category_is_correct_when_not_zero(self):
        offer = OfferFactory(category=self.category)
        offer2 = OfferFactory(category=self.category)

        response = self.client.get(self.url)

        self.assertEqual(200, response.status_code)
        self.assertContains(response, '{}'.format(self.category.name))
        self.assertContains(response, '2')
Пример #6
0
    def test_for_offers_in_category(self):
        self.client.force_login(self.user)
        offer = OfferFactory(category=self.category)
        offer2 = OfferFactory(category=self.category)
        self.assertEqual(2, Offer.objects.count())

        response = self.client.get(self.url)
        self.assertEqual(200, response.status_code)
        self.assertContains(response, offer.title)
        self.assertContains(response, offer2.title)
Пример #7
0
 def test_if_an_offer_is_displayed_correctly_if_is_pending_or_rejected(
         self):
     offer = OfferFactory()
     Offer.objects.filter(pk=self.offer.pk).update(status='r')
     self.offer.refresh_from_db()
     response = self.client.get(self.url)
     self.assertEqual(response.status_code, 200)
     self.assertQuerysetEqual(response.context['offers'], [])
     self.assertNotContains(response, self.offer.title)
     self.assertNotContains(response, offer.title)
Пример #8
0
class OfferRejectStatusViewTests(TestCase):
    def setUp(self):
        self.offer = OfferFactory()
        self.superuser = UserFactory(is_superuser=True)
        self.user = UserFactory()
        self.url = reverse('offer:offer-reject', kwargs={'pk': self.offer.pk})
        self.client = Client()

    def test_can_not_reject_if_not_superuser(self):
        self.client.force_login(self.user)
        response = self.client.post(self.url)

        self.assertEqual(403, response.status_code)

    def test_can_reject_offer_if_superuser(self):
        self.client.force_login(self.superuser)

        response = self.client.post(self.url)
        self.offer.refresh_from_db()
        self.assertEqual('r', self.offer.status)
Пример #9
0
class ApprovedAndRejectedOffersViewTests(TestCase):
    def setUp(self):
        self.user = UserFactory()
        self.accepted_offer = OfferFactory(author=self.user, status='a')
        self.rejected_offer = OfferFactory(author=self.user, status='r')
        self.pending_offer = OfferFactory(author=self.user)
        self.url = reverse('offer:user-offers', kwargs={'pk': self.user.pk})
        self.client = Client()

    def test_if_accepted_and_rejected_offers_are_shown(self):
        self.client.force_login(self.user)
        response = self.client.get(self.url)
        self.assertEqual(200, response.status_code)
        self.assertContains(response, self.accepted_offer.title)
        self.assertContains(response, self.rejected_offer.title)
        self.assertNotContains(response, self.pending_offer.title)

    def test_no_view_if_user_not_logged_in(self):
        response = self.client.get(self.url)
        self.assertEqual(302, response.status_code)

    def test_if_when_accepted_offer_appears_in_authors_view(self):
        superuser = UserFactory(is_superuser=True)
        self.client.force_login(superuser)
        url = reverse('offer:offer-accept',
                      kwargs={'pk': self.pending_offer.pk})

        accept_response = self.client.post(url)
        self.pending_offer.refresh_from_db()
        self.assertEqual(302, accept_response.status_code)
        self.assertEqual('a', self.pending_offer.status)

        self.client.logout()
        self.client.force_login(self.user)

        response = self.client.get(self.url)
        self.assertEqual(200, response.status_code)
        self.assertContains(response, self.pending_offer.title)

    def tearDown(self):
        self.client.logout()
Пример #10
0
class OfferUpdateViewTests(TestCase):
    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()

    def test_can_not_update_if_not_logged_in(self):
        response = self.client.get(self.url)

        self.assertEqual(302, response.status_code)

    def test_can_not_update_if_not_author(self):
        self.client.force_login(self.user2)
        response = self.client.get(self.url)

        self.assertEqual(302, response.status_code)

    def test_can_update_if_author(self):
        self.client.force_login(self.user)
        new_title = faker.word()
        new_desc = faker.text()
        data = {
            'title': new_title,
            'description': new_desc,
            'status': 'p',
        }

        response = self.client.post(self.url, data=data)
        self.assertEqual(302, response.status_code)
        self.offer.refresh_from_db()
        self.assertEqual(self.offer.title, new_title)

    def tearDown(self):
        self.client.logout()
Пример #11
0
 def setUp(self):
     self.offer = OfferFactory()
     self.superuser = UserFactory(is_superuser=True)
     self.user = UserFactory()
     self.url = reverse('offer:offer-reject', kwargs={'pk': self.offer.pk})
     self.client = Client()
Пример #12
0
 def setUp(self):
     self.offer = OfferFactory()
     self.user = UserFactory()
     self.url = reverse('offer:offer-delete', kwargs={'pk': self.offer.pk})
Пример #13
0
 def setUp(self):
     self.offer = OfferFactory()
     self.client = Client()
     self.url = reverse('offer:index')
Пример #14
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})
Пример #15
0
 def setUp(self):
     self.client = APIClient()
     self.offer = OfferFactory()
     self.other_offer = OfferFactory()
     self.category = CategoryFactory()
     self.user = UserFactory()
Пример #16
0
class OfferAPITests(APITestCase):
    def setUp(self):
        self.client = APIClient()
        self.offer = OfferFactory()
        self.other_offer = OfferFactory()
        self.category = CategoryFactory()
        self.user = UserFactory()

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

        response = self.client.get(reverse_lazy('offer:offer-list-api'))
        # import ipdb; ipdb.set_trace()
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, self.offer.title)
        self.assertContains(response, self.other_offer.title)

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

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

    def test_can_edit_an_offer(self):
        title = "New title"
        self.client.force_login(self.user)
        self.client.force_authenticate(user=self.user)

        response = self.client.put(reverse_lazy('offer:offer-detail-api',
                                                kwargs={'pk': self.offer.pk}),
                                   data={'title': title})
        self.offer.refresh_from_db()
        # import ipdb; ipdb.set_trace()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(self.offer.title, title)

    def test_can_create_an_offer(self):
        self.assertEqual(Offer.objects.count(), 2)
        self.client.force_login(self.user)
        self.client.force_authenticate(user=self.user)

        data = {
            'title': 'New',
            'description': 'Offer',
            'category': self.category.id
        }

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

    def test_can_delete_an_offer(self):
        self.assertEqual(Offer.objects.count(), 2)
        self.client.force_login(self.user)
        self.client.force_authenticate(user=self.user)

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

    def test_can_not_access_if_not_authenticated(self):
        self.client.force_login(self.user)

        response = self.client.get(reverse_lazy('offer:offer-list-api'))
        response2 = self.client.get(
            reverse_lazy('offer:offer-detail-api',
                         kwargs={'pk': self.offer.pk}))
        self.assertEqual(response.status_code, 401)
        self.assertEqual(response2.status_code, 401)

    def test_can_authenticate(self):
        payload = jwt_payload_handler(self.user)
        token = jwt_encode_handler(payload)

        response = self.client.get(reverse_lazy('offer:offer-list-api'), {},
                                   HTTP_AUTHORIZATION='JWT {}'.format(token))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, self.offer.title)
        self.assertContains(response, self.other_offer.title)

    def tearDown(self):
        self.client.logout()