-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update keycloak.ts and add keycloak.test.ts
- Loading branch information
1 parent
368bc63
commit 2479e86
Showing
2 changed files
with
106 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { expect } from 'chai' | ||
import sinon from 'sinon' | ||
import { addUserGroups, removeUserGroups } from './keycloak' | ||
|
||
describe('Keycloak User Group Management', () => { | ||
let api: any | ||
let existingUser: any | ||
let keycloakRealm: string | ||
|
||
beforeEach(() => { | ||
keycloakRealm = 'otomi' | ||
existingUser = { id: 'user-id' } | ||
api = { | ||
users: { | ||
realmUsersIdGroupsGet: sinon.stub(), | ||
realmUsersIdGroupsGroupIdDelete: sinon.stub(), | ||
realmUsersIdGroupsGroupIdPut: sinon.stub(), | ||
}, | ||
groups: { | ||
realmGroupsGet: sinon.stub(), | ||
}, | ||
} | ||
}) | ||
|
||
afterEach(() => { | ||
sinon.restore() | ||
}) | ||
|
||
describe('removeUserGroups', () => { | ||
it('should remove user from groups not in teamGroups', async () => { | ||
const existingUserGroups = [ | ||
{ name: 'group1', id: 'group1-id' }, | ||
{ name: 'group2', id: 'group2-id' }, | ||
] | ||
api.users.realmUsersIdGroupsGet.resolves({ body: existingUserGroups }) | ||
|
||
await removeUserGroups(api, existingUser, ['group1']) | ||
|
||
expect(api.users.realmUsersIdGroupsGroupIdDelete.calledWith(keycloakRealm, 'user-id', 'group2-id')).to.be.true | ||
expect(api.users.realmUsersIdGroupsGroupIdDelete.calledWith(keycloakRealm, 'user-id', 'group1-id')).to.be.false | ||
}) | ||
|
||
it('should handle errors gracefully', async () => { | ||
api.users.realmUsersIdGroupsGet.rejects(new Error('API Error')) | ||
|
||
await removeUserGroups(api, existingUser, ['group1']) | ||
|
||
expect(api.users.realmUsersIdGroupsGroupIdDelete.called).to.be.false | ||
}) | ||
}) | ||
|
||
describe('addUserGroups', () => { | ||
it('should add user to groups in teamGroups if not already present', async () => { | ||
const currentKeycloakGroups = [ | ||
{ name: 'group1', id: 'group1-id' }, | ||
{ name: 'group2', id: 'group2-id' }, | ||
] | ||
const existingUserGroups = [{ name: 'group1', id: 'group1-id' }] | ||
api.groups.realmGroupsGet.resolves({ body: currentKeycloakGroups }) | ||
api.users.realmUsersIdGroupsGet.resolves({ body: existingUserGroups }) | ||
|
||
await addUserGroups(api, existingUser, ['group1', 'group2']) | ||
|
||
expect(api.users.realmUsersIdGroupsGroupIdPut.calledWith(keycloakRealm, 'user-id', 'group2-id')).to.be.true | ||
expect(api.users.realmUsersIdGroupsGroupIdPut.calledWith(keycloakRealm, 'user-id', 'group1-id')).to.be.false | ||
}) | ||
|
||
it('should handle errors gracefully', async () => { | ||
api.groups.realmGroupsGet.rejects(new Error('API Error')) | ||
|
||
await addUserGroups(api, existingUser, ['group1']) | ||
|
||
expect(api.users.realmUsersIdGroupsGroupIdPut.called).to.be.false | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters