This repository has been archived on 2026-11-17. You can view files and clone it, but cannot push or open issues or pull requests.
gitgram-ai/gitgram/backend/tests/e2e/auth.e2e.test.ts
caelum0x 64fbe9f474 feat: round-3 deep product build-out (69 gaps closed, all 7 products green)
Deep-audit + build-out across all products to fully-usable state; all typecheck clean.

- gitgram-web (99): wire Store feature to real endpoints, close 9 gaps
- gitgram-backend (100): close 8 flow gaps, document infra-provisioning items
- gitgram-mobile (90): fix API base-URL/path mismatch (404s) + copy proximity
  components into components/proximity wired to real screens; 15 gaps, +react-native-svg
- aitm-core (95): wire natural-language CLI flow end-to-end; 11 gaps
- aitm-api (97): fix teams metadata runtime bug; copy a2a-js server/client/grpc
  into src/a2a and wire A2A route; crates.io package search; 8 gaps
- aitm-mcp (100): fix A2A client/server card mismatch; 9 gaps
- aitm-website (95): build missing docs routes; 9 gaps

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-28 13:54:57 +03:00

97 lines
3.1 KiB
TypeScript

/**
* E2E: Authentication flow.
*
* Walks the real register -> me -> login -> refresh -> logout flow against a
* running Express app backed by a real PostgreSQL database.
*/
import request from 'supertest';
import type { Express } from 'express';
import { buildApp, registerUser, bearer, resetDatabase } from './helpers';
describe('E2E: Authentication', () => {
let app: Express;
beforeAll(async () => {
app = buildApp();
await resetDatabase();
});
it('registers a new user and returns tokens + account', async () => {
const res = await request(app).post('/api/v1/auth/register').send({
username: 'authflowuser',
email: 'authflow@example.com',
password: 'Str0ngPass!',
});
expect(res.status).toBe(201);
expect(res.body.user).toMatchObject({ email: 'authflow@example.com', emailVerified: false });
expect(res.body.user.id).toBeTruthy();
expect(res.body.token).toBeTruthy();
expect(res.body.refreshToken).toBeTruthy();
expect(res.body.account).toMatchObject({ username: 'authflowuser', type: 'USER' });
});
it('rejects a duplicate email registration', async () => {
const res = await request(app).post('/api/v1/auth/register').send({
username: 'authflowuser2',
email: 'authflow@example.com',
password: 'Str0ngPass!',
});
expect(res.status).toBe(400);
});
it('rejects a weak password at the validation boundary', async () => {
const res = await request(app).post('/api/v1/auth/register').send({
username: 'weakpwuser',
email: 'weakpw@example.com',
password: 'weak',
});
expect(res.status).toBe(400);
});
it('returns the current user from /me with a valid token', async () => {
const user = await registerUser(app);
const res = await request(app).get('/api/v1/auth/me').set(...bearer(user.token));
expect(res.status).toBe(200);
expect(res.body.user.id).toBe(user.id);
expect(res.body.account.username).toBe(user.username);
});
it('rejects /me without a token', async () => {
const res = await request(app).get('/api/v1/auth/me');
expect(res.status).toBe(401);
});
it('logs in with valid credentials and rejects bad ones', async () => {
const user = await registerUser(app, { password: 'Str0ngPass!' });
const ok = await request(app)
.post('/api/v1/auth/login')
.send({ email: user.email, password: 'Str0ngPass!' });
expect(ok.status).toBe(200);
expect(ok.body.token).toBeTruthy();
const bad = await request(app)
.post('/api/v1/auth/login')
.send({ email: user.email, password: 'WrongPass1!' });
expect(bad.status).toBe(401);
});
it('refreshes an access token with a valid refresh token', async () => {
const user = await registerUser(app);
const res = await request(app)
.post('/api/v1/auth/refresh')
.send({ refreshToken: user.refreshToken });
expect(res.status).toBe(200);
expect(res.body.token).toBeTruthy();
});
it('logs out an authenticated user', async () => {
const user = await registerUser(app);
const res = await request(app).post('/api/v1/auth/logout').set(...bearer(user.token));
expect(res.status).toBe(200);
});
});