Skip to content
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

Add supertokens module #705

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions docs/modules/supertokens.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Supertokens Module

[Supertokens](https://supertokens.com/): Open source alternative to Auth0 / Firebase Auth / AWS Cognito.

## Install

```bash
npm install @testcontainers/supertokens --save-dev
```

## Examples

The following examples communicate with the [API](https://app.swaggerhub.com/apis/supertokens/CDI/4.0.2/) exposed by the SuperTokens Core, which are meant to be consumed by your backend only.

Register a new user using email password:

```javascript
const container = await SupertokensContainer().start()
const response = await fetch(`${container.getConnectionUri()}/recipe/signup`, {
method: "POST",
headers: {
rid: "emailpassword",
"Content-Type": "application/json",
},
body: JSON.stringify({ email, password }),
});
const user = (await response.json()).user;
```

Sign in an existing user:

```javascript
const container = await SupertokensContainer().start()
const response = await fetch(`${container.getConnectionUri()}/recipe/signin`, {
method: "POST",
headers: {
rid: "emailpassword",
"Content-Type": "application/json",
},
body: JSON.stringify({ email, password }),
});
const user = (await response.json()).user;
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ nav:
- PostgreSQL: modules/postgresql.md
- Redis: modules/redis.md
- Selenium: modules/selenium.md
- Supertokens: modules/supertokens.md
- Configuration: configuration.md
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions packages/modules/supertokens/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Config } from "jest";
import * as path from "path";

const config: Config = {
preset: "ts-jest",
moduleNameMapper: {
"^testcontainers$": path.resolve(__dirname, "../../testcontainers/src"),
},
};

export default config;
34 changes: 34 additions & 0 deletions packages/modules/supertokens/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@testcontainers/supertokens",
"version": "10.5.0",
"license": "MIT",
"keywords": [
"supertokens",
"testing",
"docker",
"testcontainers"
],
"description": "Supertokens module for Testcontainers",
"homepage": "https://github.com/testcontainers/testcontainers-node#readme",
"repository": {
"type": "git",
"url": "https://github.com/testcontainers/testcontainers-node"
},
"bugs": {
"url": "https://github.com/testcontainers/testcontainers-node/issues"
},
"main": "build/index.js",
"files": [
"build"
],
"publishConfig": {
"access": "public"
},
"scripts": {
"prepack": "shx cp ../../../README.md . && shx cp ../../../LICENSE .",
"build": "tsc --project tsconfig.build.json"
},
"dependencies": {
"testcontainers": "^10.5.0"
}
}
1 change: 1 addition & 0 deletions packages/modules/supertokens/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { SupertokensContainer, StartedSupertokensContainer } from "./supertokens-container";
98 changes: 98 additions & 0 deletions packages/modules/supertokens/src/supertokens-container.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { StartedSupertokensContainer, SupertokensContainer } from "./supertokens-container";

describe("SupertokensContainer", () => {
jest.setTimeout(180_000);
let container: StartedSupertokensContainer;
let baseUrl: string;

beforeAll(async () => {
container = await new SupertokensContainer().start();
baseUrl = container.getConnectionUri();
});

afterAll(async () => {
await container.stop();
});

const signUpUser = async (email: string, password: string) => {
const response = await fetch(`${baseUrl}/recipe/signup`, {
method: "POST",
headers: {
rid: "emailpassword",
"Content-Type": "application/json",
},
body: JSON.stringify({ email, password }),
});
return await response.json();
};

const signInUser = async (email: string, password: string) => {
const response = await fetch(`${baseUrl}/recipe/signin`, {
method: "POST",
headers: {
rid: "emailpassword",
"Content-Type": "application/json",
},
body: JSON.stringify({ email, password }),
});
return await response.json();
};

const verifyEmail = async (userId: string, email: string) => {
const emailTokenResponse = await fetch(`${baseUrl}/recipe/user/email/verify/token`, {
method: "POST",
headers: {
rid: "emailverification",
"Content-Type": "application/json",
},
body: JSON.stringify({ userId, email }),
});
const token = (await emailTokenResponse.json()).token;
const res = await fetch(`${baseUrl}/recipe/user/email/verify`, {
method: "POST",
headers: {
rid: "emailverification",
"Content-Type": "application/json",
},
body: JSON.stringify({ method: "token", token }),
});
return await res.json();
};

it("should register a new user", async () => {
const email = "newuser@example.com";
const password = "password123";
const signUpResponse = await signUpUser(email, password);

expect(signUpResponse.status).toBe("OK");
expect(signUpResponse.user.emails[0]).toBe(email);
});

it("should sign in an existing user", async () => {
const email = "existinguser@example.com";
const password = "password123";
await signUpUser(email, password);
const signInResponse = await signInUser(email, password);

expect(signInResponse.status).toBe("OK");
expect(signInResponse.user.emails[0]).toBe(email);
});

it("should verify a user's email", async () => {
const email = "verifyemail@example.com";
const password = "password123";
const signUpResponse = await signUpUser(email, password);
const verifyResponse = await verifyEmail(signUpResponse.user.id, email);

expect(verifyResponse.status).toBe("OK");
});

it("should not register a user with an existing email", async () => {
const email = "duplicate@example.com";
const password = "password123";
await signUpUser(email, password);
const secondSignUpResponse = await signUpUser(email, password);

expect(secondSignUpResponse.status).toBe("EMAIL_ALREADY_EXISTS_ERROR");
});
});
33 changes: 33 additions & 0 deletions packages/modules/supertokens/src/supertokens-container.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { AbstractStartedContainer, GenericContainer, StartedTestContainer, Wait } from "testcontainers";

export const SUPERTOKENS_PORT = 3567;

export class SupertokensContainer extends GenericContainer {
constructor(image = "registry.supertokens.io/supertokens/supertokens-postgresql:7.0") {
super(image);
}

protected override async beforeContainerCreated(): Promise<void> {
this.withExposedPorts(...(this.exposedPorts ?? [SUPERTOKENS_PORT]))
.withWaitStrategy(Wait.forHttp("/hello", SUPERTOKENS_PORT))
.withStartupTimeout(120_000);
}

public override async start(): Promise<StartedSupertokensContainer> {
return new StartedSupertokensContainer(await super.start());
}
}

export class StartedSupertokensContainer extends AbstractStartedContainer {
constructor(startedTestContainer: StartedTestContainer) {
super(startedTestContainer);
}

public getPort(): number {
return this.startedTestContainer.getMappedPort(SUPERTOKENS_PORT);
}

public getConnectionUri(): string {
return `http://${this.getHost()}:${this.getPort().toString()}`;
}
}
9 changes: 9 additions & 0 deletions packages/modules/supertokens/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"exclude": ["build", "jest.config.ts", "src/**/*.test.ts"],
"references": [
{
"path": "../../testcontainers"
}
]
}
16 changes: 16 additions & 0 deletions packages/modules/supertokens/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build",
"paths": {
"testcontainers": ["../../testcontainers/src"]
}
},
"exclude": ["build", "jest.config.ts"],
"references": [
{
"path": "../../testcontainers"
}
]
}
Loading