Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

152 Zeilen
5.9 KiB

  1. from rest_framework.test import APITestCase, APIClient
  2. from api import models
  3. from api.tests import factories
  4. class LogoutPasswordsTestCase(APITestCase):
  5. def test_get_passwords_401(self):
  6. response = self.client.get("/api/passwords/")
  7. self.assertEqual(401, response.status_code)
  8. class LoginPasswordsTestCase(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_old_api(self):
  38. password = {
  39. "site": "lesspass.com",
  40. "login": "test@lesspass.com",
  41. "lowercase": False,
  42. "uppercase": True,
  43. "numbers": False,
  44. "symbols": False,
  45. "counter": 2,
  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. profile = models.Password.objects.first()
  52. self.assertEqual(profile.site, "lesspass.com")
  53. self.assertEqual(profile.login, "test@lesspass.com")
  54. self.assertFalse(profile.lowercase)
  55. self.assertTrue(profile.uppercase)
  56. self.assertFalse(profile.digits)
  57. self.assertFalse(profile.symbols)
  58. self.assertEqual(profile.counter, 2)
  59. self.assertEqual(profile.length, 12)
  60. self.assertEqual(profile.version, 2)
  61. def test_create_password_with_missing_s_old_api(self):
  62. password = {
  63. "site": "lesspass.com",
  64. "login": "test@lesspass.com",
  65. "lowercase": True,
  66. "uppercase": True,
  67. "number": False,
  68. "symbol": False,
  69. "counter": 1,
  70. "length": 16,
  71. "version": 2,
  72. }
  73. self.client.post("/api/passwords/", password)
  74. profile = models.Password.objects.first()
  75. self.assertFalse(profile.digits)
  76. self.assertFalse(profile.symbols)
  77. def test_create_password_v2(self):
  78. password = {
  79. "site": "lesspass.com",
  80. "login": "testv2@lesspass.com",
  81. "lowercase": True,
  82. "uppercase": False,
  83. "digits": False,
  84. "symbols": False,
  85. "counter": 3,
  86. "length": 16,
  87. "version": 2,
  88. }
  89. self.client.post("/api/passwords/", password)
  90. profile = models.Password.objects.first()
  91. self.assertEqual(profile.site, "lesspass.com")
  92. self.assertEqual(profile.login, "testv2@lesspass.com")
  93. self.assertTrue(profile.lowercase)
  94. self.assertFalse(profile.uppercase)
  95. self.assertFalse(profile.digits)
  96. self.assertFalse(profile.symbols)
  97. self.assertEqual(profile.counter, 3)
  98. self.assertEqual(profile.length, 16)
  99. self.assertEqual(profile.version, 2)
  100. def test_update_password(self):
  101. password = factories.PasswordFactory(user=self.user)
  102. self.assertNotEqual("facebook.com", password.site)
  103. new_password = {
  104. "site": "facebook.com",
  105. "login": "test@lesspass.com",
  106. "lowercase": True,
  107. "uppercase": True,
  108. "digits": False,
  109. "symbols": False,
  110. "counter": 2,
  111. "length": 20,
  112. "version": 2,
  113. }
  114. request = self.client.put("/api/passwords/%s/" % password.id, new_password)
  115. self.assertEqual(200, request.status_code, request.content.decode("utf-8"))
  116. password_updated = models.Password.objects.get(id=password.id)
  117. self.assertEqual(password_updated.site, "facebook.com")
  118. self.assertEqual(password_updated.login, "test@lesspass.com")
  119. self.assertTrue(password_updated.lowercase)
  120. self.assertTrue(password_updated.uppercase)
  121. self.assertFalse(password_updated.digits)
  122. self.assertFalse(password_updated.symbols)
  123. self.assertEqual(password_updated.counter, 2)
  124. self.assertEqual(password_updated.length, 20)
  125. self.assertEqual(password_updated.version, 2)
  126. def test_cant_update_other_password(self):
  127. not_my_password = factories.PasswordFactory(user=factories.UserFactory())
  128. self.assertEqual("lesspass.com", not_my_password.site)
  129. new_password = {
  130. "site": "facebook",
  131. }
  132. request = self.client.put(
  133. "/api/passwords/%s/" % not_my_password.id, new_password
  134. )
  135. self.assertEqual(404, request.status_code)
  136. self.assertEqual(1, models.Password.objects.all().count())