Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

90 linhas
3.6 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_update_password(self):
  52. password = factories.PasswordFactory(user=self.user)
  53. self.assertNotEqual('facebook.com', password.site)
  54. new_password = {
  55. "site": "facebook.com",
  56. "login": "test@oslab.fr",
  57. "lowercase": True,
  58. "uppercase": True,
  59. "number": True,
  60. "symbol": True,
  61. "counter": 1,
  62. "length": 12
  63. }
  64. request = self.client.put('/api/passwords/%s/' % password.id, new_password)
  65. self.assertEqual(200, request.status_code, request.content.decode('utf-8'))
  66. password_updated = models.Password.objects.get(id=password.id)
  67. self.assertEqual('facebook.com', password_updated.site)
  68. def test_cant_update_other_password(self):
  69. not_my_password = factories.PasswordFactory(user=factories.UserFactory())
  70. self.assertEqual('lesspass.com', not_my_password.site)
  71. new_password = {
  72. "site": "facebook",
  73. }
  74. request = self.client.put('/api/passwords/%s/' % not_my_password.id, new_password)
  75. self.assertEqual(404, request.status_code)
  76. self.assertEqual(1, models.Password.objects.all().count())