Skip to content

Commit

Permalink
feat(admin-ui): need api test cases for auth-server module #1251
Browse files Browse the repository at this point in the history
  • Loading branch information
jv18creator committed Aug 4, 2023
1 parent 0a1590a commit 5973619
Show file tree
Hide file tree
Showing 26 changed files with 749 additions and 14 deletions.
2 changes: 2 additions & 0 deletions admin-ui/__tests__/setup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
jest.spyOn(global.console, 'log').mockImplementation(jest.fn())
jest.spyOn(global.console, 'warn').mockImplementation(jest.fn())
jest.setTimeout(30000);
// Polyfill "window.fetch" used in the React component.
import 'whatwg-fetch'
import '@testing-library/jest-dom'

it('Jans-admin UI test setup', () => {
Expand Down
1 change: 1 addition & 0 deletions admin-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"webpack-bundle-analyzer": "^4.8.0",
"webpack-cli": "^5.0.1",
"webpack-dev-server": "^4.13.2",
"whatwg-fetch": "^3.6.17",
"xhr2": "^0.2.1"
},
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions admin-ui/plugins/admin/redux/features/customScriptSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,6 @@ export const {
viewOnly,
setCurrentItem
} = customScriptSlice.actions
export { initialState }
export const { actions, reducer, state } = customScriptSlice
reducerRegistry.register('customScriptReducer', reducer)
2 changes: 2 additions & 0 deletions admin-ui/plugins/admin/redux/sagas/CustomScriptSaga.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ export function* getScriptsByType({ payload }) {
const data = yield call(scriptApi.getScriptsByType, payload.action)
yield put(getCustomScriptsResponse({ data }))
yield call(postUserAction, audit)
return data
} catch (e) {
yield put(getCustomScriptsResponse(null))
if (isFourZeroOneError(e)) {
const jwt = yield select((state) => state.authReducer.userinfo_jwt)
yield put(getAPIAccessToken(jwt))
}
return e
}
}
export function* addScript({ payload }) {
Expand Down
71 changes: 71 additions & 0 deletions admin-ui/plugins/auth-server/_tests_/clients/api/Agama.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { authReducerInit, beforeAllAsync } from './setup.test'
import { expectSaga } from 'redux-saga-test-plan'
import authReducer from 'Redux/features/authSlice'
import {
getAgamas,
addAgama,
deleteAgamas,
} from 'Plugins/auth-server/redux/sagas/AgamaSaga'
import {
initialState as agamaInitState,
reducer as agamaReducer,
} from 'Plugins/auth-server/redux/features/agamaSlice'
import { log } from 'console'
import { combineReducers } from '@reduxjs/toolkit'

let initialState

const formInitState = (token, issuer) => {
initialState = {
authReducer: authReducerInit(token, issuer),
agamaReducer: agamaInitState,
}
}

beforeAll(async () => {
try {
await beforeAllAsync(formInitState)
} catch (error) {
log(error.message)
}
})

const rootReducer = combineReducers({
authReducer,
agamaReducer,
})

describe('api CRUD actions perform for agama', () => {
it('GET Agama projects', async () => {
const result = await expectSaga(getAgamas)
.withReducer(rootReducer, initialState)
.run(false)

expect(result.returnValue instanceof Error).toBe(false)
})

it('create new Agama project', async () => {
const result = await expectSaga(addAgama, {
payload: {
name: 'test',
file: 'test',
},
})
.withReducer(rootReducer, initialState)
.run(false)

expect(result.returnValue instanceof Error).toBe(false)
})

it('should delete newly created agama project', async () => {
const result = await expectSaga(deleteAgamas, {
payload: {
name: 'test',
},
})
.withReducer(rootReducer, initialState)
.run(false)

expect(result.returnValue instanceof Error).toBe(false)
})
})
65 changes: 65 additions & 0 deletions admin-ui/plugins/auth-server/_tests_/clients/api/AuthN.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { expectSaga } from 'redux-saga-test-plan'
import { authReducerInit, beforeAllAsync } from './setup.test'
import { getScriptsByType } from 'Plugins/admin/redux/sagas/CustomScriptSaga'
import { editSimpleAuthAcr } from 'Plugins/auth-server/redux/sagas/AuthnSaga'
import {
reducer as customScriptReducer,
initialState as customScriptInitState,
} from 'Plugins/admin/redux/features/customScriptSlice'
import { combineReducers } from '@reduxjs/toolkit'
import authReducer from 'Redux/features/authSlice'
import { log } from 'console'

let initialState

const formInitState = (token, issuer) => {
initialState = {
authReducer: authReducerInit(token, issuer),
customScriptReducer: customScriptInitState,
}
}

beforeAll(async () => {
try {
await beforeAllAsync(formInitState)
} catch (error) {
log(error.message)
}
})

const rootReducer = combineReducers({
customScriptReducer,
authReducer,
})

describe('api tests for authn module', () => {
let defaultAuthNMethod
it('should get custom script by type person_authentication', async () => {
const result = await expectSaga(getScriptsByType, {
payload: { action: { type: 'person_authentication' } },
})
.withReducer(rootReducer, initialState)
.silentRun(false)

defaultAuthNMethod = result.returnValue?.entries?.find(
(item) => item.name === 'simple_password_auth'
)
expect(result.returnValue instanceof Error).toBe(false)
if (result.returnValue.entries) {
expect(result.storeState.customScriptReducer.items).toBe(
result.returnValue.entries
)
}
})

it('should save the default authn method to simple_password_auth', async () => {
const result = await expectSaga(editSimpleAuthAcr, {
payload: { data: { authenticationMethod: { defaultAcr: "simple_password_auth" } } },
})
.withReducer(rootReducer, initialState)
.silentRun(false)

log(`result.returnValue`, result.returnValue)
expect(result.returnValue instanceof Error).toBe(false)
})
})
41 changes: 41 additions & 0 deletions admin-ui/plugins/auth-server/_tests_/clients/api/Jwks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { log } from 'console'
import { authReducerInit, beforeAllAsync } from './setup.test'
import authReducer from 'Redux/features/authSlice'
import { getJwksConfig } from 'Plugins/auth-server/redux/sagas/JwksSaga'
import { combineReducers } from '@reduxjs/toolkit'
import { expectSaga } from 'redux-saga-test-plan'
import { reducer as jwksReducer } from 'Plugins/auth-server/redux/features/jwksSlice'

let initialState

const formInitState = (token, issuer) => {
initialState = {
authReducer: authReducerInit(token, issuer),
jwksReducer: {
jwks: {},
},
}
}

beforeAll(async () => {
try {
await beforeAllAsync(formInitState)
} catch (error) {
log(error.message)
}
})

const rootReducer = combineReducers({
authReducer,
jwksReducer,
})

describe('fetch & update json configuration', () => {
it('should GET current JSON configs', async () => {
const result = await expectSaga(getJwksConfig)
.withReducer(rootReducer, initialState)
.run(false)

expect(result.returnValue instanceof Error).toBe(false)
})
})
90 changes: 90 additions & 0 deletions admin-ui/plugins/auth-server/_tests_/clients/api/Logging.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import {
getLogging,
editLogging,
} from 'Plugins/auth-server/redux/sagas/LoggingSaga'
import { authReducerInit, beforeAllAsync } from './setup.test'
import { combineReducers } from '@reduxjs/toolkit'
import authReducer from 'Redux/features/authSlice'
import { reducer as loggingReducer } from 'Plugins/auth-server/redux/features/loggingSlice'
import { expectSaga } from 'redux-saga-test-plan'
import { log } from 'console'

let initialState

const formInitState = (token, issuer) => {
initialState = {
authReducer: authReducerInit(token, issuer),
loggingReducer: {
logging: {},
},
}
}

beforeAll(async () => {
try {
await beforeAllAsync(formInitState)
} catch (error) {
log(error.message)
}
})

const rootReducer = combineReducers({
authReducer,
loggingReducer,
})

describe('test GET & update action for logging page', () => {
let configs

it('GET current logging config', async () => {
const result = await expectSaga(getLogging)
.withReducer(rootReducer, initialState)
.silentRun(false)

configs = result.returnValue
expect(result.returnValue instanceof Error).toBe(false)
expect(result.returnValue).toEqual(result.storeState.loggingReducer.logging)
})

it('update httpLoggingEnabled from current logging config', async () => {
if (!(configs instanceof Error)) {
const result = await expectSaga(editLogging, {
payload: {
data: {
logging: {
...configs,
httpLoggingEnabled: !configs.httpLoggingEnabled,
},
},
},
})
.withReducer(rootReducer, initialState)
.silentRun(false)

expect(result.returnValue instanceof Error).toBe(false)
} else {
log('Error occured while fetching')
}
})

it('should toggle back value of httpLoggingEnabled', async () => {
if (!(configs instanceof Error)) {
const result = await expectSaga(editLogging, {
payload: {
data: {
logging: {
...configs,
httpLoggingEnabled: configs.httpLoggingEnabled,
},
},
},
})
.withReducer(rootReducer, initialState)
.silentRun(false)

expect(result.returnValue instanceof Error).toBe(false)
} else {
log('Error occured while fetching')
}
})
})
Loading

0 comments on commit 5973619

Please sign in to comment.