-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wrapping up more on #9 add user and get one
- Loading branch information
Showing
7 changed files
with
130 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './user.interface'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface IUser { | ||
name: string; | ||
id: string; | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as bodyParser from 'body-parser'; | ||
import { Application } from 'express'; | ||
import { Server } from '@overnightjs/core'; | ||
|
||
|
||
class TestServer extends Server { | ||
|
||
constructor() { | ||
super(); | ||
this.app.use(bodyParser.json()); | ||
this.app.use(bodyParser.urlencoded({extended: true})); | ||
} | ||
|
||
public setController(ctlr: object): void { | ||
super.addControllers(ctlr); | ||
} | ||
|
||
public getExpressInstance(): Application { | ||
return this.app; | ||
} | ||
} | ||
|
||
export default TestServer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import * as supertest from 'supertest'; | ||
|
||
import {} from 'jasmine'; | ||
import { SuperTest, Test } from 'supertest'; | ||
|
||
import TestServer from '../shared/TestServer.test'; | ||
import UserController from './UserController'; | ||
import { IUser } from '../../contracts'; | ||
|
||
describe('UserController', () => { | ||
const userController = new UserController(); | ||
let agent: SuperTest<Test>; | ||
|
||
beforeAll(done => { | ||
// Activate the routes | ||
const server = new TestServer(); | ||
server.setController(userController); | ||
|
||
// Start supertest | ||
agent = supertest.agent(server.getExpressInstance()); | ||
done(); | ||
}); | ||
|
||
describe('API: "/api/user/:id"', () => { | ||
it(`should return a JSON object with user Data`, (done: DoneFn) => { | ||
agent.get('/api/user/123').end((err, res) => { | ||
expect(res.status).toBe(250); | ||
const user: IUser = res.body.response; | ||
expect(user.id).toBe('sampleId'); | ||
expect(user.name).toBe('sample'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('API: "/api/user/"', () => { | ||
it(`should post a new user the endpoint`, (done: DoneFn) => { | ||
const testUser: IUser = { | ||
id: 'test', | ||
name: 'waffles', | ||
}; | ||
agent.post('/api/user') | ||
.type('form') | ||
.expect(250) | ||
.send(testUser) | ||
.end((err, res) => { | ||
if (err) { | ||
console.error(err); | ||
} | ||
expect(res.body.success).toBeTruthy(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Controller, Get, Post } from '@overnightjs/core'; | ||
import { Request, Response } from 'express'; | ||
|
||
import { IUser } from '../../contracts'; | ||
|
||
|
||
@Controller('api/user') | ||
class UserController { | ||
|
||
public readonly SUC_MSG = 'hello'; | ||
public readonly ERR_MSG = 'can\'t say hello'; | ||
|
||
|
||
@Get(':id') | ||
private getUser(req: Request, res: Response): void { | ||
try { | ||
const userId = req.params.id; | ||
const response: IUser = { | ||
name: 'sample', | ||
id: 'sampleId', | ||
}; | ||
res.status(250).json({response}); | ||
} catch (err) { | ||
console.error(err); | ||
res.status(400).json({response: this.ERR_MSG}); | ||
} | ||
} | ||
|
||
@Post('') | ||
private addUser(req: Request, res: Response): void { | ||
const newUser = req.body; | ||
// TODO save the user somewhere | ||
res.status(250).json({success: true}); | ||
} | ||
} | ||
|
||
export default UserController; |