Exemplo n.º 1
0
    def test_get_users(self):
        """
        test GET returns list of users when users in DB
        """
        url = self.users_url
        user1 = test_helpers.create_test_user(1)
        user2 = test_helpers.create_test_user(2)
        user3 = test_helpers.create_test_user(3)
        response = self.client.get(url)
        res_json = response.json()

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertIsInstance(res_json, list)
        self.assertEqual(len(res_json), 3)
        self.assertNotEqual(res_json[0], res_json[1] or res_json[2])
        self.assertNotEqual(res_json[1], res_json[2] or res_json[0])
        self.assertEqual(res_json[0]['email'], user1.email)
        self.assertEqual(res_json[1]['email'], user2.email)
        self.assertEqual(res_json[2]['email'], user3.email)
        self.assertEqual(res_json[0]['username'], user1.username)
        self.assertEqual(res_json[1]['username'], user2.username)
        self.assertEqual(res_json[2]['username'], user3.username)
        self.assertEqual(res_json[0]['full_name'], user1.full_name)
        self.assertEqual(res_json[1]['full_name'], user2.full_name)
        self.assertEqual(res_json[2]['full_name'], user3.full_name)
        self.assertNotIn('password', res_json[0] or res_json[1] or res_json[2])
        self.assertIn('id', res_json[0] and res_json[1] and res_json[2])
Exemplo n.º 2
0
    def test_get_articles(self):
        """
        Should return 200 and list of articles
        """

        user = test_helpers.create_test_user(1)
        article1 = test_helpers.create_test_article(1, user, False)
        article2 = test_helpers.create_test_article(2, user, False)
        article3 = test_helpers.create_test_article(3, user, True)
        serializer1 = test_helpers.serializerArticle(article1).data
        serializer2 = test_helpers.serializerArticle(article2).data
        serializer3 = test_helpers.serializerArticle(article3).data

        response = self.client.get(self.url)
        data = response.json()['results']
        if response.status_code is not status.HTTP_200_OK:
            print(data)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertIsInstance(data, list)
        self.assertEqual(len(data), 2)
        self.assertEqual(data[0]['title'], article1.title)
        self.assertEqual(data[1]['title'], article2.title)
        self.assertEqual(data[0], serializer1)
        self.assertEqual(data[1], serializer2)
        self.assertNotEqual(data[0], serializer2)
        self.assertNotEqual(data[1], serializer1)
        self.assertNotEqual(serializer3, data[0] or data[1])
        self.assertNotEqual(article3.id, data[0]['id'] or data[1]['id'])
Exemplo n.º 3
0
    def test_delete_article(self):
        """
        Should return 204 and delete the requested article
        """

        user = test_helpers.create_test_user(1)
        article = test_helpers.create_test_article(1, user, False)
        url = reverse("article", args=[article.slug])
        response = self.client.delete(url, pk=article.slug)
        get_res = self.client.get(url, pk=article.slug)
        res_json = get_res.json()

        self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
        self.assertEqual(get_res.status_code, status.HTTP_404_NOT_FOUND)
        self.assertEqual(res_json['message'], 'Article does not exist')
Exemplo n.º 4
0
    def test_get_article_by_id(self):
        """
        Should return 200 and requested article
        """

        user = test_helpers.create_test_user(0)
        article = test_helpers.create_test_article(0, user, False)
        url = reverse("article", args=[article.slug])
        response = self.client.get(url, pk=article.slug)
        res_json = response.json()

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(res_json['title'], article.title)
        self.assertEqual(res_json['content'], article.content)
        self.assertEqual(res_json['id'], article.id)
Exemplo n.º 5
0
    def test_patch_article(self):
        """
        Should return 203 and update specified article
        """

        user = test_helpers.create_test_user(2)
        article = test_helpers.create_test_article(2, user, False)
        url = reverse("article", args=[article.slug])
        update_fields = {"title": "new title yay!"}
        response = self.client.patch(url, pk=article.slug, data=update_fields)
        res_json = response.json()

        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(res_json["title"], update_fields["title"])
        self.assertNotEqual(res_json["title"], article.title)
        self.assertEqual(res_json["content"], article.content)
Exemplo n.º 6
0
    def test_get_user_by_id(self):
        """
        Should return 200 and requested user
        """
        user = test_helpers.create_test_user(1)
        url = reverse("user", args=[user.id])
        response = self.client.get(url, pk=user.id)
        res_json = response.json()

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(res_json['email'], user.email)
        self.assertEqual(res_json['username'], user.username)
        self.assertEqual(res_json['full_name'], user.full_name)
        self.assertNotIn('password', res_json)
        self.assertIn('id', res_json)
        self.assertIsNotNone(res_json['id'])
        self.assertIsInstance(res_json['id'], int)
Exemplo n.º 7
0
    def test_delete_user(self):
        """
        Should return 204 and delete requested user
        """

        user = test_helpers.create_test_user(1)
        url = reverse("user", args=[user.id])
        del_res = self.client.delete(url, pk=user.id)
        get_res = self.client.get(url, pk=user.id)
        res_json = get_res.json()

        # Check delete response
        self.assertEqual(del_res.status_code, status.HTTP_204_NO_CONTENT)

        # Test that user has actually been deleted
        self.assertEqual(get_res.status_code, status.HTTP_404_NOT_FOUND)
        self.assertEqual(res_json["message"], 'User does not exist')
Exemplo n.º 8
0
    def test_post_article(self):
        """
        Should return 201 and new article
        """

        user = test_helpers.create_test_user(0)
        article = test_helpers.test_article1(user.id)
        response = self.client.post(self.url, article)
        res_json = response.json()
        if response.status_code is not status.HTTP_201_CREATED:
            print(res_json)

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(int(res_json['author']), user.id)
        self.assertEqual(res_json['publish_date'],
                         str(article['publish_date']))
        self.assertIn('id', res_json)
        self.assertIsInstance(res_json['id'], int)
Exemplo n.º 9
0
    def test_patch_user(self):
        """
        Should return 203 and update the specified user
        """

        user = test_helpers.create_test_user(0)
        url = reverse("user", args=[user.id])
        updated_fields = {
            "username": "******",
            "email": "*****@*****.**",
            "password": "******"
        }
        response = self.client.patch(url, pk=user.id, data=updated_fields)
        res_json = response.json()

        self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
        self.assertEqual(res_json["username"], updated_fields["username"])
        self.assertEqual(res_json["email"], updated_fields["email"])
        self.assertEqual(res_json["full_name"], user.full_name)
        self.assertEqual(res_json["id"], user.id)
        self.assertNotIn("password", res_json)
        self.assertNotEqual(res_json["username"], user.username)