Skip to content

Commit

Permalink
Added test doc
Browse files Browse the repository at this point in the history
  • Loading branch information
ntotten committed Nov 29, 2023
1 parent 55a7d0e commit 019c8ed
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
62 changes: 62 additions & 0 deletions docs/articles/testing-api-key-authentication.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
title: Testing API Key Authentication
---

When running tests there are several ways you might want to handle API Key
authentication. This document outlines a few strategies for testing with API Key
authentication both locally and in deployed environments.

## Testing Locally

When running API key Authentication locally, if you
[link](/docs/cli/local-development) the project to an project the same API Key
Bucket used in working copy will also be used for local development.

Alternatively, you can specify any API Key Bucket on the
[API Key Authentication](/docs/policies/api-key-inbound) policy by setting the
`bucketName` property.

## Selectively Disabling

:::danger

Be extremely careful using this strategy. If configured incorrectly this could
leave your API open to unauthorized access.

:::

Another option is to disable authentication on endpoints for testing purposes.
One way of doing this is to configure the
[API Key Authentication](/docs/policies/api-key-inbound) policy to allow
unauthenticated requests through. This can be done by setting
`allowUnauthenticatedRequests` to true.

In order to enforce authentication with this setting disabled, you can create a
policy that comes after that selectively enforces auth based on some condition.

For example, an environment variable flag could be used to disable auth with the
following policy.

```ts
import {
ZuploContext,
ZuploRequest,
environment,
HttpProblems,
} from "@zuplo/runtime";

export default async function enforceAuth(
request: ZuploRequest,
context: ZuploContext,
) {
if (environment.DISABLE_AUTH === "AUTH_DISABLED") {
return request;
}

if (!request.user) {
return HttpProblems.unauthorized(request, context);
}

return request;
}
```
6 changes: 6 additions & 0 deletions policies/api-key-inbound/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,9 @@ You have now setup API Key Authentication on your API Gateway.

See [this document](/docs/articles/api-key-management) for more information
about API Keys and API Key Management with Zuplo.

## Writing Tests with the Auth Policy

For information on running tests while using API Key Authentication see the
document
[Testing API Key Authentication](/docs/articles/testing-api-key-authentication).
1 change: 1 addition & 0 deletions sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ const sidebars = {
"cli/test",
"cli/tunnels",
"cli/variables",
"cli/local-development",
],
},
],
Expand Down

1 comment on commit 019c8ed

@vercel
Copy link

@vercel vercel bot commented on 019c8ed Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./

docs.zuplo.site
docs.zuplopreview.net
docs-git-main.zuplopreview.net

Please sign in to comment.