Não pode escolher mais do que 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.
 
 
 
 
 
 

110 linhas
4.0 KiB

  1. import json
  2. from rest_framework.test import APITestCase, APIClient
  3. from api import models
  4. from api.tests import factories
  5. class LogoutApiTestCase(APITestCase):
  6. def test_get_entries_403(self):
  7. response = self.client.get('/api/entries/')
  8. self.assertEqual(403, response.status_code)
  9. class LoginApiTestCase(APITestCase):
  10. def setUp(self):
  11. self.user = factories.UserFactory()
  12. self.client = APIClient()
  13. self.client.force_authenticate(user=self.user)
  14. def test_get_empty_entries(self):
  15. request = self.client.get('/api/entries/')
  16. self.assertEqual(0, len(request.data['results']))
  17. def test_retrieve_its_own_entries(self):
  18. entry = factories.EntryFactory(user=self.user)
  19. request = self.client.get('/api/entries/')
  20. self.assertEqual(1, len(request.data['results']))
  21. self.assertEqual(entry.site, request.data['results'][0]['site'])
  22. def test_cant_retrieve_other_entries(self):
  23. not_my_entry = factories.EntryFactory(user=factories.UserFactory())
  24. request = self.client.get('/api/entries/%s/' % not_my_entry.id)
  25. self.assertEqual(404, request.status_code)
  26. def test_delete_its_own_entries(self):
  27. entry = factories.EntryFactory(user=self.user)
  28. self.assertEqual(1, models.Entry.objects.all().count())
  29. request = self.client.delete('/api/entries/%s/' % entry.id)
  30. self.assertEqual(204, request.status_code)
  31. self.assertEqual(0, models.Entry.objects.all().count())
  32. def test_cant_delete_other_entry(self):
  33. not_my_entry = factories.EntryFactory(user=factories.UserFactory())
  34. self.assertEqual(1, models.Entry.objects.all().count())
  35. request = self.client.delete('/api/entries/%s/' % not_my_entry.id)
  36. self.assertEqual(404, request.status_code)
  37. self.assertEqual(1, models.Entry.objects.all().count())
  38. def test_create_entry(self):
  39. entry = {
  40. "site": "twitter",
  41. "password": {
  42. "counter": 1,
  43. "settings": [
  44. "lowercase",
  45. "uppercase",
  46. "numbers",
  47. "symbols"
  48. ],
  49. "length": 12
  50. },
  51. "title": "twitter",
  52. "username": "guillaume20100",
  53. "email": "guillaume@oslab.fr",
  54. "description": "",
  55. "url": "https://twitter.com/"
  56. }
  57. self.assertEqual(0, models.Entry.objects.count())
  58. self.assertEqual(0, models.PasswordInfo.objects.count())
  59. self.client.post('/api/entries/', entry)
  60. self.assertEqual(1, models.Entry.objects.count())
  61. self.assertEqual(1, models.PasswordInfo.objects.count())
  62. def test_update_entry(self):
  63. entry = factories.EntryFactory(user=self.user)
  64. self.assertNotEqual('facebook', entry.site)
  65. new_entry = {
  66. "site": "facebook",
  67. "password": {
  68. "counter": 1,
  69. "settings": [
  70. "lowercase",
  71. "uppercase",
  72. "numbers"
  73. ],
  74. "length": 12
  75. },
  76. "title": "facebook",
  77. "username": "",
  78. "email": "",
  79. "description": "",
  80. "url": "https://facebook.com/"
  81. }
  82. self.client.put('/api/entries/%s/' % entry.id, new_entry)
  83. entry_updated = models.Entry.objects.get(id=entry.id)
  84. self.assertEqual('facebook', entry_updated.site)
  85. self.assertEqual(3, len(json.loads(entry_updated.password.settings)))
  86. def test_cant_update_other_entry(self):
  87. not_my_entry = factories.EntryFactory(user=factories.UserFactory())
  88. self.assertEqual('twitter', not_my_entry.site)
  89. new_entry = {
  90. "site": "facebook",
  91. "password": {"settings": []}
  92. }
  93. request = self.client.put('/api/entries/%s/' % not_my_entry.id, new_entry)
  94. self.assertEqual(404, request.status_code)
  95. self.assertEqual(1, models.Entry.objects.all().count())