123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- import { reducerTester } from 'test/core/redux/reducerTester';
- import { LdapState, LdapUser, UserAdminState, UserDTO, UserListAdminState } from 'app/types';
- import {
- clearUserMappingInfoAction,
- ldapConnectionInfoLoadedAction,
- ldapFailedAction,
- ldapReducer,
- ldapSyncStatusLoadedAction,
- userAdminReducer,
- userMappingInfoFailedAction,
- userMappingInfoLoadedAction,
- userProfileLoadedAction,
- userSessionsLoadedAction,
- userListAdminReducer,
- queryChanged,
- filterChanged,
- } from './reducers';
- const makeInitialLdapState = (): LdapState => ({
- connectionInfo: [],
- });
- const makeInitialUserAdminState = (): UserAdminState => ({
- sessions: [],
- orgs: [],
- isLoading: true,
- });
- const makeInitialUserListAdminState = (): UserListAdminState => ({
- users: [],
- query: '',
- page: 0,
- perPage: 50,
- totalPages: 1,
- showPaging: false,
- filters: [{ name: 'activeLast30Days', value: true }],
- isLoading: false,
- });
- const getTestUserMapping = (): LdapUser => ({
- info: {
- email: { cfgAttrValue: 'mail', ldapValue: 'user@localhost' },
- name: { cfgAttrValue: 'givenName', ldapValue: 'User' },
- surname: { cfgAttrValue: 'sn', ldapValue: '' },
- login: { cfgAttrValue: 'cn', ldapValue: 'user' },
- },
- permissions: {
- isGrafanaAdmin: false,
- isDisabled: false,
- },
- roles: [],
- teams: [],
- });
- const getTestUser = (): UserDTO => ({
- id: 1,
- email: 'user@localhost',
- login: 'user',
- name: 'User',
- avatarUrl: '',
- isGrafanaAdmin: false,
- isDisabled: false,
- });
- describe('LDAP page reducer', () => {
- describe('When page loaded', () => {
- describe('When connection info loaded', () => {
- it('should set connection info and clear error', () => {
- const initialState = {
- ...makeInitialLdapState(),
- };
- reducerTester<LdapState>()
- .givenReducer(ldapReducer, initialState)
- .whenActionIsDispatched(
- ldapConnectionInfoLoadedAction([
- {
- available: true,
- host: 'localhost',
- port: 389,
- error: null as unknown as string,
- },
- ])
- )
- .thenStateShouldEqual({
- ...makeInitialLdapState(),
- connectionInfo: [
- {
- available: true,
- host: 'localhost',
- port: 389,
- error: null as unknown as string,
- },
- ],
- ldapError: undefined,
- });
- });
- });
- describe('When connection failed', () => {
- it('should set ldap error', () => {
- const initialState = {
- ...makeInitialLdapState(),
- };
- reducerTester<LdapState>()
- .givenReducer(ldapReducer, initialState)
- .whenActionIsDispatched(
- ldapFailedAction({
- title: 'LDAP error',
- body: 'Failed to connect',
- })
- )
- .thenStateShouldEqual({
- ...makeInitialLdapState(),
- ldapError: {
- title: 'LDAP error',
- body: 'Failed to connect',
- },
- });
- });
- });
- describe('When LDAP sync status loaded', () => {
- it('should set sync info', () => {
- const initialState = {
- ...makeInitialLdapState(),
- };
- reducerTester<LdapState>()
- .givenReducer(ldapReducer, initialState)
- .whenActionIsDispatched(
- ldapSyncStatusLoadedAction({
- enabled: true,
- schedule: '0 0 * * * *',
- nextSync: '2019-01-01T12:00:00Z',
- })
- )
- .thenStateShouldEqual({
- ...makeInitialLdapState(),
- syncInfo: {
- enabled: true,
- schedule: '0 0 * * * *',
- nextSync: '2019-01-01T12:00:00Z',
- },
- });
- });
- });
- });
- describe('When user mapping info loaded', () => {
- it('should set sync info and clear user error', () => {
- const initialState = {
- ...makeInitialLdapState(),
- userError: {
- title: 'User not found',
- body: 'Cannot find user',
- },
- };
- reducerTester<LdapState>()
- .givenReducer(ldapReducer, initialState)
- .whenActionIsDispatched(userMappingInfoLoadedAction(getTestUserMapping()))
- .thenStateShouldEqual({
- ...makeInitialLdapState(),
- user: getTestUserMapping(),
- userError: undefined,
- });
- });
- });
- describe('When user not found', () => {
- it('should set user error and clear user info', () => {
- const initialState = {
- ...makeInitialLdapState(),
- user: getTestUserMapping(),
- };
- reducerTester<LdapState>()
- .givenReducer(ldapReducer, initialState)
- .whenActionIsDispatched(
- userMappingInfoFailedAction({
- title: 'User not found',
- body: 'Cannot find user',
- })
- )
- .thenStateShouldEqual({
- ...makeInitialLdapState(),
- user: undefined,
- userError: {
- title: 'User not found',
- body: 'Cannot find user',
- },
- });
- });
- });
- describe('when clearUserMappingInfoAction is dispatched', () => {
- it('then state should be correct', () => {
- reducerTester<LdapState>()
- .givenReducer(ldapReducer, {
- ...makeInitialLdapState(),
- user: getTestUserMapping(),
- })
- .whenActionIsDispatched(clearUserMappingInfoAction())
- .thenStateShouldEqual({
- ...makeInitialLdapState(),
- user: undefined,
- });
- });
- });
- });
- describe('Edit Admin user page reducer', () => {
- describe('When user loaded', () => {
- it('should set user and clear user error', () => {
- const initialState = {
- ...makeInitialUserAdminState(),
- };
- reducerTester<UserAdminState>()
- .givenReducer(userAdminReducer, initialState)
- .whenActionIsDispatched(userProfileLoadedAction(getTestUser()))
- .thenStateShouldEqual({
- ...makeInitialUserAdminState(),
- user: getTestUser(),
- });
- });
- });
- describe('when userSessionsLoadedAction is dispatched', () => {
- it('then state should be correct', () => {
- reducerTester<UserAdminState>()
- .givenReducer(userAdminReducer, { ...makeInitialUserAdminState() })
- .whenActionIsDispatched(
- userSessionsLoadedAction([
- {
- browser: 'Chrome',
- id: 1,
- browserVersion: '79',
- clientIp: '127.0.0.1',
- createdAt: '2020-01-01 00:00:00',
- device: 'a device',
- isActive: true,
- os: 'MacOS',
- osVersion: '15',
- seenAt: '2020-01-01 00:00:00',
- },
- ])
- )
- .thenStateShouldEqual({
- ...makeInitialUserAdminState(),
- sessions: [
- {
- browser: 'Chrome',
- id: 1,
- browserVersion: '79',
- clientIp: '127.0.0.1',
- createdAt: '2020-01-01 00:00:00',
- device: 'a device',
- isActive: true,
- os: 'MacOS',
- osVersion: '15',
- seenAt: '2020-01-01 00:00:00',
- },
- ],
- });
- });
- });
- });
- describe('User List Admin reducer', () => {
- describe('When query changed', () => {
- it('should reset page to 0', () => {
- const initialState = {
- ...makeInitialUserListAdminState(),
- page: 3,
- };
- reducerTester<UserListAdminState>()
- .givenReducer(userListAdminReducer, initialState)
- .whenActionIsDispatched(queryChanged('test'))
- .thenStateShouldEqual({
- ...makeInitialUserListAdminState(),
- query: 'test',
- page: 0,
- });
- });
- });
- describe('When filter changed', () => {
- it('should reset page to 0', () => {
- const initialState = {
- ...makeInitialUserListAdminState(),
- page: 3,
- };
- reducerTester<UserListAdminState>()
- .givenReducer(userListAdminReducer, initialState)
- .whenActionIsDispatched(filterChanged({ test: true }))
- .thenStateShouldEqual({
- ...makeInitialUserListAdminState(),
- page: 0,
- filters: expect.arrayContaining([{ test: true }]),
- });
- });
- });
- });
|