-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
8ff15c2
commit c9410d7
Showing
4 changed files
with
188 additions
and
63 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,65 @@ | ||
import { Factor } from "../../types"; | ||
|
||
export interface FactorList { | ||
allOptions: Factor[]; | ||
default: Factor | undefined; | ||
} | ||
|
||
export function authenticationMock(_flow: string, { userId }: { userId: string }) { | ||
let numCallsToSetPrimaryUser = 0; | ||
|
||
const state = { | ||
primaryUserId: userId, | ||
challenge: false as false | FactorList, | ||
enrollment: false as false | FactorList, | ||
newlyRecordedMethods: [] as string[], | ||
}; | ||
|
||
const build = <T>(api: T) => ({ | ||
challengeWith: (factor: Factor, options?: { additionalFactors?: Factor[] }) => { | ||
const additionalFactors = options?.additionalFactors ?? []; | ||
|
||
state.challenge = { | ||
allOptions: [factor, ...additionalFactors], | ||
default: factor, | ||
}; | ||
}, | ||
challengeWithAny(factors: Factor[]) { | ||
state.challenge = { | ||
allOptions: factors, | ||
default: undefined, | ||
}; | ||
}, | ||
enrollWith: (factor: Factor, options?: { additionalFactors?: Factor[] }) => { | ||
const additionalFactors = options?.additionalFactors ?? []; | ||
|
||
state.enrollment = { | ||
allOptions: [factor, ...additionalFactors], | ||
default: factor, | ||
}; | ||
}, | ||
enrollWithAny(factors: Factor[]) { | ||
state.enrollment = { | ||
allOptions: factors, | ||
default: undefined, | ||
}; | ||
}, | ||
setPrimaryUser: (primaryUserId: string) => { | ||
numCallsToSetPrimaryUser++; | ||
|
||
if (numCallsToSetPrimaryUser > 1) { | ||
throw new Error( | ||
"`authentication.setPrimaryUser` can only be set once per transaction" | ||
); | ||
} | ||
|
||
state.primaryUserId = primaryUserId; | ||
}, | ||
recordMethod: (providerUrl: string) => { | ||
state.newlyRecordedMethods.push(providerUrl); | ||
return api; | ||
}, | ||
}); | ||
|
||
return { state, build }; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import test from "node:test"; | ||
import { authenticationMock } from "../../mock/api/authentication"; | ||
import { ok, strictEqual } from "node:assert"; | ||
|
||
test("authentication mock", async (t) => { | ||
const baseApi = Symbol("Base API"); | ||
|
||
await t.test("challengeWith", async (t) => { | ||
await t.test("factor", async (t) => { | ||
const { build, state } = authenticationMock("Another Flow", { userId: "42" }); | ||
const api = build(baseApi); | ||
|
||
api.challengeWith({ type: "email" }); | ||
|
||
ok(state.challenge, "Expected challenge to be set"); | ||
strictEqual(state.challenge.default?.type, "email", "Expected default factor to be set"); | ||
strictEqual(state.challenge.allOptions?.length, 1, "Expected additional factors to be set"); | ||
strictEqual(state.challenge.allOptions?.[0].type, "email", "Expected additional factors to be email"); | ||
}); | ||
|
||
await t.test("additional factors", async (t) => { | ||
const { build, state } = authenticationMock("Another Flow", { userId: "42" }); | ||
const api = build(baseApi); | ||
|
||
api.challengeWith({ type: "otp" }, { additionalFactors: [{ type: "email" }] }); | ||
|
||
ok(state.challenge, "Expected challenge to be set"); | ||
strictEqual(state.challenge.default?.type, "otp", "Expected default factor to be set"); | ||
strictEqual(state.challenge.allOptions?.length, 2, "Expected additional factors to be set"); | ||
strictEqual(state.challenge.allOptions?.[0].type, "otp", "Expected additional factor to be otp"); | ||
strictEqual(state.challenge.allOptions?.[1].type, "email", "Expected additional factor to be email"); | ||
}); | ||
}); | ||
|
||
await t.test('challengeWithAny', async (t) => { | ||
const { build, state } = authenticationMock("Another Flow", { userId: "42" }); | ||
const api = build(baseApi); | ||
|
||
api.challengeWithAny([{ type: "email" }, { type: "otp" }]); | ||
|
||
ok(state.challenge, "Expected challenge to be set"); | ||
strictEqual(state.challenge.default, undefined, "Expected default factor to be undefined"); | ||
strictEqual(state.challenge.allOptions?.length, 2, "Expected additional factors to be set"); | ||
strictEqual(state.challenge.allOptions?.[0].type, "email", "Expected additional factor to be email"); | ||
strictEqual(state.challenge.allOptions?.[1].type, "otp", "Expected additional factor to be otp"); | ||
}); | ||
|
||
await t.test("enrollWith", async (t) => { | ||
await t.test("factor", async (t) => { | ||
const { build, state } = authenticationMock("Another Flow", { userId: "42" }); | ||
const api = build(baseApi); | ||
|
||
api.enrollWith({ type: "email" }); | ||
|
||
ok(state.enrollment, "Expected enrollment to be set"); | ||
strictEqual(state.enrollment.default?.type, "email", "Expected default factor to be set"); | ||
strictEqual(state.enrollment.allOptions?.length, 1, "Expected additional factors to be set"); | ||
strictEqual(state.enrollment.allOptions?.[0].type, "email", "Expected additional factors to be email"); | ||
}); | ||
|
||
await t.test("additional factors", async (t) => { | ||
const { build, state } = authenticationMock("Another Flow", { userId: "42" }); | ||
const api = build(baseApi); | ||
|
||
api.enrollWith({ type: "otp" }, { additionalFactors: [{ type: "email" }] }); | ||
|
||
ok(state.enrollment, "Expected enrollment to be set"); | ||
strictEqual(state.enrollment.default?.type, "otp", "Expected default factor to be set"); | ||
strictEqual(state.enrollment.allOptions?.length, 2, "Expected additional factors to be set"); | ||
strictEqual(state.enrollment.allOptions?.[0].type, "otp", "Expected additional factor to be otp"); | ||
strictEqual(state.enrollment.allOptions?.[1].type, "email", "Expected additional factor to be email"); | ||
}); | ||
}); | ||
|
||
await t.test('enrollWithAny', async (t) => { | ||
const { build, state } = authenticationMock("Another Flow", { userId: "42" }); | ||
const api = build(baseApi); | ||
|
||
api.enrollWithAny([{ type: "email" }, { type: "otp" }]); | ||
|
||
ok(state.enrollment, "Expected enrollment to be set"); | ||
strictEqual(state.enrollment.default, undefined, "Expected default factor to be undefined"); | ||
strictEqual(state.enrollment.allOptions?.length, 2, "Expected additional factors to be set"); | ||
strictEqual(state.enrollment.allOptions?.[0].type, "email", "Expected additional factor to be email"); | ||
strictEqual(state.enrollment.allOptions?.[1].type, "otp", "Expected additional factor to be otp"); | ||
}); | ||
|
||
await t.test("setPrimaryUser", async (t) => { | ||
const { build, state } = authenticationMock("Another Flow", { userId: "42" }); | ||
const api = build(baseApi); | ||
|
||
api.setPrimaryUser("43"); | ||
|
||
strictEqual(state.primaryUserId, "43", "Expected primary user to be set"); | ||
}); | ||
|
||
await t.test("recordMethod", async (t) => { | ||
const { build, state } = authenticationMock("Another Flow", { userId: "42" }); | ||
const api = build(baseApi); | ||
|
||
api.recordMethod("https://example.com"); | ||
|
||
strictEqual(state.newlyRecordedMethods.length, 1, "Expected newly recorded methods to be set"); | ||
strictEqual(state.newlyRecordedMethods[0], "https://example.com", "Expected newly recorded method to be set"); | ||
|
||
api.recordMethod("https://another.example.com"); | ||
|
||
strictEqual(state.newlyRecordedMethods.length, 2, "Expected newly recorded methods to be set"); | ||
strictEqual(state.newlyRecordedMethods[1], "https://another.example.com", "Expected newly recorded method to be set"); | ||
}); | ||
}); |
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