-
Notifications
You must be signed in to change notification settings - Fork 229
chore(auth-server): minor code cleanup and fixes #19940
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
Open
sreecharan-desu
wants to merge
2
commits into
mozilla:main
Choose a base branch
from
sreecharan-desu:fix/auth-server-cleanups
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+133
−4
Open
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
121 changes: 121 additions & 0 deletions
121
packages/fxa-shared/test/db/models/auth/session-token.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,121 @@ | ||
| import 'mocha'; | ||
| import { assert } from 'chai'; | ||
| import sinon from 'sinon'; | ||
| import { SessionToken } from '../../../../db/models/auth'; | ||
| import { uuidTransformer } from '../../../../db/transformers'; | ||
| import crypto from 'crypto'; | ||
|
|
||
| const toRandomBuff = (size) => | ||
| uuidTransformer.to(crypto.randomBytes(size).toString('hex')); | ||
|
|
||
| describe('SessionToken Unit Tests', () => { | ||
| let sandbox; | ||
|
|
||
| beforeEach(() => { | ||
| sandbox = sinon.createSandbox(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| sandbox.restore(); | ||
| }); | ||
|
|
||
| it('filters out expired tokens (older than 28 days by default)', async () => { | ||
| const now = Date.now(); | ||
| // 28 days in milliseconds is 2419200000 | ||
| const validTime = now - 2419200000 + 10000; // 10 seconds remaining | ||
| const expiredTime = now - 2419200000 - 10000; // 10 seconds overdue | ||
|
|
||
| const uidBuff = toRandomBuff(16); | ||
|
|
||
| const validRow = { | ||
| tokenId: toRandomBuff(16), | ||
| tokenData: toRandomBuff(16), | ||
| uid: uidBuff, | ||
| createdAt: validTime, | ||
| lastAccessTime: validTime, | ||
| authAt: 1, | ||
| verificationMethod: 0, | ||
| verifiedAt: 0, | ||
| mustVerify: 0, | ||
| deviceId: null, // explicit null for no device | ||
| emailVerified: 1, | ||
| email: 'test@test.com', | ||
| emailCode: 'code', | ||
| deviceCallbackIsExpired: 0, | ||
| }; | ||
|
|
||
| const expiredRow = { | ||
| ...validRow, | ||
| tokenId: toRandomBuff(16), | ||
| createdAt: expiredTime, | ||
| }; | ||
|
|
||
| sandbox.stub(SessionToken, 'callProcedure').resolves({ | ||
| rows: [validRow, expiredRow], | ||
| }); | ||
|
|
||
| const tokens = await SessionToken.findByUid( | ||
| '0123456789abcdef0123456789abcdef' | ||
| ); | ||
|
|
||
| assert.equal( | ||
| tokens.length, | ||
| 1, | ||
| 'Should return exactly 1 token (the valid one)' | ||
| ); | ||
| assert.equal(tokens[0].createdAt, validTime); | ||
| }); | ||
|
|
||
| it('respects configured expiry', async () => { | ||
| const originalExpiry = SessionToken.sessionExpiryMs; | ||
| try { | ||
| // Set expiry to 1 hour | ||
| SessionToken.sessionExpiryMs = 60 * 60 * 1000; // 1 hour | ||
| const now = Date.now(); | ||
| const validTime = now - 30 * 60 * 1000; // 30 mins old (valid) | ||
| const expiredTime = now - 90 * 60 * 1000; // 90 mins old (expired) | ||
|
|
||
| const uidBuff = toRandomBuff(16); | ||
|
|
||
| const validRow = { | ||
| tokenId: toRandomBuff(16), | ||
| tokenData: toRandomBuff(16), | ||
| uid: uidBuff, | ||
| createdAt: validTime, | ||
| lastAccessTime: validTime, | ||
| authAt: 1, | ||
| verificationMethod: 0, | ||
| verifiedAt: 0, | ||
| mustVerify: 0, | ||
| deviceId: null, | ||
| emailVerified: 1, | ||
| email: 'test@test.com', | ||
| emailCode: 'code', | ||
| deviceCallbackIsExpired: 0, | ||
| }; | ||
|
|
||
| const expiredRow = { | ||
| ...validRow, | ||
| tokenId: toRandomBuff(16), | ||
| createdAt: expiredTime, | ||
| }; | ||
|
|
||
| sandbox.stub(SessionToken, 'callProcedure').resolves({ | ||
| rows: [validRow, expiredRow], | ||
| }); | ||
|
|
||
| const tokens = await SessionToken.findByUid( | ||
| '0123456789abcdef0123456789abcdef' | ||
| ); | ||
|
|
||
| assert.equal( | ||
| tokens.length, | ||
| 1, | ||
| 'Should return exactly 1 token (the valid one)' | ||
| ); | ||
| assert.equal(tokens[0].createdAt, validTime); | ||
| } finally { | ||
| SessionToken.sessionExpiryMs = originalExpiry; | ||
| } | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.