Skip to content

Commit 30248dd

Browse files
committed
chore: fix tests
1 parent f23d4a8 commit 30248dd

1 file changed

Lines changed: 99 additions & 32 deletions

File tree

__test__/index.test.ts

Lines changed: 99 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import { randomUUID } from 'node:crypto';
12
import { GenericContainer, StartedTestContainer, Wait } from 'testcontainers';
23
import { ApiResponse, AuthToken, Authorizer } from '../lib';
34

4-
const authorizerConfig = {
5+
const authorizerConfig: {
6+
authorizerURL: string;
7+
redirectURL: string;
8+
adminSecret: string;
9+
clientID?: string;
10+
} = {
511
authorizerURL: 'http://localhost:8080',
612
redirectURL: 'http://localhost:8080/app',
7-
// clientID: '3fab5e58-5693-46f2-8123-83db8133cd22',
813
adminSecret: 'secret',
914
};
1015

@@ -18,22 +23,54 @@ const testConfig = {
1823
maginLinkLoginEmail: 'test_magic_link@test.com',
1924
};
2025

21-
// Using etheral.email for email sink: https://ethereal.email/create
22-
const authorizerENV = {
23-
ENV: 'production',
24-
DATABASE_URL: 'data.db',
25-
DATABASE_TYPE: 'sqlite',
26-
CUSTOM_ACCESS_TOKEN_SCRIPT:
27-
'function(user,tokenPayload){var data = tokenPayload;data.extra = {\'x-extra-id\': user.id};return data;}',
28-
DISABLE_PLAYGROUND: 'true',
29-
SMTP_HOST: 'smtp.ethereal.email',
30-
SMTP_PASSWORD: 'WncNxwVFqb6nBjKDQJ',
31-
SMTP_USERNAME: 'sydnee.lesch77@ethereal.email',
32-
LOG_LELVEL: 'debug',
33-
SMTP_PORT: '587',
34-
SENDER_EMAIL: 'test@authorizer.dev',
35-
ADMIN_SECRET: 'secret',
36-
};
26+
// Build v2 CLI args for authorizer (see authorizer/cmd/root.go). Using etheral.email for email sink.
27+
function buildAuthorizerCliArgs(): { args: string[]; clientId: string } {
28+
const clientId = randomUUID();
29+
const clientSecret = randomUUID();
30+
const jwtSecret = randomUUID();
31+
const customAccessTokenScript =
32+
'function(user,tokenPayload){var data = tokenPayload;data.extra = {\'x-extra-id\': user.id};return data;}';
33+
34+
const args = [
35+
'--client-id',
36+
clientId,
37+
'--client-secret',
38+
clientSecret,
39+
'--jwt-type',
40+
'HS256',
41+
'--jwt-secret',
42+
jwtSecret,
43+
'--admin-secret',
44+
authorizerConfig.adminSecret,
45+
'--env',
46+
'production',
47+
'--database-type',
48+
'sqlite',
49+
'--database-url',
50+
'/tmp/authorizer.db',
51+
'--custom-access-token-script',
52+
customAccessTokenScript,
53+
'--enable-playground',
54+
'false',
55+
'--log-level',
56+
'debug',
57+
'--smtp-host',
58+
'smtp.ethereal.email',
59+
'--smtp-port',
60+
'587',
61+
'--smtp-username',
62+
'sydnee.lesch77@ethereal.email',
63+
'--smtp-password',
64+
'WncNxwVFqb6nBjKDQJ',
65+
'--smtp-sender-email',
66+
'test@authorizer.dev',
67+
'--enable-email-verification',
68+
'true',
69+
'--enable-magic-link-login',
70+
'true',
71+
];
72+
return { args, clientId };
73+
}
3774

3875
// const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
3976

@@ -46,8 +83,10 @@ describe('Integration Tests - authorizer-js', () => {
4683
let authorizer: Authorizer;
4784

4885
beforeAll(async () => {
49-
container = await new GenericContainer('lakhansamani/authorizer:latest')
50-
.withEnvironment(authorizerENV)
86+
const { args, clientId } = buildAuthorizerCliArgs();
87+
88+
container = await new GenericContainer('lakhansamani/authorizer:2.0.0-rc.6')
89+
.withCommand(args)
5190
.withExposedPorts(8080)
5291
.withWaitStrategy(Wait.forHttp('/health', 8080).forStatusCode(200))
5392
.start();
@@ -58,15 +97,12 @@ describe('Integration Tests - authorizer-js', () => {
5897
authorizerConfig.redirectURL = `http://${container.getHost()}:${container.getMappedPort(
5998
8080,
6099
)}/app`;
100+
authorizerConfig.clientID = clientId;
61101
console.log('Authorizer URL:', authorizerConfig.authorizerURL);
62102
authorizer = new Authorizer(authorizerConfig);
63-
// get metadata
64103
const metadataRes = await authorizer.getMetaData();
65-
// await sleep(50000);
66104
expect(metadataRes?.data).toBeDefined();
67-
if (metadataRes?.data?.client_id) {
68-
authorizer.config.clientID = metadataRes?.data?.client_id;
69-
}
105+
expect(metadataRes?.data?.client_id).toBe(clientId);
70106
});
71107

72108
afterAll(async () => {
@@ -101,10 +137,13 @@ describe('Integration Tests - authorizer-js', () => {
101137
(i: { email: string }) => i.email === testConfig.email,
102138
);
103139
expect(item).not.toBeNull();
140+
expect(item?.token).toBeDefined();
104141

105142
const verifyEmailRes = await authorizer.verifyEmail({ token: item.token });
106143
expect(verifyEmailRes?.data).toBeDefined();
107144
expect(verifyEmailRes?.errors).toHaveLength(0);
145+
expect(verifyEmailRes?.data?.access_token).toBeDefined();
146+
expect(verifyEmailRes?.data?.access_token).not.toBeNull();
108147
expect(verifyEmailRes?.data?.access_token?.length).toBeGreaterThan(0);
109148
});
110149

@@ -117,13 +156,23 @@ describe('Integration Tests - authorizer-js', () => {
117156
});
118157
expect(loginRes?.data).toBeDefined();
119158
expect(loginRes?.errors).toHaveLength(0);
120-
expect(loginRes?.data?.access_token.length).not.toEqual(0);
121-
expect(loginRes?.data?.refresh_token?.length).not.toEqual(0);
122-
expect(loginRes?.data?.expires_in).not.toEqual(0);
123-
expect(loginRes?.data?.id_token.length).not.toEqual(0);
159+
expect(loginRes?.data?.access_token).toBeDefined();
160+
expect(loginRes?.data?.access_token).not.toBeNull();
161+
expect(loginRes?.data?.access_token?.length).toBeGreaterThan(0);
162+
expect(loginRes?.data?.refresh_token).toBeDefined();
163+
expect(loginRes?.data?.refresh_token).not.toBeNull();
164+
expect(loginRes?.data?.refresh_token?.length).toBeGreaterThan(0);
165+
expect(loginRes?.data?.expires_in).toBeDefined();
166+
expect(loginRes?.data?.expires_in).not.toBeNull();
167+
expect(loginRes?.data?.expires_in).toBeGreaterThan(0);
168+
expect(loginRes?.data?.id_token).toBeDefined();
169+
expect(loginRes?.data?.id_token).not.toBeNull();
170+
expect(loginRes?.data?.id_token?.length).toBeGreaterThan(0);
124171
});
125172

126173
it('should validate jwt token', async () => {
174+
expect(loginRes?.data?.access_token).toBeDefined();
175+
expect(loginRes?.data?.access_token).not.toBeNull();
127176
const validateRes = await authorizer.validateJWTToken({
128177
token_type: 'access_token',
129178
token: loginRes?.data?.access_token || '',
@@ -134,6 +183,8 @@ describe('Integration Tests - authorizer-js', () => {
134183
});
135184

136185
it('should update profile successfully', async () => {
186+
expect(loginRes?.data?.access_token).toBeDefined();
187+
expect(loginRes?.data?.access_token).not.toBeNull();
137188
const updateProfileRes = await authorizer.updateProfile(
138189
{
139190
given_name: 'bob',
@@ -147,28 +198,36 @@ describe('Integration Tests - authorizer-js', () => {
147198
});
148199

149200
it('should fetch profile successfully', async () => {
201+
expect(loginRes?.data?.access_token).toBeDefined();
202+
expect(loginRes?.data?.access_token).not.toBeNull();
150203
const profileRes = await authorizer.getProfile({
151204
Authorization: `Bearer ${loginRes?.data?.access_token}`,
152205
});
153206
expect(profileRes?.data).toBeDefined();
154207
expect(profileRes?.errors).toHaveLength(0);
208+
expect(profileRes?.data?.given_name).toBeDefined();
155209
expect(profileRes?.data?.given_name).toMatch('bob');
156210
});
157211

158212
it('should get access_token using refresh_token', async () => {
213+
expect(loginRes?.data?.refresh_token).toBeDefined();
214+
expect(loginRes?.data?.refresh_token).not.toBeNull();
159215
const tokenRes = await authorizer.getToken({
160216
grant_type: 'refresh_token',
161-
refresh_token: loginRes?.data?.refresh_token,
217+
refresh_token: loginRes?.data?.refresh_token || '',
162218
});
163219
expect(tokenRes?.data).toBeDefined();
164220
expect(tokenRes?.errors).toHaveLength(0);
165-
expect(tokenRes?.data?.access_token.length).not.toEqual(0);
221+
expect(tokenRes?.data?.access_token).toBeDefined();
222+
expect(tokenRes?.data?.access_token?.length).toBeGreaterThan(0);
166223
if (loginRes && loginRes.data) {
167224
loginRes.data.access_token = tokenRes?.data?.access_token || '';
168225
}
169226
});
170227

171228
it('should deactivate account', async () => {
229+
expect(loginRes?.data?.access_token).toBeDefined();
230+
expect(loginRes?.data?.access_token).not.toBeNull();
172231
const deactivateRes = await authorizer.deactivateAccount({
173232
Authorization: `Bearer ${loginRes?.data?.access_token}`,
174233
});
@@ -177,11 +236,13 @@ describe('Integration Tests - authorizer-js', () => {
177236
});
178237

179238
it('should throw error while accessing profile after deactivation', async () => {
239+
expect(loginRes?.data?.access_token).toBeDefined();
180240
const resp = await authorizer.getProfile({
181241
Authorization: `Bearer ${loginRes?.data?.access_token}`,
182242
});
183243
expect(resp?.data).toBeUndefined();
184-
expect(resp?.errors).toHaveLength(1);
244+
expect(resp?.errors).toBeDefined();
245+
expect(resp?.errors.length).toBeGreaterThan(0);
185246
});
186247

187248
describe('magic link login', () => {
@@ -195,22 +256,28 @@ describe('Integration Tests - authorizer-js', () => {
195256
it('should verify email', async () => {
196257
const verificationRequestsRes = await authorizer.graphqlQuery({
197258
query: verificationRequests,
259+
variables: {},
198260
headers: {
199261
'x-authorizer-admin-secret': authorizerConfig.adminSecret,
200262
},
201263
});
202264
const requests =
203265
verificationRequestsRes?.data?._verification_requests
204266
.verification_requests;
267+
expect(verificationRequestsRes?.data).toBeDefined();
268+
expect(verificationRequestsRes?.errors).toHaveLength(0);
205269
const item = requests.find(
206270
(i: { email: string }) => i.email === testConfig.maginLinkLoginEmail,
207271
);
208272
expect(item).not.toBeNull();
273+
expect(item?.token).toBeDefined();
209274
const verifyEmailRes = await authorizer.verifyEmail({
210275
token: item.token,
211276
});
212277
expect(verifyEmailRes?.data).toBeDefined();
213278
expect(verifyEmailRes?.errors).toHaveLength(0);
279+
expect(verifyEmailRes?.data?.user).toBeDefined();
280+
expect(verifyEmailRes?.data?.user?.signup_methods).toBeDefined();
214281
expect(verifyEmailRes?.data?.user?.signup_methods).toContain(
215282
'magic_link_login',
216283
);

0 commit comments

Comments
 (0)