|
| 1 | +import { combineReducers } from '@reduxjs/toolkit' |
| 2 | +import { |
| 3 | + authReducerInit, |
| 4 | + beforeAllAsync, |
| 5 | +} from 'Plugins/admin/__tests__/api/setup.test' |
| 6 | +import { |
| 7 | + fetchMapping, |
| 8 | + addMapping, |
| 9 | + deleteMapping, |
| 10 | + updateMapping, |
| 11 | +} from 'Plugins/admin/redux/sagas/MappingSaga' |
| 12 | +import { |
| 13 | + deletePermission, |
| 14 | + addPermission, |
| 15 | +} from 'Plugins/admin/redux/sagas/ApiPermissionSaga' |
| 16 | +import { addRole, deleteRole } from 'Plugins/admin/redux/sagas/ApiRoleSaga' |
| 17 | +import { |
| 18 | + initialState as mappingInitState, |
| 19 | + reducer as mappingReducer, |
| 20 | +} from 'Plugins/admin/redux/features/mappingSlice' |
| 21 | +import { expectSaga } from 'redux-saga-test-plan' |
| 22 | +import authReducer from 'Redux/features/authSlice' |
| 23 | +import { reducer as apiRoleReducer } from 'Plugins/admin/redux/features/apiRoleSlice' |
| 24 | +import { log } from 'console' |
| 25 | + |
| 26 | +let initialState |
| 27 | + |
| 28 | +const formInitState = (token, issuer) => { |
| 29 | + initialState = { |
| 30 | + authReducer: authReducerInit(token, issuer), |
| 31 | + mappingReducer: mappingInitState, |
| 32 | + apiRoleReducer: { items: [] }, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +beforeAll(async () => { |
| 37 | + try { |
| 38 | + await beforeAllAsync(formInitState) |
| 39 | + } catch (error) { |
| 40 | + log(error.message) |
| 41 | + } |
| 42 | +}) |
| 43 | + |
| 44 | +const rootReducer = combineReducers({ |
| 45 | + authReducer, |
| 46 | + mappingReducer, |
| 47 | + apiRoleReducer, |
| 48 | +}) |
| 49 | + |
| 50 | +const uiRolePayload = { |
| 51 | + role: 'test-role', |
| 52 | + description: 'test description', |
| 53 | + deletable: true, |
| 54 | +} |
| 55 | + |
| 56 | +const read_permission = 'https://jans.io/oauth/config/test_jest.read' |
| 57 | +const write_permission = 'https://jans.io/oauth/config/test_jest.write' |
| 58 | + |
| 59 | +const mappingPayload = { |
| 60 | + role: uiRolePayload.role, |
| 61 | + permissions: [read_permission, write_permission], |
| 62 | +} |
| 63 | + |
| 64 | +describe('perform CRUD action for mapping module', () => { |
| 65 | + let uiRole |
| 66 | + it('should fetch mapping role permissions', async () => { |
| 67 | + const result = await expectSaga(fetchMapping, { |
| 68 | + payload: { |
| 69 | + action: {}, |
| 70 | + }, |
| 71 | + }) |
| 72 | + .withReducer(rootReducer, initialState) |
| 73 | + .run(false) |
| 74 | + |
| 75 | + expect(result.returnValue instanceof Error).toBe(false) |
| 76 | + }) |
| 77 | + |
| 78 | + it('should create new permission https://jans.io/oauth/config/test_jest.read', async () => { |
| 79 | + const result = await expectSaga(addPermission, { |
| 80 | + payload: { |
| 81 | + action: { |
| 82 | + action_data: { permission: read_permission, description: 'desc' }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }) |
| 86 | + .withReducer(rootReducer, initialState) |
| 87 | + .run(false) |
| 88 | + |
| 89 | + expect(result.returnValue instanceof Error).toBe(false) |
| 90 | + }) |
| 91 | + |
| 92 | + it('should create new permission https://jans.io/oauth/config/test_jest.write', async () => { |
| 93 | + const result = await expectSaga(addPermission, { |
| 94 | + payload: { |
| 95 | + action: { |
| 96 | + action_data: { permission: write_permission, description: 'desc' }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }) |
| 100 | + .withReducer(rootReducer, initialState) |
| 101 | + .run(false) |
| 102 | + |
| 103 | + expect(result.returnValue instanceof Error).toBe(false) |
| 104 | + }) |
| 105 | + |
| 106 | + it('should create new test role', async () => { |
| 107 | + const result = await expectSaga(addRole, { |
| 108 | + payload: { action: { action_data: uiRolePayload } }, |
| 109 | + }) |
| 110 | + .withReducer(rootReducer, initialState) |
| 111 | + .run(false) |
| 112 | + |
| 113 | + expect(result.returnValue instanceof Error).toBe(false) |
| 114 | + if (!(result.returnValue instanceof Error)) { |
| 115 | + uiRole = result.returnValue |
| 116 | + } |
| 117 | + }) |
| 118 | + |
| 119 | + it('should add mapping', async () => { |
| 120 | + if (uiRole) { |
| 121 | + const result = await expectSaga(addMapping, { |
| 122 | + payload: { |
| 123 | + data: mappingPayload, |
| 124 | + }, |
| 125 | + }) |
| 126 | + .withReducer(rootReducer, initialState) |
| 127 | + .run(false) |
| 128 | + |
| 129 | + expect(result.returnValue instanceof Error).toBe(false) |
| 130 | + } else { |
| 131 | + log('skipping test, no test permission found!') |
| 132 | + } |
| 133 | + }) |
| 134 | + |
| 135 | + it('should update mapping', async () => { |
| 136 | + if (uiRole) { |
| 137 | + const result = await expectSaga(updateMapping, { |
| 138 | + payload: { |
| 139 | + data: { |
| 140 | + role: mappingPayload.role, |
| 141 | + permissions: ['https://jans.io/oauth/config/test_jest.write'], |
| 142 | + }, |
| 143 | + }, |
| 144 | + }) |
| 145 | + .withReducer(rootReducer, initialState) |
| 146 | + .run(false) |
| 147 | + |
| 148 | + expect(result.returnValue instanceof Error).toBe(false) |
| 149 | + } else { |
| 150 | + log('skipping test, no test permission found!') |
| 151 | + } |
| 152 | + }) |
| 153 | + |
| 154 | + it('should delete newly created mapping item', async () => { |
| 155 | + if (uiRole) { |
| 156 | + const result = await expectSaga(deleteMapping, { |
| 157 | + payload: { |
| 158 | + data: { |
| 159 | + role: mappingPayload.role, |
| 160 | + permissions: ['https://jans.io/oauth/config/test_jest.write'], |
| 161 | + }, |
| 162 | + }, |
| 163 | + }) |
| 164 | + .withReducer(rootReducer, initialState) |
| 165 | + .run(false) |
| 166 | + |
| 167 | + expect(result.returnValue instanceof Error).toBe(false) |
| 168 | + } else { |
| 169 | + log('skipping test, no test permission found!') |
| 170 | + } |
| 171 | + }) |
| 172 | + |
| 173 | + it('should delete permission https://jans.io/oauth/config/test_jest.read', async () => { |
| 174 | + const result = await expectSaga(deletePermission, { |
| 175 | + payload: { |
| 176 | + action: { |
| 177 | + action_data: { permission: read_permission, description: 'desc' }, |
| 178 | + }, |
| 179 | + }, |
| 180 | + }) |
| 181 | + .withReducer(rootReducer, initialState) |
| 182 | + .run(false) |
| 183 | + |
| 184 | + expect(result.returnValue instanceof Error).toBe(false) |
| 185 | + }) |
| 186 | + |
| 187 | + it('should delete permission https://jans.io/oauth/config/test_jest.write', async () => { |
| 188 | + const result = await expectSaga(deletePermission, { |
| 189 | + payload: { |
| 190 | + action: { |
| 191 | + action_data: { permission: write_permission, description: 'desc' }, |
| 192 | + }, |
| 193 | + }, |
| 194 | + }) |
| 195 | + .withReducer(rootReducer, initialState) |
| 196 | + .run(false) |
| 197 | + |
| 198 | + expect(result.returnValue instanceof Error).toBe(false) |
| 199 | + }) |
| 200 | + |
| 201 | + it('should delete newly added role', async () => { |
| 202 | + if (uiRole) { |
| 203 | + const result = await expectSaga(deleteRole, { |
| 204 | + payload: { |
| 205 | + action: { |
| 206 | + action_data: { ...uiRolePayload }, |
| 207 | + }, |
| 208 | + }, |
| 209 | + }) |
| 210 | + .withReducer(rootReducer, initialState) |
| 211 | + .run(false) |
| 212 | + |
| 213 | + expect(result.returnValue instanceof Error).toBe(false) |
| 214 | + } else { |
| 215 | + log('skipping delete test, no role found!!!') |
| 216 | + } |
| 217 | + }) |
| 218 | +}) |
0 commit comments