test(api): centralize auth helpers on server-side createUser
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { createApp } from '../src/app';
|
||||
|
||||
const json = { 'content-type': 'application/json' };
|
||||
import { authToken, bearer } from './helpers';
|
||||
|
||||
describe('GET /api/me', () => {
|
||||
it('rejects an unauthenticated request', async () => {
|
||||
@@ -10,27 +9,14 @@ describe('GET /api/me', () => {
|
||||
expect(res.status).toBe(401);
|
||||
});
|
||||
|
||||
it('returns the user for a valid bearer token (sign-up -> sign-in -> me)', async () => {
|
||||
it('returns the user for a valid bearer token (create -> sign-in -> me)', async () => {
|
||||
const app = createApp();
|
||||
const creds = { email: 'me@example.com', password: 'sterk-wachtwoord-123', name: 'Me' };
|
||||
const email = 'me@example.com';
|
||||
const token = await authToken(app, email);
|
||||
|
||||
await app.request('/api/auth/sign-up/email', {
|
||||
method: 'POST',
|
||||
headers: json,
|
||||
body: JSON.stringify(creds),
|
||||
});
|
||||
const signin = await app.request('/api/auth/sign-in/email', {
|
||||
method: 'POST',
|
||||
headers: json,
|
||||
body: JSON.stringify({ email: creds.email, password: creds.password }),
|
||||
});
|
||||
const token = signin.headers.get('set-auth-token');
|
||||
|
||||
const res = await app.request('/api/me', {
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
const res = await app.request('/api/me', { headers: bearer(token) });
|
||||
expect(res.status).toBe(200);
|
||||
const body = await res.json();
|
||||
expect(body.user.email).toBe(creds.email);
|
||||
expect(body.user.email).toBe(email);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user