-
Notifications
You must be signed in to change notification settings - Fork 4
/
integrationTest.ts
124 lines (110 loc) · 3.73 KB
/
integrationTest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import express from 'express';
import http from 'http';
import * as path from 'path';
import { makeFetch } from 'supertest-fetch';
import * as exegesisExpress from '../src';
async function sessionAuthenticator(
context: exegesisExpress.ExegesisPluginContext
) : Promise<exegesisExpress.AuthenticationSuccess | undefined> {
const session = context.req.headers.session;
if(!session || typeof(session) !== 'string') {
return undefined;
}
if(session !== 'secret') {
throw context.makeError(403, "Invalid session.");
}
return {
type: 'success',
user: {name: 'jwalton'},
roles: ['readWrite', 'admin']
};
}
async function createServer() {
const options : exegesisExpress.ExegesisOptions = {
controllers: path.resolve(__dirname, './integrationSample/controllers'),
authenticators: {
sessionKey: sessionAuthenticator
},
controllersPattern: "**/*.@(ts|js)"
};
const exegesisMiddleware = await exegesisExpress.middleware(
path.resolve(__dirname, './integrationSample/openapi.yaml'),
options
);
const app = express();
app.use(exegesisMiddleware);
app.use((err: any, _req: express.Request, res: express.Response, _next: express.NextFunction) => {
if(err) {
res.writeHead(500);
res.end(`Internal error: ${err.message}`);
} else {
res.writeHead(404);
res.end();
}
});
return http.createServer(app);
}
describe('integration', function() {
beforeEach(async function() {
this.server = await createServer();
});
afterEach(function() {
if(this.server) {this.server.close();}
});
it('should succesfully call an API', async function() {
const fetch = makeFetch(this.server);
await fetch(`/greet?name=Jason`)
.expect(200)
.expect('content-type', 'application/json')
.expectBody({greeting: 'Hello, Jason!'});
});
it('should return an error for missing parameters', async function() {
const fetch = makeFetch(this.server);
await fetch(`/greet`)
.expect(400)
.expect('content-type', 'application/json')
.expectBody({
"message": "Validation errors",
"errors": [{
"message": "Missing required query parameter \"name\"",
"location": {
"docPath": "/paths/~1greet/get/parameters/0",
"in": "query",
"name": "name",
"path": ""
},
}
]
});
});
it('should require authentication from an authenticator', async function() {
const fetch = makeFetch(this.server);
await fetch(`/secure`)
.expect(401)
.expectBody({message:
"Must authenticate using one of the following schemes: sessionKey."
});
});
it('should return an error from an authenticator', async function() {
const fetch = makeFetch(this.server);
await fetch(`/secure`, {
headers: {session: 'wrong'}
})
.expect(403)
.expectBody({message: "Invalid session."});
});
it('should authenticate correctly', async function() {
const fetch = makeFetch(this.server);
await fetch(`/secure`, {
headers: {session: 'secret'}
})
.expect(200)
.expectBody({
sessionKey: {
type: 'success',
user: {name: 'jwalton'},
roles: ['readWrite', 'admin']
}
});
});
});