From b9b0156f61f022835e89f540a90400379475e7e7 Mon Sep 17 00:00:00 2001 From: Amedeo Lepore Date: Mon, 20 Mar 2023 18:56:31 +0100 Subject: [PATCH] [AAE-12501] Fix process-services-cloud unit tests --- .../group/mock/identity-group.service.mock.ts | 20 +--------- .../services/identity-group.service.spec.ts | 28 +++++++------- .../people/mock/identity-user.service.mock.ts | 20 +--------- .../services/identity-user.service.spec.ts | 38 ++++++++++--------- 4 files changed, 37 insertions(+), 69 deletions(-) diff --git a/lib/process-services-cloud/src/lib/group/mock/identity-group.service.mock.ts b/lib/process-services-cloud/src/lib/group/mock/identity-group.service.mock.ts index 1d1bd64f6c..ef48534781 100644 --- a/lib/process-services-cloud/src/lib/group/mock/identity-group.service.mock.ts +++ b/lib/process-services-cloud/src/lib/group/mock/identity-group.service.mock.ts @@ -16,8 +16,6 @@ */ import { HttpErrorResponse } from '@angular/common/http'; -import { throwError } from 'rxjs'; -import { IdentityGroupModel } from '../models/identity-group.model'; import { IdentityGroupFilterInterface } from '../services/identity-group-filter.interface'; export const mockSearchGroupByRoles: IdentityGroupFilterInterface = { @@ -35,23 +33,7 @@ export const mockSearchGroupByApp: IdentityGroupFilterInterface = { withinApplication: 'fake-app-name' }; -export function oAuthMockApiWithIdentityGroups(groups: IdentityGroupModel[]) { - return { - oauth2Auth: { - callCustomApi: () => Promise.resolve(groups) - }, - reply: jasmine.createSpy('reply') - }; -} - -const errorResponse = new HttpErrorResponse({ +export const mockHttpErrorResponse = new HttpErrorResponse({ error: 'Mock Error', status: 404, statusText: 'Not Found' }); - -export const oAuthMockApiWithError = { - oauth2Auth: { - callCustomApi: () => throwError(errorResponse) - }, - reply: jasmine.createSpy('reply') -}; diff --git a/lib/process-services-cloud/src/lib/group/services/identity-group.service.spec.ts b/lib/process-services-cloud/src/lib/group/services/identity-group.service.spec.ts index aec0724179..48adcea715 100644 --- a/lib/process-services-cloud/src/lib/group/services/identity-group.service.spec.ts +++ b/lib/process-services-cloud/src/lib/group/services/identity-group.service.spec.ts @@ -17,22 +17,23 @@ import { TestBed } from '@angular/core/testing'; import { TranslateModule } from '@ngx-translate/core'; -import { AlfrescoApiService, setupTestBed } from '@alfresco/adf-core'; +import { setupTestBed } from '@alfresco/adf-core'; import { ProcessServiceCloudTestingModule } from '../../testing/process-service-cloud.testing.module'; import { IdentityGroupService } from './identity-group.service'; import { + mockHttpErrorResponse, mockSearchGroupByApp, mockSearchGroupByRoles, - mockSearchGroupByRolesAndApp, - oAuthMockApiWithError, - oAuthMockApiWithIdentityGroups + mockSearchGroupByRolesAndApp } from '../mock/identity-group.service.mock'; import { mockFoodGroups } from '../mock/group-cloud.mock'; +import { AdfHttpClient } from '@alfresco/adf-core/api'; describe('IdentityGroupService', () => { let service: IdentityGroupService; - let alfrescoApiService: AlfrescoApiService; + let adfHttpClient: AdfHttpClient; + let requestSpy: jasmine.Spy; setupTestBed({ imports: [ @@ -43,13 +44,14 @@ describe('IdentityGroupService', () => { beforeEach(() => { service = TestBed.inject(IdentityGroupService); - alfrescoApiService = TestBed.inject(AlfrescoApiService); + adfHttpClient = TestBed.inject(AdfHttpClient); + requestSpy = spyOn(adfHttpClient, 'request'); }); describe('Search', () => { it('should fetch groups', (done) => { - spyOn(alfrescoApiService, 'getInstance').and.returnValue(oAuthMockApiWithIdentityGroups(mockFoodGroups )as any); + requestSpy.and.returnValue(Promise.resolve(mockFoodGroups)); const searchSpy = spyOn(service, 'search').and.callThrough(); service.search('fake').subscribe( @@ -65,7 +67,7 @@ describe('IdentityGroupService', () => { }); it('should not fetch groups if error occurred', (done) => { - spyOn(alfrescoApiService, 'getInstance').and.returnValue(oAuthMockApiWithError as any); + requestSpy.and.returnValue(Promise.reject(mockHttpErrorResponse)); const searchSpy = spyOn(service, 'search').and.callThrough(); @@ -85,7 +87,7 @@ describe('IdentityGroupService', () => { }); it('should fetch groups by roles', (done) => { - spyOn(alfrescoApiService, 'getInstance').and.returnValue(oAuthMockApiWithIdentityGroups(mockFoodGroups) as any); + requestSpy.and.returnValue(Promise.resolve(mockFoodGroups)); const searchSpy = spyOn(service, 'search').and.callThrough(); service.search('fake', mockSearchGroupByRoles).subscribe( @@ -102,7 +104,7 @@ describe('IdentityGroupService', () => { }); it('should not fetch groups by roles if error occurred', (done) => { - spyOn(alfrescoApiService, 'getInstance').and.returnValue(oAuthMockApiWithError as any); + requestSpy.and.returnValue(Promise.reject(mockHttpErrorResponse)); const searchSpy = spyOn(service, 'search').and.callThrough(); service.search('fake', mockSearchGroupByRoles) @@ -125,7 +127,7 @@ describe('IdentityGroupService', () => { }); it('should fetch groups within app', (done) => { - spyOn(alfrescoApiService, 'getInstance').and.returnValue(oAuthMockApiWithIdentityGroups(mockFoodGroups) as any); + requestSpy.and.returnValue(Promise.resolve(mockFoodGroups)); service.search('fake', mockSearchGroupByApp).subscribe( res => { @@ -140,7 +142,7 @@ describe('IdentityGroupService', () => { }); it('should fetch groups within app with roles', (done) => { - spyOn(alfrescoApiService, 'getInstance').and.returnValue(oAuthMockApiWithIdentityGroups(mockFoodGroups) as any); + requestSpy.and.returnValue(Promise.resolve(mockFoodGroups)); service.search('fake', mockSearchGroupByRolesAndApp).subscribe( res => { @@ -156,7 +158,7 @@ describe('IdentityGroupService', () => { }); it('should not fetch groups within app if error occurred', (done) => { - spyOn(alfrescoApiService, 'getInstance').and.returnValue(oAuthMockApiWithError as any); + requestSpy.and.returnValue(Promise.reject(mockHttpErrorResponse)); const searchSpy = spyOn(service, 'search').and.callThrough(); service.search('fake', mockSearchGroupByApp) diff --git a/lib/process-services-cloud/src/lib/people/mock/identity-user.service.mock.ts b/lib/process-services-cloud/src/lib/people/mock/identity-user.service.mock.ts index 202d026882..4c3502fc77 100644 --- a/lib/process-services-cloud/src/lib/people/mock/identity-user.service.mock.ts +++ b/lib/process-services-cloud/src/lib/people/mock/identity-user.service.mock.ts @@ -16,8 +16,6 @@ */ import { HttpErrorResponse } from '@angular/common/http'; -import { throwError } from 'rxjs'; -import { IdentityUserModel } from '../models/identity-user.model'; import { IdentityUserFilterInterface } from '../services/identity-user-filter.interface'; export const mockSearchUserEmptyFilters: IdentityUserFilterInterface = { @@ -68,23 +66,7 @@ export const mockSearchUserByAppAndGroups: IdentityUserFilterInterface = { withinApplication: 'fake-app-name' }; -export function oAuthMockApiWithIdentityUsers(users: IdentityUserModel[]) { - return { - oauth2Auth: { - callCustomApi: () => Promise.resolve(users) - }, - reply: jasmine.createSpy('reply') - } as any; -} - -const errorResponse = new HttpErrorResponse({ +export const mockHttpErrorResponse = new HttpErrorResponse({ error: 'Mock Error', status: 404, statusText: 'Not Found' }); - -export const oAuthMockApiWithError = { - oauth2Auth: { - callCustomApi: () => throwError(errorResponse) - }, - reply: jasmine.createSpy('reply') -} as any; diff --git a/lib/process-services-cloud/src/lib/people/services/identity-user.service.spec.ts b/lib/process-services-cloud/src/lib/people/services/identity-user.service.spec.ts index 5ccf1fa0b0..a8565a16a6 100644 --- a/lib/process-services-cloud/src/lib/people/services/identity-user.service.spec.ts +++ b/lib/process-services-cloud/src/lib/people/services/identity-user.service.spec.ts @@ -17,7 +17,7 @@ import { TestBed } from '@angular/core/testing'; import { TranslateModule } from '@ngx-translate/core'; -import { AlfrescoApiService, JwtHelperService, setupTestBed } from '@alfresco/adf-core'; +import { JwtHelperService, setupTestBed } from '@alfresco/adf-core'; import { IdentityUserService } from './identity-user.service'; import { mockToken } from '../mock/jwt-helper.service.spec'; import { ProcessServiceCloudTestingModule } from '../../testing/process-service-cloud.testing.module'; @@ -28,16 +28,17 @@ import { mockSearchUserByGroupsAndRoles, mockSearchUserByGroupsAndRolesAndApp, mockSearchUserByRoles, - mockSearchUserByRolesAndApp, - oAuthMockApiWithError, - oAuthMockApiWithIdentityUsers + mockSearchUserByRolesAndApp } from '../mock/identity-user.service.mock'; import { mockFoodUsers } from '../mock/people-cloud.mock'; +import { AdfHttpClient } from '@alfresco/adf-core/api'; +import { mockHttpErrorResponse } from '../../group/mock/identity-group.service.mock'; describe('IdentityUserService', () => { let service: IdentityUserService; - let alfrescoApiService: AlfrescoApiService; + let adfHttpClient: AdfHttpClient; + let requestSpy: jasmine.Spy; setupTestBed({ imports: [ @@ -48,7 +49,8 @@ describe('IdentityUserService', () => { beforeEach(() => { service = TestBed.inject(IdentityUserService); - alfrescoApiService = TestBed.inject(AlfrescoApiService); + adfHttpClient = TestBed.inject(AdfHttpClient); + requestSpy = spyOn(adfHttpClient, 'request'); }); describe('Current user info (JWT token)', () => { @@ -85,7 +87,7 @@ describe('IdentityUserService', () => { describe('Search', () => { it('should fetch users', (done) => { - spyOn(alfrescoApiService, 'getInstance').and.returnValue(oAuthMockApiWithIdentityUsers(mockFoodUsers)); + requestSpy.and.returnValue(Promise.resolve(mockFoodUsers)); const searchSpy = spyOn(service, 'search').and.callThrough(); service.search('fake').subscribe( @@ -101,7 +103,7 @@ describe('IdentityUserService', () => { }); it('should not fetch users if error occurred', (done) => { - spyOn(alfrescoApiService, 'getInstance').and.returnValue(oAuthMockApiWithError as any); + requestSpy.and.returnValue(Promise.reject(mockHttpErrorResponse)); const searchSpy = spyOn(service, 'search').and.callThrough(); @@ -121,7 +123,7 @@ describe('IdentityUserService', () => { }); it('should fetch users by roles', (done) => { - spyOn(alfrescoApiService, 'getInstance').and.returnValue(oAuthMockApiWithIdentityUsers(mockFoodUsers)); + requestSpy.and.returnValue(Promise.resolve(mockFoodUsers)); const searchSpy = spyOn(service, 'search').and.callThrough(); service.search('fake', mockSearchUserByRoles).subscribe( @@ -138,7 +140,7 @@ describe('IdentityUserService', () => { }); it('should not fetch users by roles if error occurred', (done) => { - spyOn(alfrescoApiService, 'getInstance').and.returnValue(oAuthMockApiWithError); + requestSpy.and.returnValue(Promise.reject(mockHttpErrorResponse)); service.search('fake', mockSearchUserByRoles) .subscribe( @@ -155,7 +157,7 @@ describe('IdentityUserService', () => { }); it('should fetch users by groups', (done) => { - spyOn(alfrescoApiService, 'getInstance').and.returnValue(oAuthMockApiWithIdentityUsers(mockFoodUsers)); + requestSpy.and.returnValue(Promise.resolve(mockFoodUsers)); const searchSpy = spyOn(service, 'search').and.callThrough(); service.search('fake', mockSearchUserByGroups).subscribe( @@ -172,7 +174,7 @@ describe('IdentityUserService', () => { }); it('should fetch users by roles with groups', (done) => { - spyOn(alfrescoApiService, 'getInstance').and.returnValue(oAuthMockApiWithIdentityUsers(mockFoodUsers)); + requestSpy.and.returnValue(Promise.resolve(mockFoodUsers)); const searchSpy = spyOn(service, 'search').and.callThrough(); service.search('fake', mockSearchUserByGroupsAndRoles).subscribe( @@ -190,7 +192,7 @@ describe('IdentityUserService', () => { }); it('should fetch users by roles with groups and appName', (done) => { - spyOn(alfrescoApiService, 'getInstance').and.returnValue(oAuthMockApiWithIdentityUsers(mockFoodUsers)); + requestSpy.and.returnValue(Promise.resolve(mockFoodUsers)); const searchSpy = spyOn(service, 'search').and.callThrough(); service.search('fake', mockSearchUserByGroupsAndRolesAndApp).subscribe( @@ -209,7 +211,7 @@ describe('IdentityUserService', () => { }); it('should not fetch users by groups if error occurred', (done) => { - spyOn(alfrescoApiService, 'getInstance').and.returnValue(oAuthMockApiWithError); + requestSpy.and.returnValue(Promise.reject(mockHttpErrorResponse)); service.search('fake', mockSearchUserByGroups) .subscribe( @@ -226,7 +228,7 @@ describe('IdentityUserService', () => { }); it('should fetch users within app', (done) => { - spyOn(alfrescoApiService, 'getInstance').and.returnValue(oAuthMockApiWithIdentityUsers(mockFoodUsers)); + requestSpy.and.returnValue(Promise.resolve(mockFoodUsers)); service.search('fake', mockSearchUserByApp).subscribe( res => { @@ -241,7 +243,7 @@ describe('IdentityUserService', () => { }); it('should fetch users within app with roles', (done) => { - spyOn(alfrescoApiService, 'getInstance').and.returnValue(oAuthMockApiWithIdentityUsers(mockFoodUsers)); + requestSpy.and.returnValue(Promise.resolve(mockFoodUsers)); service.search('fake', mockSearchUserByRolesAndApp).subscribe( res => { @@ -257,7 +259,7 @@ describe('IdentityUserService', () => { }); it('should fetch users within app with groups', (done) => { - spyOn(alfrescoApiService, 'getInstance').and.returnValue(oAuthMockApiWithIdentityUsers(mockFoodUsers)); + requestSpy.and.returnValue(Promise.resolve(mockFoodUsers)); const searchSpy = spyOn(service, 'search').and.callThrough(); service.search('fake', mockSearchUserByAppAndGroups).subscribe( @@ -275,7 +277,7 @@ describe('IdentityUserService', () => { }); it('should not fetch users within app if error occurred', (done) => { - spyOn(alfrescoApiService, 'getInstance').and.returnValue(oAuthMockApiWithError); + requestSpy.and.returnValue(Promise.reject(mockHttpErrorResponse)); service.search('fake', mockSearchUserByApp) .subscribe(