reducers.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { Reducer } from 'redux';
  2. import { reducerTester } from 'test/core/redux/reducerTester';
  3. import { ActionOf } from 'app/core/redux/actionCreatorFactory';
  4. import { ldapReducer, ldapUserReducer } from './reducers';
  5. import {
  6. ldapConnectionInfoLoadedAction,
  7. ldapSyncStatusLoadedAction,
  8. userMappingInfoLoadedAction,
  9. userMappingInfoFailedAction,
  10. ldapFailedAction,
  11. userLoadedAction,
  12. } from './actions';
  13. import { LdapState, LdapUserState, LdapUser, User } from 'app/types';
  14. const makeInitialLdapState = (): LdapState => ({
  15. connectionInfo: [],
  16. syncInfo: null,
  17. user: null,
  18. ldapError: null,
  19. connectionError: null,
  20. userError: null,
  21. });
  22. const makeInitialLdapUserState = (): LdapUserState => ({
  23. user: null,
  24. ldapUser: null,
  25. ldapSyncInfo: null,
  26. sessions: [],
  27. });
  28. const getTestUserMapping = (): LdapUser => ({
  29. info: {
  30. email: { cfgAttrValue: 'mail', ldapValue: 'user@localhost' },
  31. name: { cfgAttrValue: 'givenName', ldapValue: 'User' },
  32. surname: { cfgAttrValue: 'sn', ldapValue: '' },
  33. login: { cfgAttrValue: 'cn', ldapValue: 'user' },
  34. },
  35. permissions: {
  36. isGrafanaAdmin: false,
  37. isDisabled: false,
  38. },
  39. roles: [],
  40. teams: [],
  41. });
  42. const getTestUser = (): User => ({
  43. id: 1,
  44. email: 'user@localhost',
  45. login: 'user',
  46. name: 'User',
  47. avatarUrl: '',
  48. label: '',
  49. });
  50. describe('LDAP page reducer', () => {
  51. describe('When page loaded', () => {
  52. describe('When connection info loaded', () => {
  53. it('should set connection info and clear error', () => {
  54. const initalState = {
  55. ...makeInitialLdapState(),
  56. };
  57. reducerTester()
  58. .givenReducer(ldapReducer as Reducer<LdapState, ActionOf<any>>, initalState)
  59. .whenActionIsDispatched(
  60. ldapConnectionInfoLoadedAction([
  61. {
  62. available: true,
  63. host: 'localhost',
  64. port: 389,
  65. error: null,
  66. },
  67. ])
  68. )
  69. .thenStateShouldEqual({
  70. ...makeInitialLdapState(),
  71. connectionInfo: [
  72. {
  73. available: true,
  74. host: 'localhost',
  75. port: 389,
  76. error: null,
  77. },
  78. ],
  79. ldapError: null,
  80. });
  81. });
  82. });
  83. describe('When connection failed', () => {
  84. it('should set ldap error', () => {
  85. const initalState = {
  86. ...makeInitialLdapState(),
  87. };
  88. reducerTester()
  89. .givenReducer(ldapReducer as Reducer<LdapState, ActionOf<any>>, initalState)
  90. .whenActionIsDispatched(
  91. ldapFailedAction({
  92. title: 'LDAP error',
  93. body: 'Failed to connect',
  94. })
  95. )
  96. .thenStateShouldEqual({
  97. ...makeInitialLdapState(),
  98. ldapError: {
  99. title: 'LDAP error',
  100. body: 'Failed to connect',
  101. },
  102. });
  103. });
  104. });
  105. describe('When LDAP sync status loaded', () => {
  106. it('should set sync info', () => {
  107. const initalState = {
  108. ...makeInitialLdapState(),
  109. };
  110. reducerTester()
  111. .givenReducer(ldapReducer as Reducer<LdapState, ActionOf<any>>, initalState)
  112. .whenActionIsDispatched(
  113. ldapSyncStatusLoadedAction({
  114. enabled: true,
  115. schedule: '0 0 * * * *',
  116. nextSync: '2019-01-01T12:00:00Z',
  117. })
  118. )
  119. .thenStateShouldEqual({
  120. ...makeInitialLdapState(),
  121. syncInfo: {
  122. enabled: true,
  123. schedule: '0 0 * * * *',
  124. nextSync: '2019-01-01T12:00:00Z',
  125. },
  126. });
  127. });
  128. });
  129. });
  130. describe('When user mapping info loaded', () => {
  131. it('should set sync info and clear user error', () => {
  132. const initalState = {
  133. ...makeInitialLdapState(),
  134. userError: {
  135. title: 'User not found',
  136. body: 'Cannot find user',
  137. },
  138. };
  139. reducerTester()
  140. .givenReducer(ldapReducer as Reducer<LdapState, ActionOf<any>>, initalState)
  141. .whenActionIsDispatched(userMappingInfoLoadedAction(getTestUserMapping()))
  142. .thenStateShouldEqual({
  143. ...makeInitialLdapState(),
  144. user: getTestUserMapping(),
  145. userError: null,
  146. });
  147. });
  148. });
  149. describe('When user not found', () => {
  150. it('should set user error and clear user info', () => {
  151. const initalState = {
  152. ...makeInitialLdapState(),
  153. user: getTestUserMapping(),
  154. };
  155. reducerTester()
  156. .givenReducer(ldapReducer as Reducer<LdapState, ActionOf<any>>, initalState)
  157. .whenActionIsDispatched(
  158. userMappingInfoFailedAction({
  159. title: 'User not found',
  160. body: 'Cannot find user',
  161. })
  162. )
  163. .thenStateShouldEqual({
  164. ...makeInitialLdapState(),
  165. user: null,
  166. userError: {
  167. title: 'User not found',
  168. body: 'Cannot find user',
  169. },
  170. });
  171. });
  172. });
  173. });
  174. describe('Edit LDAP user page reducer', () => {
  175. describe('When user loaded', () => {
  176. it('should set user and clear user error', () => {
  177. const initalState = {
  178. ...makeInitialLdapUserState(),
  179. userError: {
  180. title: 'User not found',
  181. body: 'Cannot find user',
  182. },
  183. };
  184. reducerTester()
  185. .givenReducer(ldapUserReducer as Reducer<LdapUserState, ActionOf<any>>, initalState)
  186. .whenActionIsDispatched(userLoadedAction(getTestUser()))
  187. .thenStateShouldEqual({
  188. ...makeInitialLdapUserState(),
  189. user: getTestUser(),
  190. userError: null,
  191. });
  192. });
  193. });
  194. });