Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lakhansamani committed May 10, 2024
1 parent e49c235 commit 3cbd8a8
Show file tree
Hide file tree
Showing 5 changed files with 1,600 additions and 1,119 deletions.
48 changes: 33 additions & 15 deletions __test__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ApiResponse, AuthToken, Authorizer } from '../lib';
const authorizerConfig = {
authorizerURL: 'http://localhost:8080',
redirectURL: 'http://localhost:8080/app',
clientID: '3fab5e58-5693-46f2-8123-83db8133cd22',
// clientID: '3fab5e58-5693-46f2-8123-83db8133cd22',
adminSecret: 'secret',
};

Expand All @@ -24,17 +24,21 @@ const authorizerENV = {
DATABASE_URL: 'data.db',
DATABASE_TYPE: 'sqlite',
CUSTOM_ACCESS_TOKEN_SCRIPT:
'function(user,tokenPayload){var data = tokenPayload;data.extra = {\'x-extra-id\': user.id};return data;}',
"function(user,tokenPayload){var data = tokenPayload;data.extra = {'x-extra-id': user.id};return data;}",
DISABLE_PLAYGROUND: 'true',
SMTP_HOST: 'smtp.ethereal.email',
SMTP_PASSWORD: 'WncNxwVFqb6nBjKDQJ',
SMTP_USERNAME: '[email protected]',
LOG_LELVEL: 'debug',
SMTP_PORT: '587',
SENDER_EMAIL: '[email protected]',
ADMIN_SECRET: 'secret',
};

const verificationRequests = 'query {_verification_requests { verification_requests { id token email expires identifier } } }';
// const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

const verificationRequests =
'query {_verification_requests { verification_requests { id token email expires identifier } } }';

describe('Integration Tests - authorizer-js', () => {
let container: StartedTestContainer;
Expand All @@ -45,21 +49,30 @@ describe('Integration Tests - authorizer-js', () => {
container = await new GenericContainer('lakhansamani/authorizer:latest')
.withEnvironment(authorizerENV)
.withExposedPorts(8080)
.withWaitStrategy(Wait.forHttp('/userinfo', 8080).forStatusCode(401))
.withWaitStrategy(Wait.forHttp('/health', 8080).forStatusCode(200))
.start();

authorizerConfig.authorizerURL = `http://${container.getHost()}:${container.getFirstMappedPort()}`;
authorizerConfig.redirectURL = `http://${container.getHost()}:${container.getFirstMappedPort()}/app`;

authorizerConfig.authorizerURL = `http://${container.getHost()}:${container.getMappedPort(
8080,
)}`;
authorizerConfig.redirectURL = `http://${container.getHost()}:${container.getMappedPort(
8080,
)}/app`;
console.log('Authorizer URL:', authorizerConfig.authorizerURL);
authorizer = new Authorizer(authorizerConfig);
// get metadata
const metadataRes = await authorizer.getMetaData();
// await sleep(50000);
expect(metadataRes?.data).toBeDefined();
if (metadataRes?.data?.client_id) {
authorizer.config.clientID = metadataRes?.data?.client_id;
}
});

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

it('should signup with email verification enabled', async () => {
const signupRes = await authorizer.signup({
email: testConfig.email,
Expand All @@ -85,7 +98,7 @@ describe('Integration Tests - authorizer-js', () => {
expect(verificationRequestsRes?.data).toBeDefined();
expect(verificationRequestsRes?.errors).toHaveLength(0);
const item = requests.find(
(i: { email: string }) => i.email === testConfig.email
(i: { email: string }) => i.email === testConfig.email,
);
expect(item).not.toBeNull();

Expand All @@ -95,7 +108,7 @@ describe('Integration Tests - authorizer-js', () => {
expect(verifyEmailRes?.data?.access_token?.length).toBeGreaterThan(0);
});

let loginRes:ApiResponse<AuthToken> | null;
let loginRes: ApiResponse<AuthToken> | null;
it('should log in successfully', async () => {
loginRes = await authorizer.login({
email: testConfig.email,
Expand Down Expand Up @@ -127,7 +140,7 @@ describe('Integration Tests - authorizer-js', () => {
},
{
Authorization: `Bearer ${loginRes?.data?.access_token}`,
}
},
);
expect(updateProfileRes?.data).toBeDefined();
expect(updateProfileRes?.errors).toHaveLength(0);
Expand Down Expand Up @@ -186,16 +199,21 @@ describe('Integration Tests - authorizer-js', () => {
'x-authorizer-admin-secret': authorizerConfig.adminSecret,
},
});
const requests
= verificationRequestsRes?.data?._verification_requests.verification_requests;
const item = requests.find((i:{email: string}) => i.email === testConfig.maginLinkLoginEmail);
expect(item).not.toBeNull();
const requests =
verificationRequestsRes?.data?._verification_requests
.verification_requests;
const item = requests.find(
(i: { email: string }) => i.email === testConfig.maginLinkLoginEmail,
);
expect(item).not.toBeNull();
const verifyEmailRes = await authorizer.verifyEmail({
token: item.token,
});
expect(verifyEmailRes?.data).toBeDefined();
expect(verifyEmailRes?.errors).toHaveLength(0);
expect(verifyEmailRes?.data?.user?.signup_methods).toContain('magic_link_login');
expect(verifyEmailRes?.data?.user?.signup_methods).toContain(
'magic_link_login',
);
});
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^11.1.5",
"@swc/core": "^1.3.99",
"@types/jest": "^29.5.10",
"@types/jest": "^29.5.12",
"@types/node": "^20.9.4",
"@typescript-eslint/eslint-plugin": "^6.13.2",
"@typescript-eslint/parser": "^6.13.2",
Expand Down
Loading

0 comments on commit 3cbd8a8

Please sign in to comment.