-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ (dmk) [NO-ISSUE]: Add new ToggleDeviceSessionRefresher method (#548)
- Loading branch information
Showing
11 changed files
with
209 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@ledgerhq/device-management-kit": minor | ||
--- | ||
|
||
Add new toggle for the device session refresher |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...evice-management-kit/src/api/device-session/use-case/ToggleDeviceSessionRefresher.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { type LoggerPublisherService } from "@api/logger-publisher/service/LoggerPublisherService"; | ||
import { type DeviceSession } from "@internal/device-session/model/DeviceSession"; | ||
import { deviceSessionStubBuilder } from "@internal/device-session/model/DeviceSession.stub"; | ||
import { DeviceSessionNotFound } from "@internal/device-session/model/Errors"; | ||
import { DefaultDeviceSessionService } from "@internal/device-session/service/DefaultDeviceSessionService"; | ||
import { type DeviceSessionService } from "@internal/device-session/service/DeviceSessionService"; | ||
import { DefaultLoggerPublisherService } from "@internal/logger-publisher/service/DefaultLoggerPublisherService"; | ||
import { AxiosManagerApiDataSource } from "@internal/manager-api/data/AxiosManagerApiDataSource"; | ||
import { DefaultManagerApiService } from "@internal/manager-api/service/DefaultManagerApiService"; | ||
import { type ManagerApiService } from "@internal/manager-api/service/ManagerApiService"; | ||
|
||
import { ToggleDeviceSessionRefresherUseCase } from "./ToggleDeviceSessionRefresher"; | ||
|
||
let logger: LoggerPublisherService; | ||
let sessionService: DeviceSessionService; | ||
let useCase: ToggleDeviceSessionRefresherUseCase; | ||
let deviceSession: DeviceSession; | ||
let managerApi: ManagerApiService; | ||
describe("ToggleDeviceSessionRefresherUseCase", () => { | ||
beforeEach(() => { | ||
logger = new DefaultLoggerPublisherService( | ||
[], | ||
"get-connected-device-use-case-test", | ||
); | ||
sessionService = new DefaultDeviceSessionService(() => logger); | ||
managerApi = new DefaultManagerApiService( | ||
new AxiosManagerApiDataSource({ | ||
managerApiUrl: "http://fake.url", | ||
mockUrl: "http://fake-mock.url", | ||
}), | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe("execute", () => { | ||
it("should toggle the device session refresher", () => { | ||
// given | ||
deviceSession = deviceSessionStubBuilder( | ||
{ id: "fakeSessionId" }, | ||
() => logger, | ||
managerApi, | ||
); | ||
sessionService.addDeviceSession(deviceSession); | ||
useCase = new ToggleDeviceSessionRefresherUseCase( | ||
sessionService, | ||
() => logger, | ||
); | ||
|
||
const spy = jest.spyOn(deviceSession, "toggleRefresher"); | ||
|
||
// when | ||
useCase.execute({ sessionId: "fakeSessionId", enabled: false }); | ||
|
||
// then | ||
expect(spy).toHaveBeenCalledWith(false); | ||
deviceSession.close(); | ||
}); | ||
|
||
it("should throw error when deviceSession is not found", () => { | ||
// given | ||
useCase = new ToggleDeviceSessionRefresherUseCase( | ||
sessionService, | ||
() => logger, | ||
); | ||
|
||
// when | ||
try { | ||
useCase.execute({ sessionId: "fakeSessionId", enabled: false }); | ||
} catch (error) { | ||
// then | ||
expect(error).toBeInstanceOf(DeviceSessionNotFound); | ||
} | ||
}); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
...ges/device-management-kit/src/api/device-session/use-case/ToggleDeviceSessionRefresher.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { inject, injectable } from "inversify"; | ||
|
||
import { type DeviceSessionId } from "@api/device-session/types"; | ||
import { LoggerPublisherService } from "@api/logger-publisher/service/LoggerPublisherService"; | ||
import { deviceSessionTypes } from "@internal/device-session/di/deviceSessionTypes"; | ||
import { type DeviceSessionService } from "@internal/device-session/service/DeviceSessionService"; | ||
import { loggerTypes } from "@internal/logger-publisher/di/loggerTypes"; | ||
|
||
export type ToggleDeviceSessionRefresherUseCaseArgs = { | ||
sessionId: DeviceSessionId; | ||
enabled: boolean; | ||
}; | ||
|
||
/** | ||
* Toggle the device session refresher. | ||
*/ | ||
@injectable() | ||
export class ToggleDeviceSessionRefresherUseCase { | ||
private readonly _logger: LoggerPublisherService; | ||
constructor( | ||
@inject(deviceSessionTypes.DeviceSessionService) | ||
private readonly _sessionService: DeviceSessionService, | ||
@inject(loggerTypes.LoggerPublisherServiceFactory) | ||
loggerFactory: (tag: string) => LoggerPublisherService, | ||
) { | ||
this._logger = loggerFactory("ToggleDeviceSessionRefresherUseCase"); | ||
} | ||
|
||
execute({ sessionId, enabled }: ToggleDeviceSessionRefresherUseCaseArgs) { | ||
const errorOrDeviceSession = | ||
this._sessionService.getDeviceSessionById(sessionId); | ||
|
||
return errorOrDeviceSession.caseOf({ | ||
Left: (error) => { | ||
this._logger.error("Error getting device session", { data: { error } }); | ||
throw error; | ||
}, | ||
Right: (deviceSession) => deviceSession.toggleRefresher(enabled), | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters