-
Notifications
You must be signed in to change notification settings - Fork 89
[PM-20134] Fix overwriteExisting and largeImport causing users to be deleted #737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
f560f76
70db5e9
08eaff7
068a97d
79e74a6
00ca89d
2efe56a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I forgot these tests are testing the directory service specifically, whereas our new tests are testing syncService. After thinking about it some more I recommend:
eliykat marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,24 @@ | ||
import { mock, MockProxy } from "jest-mock-extended"; | ||
|
||
import { ApiService } from "@/jslib/common/src/abstractions/api.service"; | ||
import { CryptoFunctionService } from "@/jslib/common/src/abstractions/cryptoFunction.service"; | ||
import { MessagingService } from "@/jslib/common/src/abstractions/messaging.service"; | ||
import { EnvironmentService } from "@/jslib/common/src/services/environment.service"; | ||
|
||
import { I18nService } from "../../jslib/common/src/abstractions/i18n.service"; | ||
import { LogService } from "../../jslib/common/src/abstractions/log.service"; | ||
import { groupFixtures } from "../../openldap/group-fixtures"; | ||
import { userFixtures } from "../../openldap/user-fixtures"; | ||
import { DirectoryFactoryService } from "../abstractions/directory-factory.service"; | ||
import { DirectoryType } from "../enums/directoryType"; | ||
import { getLdapConfiguration, getSyncConfiguration } from "../utils/test-fixtures"; | ||
|
||
import { BatchRequestBuilder } from "./batch-request-builder"; | ||
import { LdapDirectoryService } from "./ldap-directory.service"; | ||
import { SingleRequestBuilder } from "./single-request-builder"; | ||
import { StateService } from "./state.service"; | ||
import { SyncService } from "./sync.service"; | ||
import * as constants from "./sync.service"; | ||
|
||
// These tests integrate with the OpenLDAP docker image and seed data located in the openldap folder. | ||
// To run theses tests: | ||
|
@@ -20,19 +30,49 @@ | |
let logService: MockProxy<LogService>; | ||
let i18nService: MockProxy<I18nService>; | ||
let stateService: MockProxy<StateService>; | ||
|
||
let cryptoFunctionService: MockProxy<CryptoFunctionService>; | ||
let apiService: MockProxy<ApiService>; | ||
let messagingService: MockProxy<MessagingService>; | ||
let environmentService: MockProxy<EnvironmentService>; | ||
let directoryFactory: MockProxy<DirectoryFactoryService>; | ||
|
||
let batchRequestBuilder: BatchRequestBuilder; | ||
let singleRequestBuilder: SingleRequestBuilder; | ||
let syncService: SyncService; | ||
let directoryService: LdapDirectoryService; | ||
|
||
beforeEach(() => { | ||
logService = mock(); | ||
i18nService = mock(); | ||
stateService = mock(); | ||
cryptoFunctionService = mock(); | ||
apiService = mock(); | ||
messagingService = mock(); | ||
environmentService = mock(); | ||
directoryFactory = mock(); | ||
|
||
stateService.getDirectoryType.mockResolvedValue(DirectoryType.Ldap); | ||
stateService.getOrganizationId.mockResolvedValue("fakeId"); | ||
stateService.getLastUserSync.mockResolvedValue(null); // do not filter results by last modified date | ||
i18nService.t.mockImplementation((id) => id); // passthrough implementation for any error messages | ||
|
||
directoryService = new LdapDirectoryService(logService, i18nService, stateService); | ||
directoryFactory.createService.mockReturnValue(directoryService); | ||
|
||
batchRequestBuilder = new BatchRequestBuilder(); | ||
singleRequestBuilder = new SingleRequestBuilder(); | ||
|
||
syncService = new SyncService( | ||
cryptoFunctionService, | ||
apiService, | ||
messagingService, | ||
i18nService, | ||
environmentService, | ||
stateService, | ||
batchRequestBuilder, | ||
singleRequestBuilder, | ||
directoryFactory, | ||
); | ||
}); | ||
|
||
describe("basic sync fetching users and groups", () => { | ||
|
@@ -75,6 +115,60 @@ | |
const result = await directoryService.getEntries(true, true); | ||
expect(result).toEqual([groupFixtures, userFixtures]); | ||
}); | ||
|
||
it("with largeImport disabled matches directory fixture data", async () => { | ||
stateService.getDirectory | ||
.calledWith(DirectoryType.Ldap) | ||
.mockResolvedValue(getLdapConfiguration()); | ||
stateService.getSync.mockResolvedValue( | ||
getSyncConfiguration({ | ||
users: true, | ||
groups: true, | ||
}), | ||
); | ||
|
||
cryptoFunctionService.hash.mockResolvedValue(new ArrayBuffer(1)); | ||
// This arranges the last hash to be differet from the ArrayBuffer after it is converted to b64 | ||
stateService.getLastSyncHash.mockResolvedValue("unique hash"); | ||
|
||
const syncResult = await syncService.sync(false, false); | ||
const result = await directoryService.getEntries(true, true); | ||
|
||
expect(syncResult).toEqual([groupFixtures, userFixtures]); | ||
expect(result).toEqual([groupFixtures, userFixtures]); | ||
expect(result).toEqual(syncResult); | ||
|
||
expect(apiService.postPublicImportDirectory).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("with largeImport enabled matches directory fixture data", async () => { | ||
stateService.getDirectory | ||
.calledWith(DirectoryType.Ldap) | ||
.mockResolvedValue(getLdapConfiguration()); | ||
stateService.getSync.mockResolvedValue( | ||
getSyncConfiguration({ | ||
users: true, | ||
groups: true, | ||
largeImport: true, | ||
overwriteExisting: false, | ||
}), | ||
); | ||
|
||
cryptoFunctionService.hash.mockResolvedValue(new ArrayBuffer(1)); | ||
// This arranges the last hash to be differet from the ArrayBuffer after it is converted to b64 | ||
stateService.getLastSyncHash.mockResolvedValue("unique hash"); | ||
|
||
// @ts-expect-error This is a workaround to make the batchsize smaller to trigger the batching logic since its a const. | ||
constants.batchSize = 4; | ||
eliykat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const result = await directoryService.getEntries(true, true); | ||
const syncResult = await syncService.sync(false, false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also assert against the call received by the mock api service. That's an important side-effect here, and it lets you check the other request properties that aren't included in the return value, particularly |
||
|
||
expect(syncResult).toEqual([groupFixtures, userFixtures]); | ||
expect(result).toEqual([groupFixtures, userFixtures]); | ||
expect(result).toEqual(syncResult); | ||
expect(apiService.postPublicImportDirectory).toHaveBeenCalledTimes(6); | ||
}); | ||
}); | ||
|
||
describe("users", () => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { GroupEntry } from "@/src/models/groupEntry"; | ||
import { UserEntry } from "@/src/models/userEntry"; | ||
|
||
import { RequestBuilderOptions } from "../abstractions/request-builder.service"; | ||
|
||
import { SingleRequestBuilder } from "./single-request-builder"; | ||
|
||
describe("SingleRequestBuilder", () => { | ||
let singleRequestBuilder: SingleRequestBuilder; | ||
|
||
function userSimulator(userCount: number) { | ||
return Array(userCount).fill(new UserEntry()); | ||
} | ||
|
||
function groupSimulator(groupCount: number) { | ||
return Array(groupCount).fill(new GroupEntry()); | ||
} | ||
eliykat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
beforeEach(async () => { | ||
singleRequestBuilder = new SingleRequestBuilder(); | ||
}); | ||
|
||
const defaultOptions = new RequestBuilderOptions({ | ||
overwriteExisting: false, | ||
removeDisabled: false, | ||
}); | ||
eliykat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
it("SingleRequestBuilder returns single request for 200 users", () => { | ||
const mockGroups = groupSimulator(200); | ||
const mockUsers = userSimulator(200); | ||
|
||
const requests = singleRequestBuilder.buildRequest(mockGroups, mockUsers, defaultOptions); | ||
|
||
expect(requests.length).toEqual(1); | ||
}); | ||
|
||
it("SingleRequestBuilder returns request with overwriteExisting enabled", () => { | ||
const mockGroups = groupSimulator(200); | ||
const mockUsers = userSimulator(200); | ||
const options = new RequestBuilderOptions({ ...defaultOptions, overwriteExisting: true }); | ||
|
||
const request = singleRequestBuilder.buildRequest(mockGroups, mockUsers, options)[0]; | ||
expect(request.overwriteExisting).toBe(true); | ||
}); | ||
|
||
it("SingleRequestBuilder returns request with deleted user when removeDisabled is true", () => { | ||
const mockGroups = groupSimulator(200); | ||
const mockUsers = userSimulator(200); | ||
const disabledUser = new UserEntry(); | ||
const options = new RequestBuilderOptions({ ...defaultOptions, removeDisabled: true }); | ||
|
||
disabledUser.disabled = true; | ||
mockUsers.push(disabledUser); | ||
|
||
const request = singleRequestBuilder.buildRequest(mockGroups, mockUsers, options)[0]; | ||
expect(request.members.length).toEqual(201); | ||
expect(request.members.pop().deleted).toBe(true); | ||
}); | ||
|
||
it("SingleRequestBuilder returns request with deleted user and overwriteExisting enabled when overwriteExisting and removeDisabled are true", () => { | ||
const mockGroups = groupSimulator(200); | ||
const mockUsers = userSimulator(200); | ||
const disabledUser = new UserEntry(); | ||
const options = new RequestBuilderOptions({ overwriteExisting: true, removeDisabled: true }); | ||
|
||
disabledUser.disabled = true; | ||
mockUsers.push(disabledUser); | ||
|
||
const request = singleRequestBuilder.buildRequest(mockGroups, mockUsers, options)[0]; | ||
expect(request.members.pop().deleted).toBe(true); | ||
expect(request.overwriteExisting).toBe(true); | ||
}); | ||
}); |
Uh oh!
There was an error while loading. Please reload this page.