Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

120 lignes
4.4 KiB

  1. from rest_framework.test import APITestCase
  2. from api import models
  3. from api.tests import factories
  4. class OldRegisterTestCase(APITestCase):
  5. def test_register(self):
  6. self.assertEqual(0, models.LessPassUser.objects.all().count())
  7. data = {
  8. "email": "contact@example.org",
  9. "password": "correct horse battery staple",
  10. }
  11. request = self.client.post("/api/auth/register/", data)
  12. self.assertEqual(request.status_code, 201)
  13. self.assertEqual(1, models.LessPassUser.objects.all().count())
  14. def test_register_404_weak_password(self):
  15. self.assertEqual(0, models.LessPassUser.objects.all().count())
  16. data = {
  17. "email": "contact@example.org",
  18. "password": "password",
  19. }
  20. request = self.client.post("/api/auth/register/", data)
  21. self.assertEqual(request.status_code, 400)
  22. self.assertEqual(0, models.LessPassUser.objects.all().count())
  23. class OldLoginTestCase(APITestCase):
  24. def test_login(self):
  25. factories.UserFactory(
  26. email="contact@example.org", password="correct horse battery staple"
  27. )
  28. data = {
  29. "email": "contact@example.org",
  30. "password": "correct horse battery staple",
  31. }
  32. request = self.client.post("/api/tokens/auth/", data)
  33. self.assertEqual(request.status_code, 200)
  34. self.assertIsNotNone(request.data["token"])
  35. def test_login_bad_password(self):
  36. factories.UserFactory(
  37. email="contact@example.org", password="correct horse battery staple"
  38. )
  39. data = {
  40. "email": "contact@example.org",
  41. "password": "not the good password",
  42. }
  43. request = self.client.post("/api/tokens/auth/", data)
  44. self.assertEqual(request.status_code, 401)
  45. def test_nrt_get_passwords(self):
  46. user = factories.UserFactory(
  47. email="contact@example.org", password="correct horse battery staple"
  48. )
  49. password = factories.PasswordFactory(user=user)
  50. data = {
  51. "email": "contact@example.org",
  52. "password": "correct horse battery staple",
  53. }
  54. request = self.client.post("/api/tokens/auth/", data)
  55. headers = {
  56. "HTTP_AUTHORIZATION": "JWT {token}".format(token=request.data["token"])
  57. }
  58. request = self.client.get("/api/passwords/", **headers)
  59. self.assertEqual(request.status_code, 200)
  60. self.assertEqual(request.data["results"][0]["login"], password.login)
  61. def test_nrt_get_passwords_with_bearer(self):
  62. user = factories.UserFactory(
  63. email="contact@example.org", password="correct horse battery staple"
  64. )
  65. password = factories.PasswordFactory(user=user)
  66. data = {
  67. "email": "contact@example.org",
  68. "password": "correct horse battery staple",
  69. }
  70. request = self.client.post("/api/auth/jwt/create/", data)
  71. headers = {
  72. "HTTP_AUTHORIZATION": "Bearer {token}".format(token=request.data["access"])
  73. }
  74. request = self.client.get("/api/passwords/", **headers)
  75. self.assertEqual(request.status_code, 200)
  76. self.assertEqual(request.data["results"][0]["login"], password.login)
  77. class LogInTestCase(APITestCase):
  78. def setUp(self):
  79. factories.UserFactory(
  80. email="test@example.org",
  81. password="test@example.org",
  82. )
  83. def test_login(self):
  84. data = {
  85. "email": "test@example.org",
  86. "password": "test@example.org",
  87. }
  88. request = self.client.post("/api/auth/jwt/create/", data)
  89. self.assertEqual(request.status_code, 200)
  90. payload = request.json()
  91. self.assertTrue("access" in payload)
  92. self.assertTrue("refresh" in payload)
  93. def test_refresh_token(self):
  94. data = {
  95. "email": "test@example.org",
  96. "password": "test@example.org",
  97. }
  98. request = self.client.post("/api/auth/jwt/create/", data)
  99. self.assertEqual(request.status_code, 200)
  100. payload = request.json()
  101. refresh_token = payload["refresh"]
  102. request = self.client.post("/api/auth/jwt/refresh/", {"refresh": refresh_token})
  103. self.assertEqual(request.status_code, 200)
  104. payload = request.json()
  105. self.assertTrue("access" in payload)
  106. self.assertTrue("refresh" in payload)