예제 #1
0
class UserViewSetTests(APITestCase):
    def setUp(self):
        self.user = UserFactory()
        self.user.set_password('12345')
        self.user.save()
        api_client.login(email=self.user.email, password='******')

    def test_put_response_ok(self):
        response = api_client.put('/users/%s/' % self.user.pk, data={'phone1': self.user.phone1,
                                                                     'email': self.user.email,
                                                                     'location': self.user.location.pk})
        self.assertEqual(response.status_code, 200)

    def test_response_with_error(self):
        response = api_client.put('/users/%s/' % self.user.pk, data={'phone1': self.user.phone1,
                                                                     'email': '',
                                                                     'location': self.user.location.pk})

        self.assertEqual(response.status_code, 400)
        self.assertIn('email', response.data)
        self.assertNotIn('phone1', response.data)

    def test_response_ok(self):
        response = api_client.get('/users/%s/' % self.user.pk)
        self.assertEqual(response.status_code, 200)
        self.assertIn('email', response.data)
        self.assertEqual(response.data['email'], self.user.email)
        self.assertEqual(response.data['location'], {'pk': self.user.location.pk,
                                                     'value': str(self.user.location)})

    def test_response_not_found(self):
        response = api_client.get('/users/11111/')
        self.assertEqual(response.status_code, 404)

    def test_personal_permission(self):
        user = UserFactory()
        # Test get
        response = api_client.get('/users/%s/' % user.pk)
        self.assertEqual(response.status_code, 403)
        # Test put
        response = api_client.put('/users/%s/' % user.pk, data={'phone1': user.phone1,
                                                                'email': user.email,
                                                                'location': user.location.pk})

        self.assertEqual(response.status_code, 403)
예제 #2
0
class UserPostsViewSetTests(APITestCase):
    def setUp(self):
        self.user = UserFactory()
        self.user.set_password('12345')
        self.user.save()
        self.post = PostFactory(publisher=self.user)

    def test_return_post(self):
        api_client.login(email=self.user.email, password='******')
        response = api_client.get('/posts/user/')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.data['results']), 1)

    def test_not_autorized(self):
        response = api_client.get('/posts/user/')
        self.assertEqual(response.status_code, 403)

    def test_detail_post(self):
        PhotoFactory(post=self.post)
        api_client.login(email=self.user.email, password='******')
        response = api_client.get('/posts/user/%s/' % self.post.pk)
        self.assertEqual(response.status_code, 200)
        self.assertIn('title', response.data)
        self.assertIn('text', response.data)
        self.assertIn('photo', response.data)
        self.assertTrue(response.data['status'])

    def test_creating_post_by_user(self):
        root = CategoryFactory()
        parent = CategoryFactory(parent=root, slug='pigeon')
        api_client.login(email=self.user.email, password='******')
        response = api_client.post('/posts/user/', data=dict(title=u'тайтл',
                                                             text='text',
                                                             photos=[make_image(), make_image()],
                                                             rubric=parent.pk))
        self.assertEqual(response.status_code, 201)
        self.assertEqual(Post.objects.filter(publisher=self.user).count(), 2)
        self.assertEqual(Post.objects.get(title=u'тайтл').slug, 'tajtl')
        self.assertEqual(Post.objects.get(title=u'тайтл').status, 0)
        self.assertEqual(Photo.objects.count(), 2)
        # Test creating user category for parent
        self.assertTrue(Category.objects.get(slug="pigeon-user"))
        # Test returns url for created post
        response = client.get(response.data['url'])
        self.assertEqual(response.status_code, 200)