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.
 
 
 
 
 
 

107 wiersze
4.4 KiB

  1. from rest_framework.test import APITestCase, APIClient
  2. from api import models
  3. from api.tests import factories
  4. class LogoutEncryptedPasswordProfileTestCase(APITestCase):
  5. def test_get_password_profiles_401(self):
  6. response = self.client.get("/api/encrypted_password_profiles/")
  7. self.assertEqual(401, response.status_code)
  8. class LoginEncryptedPasswordProfileTestCase(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_password_profiless(self):
  14. request = self.client.get("/api/encrypted_password_profiles/")
  15. self.assertEqual(0, len(request.data["results"]))
  16. def test_retrieve_its_own_password_profiles(self):
  17. factories.EncryptedPasswordProfileFactory(
  18. user=self.user, password_profile="encrypted_content"
  19. )
  20. request = self.client.get("/api/encrypted_password_profiles/")
  21. self.assertEqual(1, len(request.data["results"]))
  22. self.assertEqual(
  23. "encrypted_content",
  24. request.data["results"][0]["password_profile"],
  25. )
  26. def test_cant_retrieve_other_password_profiles(self):
  27. not_my_password_profile = factories.EncryptedPasswordProfileFactory(
  28. user=factories.UserFactory()
  29. )
  30. request = self.client.get(
  31. "/api/encrypted_password_profiles/%s/" % not_my_password_profile.id
  32. )
  33. self.assertEqual(404, request.status_code)
  34. def test_delete_its_own_password_profiles(self):
  35. password_profile = factories.EncryptedPasswordProfileFactory(user=self.user)
  36. self.assertEqual(1, models.EncryptedPasswordProfile.objects.all().count())
  37. request = self.client.delete(
  38. "/api/encrypted_password_profiles/%s/" % password_profile.id
  39. )
  40. self.assertEqual(204, request.status_code)
  41. self.assertEqual(0, models.EncryptedPasswordProfile.objects.all().count())
  42. def test_cant_delete_other_password_profiles(self):
  43. not_my_password_profile = factories.EncryptedPasswordProfileFactory(
  44. user=factories.UserFactory()
  45. )
  46. self.assertEqual(1, models.EncryptedPasswordProfile.objects.all().count())
  47. request = self.client.delete(
  48. "/api/encrypted_password_profiles/%s/" % not_my_password_profile.id
  49. )
  50. self.assertEqual(404, request.status_code)
  51. self.assertEqual(1, models.EncryptedPasswordProfile.objects.all().count())
  52. def test_create_password(self):
  53. self.assertEqual(0, models.EncryptedPasswordProfile.objects.count())
  54. self.client.post(
  55. "/api/encrypted_password_profiles/",
  56. {"password_profile": "test_create_password"},
  57. )
  58. self.assertEqual(1, models.EncryptedPasswordProfile.objects.count())
  59. password_profile = models.EncryptedPasswordProfile.objects.first()
  60. self.assertEqual(password_profile.password_profile, "test_create_password")
  61. def test_update_password_profile(self):
  62. password_profile = factories.EncryptedPasswordProfileFactory(user=self.user)
  63. self.assertNotEqual(
  64. "test_update_password_profile", password_profile.password_profile
  65. )
  66. request = self.client.put(
  67. "/api/encrypted_password_profiles/%s/" % password_profile.id,
  68. {"password_profile": "test_update_password_profile"},
  69. )
  70. self.assertEqual(200, request.status_code)
  71. password_profile_updated = models.EncryptedPasswordProfile.objects.get(
  72. id=password_profile.id
  73. )
  74. self.assertEqual(
  75. "test_update_password_profile", password_profile_updated.password_profile
  76. )
  77. def test_cant_update_other_password(self):
  78. not_my_password_profile = factories.EncryptedPasswordProfileFactory(
  79. user=factories.UserFactory(),
  80. password_profile="test_cant_update_other_password",
  81. )
  82. self.assertEqual(
  83. "test_cant_update_other_password", not_my_password_profile.password_profile
  84. )
  85. request = self.client.put(
  86. "/api/encrypted_password_profiles/%s/" % not_my_password_profile.id,
  87. {"password_profile": "not_my_password_profile"},
  88. )
  89. self.assertEqual(404, request.status_code)
  90. self.assertEqual(
  91. "test_cant_update_other_password",
  92. models.EncryptedPasswordProfile.objects.first().password_profile,
  93. )