-
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.
Merge pull request #15 from kilterset/post-user-registration
Post User Registration
- Loading branch information
Showing
17 changed files
with
249 additions
and
14 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
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,14 @@ | ||
/** | ||
* Notify a Slack channel when a new user registers. | ||
*/ | ||
exports.onExecutePostUserRegistration = async (event) => { | ||
const payload = { | ||
text: `New User: ${event.user.email}`, | ||
}; | ||
|
||
await fetch(event.secrets.SLACK_WEBHOOK_URL, { | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify(payload), | ||
}); | ||
}; |
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,42 @@ | ||
const test = require("node:test"); | ||
const { strictEqual, deepStrictEqual } = require("node:assert"); | ||
const { | ||
onExecutePostUserRegistration, | ||
} = require("./notify-slack-post-user-registration"); | ||
const { nodeTestRunner } = require("@kilterset/auth0-actions-testing"); | ||
|
||
test("post user registration", async (t) => { | ||
const { auth0, fetchMock } = await nodeTestRunner.actionTestSetup(t); | ||
|
||
await t.test("service is notified when user logs in", async (t) => { | ||
fetchMock.mock("https://slack/hook", 201); | ||
|
||
const action = auth0.mock.actions.postUserRegistration({ | ||
secrets: { SLACK_WEBHOOK_URL: "https://slack/hook" }, | ||
user: auth0.mock.user({ email: "[email protected]" }), | ||
}); | ||
|
||
await action.simulate(onExecutePostUserRegistration); | ||
|
||
const calls = fetchMock.calls(); | ||
|
||
strictEqual(calls.length, 1, "Expected 1 fetch call to be made."); | ||
|
||
const [url, options] = calls[0]; | ||
|
||
strictEqual(url, "https://slack/hook", "Unexpected URL"); | ||
strictEqual(options.method, "POST", "Unexpected request method"); | ||
|
||
deepStrictEqual( | ||
options.headers, | ||
{ "Content-Type": "application/json" }, | ||
"Unexpected headers" | ||
); | ||
|
||
deepStrictEqual( | ||
JSON.parse(options.body), | ||
{ text: "New User: [email protected]" }, | ||
"Unexpected body" | ||
); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from "./credentials-exchange"; | ||
export * from "./post-challenge"; | ||
export * from "./post-login"; | ||
export * from "./post-user-registration"; | ||
export * from "./pre-user-registration"; |
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,44 @@ | ||
import { api, events } from ".."; | ||
import Auth0 from "../../types"; | ||
import { PostChallengeOptions } from "../api"; | ||
|
||
type Handler = ( | ||
event: Auth0.Events.PostUserRegistration, | ||
api: Auth0.API.PostUserRegistration | ||
) => Promise<void>; | ||
|
||
export function postUserRegistration({ | ||
cache, | ||
...attributes | ||
}: Parameters<typeof events.postUserRegistration>[0] & | ||
Omit<PostChallengeOptions, "user" | "request"> = {}) { | ||
const event = events.postUserRegistration(attributes); | ||
|
||
const { implementation, state } = api.postUserRegistration({ cache }); | ||
|
||
async function simulate(handler: Handler) { | ||
await handler(event, implementation); | ||
} | ||
|
||
return new Proxy( | ||
{ | ||
event, | ||
simulate, | ||
}, | ||
{ | ||
get(target, prop) { | ||
if (typeof prop !== "string") { | ||
return; | ||
} | ||
|
||
if (prop in target) { | ||
return target[prop as keyof typeof target]; | ||
} | ||
|
||
if (prop in state) { | ||
return state[prop as keyof typeof state]; | ||
} | ||
}, | ||
} | ||
); | ||
} |
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,29 @@ | ||
import Auth0 from "../../types"; | ||
import { cache as mockCache } from "./cache"; | ||
|
||
export interface PostUserRegistrationOptions { | ||
cache?: Record<string, string>; | ||
} | ||
|
||
export interface PostUserRegistrationState { | ||
cache: Auth0.API.Cache; | ||
} | ||
|
||
export function postUserRegistration({ | ||
cache, | ||
}: PostUserRegistrationOptions = {}) { | ||
const apiCache = mockCache(cache); | ||
|
||
const state: PostUserRegistrationState = { | ||
cache: apiCache, | ||
}; | ||
|
||
const api: Auth0.API.PostUserRegistration = { | ||
cache: apiCache, | ||
}; | ||
|
||
return { | ||
implementation: api, | ||
state, | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./post-login"; | ||
export * from "./credentials-exchange"; | ||
export * from "./post-user-registration"; | ||
export * from "./pre-user-registration"; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { define } from "../define"; | ||
import Auth0, { Connection } from "../../types"; | ||
import { user } from "../user"; | ||
import { request } from "../request"; | ||
import { transaction } from "../transaction"; | ||
import { connection } from "../connection"; | ||
import { chance } from "../chance"; | ||
import { identity } from "../identity"; | ||
|
||
export const postUserRegistration = define<Auth0.Events.PostUserRegistration>( | ||
({ params }): Auth0.Events.PostUserRegistration => { | ||
const tenantId = params.tenant?.id || chance.auth0().tenantId(); | ||
const hostname = params.request?.hostname || `${tenantId}.auth0.com`; | ||
|
||
const connectionValue = params.connection | ||
? (params.connection as Connection) | ||
: connection(); | ||
|
||
const identities = params.user?.identities || []; | ||
|
||
identities.splice( | ||
0, | ||
1, | ||
identity({ | ||
connection: connectionValue.name, | ||
provider: connectionValue.strategy, | ||
...(identities[0] || {}), | ||
}) | ||
); | ||
|
||
const userValue = user({ ...params.user, identities }); | ||
|
||
const requestValue = request({ | ||
hostname, | ||
...params.request, | ||
query: { | ||
connection: connectionValue.name, | ||
...params.request?.query, | ||
}, | ||
}); | ||
|
||
const transactionValue = transaction(); | ||
|
||
return { | ||
transaction: transactionValue, | ||
connection: connectionValue, | ||
tenant: { id: tenantId }, | ||
request: requestValue, | ||
user: userValue, | ||
secrets: {}, | ||
}; | ||
} | ||
); |
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,22 @@ | ||
import test from "node:test"; | ||
import { strictEqual, deepStrictEqual } from "node:assert"; | ||
import { postUserRegistration } from "../../mock/api"; | ||
|
||
test("Post User Registration API", async (t) => { | ||
await t.test("cache", async (t) => { | ||
await t.test("can set cache", async (t) => { | ||
const { implementation: api, state } = postUserRegistration(); | ||
strictEqual(api.cache.set("location", "Ōtautahi").type, "success"); | ||
deepStrictEqual(state.cache.get("location"), "Ōtautahi"); | ||
}); | ||
|
||
await t.test("can get cache", async (t) => { | ||
const { implementation: api, state } = postUserRegistration({ | ||
cache: { location: "Ōtautahi" }, | ||
}); | ||
|
||
strictEqual(state.cache.get("location"), "Ōtautahi"); | ||
strictEqual(state.cache.get("nonexistent"), undefined); | ||
}); | ||
}); | ||
}); |
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,9 @@ | ||
import { User } from "../user"; | ||
import { Cache } from "./cache"; | ||
|
||
export interface PostUserRegistration { | ||
/** | ||
* Store and retrieve data that persists across executions. | ||
*/ | ||
readonly cache: Cache; | ||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from "./credentials-exchange"; | ||
export * from "./post-challenge"; | ||
export * from "./post-login"; | ||
export * from "./post-user-registration"; | ||
export * from "./pre-user-registration"; |
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,19 @@ | ||
import { Authentication } from "../authentication"; | ||
import { Client } from "../client"; | ||
import { Connection } from "../connection"; | ||
import { Request } from "../request"; | ||
import { Transaction } from "../transaction"; | ||
import { User } from "../user"; | ||
|
||
export interface PostUserRegistration { | ||
connection: Connection; | ||
request?: Request; | ||
tenant: { | ||
id: string; | ||
}; | ||
transaction?: Transaction; | ||
user: User; | ||
secrets: { | ||
[key: string]: string; | ||
}; | ||
} |