96 lines
3.0 KiB
TypeScript
96 lines
3.0 KiB
TypeScript
import { randomUUID } from 'node:crypto';
|
|
import { identityApp, type IdentityApp } from './helpers/identity-app';
|
|
|
|
describe('customer self-registration', () => {
|
|
let ctx: IdentityApp;
|
|
const customer = {
|
|
email: 'Buyer@Example.com',
|
|
name: 'Asha Buyer',
|
|
password: 'a long enough customer password',
|
|
};
|
|
const register = (body: object) =>
|
|
ctx.api().post('/api/v1/auth/register').send(body);
|
|
beforeAll(async () => {
|
|
ctx = await identityApp();
|
|
}, 60000);
|
|
afterAll(async () => {
|
|
await ctx?.close();
|
|
});
|
|
beforeEach(async () => {
|
|
await ctx.clearLimits();
|
|
});
|
|
|
|
it('creates an active role-less customer with a working session', async () => {
|
|
const response = await register({
|
|
organizationId: ctx.owner.organizationId,
|
|
...customer,
|
|
}).expect(201);
|
|
expect(response.body).toMatchObject({ tokenType: 'Bearer' });
|
|
expect(JSON.stringify(response.body)).not.toContain(customer.password);
|
|
const me = await ctx
|
|
.api()
|
|
.get('/api/v1/auth/me')
|
|
.auth(response.body.accessToken, { type: 'bearer' })
|
|
.expect(200);
|
|
expect(me.body.permissions).toEqual([]);
|
|
await ctx
|
|
.api()
|
|
.get('/api/v1/cart')
|
|
.auth(response.body.accessToken, { type: 'bearer' })
|
|
.expect(200);
|
|
await ctx
|
|
.api()
|
|
.get('/api/v1/users')
|
|
.auth(response.body.accessToken, { type: 'bearer' })
|
|
.expect(403);
|
|
const user = await ctx.db.user.findFirstOrThrow({
|
|
where: { email: 'buyer@example.com' },
|
|
include: { roles: true },
|
|
});
|
|
expect(user).toMatchObject({ status: 'ACTIVE', isOwner: false, roles: [] });
|
|
expect(
|
|
await ctx.db.auditEvent.count({
|
|
where: { action: 'user.registered', targetId: user.id },
|
|
}),
|
|
).toBe(1);
|
|
expect(
|
|
(await ctx.login('buyer@example.com', customer.password)).status,
|
|
).toBe(200);
|
|
});
|
|
|
|
it('rejects duplicates, weak passwords, unknown organizations and extra fields', async () => {
|
|
const organizationId = ctx.owner.organizationId;
|
|
await register({ organizationId, ...customer, email: 'dup@example.com' });
|
|
await register({
|
|
organizationId,
|
|
...customer,
|
|
email: 'DUP@example.com',
|
|
}).expect(409);
|
|
await register({ organizationId, ...customer, password: 'short' }).expect(
|
|
400,
|
|
);
|
|
await register({ ...customer, organizationId: randomUUID() }).expect(
|
|
(response) => expect(response.status).toBeGreaterThanOrEqual(400),
|
|
);
|
|
await register({ organizationId, ...customer, isOwner: true }).expect(400);
|
|
expect(
|
|
await ctx.db.user.count({ where: { email: 'dup@example.com' } }),
|
|
).toBe(1);
|
|
});
|
|
|
|
it('limits registrations per client address', async () => {
|
|
const organizationId = ctx.owner.organizationId;
|
|
for (let index = 0; index < 10; index++)
|
|
await register({
|
|
organizationId,
|
|
...customer,
|
|
email: `bulk${index}@example.com`,
|
|
}).expect(201);
|
|
await register({
|
|
organizationId,
|
|
...customer,
|
|
email: 'bulk-over@example.com',
|
|
}).expect(429);
|
|
});
|
|
});
|