Skip to content
Snippets Groups Projects
Commit 2a49da4c authored by Martin's avatar Martin
Browse files

OZG-7472 fix test; replace deprecated function

parent 63f477aa
No related branches found
No related tags found
No related merge requests found
......@@ -31,7 +31,9 @@ import KcAdminClient from '@keycloak/keycloak-admin-client';
import GroupRepresentation from '@keycloak/keycloak-admin-client/lib/defs/groupRepresentation';
import MappingsRepresentation from '@keycloak/keycloak-admin-client/lib/defs/mappingsRepresentation';
import RoleRepresentation, { RoleMappingPayload } from '@keycloak/keycloak-admin-client/lib/defs/roleRepresentation';
import { Users } from '@keycloak/keycloak-admin-client/lib/resources/users';
import { cold } from 'jest-marbles';
import { omit } from 'lodash-es';
import { throwError } from 'rxjs';
import { createUser } from '../../../user-shared/test/user';
import { UserFormService } from '../../../user/src/lib/user-form/user.formservice';
......@@ -42,9 +44,16 @@ describe('UserRepository', () => {
let kcAdminClient: Mock<KcAdminClient>;
const user: User = createUser();
let kcAdminClientUsers: Mock<Users>;
beforeEach(() => {
kcAdminClient = mock(KcAdminClient);
kcAdminClientUsers = {
...mock(Users),
executeActionsEmail: jest.fn().mockImplementation(),
create: jest.fn().mockReturnValue(Promise.resolve({ id: user.id })),
};
kcAdminClient = { ...mock(KcAdminClient), users: <any>kcAdminClientUsers };
TestBed.configureTestingModule({
providers: [{ provide: KcAdminClient, useValue: kcAdminClient }],
});
......@@ -56,16 +65,10 @@ describe('UserRepository', () => {
});
describe('createInKeycloak', () => {
beforeEach(() => {
kcAdminClient.users = <any>{
create: jest.fn().mockReturnValue(Promise.resolve({ id: user.id })),
};
});
it('should call kcAdminClient users create', () => {
repository.createInKeycloak(user);
expect(kcAdminClient.users['create']).toHaveBeenCalledWith(user);
expect(kcAdminClient.users['create']).toHaveBeenCalledWith(omit(user, 'groupIds'));
});
it('should call addUserRoles', fakeAsync(() => {
......@@ -111,7 +114,7 @@ describe('UserRepository', () => {
it('should call kcAdminClient users update', () => {
repository.saveInKeycloak(user);
expect(kcAdminClient.users['update']).toHaveBeenCalledWith({ id: user.id }, user);
expect(kcAdminClient.users['update']).toHaveBeenCalledWith({ id: user.id }, omit(user, 'groupIds'));
});
it('should call addUserRoles', fakeAsync(() => {
......@@ -151,7 +154,7 @@ describe('UserRepository', () => {
it('should call getOldUserGroups', async () => {
await repository._updateUserGroups(user.id, user.groups);
expect(repository._getOldUserGroupIds).toBeCalled();
expect(repository._getOldUserGroupIds).toHaveBeenCalled();
});
it('should call deleteUserGroups', async () => {
......@@ -209,7 +212,7 @@ describe('UserRepository', () => {
it('should call kcAdminClient users listGroups for every group to delete', async () => {
await repository._deleteUserGroups(user.id, [], oldUserGroupIds);
expect(kcAdminClient.users['delFromGroup']).toBeCalledTimes(oldUserGroupIds.length);
expect(kcAdminClient.users['delFromGroup']).toHaveBeenCalledTimes(oldUserGroupIds.length);
});
});
......@@ -234,7 +237,7 @@ describe('UserRepository', () => {
user.groups = groups;
repository._addUserGroups(user.id, newUserGroupIds, []);
expect(kcAdminClient.users['addToGroup']).toBeCalledTimes(newUserGroupIds.length);
expect(kcAdminClient.users['addToGroup']).toHaveBeenCalledTimes(newUserGroupIds.length);
});
});
......@@ -289,7 +292,7 @@ describe('UserRepository', () => {
it('should call getClientId', async () => {
await repository._addUserRolesForClient(user.id, [UserFormService.ADMIN], UserRepository.ADMIN_CLIENT_NAME);
expect(repository._getClientId).toBeCalled();
expect(repository._getClientId).toHaveBeenCalled();
});
it('should call getAlfaClientId', async () => {
......@@ -381,18 +384,12 @@ describe('UserRepository', () => {
});
describe('sendActivationMail', () => {
beforeEach(() => {
kcAdminClient.users = <any>{
executeActionsEmail: jest.fn().mockReturnValue(Promise.resolve()),
};
});
it('should call kcAdminClient users executeActionsEmail', () => {
const userId: string = faker.string.uuid();
repository._sendActivationMail(userId);
expect(kcAdminClient.users['executeActionsEmail']).toHaveBeenCalledWith({
expect(kcAdminClientUsers.executeActionsEmail).toHaveBeenCalledWith({
id: userId,
actions: ['VERIFY_EMAIL'],
lifespan: 3600 * 24 * 7,
......@@ -414,7 +411,7 @@ describe('UserRepository', () => {
const error: Error = new Error('error');
const result = repository._handleSaveError(error);
expect(result).toBeObservable(cold('#', null, new Error('An error occurred while patching the user.')));
expect(result).toBeObservable(cold('#', null, new Error('An error occurred while saving the user.')));
});
});
......@@ -453,21 +450,21 @@ describe('UserRepository', () => {
repository.getUsers().subscribe();
tick();
expect(repository._mapToUser).toBeCalledTimes(userArray.length);
expect(repository._mapToUser).toHaveBeenCalledTimes(userArray.length);
}));
it('should call kcadminClient listGroups for every user', fakeAsync(() => {
repository.getUsers().subscribe();
tick();
expect(kcAdminClient.users['listGroups']).toBeCalledTimes(userArray.length);
expect(kcAdminClient.users['listGroups']).toHaveBeenCalledTimes(userArray.length);
}));
it('should call kcadminClient listRoleMappings for every user', fakeAsync(() => {
repository.getUsers().subscribe();
tick();
expect(kcAdminClient.users['listRoleMappings']).toBeCalledTimes(userArray.length);
expect(kcAdminClient.users['listRoleMappings']).toHaveBeenCalledTimes(userArray.length);
}));
it('should return users with groups and roles', (done) => {
......
......@@ -148,7 +148,7 @@ export class UserRepository {
_handleSaveError(err: any): Observable<never> {
console.error('Error on patch user in Keycloak:', err);
return throwError(() => new Error('An error occurred while patching the user.'));
return throwError(() => new Error('An error occurred while saving the user.'));
}
public getUsers(): Observable<User[]> {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment