-
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
Showing
5 changed files
with
89 additions
and
15 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,10 @@ | ||
exports.onExecutePostLogin = async (event, api) => { | ||
if (event.user.app_metadata["lucky_number"]) { | ||
// Do nothing. User already has a lucky number. | ||
return; | ||
} | ||
|
||
const diceRoll = Math.round(Math.random() * event.secrets.MAX_LUCKY_NUMBER); | ||
|
||
api.user.setAppMetadata("lucky_number", diceRoll); | ||
}; |
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 @@ | ||
const test = require("node:test"); | ||
const { ok, strictEqual } = require("node:assert"); | ||
const { onExecutePostLogin } = require("./app-metadata"); | ||
|
||
const { | ||
actionTestSetup, | ||
} = require("@kilterset/auth0-actions-testing/node-test-runner"); | ||
|
||
test("onExecutePostLogin", async (t) => { | ||
const { auth0 } = await actionTestSetup(t); | ||
|
||
await t.test("records a lucky number", async () => { | ||
const action = auth0.mock.actions.postLogin({ | ||
secrets: { MAX_LUCKY_NUMBER: 42 }, | ||
user: auth0.mock.user({ | ||
app_metadata: {}, | ||
}), | ||
}); | ||
|
||
await action.simulate(onExecutePostLogin); | ||
|
||
const { lucky_number } = action.user.app_metadata; | ||
|
||
ok( | ||
typeof lucky_number === "number", | ||
`Expected the user's lucky number to be a number (got ${lucky_number})` | ||
); | ||
|
||
ok( | ||
lucky_number < 42, | ||
`Expected lucky number not to exceed the maximum allowed (got ${lucky_number})` | ||
); | ||
}); | ||
|
||
await t.test("does not overwrite lucky numbers", async () => { | ||
const action = auth0.mock.actions.postLogin({ | ||
secrets: { MAX_LUCKY_NUMBER: 42 }, | ||
user: auth0.mock.user({ | ||
app_metadata: { lucky_number: 17 }, | ||
}), | ||
}); | ||
|
||
await action.simulate(onExecutePostLogin); | ||
|
||
const { lucky_number } = action.user.app_metadata; | ||
|
||
strictEqual( | ||
lucky_number, | ||
17, | ||
`Expected the user's lucky number to be unchanged` | ||
); | ||
}); | ||
}); |
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