Skip to content

Commit 5c72783

Browse files
committed
chore(express,hono): Add machine auth integration tests
1 parent cfb8cbe commit 5c72783

2 files changed

Lines changed: 291 additions & 0 deletions

File tree

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import { test } from '@playwright/test';
2+
3+
import { appConfigs } from '../../presets';
4+
import type { MachineAuthTestAdapter } from '../../testUtils/machineAuthHelpers';
5+
import {
6+
registerApiKeyAuthTests,
7+
registerM2MAuthTests,
8+
registerOAuthAuthTests,
9+
} from '../../testUtils/machineAuthHelpers';
10+
11+
const createMainFile = () => `
12+
import 'dotenv/config';
13+
14+
import { clerkMiddleware } from '@clerk/express';
15+
import express from 'express';
16+
import ViteExpress from 'vite-express';
17+
import { machineRoutes } from './routes/machine';
18+
19+
const app = express();
20+
21+
app.use(express.json());
22+
app.use(
23+
clerkMiddleware({
24+
publishableKey: process.env.VITE_CLERK_PUBLISHABLE_KEY,
25+
}),
26+
);
27+
28+
app.use('/api', machineRoutes);
29+
30+
const port = parseInt(process.env.PORT as string) || 3002;
31+
ViteExpress.listen(app, port, () => console.log(\`Server is listening on port \${port}...\`));
32+
`;
33+
34+
const adapter: MachineAuthTestAdapter = {
35+
baseConfig: appConfigs.express.vite,
36+
apiKey: {
37+
path: '/api/me',
38+
addRoutes: config =>
39+
config
40+
.addFile(
41+
'src/server/routes/machine.ts',
42+
() => `
43+
import { getAuth } from '@clerk/express';
44+
import { Router } from 'express';
45+
46+
const router = Router();
47+
48+
router.get('/me', (req: any, res: any) => {
49+
const { userId, tokenType } = getAuth(req, { acceptsToken: 'api_key' });
50+
51+
if (!userId) {
52+
res.status(401).send('Unauthorized');
53+
return;
54+
}
55+
56+
res.json({ userId, tokenType });
57+
});
58+
59+
router.post('/me', (req: any, res: any) => {
60+
const authObject = getAuth(req, { acceptsToken: ['api_key', 'session_token'] });
61+
62+
if (!authObject.isAuthenticated) {
63+
res.status(401).send('Unauthorized');
64+
return;
65+
}
66+
67+
res.json({ userId: authObject.userId, tokenType: authObject.tokenType });
68+
});
69+
70+
export const machineRoutes = router;
71+
`,
72+
)
73+
.addFile('src/server/main.ts', () => createMainFile()),
74+
},
75+
m2m: {
76+
path: '/api/m2m',
77+
addRoutes: config =>
78+
config
79+
.addFile(
80+
'src/server/routes/machine.ts',
81+
() => `
82+
import { getAuth } from '@clerk/express';
83+
import { Router } from 'express';
84+
85+
const router = Router();
86+
87+
router.get('/m2m', (req: any, res: any) => {
88+
const { subject, tokenType, isAuthenticated } = getAuth(req, { acceptsToken: 'm2m_token' });
89+
90+
if (!isAuthenticated) {
91+
res.status(401).send('Unauthorized');
92+
return;
93+
}
94+
95+
res.json({ subject, tokenType });
96+
});
97+
98+
export const machineRoutes = router;
99+
`,
100+
)
101+
.addFile('src/server/main.ts', () => createMainFile()),
102+
},
103+
oauth: {
104+
verifyPath: '/api/oauth-verify',
105+
callbackPath: '/api/oauth/callback',
106+
addRoutes: config =>
107+
config
108+
.addFile(
109+
'src/server/routes/machine.ts',
110+
() => `
111+
import { getAuth } from '@clerk/express';
112+
import { Router } from 'express';
113+
114+
const router = Router();
115+
116+
router.get('/oauth-verify', (req: any, res: any) => {
117+
const { userId, tokenType } = getAuth(req, { acceptsToken: 'oauth_token' });
118+
119+
if (!userId) {
120+
res.status(401).send('Unauthorized');
121+
return;
122+
}
123+
124+
res.json({ userId, tokenType });
125+
});
126+
127+
router.get('/oauth/callback', (_req: any, res: any) => {
128+
res.json({ message: 'OAuth callback received' });
129+
});
130+
131+
export const machineRoutes = router;
132+
`,
133+
)
134+
.addFile('src/server/main.ts', () => createMainFile()),
135+
},
136+
};
137+
138+
test.describe('Express machine authentication @machine', () => {
139+
registerApiKeyAuthTests(adapter);
140+
registerM2MAuthTests(adapter);
141+
registerOAuthAuthTests(adapter);
142+
});
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import { test } from '@playwright/test';
2+
3+
import { appConfigs } from '../../presets';
4+
import type { MachineAuthTestAdapter } from '../../testUtils/machineAuthHelpers';
5+
import {
6+
registerApiKeyAuthTests,
7+
registerM2MAuthTests,
8+
registerOAuthAuthTests,
9+
} from '../../testUtils/machineAuthHelpers';
10+
11+
const createMainFile = () => `
12+
import 'dotenv/config';
13+
14+
import { getRequestListener } from '@hono/node-server';
15+
import express from 'express';
16+
import ViteExpress from 'vite-express';
17+
import { app } from './app';
18+
19+
const expressApp = express();
20+
const honoRequestListener = getRequestListener(app.fetch);
21+
22+
expressApp.use('/api', async (req: any, res: any) => {
23+
await honoRequestListener(req, res);
24+
});
25+
26+
const port = parseInt(process.env.PORT as string) || 3002;
27+
ViteExpress.listen(expressApp, port, () => console.log(\`Server is listening on port \${port}...\`));
28+
`;
29+
30+
const adapter: MachineAuthTestAdapter = {
31+
baseConfig: appConfigs.hono.vite,
32+
apiKey: {
33+
path: '/api/me',
34+
addRoutes: config =>
35+
config
36+
.addFile(
37+
'src/server/app.ts',
38+
() => `
39+
import { clerkMiddleware, getAuth } from '@clerk/hono';
40+
import { Hono } from 'hono';
41+
42+
export const app = new Hono();
43+
44+
app.use(
45+
'*',
46+
clerkMiddleware({
47+
publishableKey: process.env.VITE_CLERK_PUBLISHABLE_KEY,
48+
}),
49+
);
50+
51+
app.get('/me', c => {
52+
const { userId, tokenType } = getAuth(c, { acceptsToken: 'api_key' });
53+
54+
if (!userId) {
55+
return c.text('Unauthorized', 401);
56+
}
57+
58+
return c.json({ userId, tokenType });
59+
});
60+
61+
app.post('/me', c => {
62+
const authObject = getAuth(c, { acceptsToken: ['api_key', 'session_token'] });
63+
64+
if (!authObject.isAuthenticated) {
65+
return c.text('Unauthorized', 401);
66+
}
67+
68+
return c.json({ userId: authObject.userId, tokenType: authObject.tokenType });
69+
});
70+
`,
71+
)
72+
.addFile('src/server/main.ts', () => createMainFile()),
73+
},
74+
m2m: {
75+
path: '/api/m2m',
76+
addRoutes: config =>
77+
config
78+
.addFile(
79+
'src/server/app.ts',
80+
() => `
81+
import { clerkMiddleware, getAuth } from '@clerk/hono';
82+
import { Hono } from 'hono';
83+
84+
export const app = new Hono();
85+
86+
app.use(
87+
'*',
88+
clerkMiddleware({
89+
publishableKey: process.env.VITE_CLERK_PUBLISHABLE_KEY,
90+
}),
91+
);
92+
93+
app.get('/m2m', c => {
94+
const { subject, tokenType, isAuthenticated } = getAuth(c, { acceptsToken: 'm2m_token' });
95+
96+
if (!isAuthenticated) {
97+
return c.text('Unauthorized', 401);
98+
}
99+
100+
return c.json({ subject, tokenType });
101+
});
102+
`,
103+
)
104+
.addFile('src/server/main.ts', () => createMainFile()),
105+
},
106+
oauth: {
107+
verifyPath: '/api/oauth-verify',
108+
callbackPath: '/api/oauth/callback',
109+
addRoutes: config =>
110+
config
111+
.addFile(
112+
'src/server/app.ts',
113+
() => `
114+
import { clerkMiddleware, getAuth } from '@clerk/hono';
115+
import { Hono } from 'hono';
116+
117+
export const app = new Hono();
118+
119+
app.use(
120+
'*',
121+
clerkMiddleware({
122+
publishableKey: process.env.VITE_CLERK_PUBLISHABLE_KEY,
123+
}),
124+
);
125+
126+
app.get('/oauth-verify', c => {
127+
const { userId, tokenType } = getAuth(c, { acceptsToken: 'oauth_token' });
128+
129+
if (!userId) {
130+
return c.text('Unauthorized', 401);
131+
}
132+
133+
return c.json({ userId, tokenType });
134+
});
135+
136+
app.get('/oauth/callback', c => {
137+
return c.json({ message: 'OAuth callback received' });
138+
});
139+
`,
140+
)
141+
.addFile('src/server/main.ts', () => createMainFile()),
142+
},
143+
};
144+
145+
test.describe('Hono machine authentication @machine', () => {
146+
registerApiKeyAuthTests(adapter);
147+
registerM2MAuthTests(adapter);
148+
registerOAuthAuthTests(adapter);
149+
});

0 commit comments

Comments
 (0)