reducers.test.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. import { reducerTester } from 'test/core/redux/reducerTester';
  2. import { LdapState, LdapUser, UserAdminState, UserDTO, UserListAdminState } from 'app/types';
  3. import {
  4. clearUserMappingInfoAction,
  5. ldapConnectionInfoLoadedAction,
  6. ldapFailedAction,
  7. ldapReducer,
  8. ldapSyncStatusLoadedAction,
  9. userAdminReducer,
  10. userMappingInfoFailedAction,
  11. userMappingInfoLoadedAction,
  12. userProfileLoadedAction,
  13. userSessionsLoadedAction,
  14. userListAdminReducer,
  15. queryChanged,
  16. filterChanged,
  17. } from './reducers';
  18. const makeInitialLdapState = (): LdapState => ({
  19. connectionInfo: [],
  20. });
  21. const makeInitialUserAdminState = (): UserAdminState => ({
  22. sessions: [],
  23. orgs: [],
  24. isLoading: true,
  25. });
  26. const makeInitialUserListAdminState = (): UserListAdminState => ({
  27. users: [],
  28. query: '',
  29. page: 0,
  30. perPage: 50,
  31. totalPages: 1,
  32. showPaging: false,
  33. filters: [{ name: 'activeLast30Days', value: true }],
  34. isLoading: false,
  35. });
  36. const getTestUserMapping = (): LdapUser => ({
  37. info: {
  38. email: { cfgAttrValue: 'mail', ldapValue: 'user@localhost' },
  39. name: { cfgAttrValue: 'givenName', ldapValue: 'User' },
  40. surname: { cfgAttrValue: 'sn', ldapValue: '' },
  41. login: { cfgAttrValue: 'cn', ldapValue: 'user' },
  42. },
  43. permissions: {
  44. isGrafanaAdmin: false,
  45. isDisabled: false,
  46. },
  47. roles: [],
  48. teams: [],
  49. });
  50. const getTestUser = (): UserDTO => ({
  51. id: 1,
  52. email: 'user@localhost',
  53. login: 'user',
  54. name: 'User',
  55. avatarUrl: '',
  56. isGrafanaAdmin: false,
  57. isDisabled: false,
  58. });
  59. describe('LDAP page reducer', () => {
  60. describe('When page loaded', () => {
  61. describe('When connection info loaded', () => {
  62. it('should set connection info and clear error', () => {
  63. const initialState = {
  64. ...makeInitialLdapState(),
  65. };
  66. reducerTester<LdapState>()
  67. .givenReducer(ldapReducer, initialState)
  68. .whenActionIsDispatched(
  69. ldapConnectionInfoLoadedAction([
  70. {
  71. available: true,
  72. host: 'localhost',
  73. port: 389,
  74. error: null as unknown as string,
  75. },
  76. ])
  77. )
  78. .thenStateShouldEqual({
  79. ...makeInitialLdapState(),
  80. connectionInfo: [
  81. {
  82. available: true,
  83. host: 'localhost',
  84. port: 389,
  85. error: null as unknown as string,
  86. },
  87. ],
  88. ldapError: undefined,
  89. });
  90. });
  91. });
  92. describe('When connection failed', () => {
  93. it('should set ldap error', () => {
  94. const initialState = {
  95. ...makeInitialLdapState(),
  96. };
  97. reducerTester<LdapState>()
  98. .givenReducer(ldapReducer, initialState)
  99. .whenActionIsDispatched(
  100. ldapFailedAction({
  101. title: 'LDAP error',
  102. body: 'Failed to connect',
  103. })
  104. )
  105. .thenStateShouldEqual({
  106. ...makeInitialLdapState(),
  107. ldapError: {
  108. title: 'LDAP error',
  109. body: 'Failed to connect',
  110. },
  111. });
  112. });
  113. });
  114. describe('When LDAP sync status loaded', () => {
  115. it('should set sync info', () => {
  116. const initialState = {
  117. ...makeInitialLdapState(),
  118. };
  119. reducerTester<LdapState>()
  120. .givenReducer(ldapReducer, initialState)
  121. .whenActionIsDispatched(
  122. ldapSyncStatusLoadedAction({
  123. enabled: true,
  124. schedule: '0 0 * * * *',
  125. nextSync: '2019-01-01T12:00:00Z',
  126. })
  127. )
  128. .thenStateShouldEqual({
  129. ...makeInitialLdapState(),
  130. syncInfo: {
  131. enabled: true,
  132. schedule: '0 0 * * * *',
  133. nextSync: '2019-01-01T12:00:00Z',
  134. },
  135. });
  136. });
  137. });
  138. });
  139. describe('When user mapping info loaded', () => {
  140. it('should set sync info and clear user error', () => {
  141. const initialState = {
  142. ...makeInitialLdapState(),
  143. userError: {
  144. title: 'User not found',
  145. body: 'Cannot find user',
  146. },
  147. };
  148. reducerTester<LdapState>()
  149. .givenReducer(ldapReducer, initialState)
  150. .whenActionIsDispatched(userMappingInfoLoadedAction(getTestUserMapping()))
  151. .thenStateShouldEqual({
  152. ...makeInitialLdapState(),
  153. user: getTestUserMapping(),
  154. userError: undefined,
  155. });
  156. });
  157. });
  158. describe('When user not found', () => {
  159. it('should set user error and clear user info', () => {
  160. const initialState = {
  161. ...makeInitialLdapState(),
  162. user: getTestUserMapping(),
  163. };
  164. reducerTester<LdapState>()
  165. .givenReducer(ldapReducer, initialState)
  166. .whenActionIsDispatched(
  167. userMappingInfoFailedAction({
  168. title: 'User not found',
  169. body: 'Cannot find user',
  170. })
  171. )
  172. .thenStateShouldEqual({
  173. ...makeInitialLdapState(),
  174. user: undefined,
  175. userError: {
  176. title: 'User not found',
  177. body: 'Cannot find user',
  178. },
  179. });
  180. });
  181. });
  182. describe('when clearUserMappingInfoAction is dispatched', () => {
  183. it('then state should be correct', () => {
  184. reducerTester<LdapState>()
  185. .givenReducer(ldapReducer, {
  186. ...makeInitialLdapState(),
  187. user: getTestUserMapping(),
  188. })
  189. .whenActionIsDispatched(clearUserMappingInfoAction())
  190. .thenStateShouldEqual({
  191. ...makeInitialLdapState(),
  192. user: undefined,
  193. });
  194. });
  195. });
  196. });
  197. describe('Edit Admin user page reducer', () => {
  198. describe('When user loaded', () => {
  199. it('should set user and clear user error', () => {
  200. const initialState = {
  201. ...makeInitialUserAdminState(),
  202. };
  203. reducerTester<UserAdminState>()
  204. .givenReducer(userAdminReducer, initialState)
  205. .whenActionIsDispatched(userProfileLoadedAction(getTestUser()))
  206. .thenStateShouldEqual({
  207. ...makeInitialUserAdminState(),
  208. user: getTestUser(),
  209. });
  210. });
  211. });
  212. describe('when userSessionsLoadedAction is dispatched', () => {
  213. it('then state should be correct', () => {
  214. reducerTester<UserAdminState>()
  215. .givenReducer(userAdminReducer, { ...makeInitialUserAdminState() })
  216. .whenActionIsDispatched(
  217. userSessionsLoadedAction([
  218. {
  219. browser: 'Chrome',
  220. id: 1,
  221. browserVersion: '79',
  222. clientIp: '127.0.0.1',
  223. createdAt: '2020-01-01 00:00:00',
  224. device: 'a device',
  225. isActive: true,
  226. os: 'MacOS',
  227. osVersion: '15',
  228. seenAt: '2020-01-01 00:00:00',
  229. },
  230. ])
  231. )
  232. .thenStateShouldEqual({
  233. ...makeInitialUserAdminState(),
  234. sessions: [
  235. {
  236. browser: 'Chrome',
  237. id: 1,
  238. browserVersion: '79',
  239. clientIp: '127.0.0.1',
  240. createdAt: '2020-01-01 00:00:00',
  241. device: 'a device',
  242. isActive: true,
  243. os: 'MacOS',
  244. osVersion: '15',
  245. seenAt: '2020-01-01 00:00:00',
  246. },
  247. ],
  248. });
  249. });
  250. });
  251. });
  252. describe('User List Admin reducer', () => {
  253. describe('When query changed', () => {
  254. it('should reset page to 0', () => {
  255. const initialState = {
  256. ...makeInitialUserListAdminState(),
  257. page: 3,
  258. };
  259. reducerTester<UserListAdminState>()
  260. .givenReducer(userListAdminReducer, initialState)
  261. .whenActionIsDispatched(queryChanged('test'))
  262. .thenStateShouldEqual({
  263. ...makeInitialUserListAdminState(),
  264. query: 'test',
  265. page: 0,
  266. });
  267. });
  268. });
  269. describe('When filter changed', () => {
  270. it('should reset page to 0', () => {
  271. const initialState = {
  272. ...makeInitialUserListAdminState(),
  273. page: 3,
  274. };
  275. reducerTester<UserListAdminState>()
  276. .givenReducer(userListAdminReducer, initialState)
  277. .whenActionIsDispatched(filterChanged({ test: true }))
  278. .thenStateShouldEqual({
  279. ...makeInitialUserListAdminState(),
  280. page: 0,
  281. filters: expect.arrayContaining([{ test: true }]),
  282. });
  283. });
  284. });
  285. });