121 lines
3.4 KiB
TypeScript
121 lines
3.4 KiB
TypeScript
import request from 'supertest';
|
|
import {
|
|
createTestApp,
|
|
TestAccounts,
|
|
type TestApp,
|
|
type TestUser,
|
|
} from './helpers';
|
|
|
|
describe('Users (e2e)', () => {
|
|
let app: TestApp;
|
|
let accounts: TestAccounts;
|
|
|
|
beforeAll(async () => {
|
|
app = await createTestApp();
|
|
accounts = new TestAccounts(app, '@users-e2e.test');
|
|
await accounts.cleanup();
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await accounts.cleanup();
|
|
await app.close();
|
|
});
|
|
|
|
it('GET /users/me requires a session', () => {
|
|
return request(app.getHttpServer()).get('/users/me').expect(401);
|
|
});
|
|
|
|
it('refuses to sign up without a username', async () => {
|
|
const agent = request.agent(app.getHttpServer());
|
|
const response = await accounts.requestSignUp(agent, 'Nouser');
|
|
expect(response.status).toBe(400);
|
|
expect(response.body.message).toBe('Username is required.');
|
|
});
|
|
|
|
it('refuses to sign up with an invalid username', async () => {
|
|
const agent = request.agent(app.getHttpServer());
|
|
await accounts.requestSignUp(agent, 'Badname', 'not valid!').expect(400);
|
|
});
|
|
|
|
describe('with two accounts', () => {
|
|
let alice: TestUser;
|
|
let bob: TestUser;
|
|
|
|
beforeAll(async () => {
|
|
alice = await accounts.signUp('Alice');
|
|
bob = await accounts.signUp('Bob');
|
|
});
|
|
|
|
it('returns the own profile with a lower-cased username', async () => {
|
|
const response = await alice.agent.get('/users/me').expect(200);
|
|
expect(response.body).toEqual({
|
|
id: alice.id,
|
|
username: alice.username,
|
|
name: 'Alice',
|
|
bio: null,
|
|
image: null,
|
|
email: accounts.emailOf('Alice'),
|
|
createdAt: expect.any(String),
|
|
});
|
|
});
|
|
|
|
it('lower-cases a username on update', async () => {
|
|
const response = await bob.agent
|
|
.patch('/users/me')
|
|
.send({ username: bob.username.toUpperCase() })
|
|
.expect(200);
|
|
expect(response.body.username).toBe(bob.username);
|
|
});
|
|
|
|
it('updates only the fields sent', async () => {
|
|
const response = await alice.agent
|
|
.patch('/users/me')
|
|
.send({ bio: ' Brewing something. ' })
|
|
.expect(200);
|
|
expect(response.body).toMatchObject({
|
|
name: 'Alice',
|
|
bio: 'Brewing something.',
|
|
});
|
|
});
|
|
|
|
it('clears the bio when it is blank', async () => {
|
|
const response = await alice.agent
|
|
.patch('/users/me')
|
|
.send({ bio: ' ' })
|
|
.expect(200);
|
|
expect(response.body.bio).toBeNull();
|
|
});
|
|
|
|
it('rejects an invalid body', async () => {
|
|
await alice.agent.patch('/users/me').send({ name: '' }).expect(400);
|
|
await alice.agent
|
|
.patch('/users/me')
|
|
.send({ username: 'no spaces allowed' })
|
|
.expect(400);
|
|
});
|
|
|
|
it('answers 409 for a username someone else has', async () => {
|
|
const response = await bob.agent
|
|
.patch('/users/me')
|
|
.send({ username: alice.username })
|
|
.expect(409);
|
|
expect(response.body.message).toBe('Username is already taken.');
|
|
});
|
|
|
|
it('shows a public profile without the email', async () => {
|
|
const response = await bob.agent
|
|
.get(`/users/${alice.username}`)
|
|
.expect(200);
|
|
expect(response.body).toMatchObject({
|
|
username: alice.username,
|
|
name: 'Alice',
|
|
});
|
|
expect(response.body).not.toHaveProperty('email');
|
|
});
|
|
|
|
it('answers 404 for an unknown username', () => {
|
|
return bob.agent.get('/users/nobody_by_this_name').expect(404);
|
|
});
|
|
});
|
|
});
|