From 24627046b5134a963af9bfa613284e7eec70a7fc Mon Sep 17 00:00:00 2001 From: midhun-aot Date: Wed, 4 Sep 2024 10:15:35 -0700 Subject: [PATCH] removed the token --- .../src/app/services/keycloak.service.spec.ts | 172 +++++++++--------- 1 file changed, 90 insertions(+), 82 deletions(-) diff --git a/backend/users/src/app/services/keycloak.service.spec.ts b/backend/users/src/app/services/keycloak.service.spec.ts index 34833d52..7e69403b 100644 --- a/backend/users/src/app/services/keycloak.service.spec.ts +++ b/backend/users/src/app/services/keycloak.service.spec.ts @@ -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), @@ -55,66 +54,71 @@ describe('KeycloakService', () => { }).compile(); keycloakService = module.get(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: {} }); @@ -132,7 +136,7 @@ describe('KeycloakService', () => { Authorization: `Bearer ${accessToken}`, 'Content-Type': 'application/json', }, - } + }, ); }); @@ -140,50 +144,54 @@ describe('KeycloakService', () => { // 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: [], + }); + }); }); }); -