Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

105 wiersze
4.0 KiB

  1. from rest_framework.test import APITestCase, APIClient
  2. from api import models
  3. from api.tests import factories
  4. class LogoutApiTestCase(APITestCase):
  5. def test_get_passwords_401(self):
  6. response = self.client.get('/api/passwords/')
  7. self.assertEqual(401, response.status_code)
  8. class LoginApiTestCase(APITestCase):
  9. def setUp(self):
  10. self.user = factories.UserFactory()
  11. self.client = APIClient()
  12. self.client.force_authenticate(user=self.user)
  13. def test_get_empty_passwords(self):
  14. request = self.client.get('/api/passwords/')
  15. self.assertEqual(0, len(request.data['results']))
  16. def test_retrieve_its_own_passwords(self):
  17. password = factories.PasswordFactory(user=self.user)
  18. request = self.client.get('/api/passwords/')
  19. self.assertEqual(1, len(request.data['results']))
  20. self.assertEqual(password.site, request.data['results'][0]['site'])
  21. def test_cant_retrieve_other_passwords(self):
  22. not_my_password = factories.PasswordFactory(user=factories.UserFactory())
  23. request = self.client.get('/api/passwords/%s/' % not_my_password.id)
  24. self.assertEqual(404, request.status_code)
  25. def test_delete_its_own_passwords(self):
  26. password = factories.PasswordFactory(user=self.user)
  27. self.assertEqual(1, models.Password.objects.all().count())
  28. request = self.client.delete('/api/passwords/%s/' % password.id)
  29. self.assertEqual(204, request.status_code)
  30. self.assertEqual(0, models.Password.objects.all().count())
  31. def test_cant_delete_other_password(self):
  32. not_my_password = factories.PasswordFactory(user=factories.UserFactory())
  33. self.assertEqual(1, models.Password.objects.all().count())
  34. request = self.client.delete('/api/passwords/%s/' % not_my_password.id)
  35. self.assertEqual(404, request.status_code)
  36. self.assertEqual(1, models.Password.objects.all().count())
  37. def test_create_password(self):
  38. password = {
  39. "site": "lesspass.com",
  40. "login": "test@oslab.fr",
  41. "lowercase": True,
  42. "uppercase": True,
  43. "number": True,
  44. "symbol": True,
  45. "counter": 1,
  46. "length": 12
  47. }
  48. self.assertEqual(0, models.Password.objects.count())
  49. self.client.post('/api/passwords/', password)
  50. self.assertEqual(1, models.Password.objects.count())
  51. def test_create_password_v2(self):
  52. password = {
  53. "site": "lesspass.com",
  54. "login": "test@oslab.fr",
  55. "lowercase": True,
  56. "uppercase": True,
  57. "number": True,
  58. "symbol": True,
  59. "counter": 1,
  60. "length": 12,
  61. "version": 2
  62. }
  63. self.client.post('/api/passwords/', password)
  64. self.assertEqual(2, models.Password.objects.first().version)
  65. def test_update_password(self):
  66. password = factories.PasswordFactory(user=self.user)
  67. self.assertNotEqual('facebook.com', password.site)
  68. new_password = {
  69. "site": "facebook.com",
  70. "login": "test@oslab.fr",
  71. "lowercase": True,
  72. "uppercase": True,
  73. "number": True,
  74. "symbol": True,
  75. "counter": 1,
  76. "length": 12
  77. }
  78. request = self.client.put('/api/passwords/%s/' % password.id, new_password)
  79. self.assertEqual(200, request.status_code, request.content.decode('utf-8'))
  80. password_updated = models.Password.objects.get(id=password.id)
  81. self.assertEqual('facebook.com', password_updated.site)
  82. def test_cant_update_other_password(self):
  83. not_my_password = factories.PasswordFactory(user=factories.UserFactory())
  84. self.assertEqual('lesspass.com', not_my_password.site)
  85. new_password = {
  86. "site": "facebook",
  87. }
  88. request = self.client.put('/api/passwords/%s/' % not_my_password.id, new_password)
  89. self.assertEqual(404, request.status_code)
  90. self.assertEqual(1, models.Password.objects.all().count())