Example #1
0
    def test_list(self):
        login_user(self.c, self.user)

        response = self.c.get('/api/post/')

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data, [
            {
                "id": self.post02.id,
                "author": {
                    "id": self.user.id,
                    "name": "John Doe"
                },
                "created_on": response.data[0]['created_on'],
                "title": "Post 02",
                "text": "Text of post 02.",
                "category": None
            },
            {
                "id": self.post01.id,
                "author": {
                    "id": self.user.id,
                    "name": "John Doe"
                },
                "created_on": response.data[1]['created_on'],
                "title": "Post 01",
                "text": "Text of post 01.",
                "category": {
                    "id": self.category01.id,
                    "slug": "cat01",
                    "name": "Category 01"
                }
            }
        ])
Example #2
0
    def test_list(self):
        login_user(self.c, self.user)

        response = self.c.get(
            '/api/post/{post_id}/comment/'.format(post_id=self.post01.id))

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data,
                         [{
                             "id": self.comment01.id,
                             "post_id": self.post01.id,
                             "author": {
                                 "id": self.user.id,
                                 "name": "John Doe"
                             },
                             "created_on": response.data[0]['created_on'],
                             "text": "Text of comment 01.",
                             "is_banned": False
                         }, {
                             "id": self.comment02.id,
                             "post_id": self.post01.id,
                             "author": {
                                 "id": self.user.id,
                                 "name": "John Doe"
                             },
                             "created_on": response.data[1]['created_on'],
                             "text": "Text of comment 02.",
                             "is_banned": False
                         }])
Example #3
0
    def test_retrieve_by_not_staff(self):
        login_user(self.c, self.user)

        response = self.c.get('/api/comment/{comment_id}/banned/'.format(
            comment_id=self.comment01.id))

        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
Example #4
0
    def test_update_category(self):
        category02 = Category.objects.create(
            slug='cat02',
            name="Category 02"
        )

        login_user(self.c, self.user)

        response = self.c.patch(
            '/api/post/{post_id}/'.format(
                post_id=self.post01.id
            ),
            data={
                'category': {
                    'id': category02.id
                }
            },
            format='json'
        )

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['category'], {
            "id": category02.id,
            "slug": "cat02",
            "name": "Category 02"
        })
Example #5
0
    def test_retrieve(self):
        login_user(self.c, self.user)

        response = self.c.get(
            '/api/post/{post_id}/'.format(
                post_id=self.post01.id
            )
        )

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data, {
            "id": self.post01.id,
            "author": {
                "id": self.user.id,
                "name": "John Doe"
            },
            "created_on": response.data['created_on'],
            "title": "Post 01",
            "text": "Text of post 01.",
            "category": {
                "id": self.category01.id,
                "slug": "cat01",
                "name": "Category 01"
            }
        })
Example #6
0
    def test_delete(self):
        login_user(self.c, self.user)

        response = self.c.delete(
            '/api/comment/{comment_id}/'.format(comment_id=self.comment01.id))

        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
        self.assertEqual(
            Comment.objects.filter(id=self.comment01.id).exists(), False)
Example #7
0
    def test_delete_by_not_author(self):
        other_user = create_user('other')

        login_user(self.c, other_user)

        response = self.c.delete(
            '/api/comment/{comment_id}/'.format(comment_id=self.comment01.id))

        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
Example #8
0
    def test_update(self):
        login_user(self.c, self.user)

        response = self.c.patch(
            '/api/comment/{comment_id}/'.format(comment_id=self.comment01.id),
            data={'text': "Updated text"})

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['text'], "Updated text")
Example #9
0
    def test_update_by_not_author(self):
        other_user = create_user('other')

        login_user(self.c, other_user)

        response = self.c.patch(
            '/api/comment/{comment_id}/'.format(comment_id=self.comment01.id),
            data={'text': "Updated text"})

        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
Example #10
0
    def test_delete_by_staff(self):
        staff_user = create_user('other', is_staff=True)

        login_user(self.c, staff_user)

        response = self.c.delete(
            '/api/comment/{comment_id}/'.format(comment_id=self.comment01.id))

        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
        self.assertEqual(
            Comment.objects.filter(id=self.comment01.id).exists(), False)
Example #11
0
    def test_create_banned(self):
        login_user(self.c, self.user)

        response = self.c.post(
            '/api/post/{post_id}/comment/'.format(post_id=self.post01.id),
            data={
                'text': "Text of comment 03.",
                'is_banned': True
            })

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data['is_banned'], False)  # not saved!
Example #12
0
    def test_retrieve(self):
        login_user(self.c, self.staff_user)

        response = self.c.get('/api/comment/{comment_id}/banned/'.format(
            comment_id=self.comment01.id))

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(
            response.data, {
                "id": self.comment01.id,
                "is_banned": False,
                "banned_on": None,
                "banned_by": None
            })
Example #13
0
    def test_update(self):
        login_user(self.c, self.user)

        response = self.c.patch(
            '/api/post/{post_id}/'.format(
                post_id=self.post01.id
            ),
            data={
                'title': "Updated title"
            }
        )

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['title'], "Updated title")
Example #14
0
    def test_update_category_to_none(self):
        login_user(self.c, self.user)

        # ensure that category is set
        self.assertEqual(self.post01.category, self.category01)

        response = self.c.patch(
            '/api/post/{post_id}/'.format(
                post_id=self.post01.id
            ),
            data={
                'category': None
            }
        )

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data['category'], None)
Example #15
0
    def test_update(self):
        login_user(self.c, self.staff_user)

        response = self.c.patch('/api/comment/{comment_id}/banned/'.format(
            comment_id=self.comment01.id),
                                data={'is_banned': True})

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(
            response.data, {
                "id": self.comment01.id,
                "is_banned": True,
                "banned_on": response.data['banned_on'],
                "banned_by": {
                    "id": self.staff_user.id,
                    "name": "John Doe"
                }
            })
Example #16
0
    def test_create(self):
        login_user(self.c, self.user)

        response = self.c.post(
            '/api/post/{post_id}/comment/'.format(post_id=self.post01.id),
            data={'text': "Text of comment 03."})

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(
            response.data, {
                "id": response.data['id'],
                "post_id": self.post01.id,
                "author": {
                    "id": self.user.id,
                    "name": "John Doe"
                },
                "created_on": response.data['created_on'],
                "text": "Text of comment 03.",
                "is_banned": False
            })
Example #17
0
    def test_create_with_category(self):
        login_user(self.c, self.user)

        response = self.c.post(
            '/api/post/',
            data={
                'title': "Post 03",
                'text': "Text of post 03.",
                'category': {
                    'id': self.category01.id
                }
            },
            format='json'  # <= mandatory if nested JSON is in payload
        )

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data['category'], {
            "id": self.category01.id,
            "slug": "cat01",
            "name": "Category 01"
        })
Example #18
0
    def test_create(self):
        login_user(self.c, self.user)

        response = self.c.post(
            '/api/post/',
            data={
                'title': "Post 03",
                'text': "Text of post 03."
            }
        )

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(response.data, {
            "id": response.data['id'],
            "author": {
                "id": self.user.id,
                "name": "John Doe"
            },
            "created_on": response.data['created_on'],
            "title": "Post 03",
            "text": "Text of post 03.",
            "category": None
        })