-
Notifications
You must be signed in to change notification settings - Fork 55
doc: testing lambdas #2340
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
doc: testing lambdas #2340
Conversation
Testing lambdas fix formatting
…edit Testing lambdas language first edit
Testing lambdas language edit
mooreds
left a comment
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.
General feedback:
- weird that the TOC is cramped (at least, I saw that--did you?)
- first character of each word in a header should be capitalized
|
|
||
| ## Prerequisites | ||
|
|
||
| To follow this guide, you will need a test instance of FusionAuth. For FusionAuth installation instructions, please visit [the 5-minute setup guide](/docs/v1/tech/5-minute-setup-guide). |
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.
prerequisite isn't just a running fusionauth instance, seems like you have to go through the 5 minute guide to get the .js scripts
|
|
||
| ### Lambda Limitations | ||
|
|
||
| {% include _callout-note.liquid content="<p>Remember the following limitations of lambdas when planning what they'll do:<ul><li>Lambdas do not have full access to JavaScript libraries, nor can they load them currently.</li> <li>The console methods take only one argument.</li><li>HTTP requests are not available in the Community or Starter FusionAuth plans.</li><li>If you set the Identity Provider <a href='/docs/v1/tech/identity-providers/#linking-strategies'>linking strategy</a> to 'Link Anonymously', no lambdas will be used for external authentication.</li> </ul></p>"%} |
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.
Can you break this out to a separate include file?
Would like to include them in the known limitations section as well.
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.
added the file site/_includes/docs/_lambda-limitations.adoc
| eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImd0eSI6WyJhdXRob3JpemF0aW9uX2NvZGUiXSwia2lkIjoiMWU1NmM0OWU4In0.eyJhdWQiOiJkZGQwNTAyMS0wNjgyLTQ4NWUtYThlMi1kMDMyOTY0YjAyMTEiLCJleHAiOjE2ODkyNjQwNzEsImlhdCI6MTY4OTI2MDQ3MSwiaXNzIjoiYWNtZS5jb20iLCJzdWIiOiIwYTkzOTYwNi0zNmVjLTQ1M2ItOTM0Mi04ZWZmOTE3ZjJhZWYiLCJqdGkiOiIyYmZlMjUwNy1hZWM0LTRjOTEtYWY5Yy1hOWVhYjQzNmQ4MGYiLCJhdXRoZW50aWNhdGlvblR5cGUiOiJQQVNTV09SRCIsImVtYWlsIjoiZXJsaWNoQGV4YW1wbGUuY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImFwcGxpY2F0aW9uSWQiOiJkZGQwNTAyMS0wNjgyLTQ4NWUtYThlMi1kMDMyOTY0YjAyMTEiLCJzY29wZSI6Im9mZmxpbmVfYWNjZXNzIiwicm9sZXMiOltdLCJzaWQiOiIzNDk5MTAxMS1kNzUxLTRlOTctYWZiNi0zNzQ2N2RlYTc5YWIiLCJhdXRoX3RpbWUiOjE2ODkyNjA0NzEsInRpZCI6ImNiY2VkOWVhLWQ3NzgtZDBlYi03ZjU4LWE0MGYxY2VlNWFhYiIsIm1lc3NhZ2UiOiJIZWxsbyBXb3JsZCEifQ.3DOvP8LRAp6pIh0guUjJjYbNwZKzruVWre8Xq8x_S8k | ||
| ``` | ||
|
|
||
| Copy this token from your terminal and paste it into the <span>Encoded</span>{:.field} text box at [jwt.io](https://jwt.io). You'll see `"message": "Hello World!"` in the <span>Payload</span>{:.uielement} box at the bottom right, showing you that your new lambda ran correctly. |
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.
Please don't point to jwt.io. Point to the fusionauth jwt decoder.
| Make a new file in the app called `test.js` and paste the following code into it. | ||
| ```js | ||
| const client = require('@fusionauth/typescript-client'); |
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.
Let's put this in a repo and import it using remote_include, please.
| node test.js | npx faucet | ||
| ``` | ||
|
|
||
| In reality, you'll want to create a random user at the beginning of each login test and remove it at the end. This will allow you to add multiple lambda tests while avoiding potential conflicts between test users and permissions. |
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.
If this is best practice, why don't we do in the script? That seems reasonable to do.
|
|
||
| The next test you'll write is a unit test that verifies your lambda locally using a fake mock service and not in FusionAuth. The benefit of this test is that you can test your logic works without needing an external service to be reliable at the time of testing. The danger is that your test might pass locally, but the lambda might fail on FusionAuth due to it running on a different JavaScript environment with different restrictions and configuration. | ||
|
|
||
| Let's take an example where you check if users have email addresses from a country sanctioned by the United States, such as North Korea or Cuba. You call an external site, `https://issanctioned.example.com`, with an email address, and are told whether the domain is banned or not. |
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.
Cool example!
| ``` | ||
|
|
||
| In reality, you'll want to create a random user at the beginning of each login test and remove it at the end. This will allow you to add multiple lambda tests while avoiding potential conflicts between test users and permissions. | ||
|
|
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.
Would be great to have another unit test example where you are pulling attributes from the user object, performing logic on it, and then setting a value on a jwt. Maybe the user has a certain role ('admin', 'editor', 'viewer') and the JWT is given certain permissions based on those: (admin -> 'all', 'editor' -> 'read', 'write', viewer' -> 'read'). or something similar. But I think this kind of logic is where unit tests shine, plus showing how to mock up a user object would be useful.
Co-authored-by: Dan Moore <dan@fusionauth.io>
…up on including local adoc snippets in md.
# Conflicts: # DocsDevREADME.md
…e-edit Revised Testing Lambdas language edit
|
@mooreds The example repo is here https://github.com/ritza-co/fusionauth-example-testing-lambdas Depends on FusionAuth/fusionauth-node-cli#8 |
|
The TOC being cramped seems to have something to do with the file being markdown and not adoc |
Don't worry about it then. Just remove it. Thanks! |
| * `method`, which defaults to `GET` | ||
| * `headers`, which defaults to null | ||
| * `body`, which must be a string | ||
| {% include docs/_lambda-limitations.adoc %} |
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.
TODO for Dan: update the lambda section of the limitations doc to include this.
| @@ -0,0 +1,380 @@ | |||
| --- | |||
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.
Can you move this file and the images to the guides section and rename this "Lambda Guide"
Because it does more than just testing.
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.
so it should live under https://fusionauth.io/docs/v1/tech/guides/
| } | ||
| ``` | ||
| You can see that the user object has data that the JWT does not, like names, birthdates, and languages, that you might want to add in a lambda. |
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.
I'm sorry I was vague here. What I'd love to do is see someone able to:
- download their lambda via the node CLI
- run a unit test against that lambda where the test code is outside of the directory, but the downloaded lambda is being tested
- have the user and other objects mocked up by the test
Does that make sense? What you provided is helpful, but the user will still have to copy and paste their lambda code into test.js every time it is downloaded.
Can we import the lambda function into the test context?
| Although you can use the [lambda API](/docs/v1/tech/apis/lambdas) directly by making HTTP requests, it's much easier to use one of the provided [client libraries](/docs/v1/tech/client-libraries/). | ||
|
|
||
| There are two libraries for Javascript: |
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.
| There are two libraries for Javascript: | |
| There are two ways to do this using Javascript: |
| eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImd0eSI6WyJhdXRob3JpemF0aW9uX2NvZGUiXSwia2lkIjoiMWU1NmM0OWU4In0.eyJhdWQiOiJkZGQwNTAyMS0wNjgyLTQ4NWUtYThlMi1kMDMyOTY0YjAyMTEiLCJleHAiOjE2ODkyNjQwNzEsImlhdCI6MTY4OTI2MDQ3MSwiaXNzIjoiYWNtZS5jb20iLCJzdWIiOiIwYTkzOTYwNi0zNmVjLTQ1M2ItOTM0Mi04ZWZmOTE3ZjJhZWYiLCJqdGkiOiIyYmZlMjUwNy1hZWM0LTRjOTEtYWY5Yy1hOWVhYjQzNmQ4MGYiLCJhdXRoZW50aWNhdGlvblR5cGUiOiJQQVNTV09SRCIsImVtYWlsIjoiZXJsaWNoQGV4YW1wbGUuY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImFwcGxpY2F0aW9uSWQiOiJkZGQwNTAyMS0wNjgyLTQ4NWUtYThlMi1kMDMyOTY0YjAyMTEiLCJzY29wZSI6Im9mZmxpbmVfYWNjZXNzIiwicm9sZXMiOltdLCJzaWQiOiIzNDk5MTAxMS1kNzUxLTRlOTctYWZiNi0zNzQ2N2RlYTc5YWIiLCJhdXRoX3RpbWUiOjE2ODkyNjA0NzEsInRpZCI6ImNiY2VkOWVhLWQ3NzgtZDBlYi03ZjU4LWE0MGYxY2VlNWFhYiIsIm1lc3NhZ2UiOiJIZWxsbyBXb3JsZCEifQ.3DOvP8LRAp6pIh0guUjJjYbNwZKzruVWre8Xq8x_S8k | ||
| ``` | ||
|
|
||
| Copy this token from your terminal and paste it into the <span>Token</span>{:.field} text box on the [FusionAuth Online JWT Decoder](https://fusionauth.io/dev-tools/jwt-decoder). You'll see `"message": "Hello World!"` in the <span>Payload</span>{:.uielement} box, showing you that your new lambda ran correctly. |
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.
| Copy this token from your terminal and paste it into the <span>Token</span>{:.field} text box on the [FusionAuth Online JWT Decoder](https://fusionauth.io/dev-tools/jwt-decoder). You'll see `"message": "Hello World!"` in the <span>Payload</span>{:.uielement} box, showing you that your new lambda ran correctly. | |
| Copy this token from your terminal and paste it into the <span>Token</span>{:.field} text box on the [FusionAuth Online JWT Decoder](/dev-tools/jwt-decoder). You'll see `"message": "Hello World!"` in the <span>Payload</span>{:.uielement} box, showing you that your new lambda ran correctly. |
Please remove all absolute URL references to fusionauth.io.
| Add this new function to `test.js`. | ||
|
|
||
| ```js | ||
| {% remote_include 'https://raw.githubusercontent.com/FusionAuth/fusionauth-example-testing-lambdas/main/complete-application/documentation_snippets/test_3.js' %} |
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.
I don't understand the point of putting this into a documentation snippet? The point of pulling this from a remote server is that the code will be runnable and stay up to date. See for example the doc here: https://fusionauth.io/docs/v1/tech/themes/client-side-password-rule-validation
How does putting these into a documentation snippet help with this goal?
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.
How about instead you build up a test suite that the user can run?
| node test.js | ||
| ``` | ||
|
|
||
| In FusionAuth, click on <span>Users</span>{:.breadcrumb} to check that a new user called `lambdatestuser` has been created. You can delete the `createRandomUser(uuidv4());` line in `test.js` as each test will use a new temporary user. This will allow you to add multiple lambda tests while avoiding potential conflicts between test users and permissions. |
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.
why did we call createRandomUser here just to delete it? Is this proving that the typescript library is configured correctly or for some other reason?
|
|
||
| Now you will test that the lambda returns "Goodbye World", which will confirm that the CLI `update` command worked. | ||
|
|
||
| Add this code to the `test.js` file. |
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.
Can you have multiple test files using the NPM test library? Looks like it: tape tests/**/*.js
Then you could have each file live for real separately in the repo, rather than have users keep adding to test.js
| The second object is the user supplied to your `populate()` function in a lambda. | ||
| ```js | ||
| user: { |
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.
Please pull these from https://github.com/FusionAuth/fusionauth-site/tree/master/site/docs/src/json as those json files will be updated over time.
| ```bash | ||
| node test.js | npx faucet | ||
| ``` |
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.
Can we see sample output of the tests (all three)? No need for a screenshot, just copying and pasting the output is fine.
Lambda Guide language edit
|
I've migrated this guide to MDX and am closing this in favor of #2705 |
* 📝 refs 2340 Migrating "Testing Lambdas" to MDX * 📝 refs #2340 Renaming "Testing Lambdas" * 📝 refs #2705 Fixing description in the "Testing Lambdas" guide * 🔨 refs #2705 Add missing JSON to the Lambda testing guide * 📝 refs #2705 Few touch-ups to the Lambda Testing guide after review * 📝 refs #2705 Removing comments from actual code and moving to the paragraph before
Depends on FusionAuth/fusionauth-node-cli#5