Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.test import TestCase, Client
- class TestArticleUpdate(TestCase):
- def setUp(self):
- self.client = Client()
- self.reg_data = {
- "username": "oma0256",
- "email": "oma@email.com",
- "password": "pass1234"
- }
- self.login_data = {
- "username": "oma0256",
- "password": "pass1234"
- }
- self.article_data = {
- "title": "First article"
- "description": "This is the first article",
- "content": "Nice"
- }
- def authorize_user(self):
- self.client.post('api/user/signup', self.reg_data)
- res = self.client.post('api/user/login', self.login_data)
- token = res.data.get("token")
- self.headers = {"Authorization": "token " + token}
- def test_update_article_successfully(self):
- self.authorize_user()
- self.client.post('api/articles', self.article_data, headers=self.headers)
- self.article_data["title"] = "okay"
- res = self.client.put('api/articles/1', self.article_data, headers=self.headers)
- self.assertEqual(res.status_code, 200)
- def test_update_article_not_found(self):
- self.authorize_user()
- res = self.client.put('api/articles/1', self.article_data, headers=self.headers)
- self.assertEqual(res.status_code, 404)
- def test_update_article_unauthorized_user(self):
- res = self.client.put('api/articles/1', self.article_data)
- self.assertEqual(res.status_code, 401)
- def test_update_article_forbidden_user(self):
- self.authorize_user()
- self.client.post('api/articles', self.article_data, headers=self.headers)
- self.reg_data = {"username": "admin", "email": "admin@email.com", "password": "pass1234"}
- self.login_data = {"username": "admin", "password": "pass1234"}
- self.authorize_user()
- self.article_data["title"] = "okay"
- res = self.client.put('api/articles/1', self.article_data, headers=self.headers)
- self.assertEqual(res.status_code, 403)
- def test_update_article_wrong_inputs(self):
- self.authorize_user()
- self.client.post('api/articles', self.article_data, headers=self.headers)
- self.article_data["title"] = "@okay"
- res = self.client.put('api/articles/1', self.article_data, headers=self.headers)
- self.assertEqual(res.status_code, 400)
Add Comment
Please, Sign In to add comment