grafana/public/app/features/admin/state/reducers.test.ts
Peter Holmberg 3c61b563c3 Ldap: Add LDAP debug page (#18759)
* Add items for navmodel and basic page

* add reducer and actions

* adding user mapping table component

* adding components for ldap tables

* add alert box on error

* close error alert box

* LDAP status page: connect APIs WIP

* LDAP debug: fetch connection status from API

* LDAP debug: fetch user info from API

* LDAP debug: improve connection error view

* LDAP debug: connection error tweaks

* LDAP debug: fix role mapping view

* LDAP debug: role mapping view tweaks

* LDAP debug: add bulk-sync button stub

* LDAP debug: minor refactor

* LDAP debug: show user teams

* LDAP debug: user info refactor

* LDAP debug: initial user page

* LDAP debug: minor refactor, remove unused angular wrapper

* LDAP debug: add sessions to user page

* LDAP debug: tweak user page

* LDAP debug: tweak view for disabled user

* LDAP debug: get sync info from API

* LDAP debug: user sync info

* LDAP debug: sync user button

* LDAP debug: clear error on page load

* LDAP debug: add user last sync info

* LDAP debug: actions refactor

* LDAP debug: roles and teams style tweaks

* Pass showAttributeMapping to LdapUserTeams

* LDAP debug: hide bulk sync button

* LDAP debug: refactor sessions component

* LDAP debug: fix loading user sessions

* LDAP debug: hide sync user button

* LDAP debug: fix fetching unavailable /ldap-sync-status endpoint

* LDAP debug: revert accidentally added fix

* LDAP debug: show error when LDAP is not enabled

* LDAP debug: refactor, move ldap components into ldap/ folder

* LDAP debug: styles refactoring

* LDAP debug: ldap reducer tests

* LDAP debug: ldap user reducer tests

* LDAP debug: fix connection error placement

* Text update

* LdapUser: Minor UI changes moving things around

* AlertBox: Removed icon-on-top as everywhere else it is centered, want to have it be consistent
2019-09-16 18:56:01 +03:00

211 lines
5.6 KiB
TypeScript

import { Reducer } from 'redux';
import { reducerTester } from 'test/core/redux/reducerTester';
import { ActionOf } from 'app/core/redux/actionCreatorFactory';
import { ldapReducer, ldapUserReducer } from './reducers';
import {
ldapConnectionInfoLoadedAction,
ldapSyncStatusLoadedAction,
userMappingInfoLoadedAction,
userMappingInfoFailedAction,
ldapFailedAction,
userLoadedAction,
} from './actions';
import { LdapState, LdapUserState, LdapUser, User } from 'app/types';
const makeInitialLdapState = (): LdapState => ({
connectionInfo: [],
syncInfo: null,
user: null,
ldapError: null,
connectionError: null,
userError: null,
});
const makeInitialLdapUserState = (): LdapUserState => ({
user: null,
ldapUser: null,
ldapSyncInfo: null,
sessions: [],
});
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 = (): User => ({
id: 1,
email: 'user@localhost',
login: 'user',
name: 'User',
avatarUrl: '',
label: '',
});
describe('LDAP page reducer', () => {
describe('When page loaded', () => {
describe('When connection info loaded', () => {
it('should set connection info and clear error', () => {
const initalState = {
...makeInitialLdapState(),
};
reducerTester()
.givenReducer(ldapReducer as Reducer<LdapState, ActionOf<any>>, initalState)
.whenActionIsDispatched(
ldapConnectionInfoLoadedAction([
{
available: true,
host: 'localhost',
port: 389,
error: null,
},
])
)
.thenStateShouldEqual({
...makeInitialLdapState(),
connectionInfo: [
{
available: true,
host: 'localhost',
port: 389,
error: null,
},
],
ldapError: null,
});
});
});
describe('When connection failed', () => {
it('should set ldap error', () => {
const initalState = {
...makeInitialLdapState(),
};
reducerTester()
.givenReducer(ldapReducer as Reducer<LdapState, ActionOf<any>>, initalState)
.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 initalState = {
...makeInitialLdapState(),
};
reducerTester()
.givenReducer(ldapReducer as Reducer<LdapState, ActionOf<any>>, initalState)
.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 initalState = {
...makeInitialLdapState(),
userError: {
title: 'User not found',
body: 'Cannot find user',
},
};
reducerTester()
.givenReducer(ldapReducer as Reducer<LdapState, ActionOf<any>>, initalState)
.whenActionIsDispatched(userMappingInfoLoadedAction(getTestUserMapping()))
.thenStateShouldEqual({
...makeInitialLdapState(),
user: getTestUserMapping(),
userError: null,
});
});
});
describe('When user not found', () => {
it('should set user error and clear user info', () => {
const initalState = {
...makeInitialLdapState(),
user: getTestUserMapping(),
};
reducerTester()
.givenReducer(ldapReducer as Reducer<LdapState, ActionOf<any>>, initalState)
.whenActionIsDispatched(
userMappingInfoFailedAction({
title: 'User not found',
body: 'Cannot find user',
})
)
.thenStateShouldEqual({
...makeInitialLdapState(),
user: null,
userError: {
title: 'User not found',
body: 'Cannot find user',
},
});
});
});
});
describe('Edit LDAP user page reducer', () => {
describe('When user loaded', () => {
it('should set user and clear user error', () => {
const initalState = {
...makeInitialLdapUserState(),
userError: {
title: 'User not found',
body: 'Cannot find user',
},
};
reducerTester()
.givenReducer(ldapUserReducer as Reducer<LdapUserState, ActionOf<any>>, initalState)
.whenActionIsDispatched(userLoadedAction(getTestUser()))
.thenStateShouldEqual({
...makeInitialLdapUserState(),
user: getTestUser(),
userError: null,
});
});
});
});