test_views.py 6.1 KB

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