test_views.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. from django.test import override_settings
  2. from core.models import ObjectType
  3. from users.models import *
  4. from utilities.testing import ViewTestCases, create_test_user, extract_form_failures
  5. class UserTestCase(
  6. ViewTestCases.GetObjectViewTestCase,
  7. ViewTestCases.CreateObjectViewTestCase,
  8. ViewTestCases.EditObjectViewTestCase,
  9. ViewTestCases.DeleteObjectViewTestCase,
  10. ViewTestCases.ListObjectsViewTestCase,
  11. ViewTestCases.BulkImportObjectsViewTestCase,
  12. ViewTestCases.BulkEditObjectsViewTestCase,
  13. ViewTestCases.BulkDeleteObjectsViewTestCase,
  14. ):
  15. model = User
  16. maxDiff = None
  17. validation_excluded_fields = ['password']
  18. def _get_queryset(self):
  19. # Omit the user attached to the test client
  20. return self.model.objects.exclude(username='testuser')
  21. @classmethod
  22. def setUpTestData(cls):
  23. users = (
  24. User(username='username1', first_name='first1', last_name='last1', email='user1@foo.com', password='pass1xxx'),
  25. User(username='username2', first_name='first2', last_name='last2', email='user2@foo.com', password='pass2xxx'),
  26. User(username='username3', first_name='first3', last_name='last3', email='user3@foo.com', password='pass3xxx'),
  27. )
  28. User.objects.bulk_create(users)
  29. cls.form_data = {
  30. 'username': 'usernamex',
  31. 'first_name': 'firstx',
  32. 'last_name': 'lastx',
  33. 'email': 'userx@foo.com',
  34. 'password': 'pass1xxx',
  35. 'confirm_password': 'pass1xxx',
  36. }
  37. cls.csv_data = (
  38. "username,first_name,last_name,email,password",
  39. "username4,first4,last4,email4@foo.com,pass4xxx",
  40. "username5,first5,last5,email5@foo.com,pass5xxx",
  41. "username6,first6,last6,email6@foo.com,pass6xxx",
  42. )
  43. cls.csv_update_data = (
  44. "id,first_name,last_name",
  45. f"{users[0].pk},first7,last7",
  46. f"{users[1].pk},first8,last8",
  47. f"{users[2].pk},first9,last9",
  48. )
  49. cls.bulk_edit_data = {
  50. 'last_name': 'newlastname',
  51. }
  52. @override_settings(AUTH_PASSWORD_VALIDATORS=[{
  53. 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
  54. 'OPTIONS': {'min_length': 8}
  55. }])
  56. def test_password_validation_enforced(self):
  57. """
  58. Test that any configured password validation rules (AUTH_PASSWORD_VALIDATORS) are enforced.
  59. """
  60. self.add_permissions('users.add_user')
  61. data = {
  62. 'username': 'new_user',
  63. 'password': 'foo',
  64. 'confirm_password': 'foo',
  65. }
  66. # Password too short
  67. request = {
  68. 'path': self._get_url('add'),
  69. 'data': data,
  70. }
  71. response = self.client.post(**request)
  72. self.assertHttpStatus(response, 200)
  73. # Password long enough
  74. data['password'] = 'foobar123'
  75. data['confirm_password'] = 'foobar123'
  76. self.assertHttpStatus(self.client.post(**request), 302)
  77. class GroupTestCase(
  78. ViewTestCases.GetObjectViewTestCase,
  79. ViewTestCases.CreateObjectViewTestCase,
  80. ViewTestCases.EditObjectViewTestCase,
  81. ViewTestCases.DeleteObjectViewTestCase,
  82. ViewTestCases.ListObjectsViewTestCase,
  83. ViewTestCases.BulkImportObjectsViewTestCase,
  84. ViewTestCases.BulkEditObjectsViewTestCase,
  85. ViewTestCases.BulkDeleteObjectsViewTestCase,
  86. ):
  87. model = Group
  88. maxDiff = None
  89. @classmethod
  90. def setUpTestData(cls):
  91. groups = (
  92. Group(name='group1'),
  93. Group(name='group2'),
  94. Group(name='group3'),
  95. )
  96. Group.objects.bulk_create(groups)
  97. cls.form_data = {
  98. 'name': 'groupx',
  99. }
  100. cls.csv_data = (
  101. "name",
  102. "group4"
  103. "group5"
  104. "group6"
  105. )
  106. cls.csv_update_data = (
  107. "id,name",
  108. f"{groups[0].pk},group7",
  109. f"{groups[1].pk},group8",
  110. f"{groups[2].pk},group9",
  111. )
  112. cls.bulk_edit_data = {
  113. 'description': 'New description',
  114. }
  115. class ObjectPermissionTestCase(
  116. ViewTestCases.GetObjectViewTestCase,
  117. ViewTestCases.CreateObjectViewTestCase,
  118. ViewTestCases.EditObjectViewTestCase,
  119. ViewTestCases.DeleteObjectViewTestCase,
  120. ViewTestCases.ListObjectsViewTestCase,
  121. ViewTestCases.BulkEditObjectsViewTestCase,
  122. ViewTestCases.BulkDeleteObjectsViewTestCase,
  123. ):
  124. model = ObjectPermission
  125. maxDiff = None
  126. @classmethod
  127. def setUpTestData(cls):
  128. object_type = ObjectType.objects.get_by_natural_key('dcim', 'site')
  129. permissions = (
  130. ObjectPermission(name='Permission 1', actions=['view', 'add', 'delete']),
  131. ObjectPermission(name='Permission 2', actions=['view', 'add', 'delete']),
  132. ObjectPermission(name='Permission 3', actions=['view', 'add', 'delete']),
  133. )
  134. ObjectPermission.objects.bulk_create(permissions)
  135. cls.form_data = {
  136. 'name': 'Permission X',
  137. 'description': 'A new permission',
  138. 'object_types': [object_type.pk],
  139. 'actions': 'view,edit,delete',
  140. }
  141. cls.csv_data = (
  142. "name",
  143. "permission4"
  144. "permission5"
  145. "permission6"
  146. )
  147. cls.csv_update_data = (
  148. "id,name,actions",
  149. f"{permissions[0].pk},permission7",
  150. f"{permissions[1].pk},permission8",
  151. f"{permissions[2].pk},permission9",
  152. )
  153. cls.bulk_edit_data = {
  154. 'description': 'New description',
  155. }
  156. class TokenTestCase(
  157. ViewTestCases.GetObjectViewTestCase,
  158. ViewTestCases.CreateObjectViewTestCase,
  159. ViewTestCases.EditObjectViewTestCase,
  160. ViewTestCases.DeleteObjectViewTestCase,
  161. ViewTestCases.ListObjectsViewTestCase,
  162. ViewTestCases.BulkImportObjectsViewTestCase,
  163. ViewTestCases.BulkEditObjectsViewTestCase,
  164. ViewTestCases.BulkDeleteObjectsViewTestCase,
  165. ):
  166. model = Token
  167. maxDiff = None
  168. @classmethod
  169. def setUpTestData(cls):
  170. users = (
  171. create_test_user('User 1'),
  172. create_test_user('User 2'),
  173. )
  174. tokens = (
  175. Token(key='123456789012345678901234567890123456789A', user=users[0]),
  176. Token(key='123456789012345678901234567890123456789B', user=users[0]),
  177. Token(key='123456789012345678901234567890123456789C', user=users[1]),
  178. )
  179. Token.objects.bulk_create(tokens)
  180. cls.form_data = {
  181. 'user': users[0].pk,
  182. 'key': '1234567890123456789012345678901234567890',
  183. 'description': 'testdescription',
  184. }
  185. cls.csv_data = (
  186. "key,user,description",
  187. f"123456789012345678901234567890123456789D,{users[0].pk},testdescriptionD",
  188. f"123456789012345678901234567890123456789E,{users[1].pk},testdescriptionE",
  189. f"123456789012345678901234567890123456789F,{users[1].pk},testdescriptionF",
  190. )
  191. cls.csv_update_data = (
  192. "id,description",
  193. f"{tokens[0].pk},testdescriptionH",
  194. f"{tokens[1].pk},testdescriptionI",
  195. f"{tokens[2].pk},testdescriptionJ",
  196. )
  197. cls.bulk_edit_data = {
  198. 'description': 'newdescription',
  199. }