Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion __tests__/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import app from '../src/app';
import request from 'supertest';
import endpointsJson from '../src/endpoints.json';
import seeder from '../prisma/seed';
import { promisify } from 'util';
import { exec } from 'child_process';

const server = app.listen(6666);

beforeEach(async () => {
beforeAll(async () => {
console.log(`💽 Wiping and re-seeding`);
await promisify(exec)('npx prisma migrate reset --force');
await seeder();
});

Expand Down Expand Up @@ -145,6 +149,25 @@ describe('🧪 Express Application', () => {
.expect(400);
});
});

describe('POST /api/users/create', () => {
it('201: should create a new user', () => {
const body = {
username: 'jeepies',
};
return request(app)
.post('/api/users/create')
.send(body)
.expect(201)
.then(({ body: { success, data } }) => {
expect(success).toBe(true);
expect(data).toMatchObject({
username: 'jeepies',
requested_privacy: 'PUBLIC',
});
});
});
});
});

describe('Devices', () => {
Expand Down
22 changes: 22 additions & 0 deletions src/controllers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,25 @@ export function updateUser(
res.status(500).send({ msg: 'An internal server error occurred' });
});
}

export function createUser(
request: Request,
response: Response,
next: NextFunction
) {
const schema = {
username: 'string',
};
const payload = request.body;
const result = validator(payload, schema);
if (!result.success) return next({ status: 400, message: result.errors });

users
.createUser(result.body.username)
.then((data) => {
response.status(201).json({ success: true, data: data });
})
.catch(() =>
next({ status: 500, message: 'An internal server error occurred' })
);
}
8 changes: 8 additions & 0 deletions src/models/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ export function updateUser(username: string, data: object) {
data,
});
}

export function createUser(username: string) {
return extendedClient.user.create({
data: {
username: username,
},
});
}
1 change: 1 addition & 0 deletions src/routes/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ users.get('/:id/devices', controller.getDevices);
users.get('/:id/cats', controller.getCats);
users.get('/settings', usernameAuth, controller.getUser);
users.patch('/settings', usernameAuth, controller.updateUser);
users.post('/create', controller.createUser);

export default users;