-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
131 additions
and
19 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
libs/vault/src/services/new-device-verification-notice.service.spec.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,86 @@ | ||
import { firstValueFrom, of } from "rxjs"; | ||
|
||
import { Utils } from "@bitwarden/common/platform/misc/utils"; | ||
import { UserId } from "@bitwarden/common/types/guid"; | ||
|
||
import { | ||
FakeAccountService, | ||
FakeSingleUserState, | ||
FakeStateProvider, | ||
mockAccountServiceWith, | ||
} from "../../../common/spec"; | ||
|
||
import { | ||
NewDeviceVerificationNoticeService, | ||
NewDeviceVerificationNotice, | ||
NEW_DEVICE_VERIFICATION_NOTICE_KEY, | ||
} from "./new-device-verification-notice.service"; | ||
|
||
describe("New Device Verification Notice", () => { | ||
const sut = NEW_DEVICE_VERIFICATION_NOTICE_KEY; | ||
const userId = Utils.newGuid() as UserId; | ||
const mockUserId$ = of(userId); | ||
let newDeviceVerificationService: NewDeviceVerificationNoticeService; | ||
let mockNoticeState: FakeSingleUserState<NewDeviceVerificationNotice>; | ||
let stateProvider: FakeStateProvider; | ||
let accountService: FakeAccountService; | ||
|
||
beforeEach(() => { | ||
accountService = mockAccountServiceWith(userId); | ||
stateProvider = new FakeStateProvider(accountService); | ||
mockNoticeState = stateProvider.singleUser.getFake(userId, NEW_DEVICE_VERIFICATION_NOTICE_KEY); | ||
newDeviceVerificationService = new NewDeviceVerificationNoticeService(stateProvider); | ||
}); | ||
|
||
it("should deserialize newDeviceVerificationNotice values", async () => { | ||
const currentDate = new Date(); | ||
const inputObj = { | ||
last_dismissal: currentDate, | ||
permanent_dismissal: false, | ||
}; | ||
|
||
const expectedFolderData = { | ||
last_dismissal: currentDate.toJSON(), | ||
permanent_dismissal: false, | ||
}; | ||
|
||
const result = sut.deserializer(JSON.parse(JSON.stringify(inputObj))); | ||
|
||
expect(result).toEqual(expectedFolderData); | ||
}); | ||
|
||
describe("notice$", () => { | ||
it("emits new device verification notice state", async () => { | ||
const currentDate = new Date(); | ||
const data = { | ||
last_dismissal: currentDate, | ||
permanent_dismissal: false, | ||
}; | ||
await stateProvider.setUserState(NEW_DEVICE_VERIFICATION_NOTICE_KEY, data, userId); | ||
|
||
const result = await firstValueFrom(newDeviceVerificationService.noticeState$(mockUserId$)); | ||
|
||
expect(result).toBe(data); | ||
}); | ||
}); | ||
|
||
describe("update notice state", () => { | ||
it("should update the date with a new value", async () => { | ||
const currentDate = new Date(); | ||
const oldDate = new Date("11-11-2011"); | ||
const oldState = { | ||
last_dismissal: oldDate, | ||
permanent_dismissal: false, | ||
}; | ||
const newState = { | ||
last_dismissal: currentDate, | ||
permanent_dismissal: true, | ||
}; | ||
mockNoticeState.nextState(oldState); | ||
await newDeviceVerificationService.updateNewDeviceVerificationNoticeState(userId, newState); | ||
|
||
const result = await firstValueFrom(newDeviceVerificationService.noticeState$(mockUserId$)); | ||
expect(result).toEqual(newState); | ||
}); | ||
}); | ||
}); |
64 changes: 45 additions & 19 deletions
64
libs/vault/src/services/new-device-verification-notice.service.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