Skip to content

Commit

Permalink
Merge pull request #721 from bcgov/remove-token
Browse files Browse the repository at this point in the history
removed the token
  • Loading branch information
nikhila-aot authored Sep 4, 2024
2 parents cf187a9 + 2462704 commit f762db8
Showing 1 changed file with 90 additions and 82 deletions.
172 changes: 90 additions & 82 deletions backend/users/src/app/services/keycloak.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,38 @@ jest.mock('axios');
describe('KeycloakService', () => {
let keycloakService: KeycloakService;

// Mock axios response
// Mock axios response
const axiosResponse = {
data: [
{
"id": "1",
"name": "formsflow",
"path": "/formsflow",
"subGroups": [
{
"id": "2",
"name": "formsflow-reviewer",
"path": "/formsflow/formsflow-reviewer",
"subGroups": []
},
{
"id": "3",
"name": "formsflow-client",
"path": "/formsflow/formsflow-client",
"subGroups": []
},
{
"id": "4",
"name": "formsflow-designer",
"path": "/formsflow/formsflow-designer",
"subGroups": []
}
]
}
],
data: [
{
id: '1',
name: 'formsflow',
path: '/formsflow',
subGroups: [
{
id: '2',
name: 'formsflow-reviewer',
path: '/formsflow/formsflow-reviewer',
subGroups: [],
},
{
id: '3',
name: 'formsflow-client',
path: '/formsflow/formsflow-client',
subGroups: [],
},
{
id: '4',
name: 'formsflow-designer',
path: '/formsflow/formsflow-designer',
subGroups: [],
},
],
},
],
};

beforeEach(async () => {

// Mock ConfigService
const configServiceMock = {
get: jest.fn().mockReturnValueOnce(axiosResponse),
Expand All @@ -55,66 +54,71 @@ describe('KeycloakService', () => {
}).compile();

keycloakService = module.get<KeycloakService>(KeycloakService);

});

it('should be defined', () => {
expect(keycloakService).toBeDefined();
});

describe('getGroupIdByName', () =>{

describe('getGroupIdByName', () => {
it('should return group ID when group name exists', async () => {
// Arrange
const groupName = 'formsflow-client';
const accessToken = 'hsneu889siejnd99003kkd0kdldl';
// Mock axios
const accessToken = 'acccess-token-123';

// Mock axios
jest.spyOn(axios, 'get').mockResolvedValueOnce(axiosResponse);

// Act
const groupId = await keycloakService.getGroupIdByName(groupName, accessToken);

const groupId = await keycloakService.getGroupIdByName(
groupName,
accessToken,
);

// Assert
expect(groupId).toEqual('3');
});

it('should return null when group name does not exist', async () => {
// Arrange
const groupName = 'non-existing-group';
const accessToken = 'hsneu889siejnd99003kkd0kdldl';
//Mock axios
const accessToken = 'acccess-token-123';

//Mock axios
jest.spyOn(axios, 'get').mockResolvedValueOnce(axiosResponse);

// Act
const groupId = await keycloakService.getGroupIdByName(groupName, accessToken);

const groupId = await keycloakService.getGroupIdByName(
groupName,
accessToken,
);

// Assert
expect(groupId).toBeNull();
});

it('should throw an error when retrieval fails', async () => {
// Arrange
const groupName = 'group-name';
const accessToken = 'hsneu889siejnd99003kkd0kdldl';
const accessToken = 'acccess-token-123';
const errorMessage = 'Failed to retrieve group information';

// Mock axios to throw an error
jest.spyOn(axios, 'get').mockRejectedValueOnce(new Error(errorMessage));

// Act & Assert
await expect(keycloakService.getGroupIdByName(groupName, accessToken)).rejects.toThrowError(errorMessage);
await expect(
keycloakService.getGroupIdByName(groupName, accessToken),
).rejects.toThrowError(errorMessage);
});
});

describe('addUserToGroup', () => {

describe('addUserToGroup', () => {
it('should add user to group in Keycloak', async () => {
// Arrange
const userId = '1';
const groupId = '1';
const accessToken = 'hsneu889siejnd99003kkd0kdldl';
const accessToken = 'acccess-token-123';

// Mock axios.put to resolve
jest.spyOn(axios, 'put').mockResolvedValueOnce({ status: 200, data: {} });
Expand All @@ -132,58 +136,62 @@ describe('KeycloakService', () => {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
}
},
);
});

it('should throw an error when user addition fails', async () => {
// Arrange
const userId = '1';
const groupId = '1';
const accessToken = 'hsneu889siejnd99003kkd0kdldl';
const accessToken = 'acccess-token-123';
const errorMessage = 'Failed to add user to group';

// Mock axios.put to reject with an error
jest.spyOn(axios, 'put').mockRejectedValueOnce(new Error(errorMessage));

// Act & Assert
await expect(keycloakService.addUserToGroup(userId, groupId, accessToken)).rejects.toThrowError(errorMessage);

await expect(
keycloakService.addUserToGroup(userId, groupId, accessToken),
).rejects.toThrowError(errorMessage);

// Ensure axios.put is called
expect(axios.put).toHaveBeenCalledTimes(2);
});
});

describe('findGroupIdByName', () =>{

describe('findGroupIdByName', () => {
it('should return null if group is not found', () => {

// Arrange
const groupName = 'nonexistentGroup';

// Act
const result = keycloakService.findGroupIdByName(axiosResponse.data, groupName);


// Act
const result = keycloakService.findGroupIdByName(
axiosResponse.data,
groupName,
);

// Assert
expect(result).toBeNull();
});

it('should return group object if group is found', () => {
// Arrange
const groupName = "formsflow-client";

// Act
const result = keycloakService.findGroupIdByName(axiosResponse.data, groupName);

// Assert
expect(result).toEqual({
"id": "3",
"name": "formsflow-client",
"path": "/formsflow/formsflow-client",
"subGroups": []
});
});
// Arrange
const groupName = 'formsflow-client';

// Act
const result = keycloakService.findGroupIdByName(
axiosResponse.data,
groupName,
);

// Assert
expect(result).toEqual({
id: '3',
name: 'formsflow-client',
path: '/formsflow/formsflow-client',
subGroups: [],
});
});
});
});

0 comments on commit f762db8

Please sign in to comment.