Guest User

Untitled

a guest
Jan 24th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. from django.test import TestCase, Client
  2.  
  3. class TestArticleUpdate(TestCase):
  4. def setUp(self):
  5. self.client = Client()
  6. self.reg_data = {
  7. "username": "oma0256",
  8. "email": "oma@email.com",
  9. "password": "pass1234"
  10. }
  11. self.login_data = {
  12. "username": "oma0256",
  13. "password": "pass1234"
  14. }
  15. self.article_data = {
  16. "title": "First article"
  17. "description": "This is the first article",
  18. "content": "Nice"
  19. }
  20.  
  21. def authorize_user(self):
  22. self.client.post('api/user/signup', self.reg_data)
  23. res = self.client.post('api/user/login', self.login_data)
  24. token = res.data.get("token")
  25. self.headers = {"Authorization": "token " + token}
  26.  
  27. def test_update_article_successfully(self):
  28. self.authorize_user()
  29. self.client.post('api/articles', self.article_data, headers=self.headers)
  30. self.article_data["title"] = "okay"
  31. res = self.client.put('api/articles/1', self.article_data, headers=self.headers)
  32. self.assertEqual(res.status_code, 200)
  33.  
  34. def test_update_article_not_found(self):
  35. self.authorize_user()
  36. res = self.client.put('api/articles/1', self.article_data, headers=self.headers)
  37. self.assertEqual(res.status_code, 404)
  38.  
  39. def test_update_article_unauthorized_user(self):
  40. res = self.client.put('api/articles/1', self.article_data)
  41. self.assertEqual(res.status_code, 401)
  42.  
  43. def test_update_article_forbidden_user(self):
  44. self.authorize_user()
  45. self.client.post('api/articles', self.article_data, headers=self.headers)
  46. self.reg_data = {"username": "admin", "email": "admin@email.com", "password": "pass1234"}
  47. self.login_data = {"username": "admin", "password": "pass1234"}
  48. self.authorize_user()
  49. self.article_data["title"] = "okay"
  50. res = self.client.put('api/articles/1', self.article_data, headers=self.headers)
  51. self.assertEqual(res.status_code, 403)
  52.  
  53. def test_update_article_wrong_inputs(self):
  54. self.authorize_user()
  55. self.client.post('api/articles', self.article_data, headers=self.headers)
  56. self.article_data["title"] = "@okay"
  57. res = self.client.put('api/articles/1', self.article_data, headers=self.headers)
  58. self.assertEqual(res.status_code, 400)
Add Comment
Please, Sign In to add comment