-
Notifications
You must be signed in to change notification settings - Fork 222
task(libs): Add passkeys data model and db migration #20019
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
Merged
+949
−27
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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
156 changes: 156 additions & 0 deletions
156
libs/accounts/passkey/src/lib/passkey.repository.in.spec.ts
This file contains hidden or 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,156 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| import { faker } from '@faker-js/faker'; | ||
| import { | ||
| AccountDatabase, | ||
| testAccountDatabaseSetup, | ||
| PasskeyFactory, | ||
| } from '@fxa/shared/db/mysql/account'; | ||
| import { AccountManager } from '@fxa/shared/account/account'; | ||
| import * as PasskeyRepository from './passkey.repository'; | ||
|
|
||
| describe('PasskeyRepository (Integration)', () => { | ||
| let db: AccountDatabase; | ||
| let accountManager: AccountManager; | ||
|
|
||
| beforeAll(async () => { | ||
| try { | ||
| db = await testAccountDatabaseSetup(['accounts', 'emails', 'passkeys']); | ||
| accountManager = new AccountManager(db); | ||
| } catch (error) { | ||
| console.warn('\n⚠️ Integration tests require database infrastructure.'); | ||
| console.warn( | ||
| ' Run "yarn start infrastructure" to enable these tests.\n' | ||
| ); | ||
| throw error; | ||
| } | ||
| }); | ||
|
|
||
| // Helper to create an account for testing | ||
| async function createTestAccount() { | ||
| const email = faker.internet.email(); | ||
| const uidHex = await accountManager.createAccountStub(email, 1, 'en-US'); | ||
| return Buffer.from(uidHex, 'hex'); | ||
| } | ||
|
|
||
| afterAll(async () => { | ||
| if (db) { | ||
| await db.destroy(); | ||
| } | ||
| }); | ||
|
|
||
| describe('insert and find operations', () => { | ||
| it('should insert and retrieve a passkey', async () => { | ||
| const uid = await createTestAccount(); | ||
| const passkey = PasskeyFactory({ uid }); | ||
|
|
||
| await PasskeyRepository.insertPasskey(db, passkey); | ||
|
|
||
| const found = await PasskeyRepository.findPasskeyByCredentialId( | ||
| db, | ||
| passkey.credentialId | ||
| ); | ||
|
|
||
| expect(found).toBeDefined(); | ||
| expect(found?.uid).toEqual(passkey.uid); | ||
| expect(found?.name).toBe(passkey.name); | ||
| expect(found?.transports).toBeDefined(); // JSON field | ||
| expect(found?.aaguid).toEqual(passkey.aaguid); // NOT NULL | ||
| }); | ||
|
|
||
| it('should find all passkeys for a user', async () => { | ||
| const uid = await createTestAccount(); | ||
| const passkey1 = PasskeyFactory({ uid }); | ||
| const passkey2 = PasskeyFactory({ uid }); | ||
|
|
||
| await PasskeyRepository.insertPasskey(db, passkey1); | ||
| await PasskeyRepository.insertPasskey(db, passkey2); | ||
|
|
||
| const passkeys = await PasskeyRepository.findPasskeysByUid(db, uid); | ||
|
|
||
| expect(passkeys.length).toBeGreaterThanOrEqual(2); | ||
| }); | ||
| }); | ||
|
|
||
| describe('update operations', () => { | ||
| it('should update passkey name', async () => { | ||
| const uid = await createTestAccount(); | ||
| const passkey = PasskeyFactory({ uid }); | ||
| await PasskeyRepository.insertPasskey(db, passkey); | ||
|
|
||
| const rowsUpdated = await PasskeyRepository.updatePasskeyName( | ||
| db, | ||
| passkey.credentialId, | ||
| 'New Name' | ||
| ); | ||
|
|
||
| expect(rowsUpdated).toBe(1); | ||
|
|
||
| const updated = await PasskeyRepository.findPasskeyByCredentialId( | ||
| db, | ||
| passkey.credentialId | ||
| ); | ||
| expect(updated?.name).toBe('New Name'); | ||
| }); | ||
|
|
||
| it('should update counter and lastUsed after authentication', async () => { | ||
| const uid = await createTestAccount(); | ||
| const passkey = PasskeyFactory({ uid }); | ||
| await PasskeyRepository.insertPasskey(db, passkey); | ||
|
|
||
| const success = await PasskeyRepository.updatePasskeyCounterAndLastUsed( | ||
| db, | ||
| passkey.credentialId, | ||
| 5, | ||
| 1 | ||
| ); | ||
|
|
||
| expect(success).toBe(true); | ||
|
|
||
| const updated = await PasskeyRepository.findPasskeyByCredentialId( | ||
| db, | ||
| passkey.credentialId | ||
| ); | ||
| expect(updated?.signCount).toBe(5); | ||
| expect(updated?.backupState).toBe(true); | ||
| expect(updated?.lastUsedAt).toBeGreaterThan(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe('delete operations', () => { | ||
| it('should delete a specific passkey', async () => { | ||
| const uid = await createTestAccount(); | ||
| const passkey = PasskeyFactory({ uid }); | ||
| await PasskeyRepository.insertPasskey(db, passkey); | ||
|
|
||
| const deleted = await PasskeyRepository.deletePasskey( | ||
| db, | ||
| passkey.uid, | ||
| passkey.credentialId | ||
| ); | ||
|
|
||
| expect(deleted).toBe(true); | ||
|
|
||
| const found = await PasskeyRepository.findPasskeyByCredentialId( | ||
| db, | ||
| passkey.credentialId | ||
| ); | ||
| expect(found).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should count passkeys for a user', async () => { | ||
| const uid = await createTestAccount(); | ||
| const passkey1 = PasskeyFactory({ uid }); | ||
| const passkey2 = PasskeyFactory({ uid }); | ||
|
|
||
| await PasskeyRepository.insertPasskey(db, passkey1); | ||
| await PasskeyRepository.insertPasskey(db, passkey2); | ||
|
|
||
| const count = await PasskeyRepository.countPasskeysByUid(db, uid); | ||
|
|
||
| expect(count).toBeGreaterThanOrEqual(2); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔥