@rvoh/psychic-spec-helpers 0.6.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/spec/unit/OpenapiSpecRequest/delete.spec.js +29 -0
- package/dist/esm/spec/unit/OpenapiSpecRequest/get.spec.js +35 -0
- package/dist/esm/spec/unit/OpenapiSpecRequest/patch.spec.js +32 -0
- package/dist/esm/spec/unit/OpenapiSpecRequest/post.spec.js +30 -0
- package/dist/esm/spec/unit/OpenapiSpecRequest/put.spec.js +32 -0
- package/dist/esm/spec/unit/OpenapiSpecSession/delete.spec.js +17 -0
- package/dist/esm/spec/unit/OpenapiSpecSession/get.spec.js +24 -0
- package/dist/esm/spec/unit/OpenapiSpecSession/patch.spec.js +20 -0
- package/dist/esm/spec/unit/OpenapiSpecSession/post.spec.js +18 -0
- package/dist/esm/spec/unit/OpenapiSpecSession/put.spec.js +20 -0
- package/dist/esm/spec/unit/specRequest/helpers/fillOpenapiUrlParams.spec.js +11 -0
- package/dist/esm/src/index.js +2 -0
- package/dist/esm/src/unit/OpenapiSpecRequest.js +103 -0
- package/dist/esm/src/unit/OpenapiSpecSession.js +54 -0
- package/dist/esm/src/unit/helpers/fillOpenapiParams.js +8 -0
- package/dist/esm/src/unit/helpers/openapiTypeHelpers.js +1 -0
- package/dist/esm/src/unit/helpers/typeHelpers.js +1 -0
- package/dist/esm/test-app/src/app/controllers/UserController.js +84 -0
- package/dist/esm/test-app/src/app/controllers/UsersController.js +107 -0
- package/dist/esm/test-app/src/conf/app.js +3 -0
- package/dist/esm/test-app/src/conf/routes.js +9 -0
- package/dist/types/spec/unit/OpenapiSpecRequest/delete.spec.d.ts +1 -0
- package/dist/types/spec/unit/OpenapiSpecRequest/get.spec.d.ts +1 -0
- package/dist/types/spec/unit/OpenapiSpecRequest/patch.spec.d.ts +1 -0
- package/dist/types/spec/unit/OpenapiSpecRequest/post.spec.d.ts +1 -0
- package/dist/types/spec/unit/OpenapiSpecRequest/put.spec.d.ts +1 -0
- package/dist/types/spec/unit/OpenapiSpecSession/delete.spec.d.ts +1 -0
- package/dist/types/spec/unit/OpenapiSpecSession/get.spec.d.ts +1 -0
- package/dist/types/spec/unit/OpenapiSpecSession/patch.spec.d.ts +1 -0
- package/dist/types/spec/unit/OpenapiSpecSession/post.spec.d.ts +1 -0
- package/dist/types/spec/unit/OpenapiSpecSession/put.spec.d.ts +1 -0
- package/dist/types/spec/unit/specRequest/helpers/fillOpenapiUrlParams.spec.d.ts +1 -0
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/unit/OpenapiSpecRequest.d.ts +691 -0
- package/dist/types/src/unit/OpenapiSpecSession.d.ts +571 -0
- package/dist/types/src/unit/SpecSession.d.ts +2 -18
- package/dist/types/src/unit/helpers/fillOpenapiParams.d.ts +1 -0
- package/dist/types/src/unit/helpers/openapiTypeHelpers.d.ts +29 -0
- package/dist/types/src/unit/helpers/typeHelpers.d.ts +1 -0
- package/dist/types/test-app/src/app/controllers/UserController.d.ts +8 -0
- package/dist/types/test-app/src/app/controllers/UsersController.d.ts +9 -0
- package/package.json +3 -3
- package/src/index.ts +2 -0
- package/src/unit/OpenapiSpecRequest.ts +1435 -0
- package/src/unit/OpenapiSpecSession.ts +1169 -0
- package/src/unit/SpecSession.ts +2 -25
- package/src/unit/helpers/fillOpenapiParams.ts +8 -0
- package/src/unit/helpers/openapiTypeHelpers.ts +79 -0
- package/src/unit/helpers/typeHelpers.ts +1 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { PsychicServer } from '@rvoh/psychic';
|
|
2
|
+
import { OpenapiSpecRequest } from '../../../src/unit/OpenapiSpecRequest.js';
|
|
3
|
+
import User from '../../../test-app/src/app/models/User.js';
|
|
4
|
+
const request = new OpenapiSpecRequest();
|
|
5
|
+
describe('OpenapiSpecRequest#delete', () => {
|
|
6
|
+
beforeEach(async () => {
|
|
7
|
+
await request.init(PsychicServer);
|
|
8
|
+
});
|
|
9
|
+
it('issues a delete request to a controller endpoint, passing when the status matches', async () => {
|
|
10
|
+
const user = await User.create({ email: 'abc@def' });
|
|
11
|
+
await request.delete('/users/{id}', 204, {
|
|
12
|
+
id: user.id,
|
|
13
|
+
headers: {
|
|
14
|
+
hello: 'world',
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
expect(await User.count()).toEqual(0);
|
|
18
|
+
});
|
|
19
|
+
context('without params', () => {
|
|
20
|
+
it('issues a delete request to a controller endpoint, passing when the status matches', async () => {
|
|
21
|
+
await request.delete('/user', 204, {
|
|
22
|
+
headers: {
|
|
23
|
+
hello: 'world',
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
await request.delete('/user', 204);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { PsychicServer } from '@rvoh/psychic';
|
|
2
|
+
import { OpenapiSpecRequest } from '../../../src/unit/OpenapiSpecRequest.js';
|
|
3
|
+
import User from '../../../test-app/src/app/models/User.js';
|
|
4
|
+
const request = new OpenapiSpecRequest();
|
|
5
|
+
describe('OpenapiSpecRequest#get', () => {
|
|
6
|
+
beforeEach(async () => {
|
|
7
|
+
await request.init(PsychicServer);
|
|
8
|
+
});
|
|
9
|
+
it('issues a get request to a controller endpoint, passing when the status matches', async () => {
|
|
10
|
+
const user = await User.create({ email: 'abc@def' });
|
|
11
|
+
const res = await request.get('/users', 200, { headers: { hello: 'world' } });
|
|
12
|
+
expect(res.body).toEqual([{ id: user.id, email: 'abc@def' }]);
|
|
13
|
+
});
|
|
14
|
+
context('with params', () => {
|
|
15
|
+
it('succeeds', async () => {
|
|
16
|
+
const user = await User.create({ email: 'abc@def' });
|
|
17
|
+
const res = await request.get('/users/{id}', 200, {
|
|
18
|
+
id: user.id,
|
|
19
|
+
headers: { hello: 'world' },
|
|
20
|
+
});
|
|
21
|
+
expect(res.body).toEqual({ id: user.id, email: 'abc@def' });
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
context('query params', () => {
|
|
25
|
+
it('issues a get request to a controller endpoint, passing when the status matches', async () => {
|
|
26
|
+
await User.create({ email: 'other@user' });
|
|
27
|
+
const user = await User.create({ email: 'abc@def' });
|
|
28
|
+
const res = await request.get('/users', 200, {
|
|
29
|
+
query: { search: 'abc' },
|
|
30
|
+
headers: { hello: 'world' },
|
|
31
|
+
});
|
|
32
|
+
expect(res.body).toEqual([{ id: user.id, email: 'abc@def' }]);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { PsychicServer } from '@rvoh/psychic';
|
|
2
|
+
import { OpenapiSpecRequest } from '../../../src/unit/OpenapiSpecRequest.js';
|
|
3
|
+
import User from '../../../test-app/src/app/models/User.js';
|
|
4
|
+
const request = new OpenapiSpecRequest();
|
|
5
|
+
describe('OpenapiSpecRequest#patch', () => {
|
|
6
|
+
beforeEach(async () => {
|
|
7
|
+
await request.init(PsychicServer);
|
|
8
|
+
});
|
|
9
|
+
it('issues a patch request to a controller endpoint, passing when the status matches', async () => {
|
|
10
|
+
const user = await User.create({ email: 'abc@def' });
|
|
11
|
+
await request.patch('/users/{id}', 204, {
|
|
12
|
+
id: user.id,
|
|
13
|
+
data: {
|
|
14
|
+
email: 'how@yadoin',
|
|
15
|
+
},
|
|
16
|
+
headers: {
|
|
17
|
+
hello: 'world',
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
await User.findOrFailBy({ email: 'how@yadoin' });
|
|
21
|
+
});
|
|
22
|
+
context('without params', () => {
|
|
23
|
+
it('issues a patch request to a controller endpoint, passing when the status matches', async () => {
|
|
24
|
+
await request.patch('/user', 204, {
|
|
25
|
+
headers: {
|
|
26
|
+
hello: 'world',
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
await request.patch('/user', 204);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { PsychicServer } from '@rvoh/psychic';
|
|
2
|
+
import { OpenapiSpecRequest } from '../../../src/unit/OpenapiSpecRequest.js';
|
|
3
|
+
import User from '../../../test-app/src/app/models/User.js';
|
|
4
|
+
const request = new OpenapiSpecRequest();
|
|
5
|
+
describe('OpenapiSpecRequest#post', () => {
|
|
6
|
+
beforeEach(async () => {
|
|
7
|
+
await request.init(PsychicServer);
|
|
8
|
+
});
|
|
9
|
+
it('issues a get request to a controller endpoint, passing when the status matches', async () => {
|
|
10
|
+
await request.post('/users', 201, {
|
|
11
|
+
data: {
|
|
12
|
+
email: 'how@yadoin',
|
|
13
|
+
},
|
|
14
|
+
headers: {
|
|
15
|
+
hello: 'world',
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
await User.findOrFailBy({ email: 'how@yadoin' });
|
|
19
|
+
});
|
|
20
|
+
context('without params', () => {
|
|
21
|
+
it('issues a post request to a controller endpoint, passing when the status matches', async () => {
|
|
22
|
+
await request.post('/user', 201, {
|
|
23
|
+
headers: {
|
|
24
|
+
hello: 'world',
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
await request.post('/user', 201);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { PsychicServer } from '@rvoh/psychic';
|
|
2
|
+
import { OpenapiSpecRequest } from '../../../src/unit/OpenapiSpecRequest.js';
|
|
3
|
+
import User from '../../../test-app/src/app/models/User.js';
|
|
4
|
+
const request = new OpenapiSpecRequest();
|
|
5
|
+
describe('OpenapiSpecRequest#put', () => {
|
|
6
|
+
beforeEach(async () => {
|
|
7
|
+
await request.init(PsychicServer);
|
|
8
|
+
});
|
|
9
|
+
it('issues a patch request to a controller endpoint, passing when the status matches', async () => {
|
|
10
|
+
const user = await User.create({ email: 'abc@def' });
|
|
11
|
+
await request.put('/users/{id}/update-put', 204, {
|
|
12
|
+
id: user.id,
|
|
13
|
+
data: {
|
|
14
|
+
email: 'how@yadoin',
|
|
15
|
+
},
|
|
16
|
+
headers: {
|
|
17
|
+
hello: 'world',
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
await User.findOrFailBy({ email: 'how@yadoin' });
|
|
21
|
+
});
|
|
22
|
+
context('without params', () => {
|
|
23
|
+
it('issues a put request to a controller endpoint, passing when the status matches', async () => {
|
|
24
|
+
await request.put('/user/update-put', 204, {
|
|
25
|
+
headers: {
|
|
26
|
+
hello: 'world',
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
await request.put('/user/update-put', 204);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { PsychicServer } from '@rvoh/psychic';
|
|
2
|
+
import { OpenapiSpecRequest } from '../../../src/unit/OpenapiSpecRequest.js';
|
|
3
|
+
import User from '../../../test-app/src/app/models/User.js';
|
|
4
|
+
const request = new OpenapiSpecRequest();
|
|
5
|
+
describe('OpenapiSpecSession#delete', () => {
|
|
6
|
+
beforeEach(async () => {
|
|
7
|
+
await request.init(PsychicServer);
|
|
8
|
+
});
|
|
9
|
+
it('issues a delete request to a controller endpoint, passing when the status matches', async () => {
|
|
10
|
+
const user = await User.create({ email: 'abc@def' });
|
|
11
|
+
const session = await request.session('get', '/users', 200);
|
|
12
|
+
await session.delete('/users/{id}', 204, {
|
|
13
|
+
id: user.id,
|
|
14
|
+
});
|
|
15
|
+
expect(await User.count()).toEqual(0);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { PsychicServer } from '@rvoh/psychic';
|
|
2
|
+
import { OpenapiSpecRequest } from '../../../src/unit/OpenapiSpecRequest.js';
|
|
3
|
+
import User from '../../../test-app/src/app/models/User.js';
|
|
4
|
+
const request = new OpenapiSpecRequest();
|
|
5
|
+
describe('OpenapiSpecSession#get', () => {
|
|
6
|
+
beforeEach(async () => {
|
|
7
|
+
await request.init(PsychicServer);
|
|
8
|
+
});
|
|
9
|
+
it('issues a get request to a controller endpoint, passing when the status matches', async () => {
|
|
10
|
+
const user = await User.create({ email: 'abc@def' });
|
|
11
|
+
const session = await request.session('get', '/users', 200);
|
|
12
|
+
const res = await session.get('/users', 200);
|
|
13
|
+
expect(res.body).toEqual([{ id: user.id, email: 'abc@def' }]);
|
|
14
|
+
});
|
|
15
|
+
context('query params', () => {
|
|
16
|
+
it('issues a get request to a controller endpoint, passing when the status matches', async () => {
|
|
17
|
+
await User.create({ email: 'other@user' });
|
|
18
|
+
const user = await User.create({ email: 'abc@def' });
|
|
19
|
+
const session = await request.session('get', '/users', 200);
|
|
20
|
+
const res = await session.get('/users', 200, { query: { search: 'abc' } });
|
|
21
|
+
expect(res.body).toEqual([{ id: user.id, email: 'abc@def' }]);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { PsychicServer } from '@rvoh/psychic';
|
|
2
|
+
import { OpenapiSpecRequest } from '../../../src/unit/OpenapiSpecRequest.js';
|
|
3
|
+
import User from '../../../test-app/src/app/models/User.js';
|
|
4
|
+
const request = new OpenapiSpecRequest();
|
|
5
|
+
describe('OpenapiSpecSession#put', () => {
|
|
6
|
+
beforeEach(async () => {
|
|
7
|
+
await request.init(PsychicServer);
|
|
8
|
+
});
|
|
9
|
+
it('issues a patch request to a controller endpoint, passing when the status matches', async () => {
|
|
10
|
+
const user = await User.create({ email: 'abc@def' });
|
|
11
|
+
const session = await request.session('get', '/users', 200);
|
|
12
|
+
await session.patch('/users/{id}', 204, {
|
|
13
|
+
id: user.id,
|
|
14
|
+
data: {
|
|
15
|
+
email: 'how@yadoin',
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
await User.findOrFailBy({ email: 'how@yadoin' });
|
|
19
|
+
});
|
|
20
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { PsychicServer } from '@rvoh/psychic';
|
|
2
|
+
import { OpenapiSpecRequest } from '../../../src/unit/OpenapiSpecRequest.js';
|
|
3
|
+
import User from '../../../test-app/src/app/models/User.js';
|
|
4
|
+
const request = new OpenapiSpecRequest();
|
|
5
|
+
describe('OpenapiSpecSession#post', () => {
|
|
6
|
+
beforeEach(async () => {
|
|
7
|
+
await request.init(PsychicServer);
|
|
8
|
+
});
|
|
9
|
+
it('issues a get request to a controller endpoint, passing when the status matches', async () => {
|
|
10
|
+
const session = await request.session('get', '/users', 200);
|
|
11
|
+
await session.post('/users', 201, {
|
|
12
|
+
data: {
|
|
13
|
+
email: 'how@yadoin',
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
await User.findOrFailBy({ email: 'how@yadoin' });
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { PsychicServer } from '@rvoh/psychic';
|
|
2
|
+
import { OpenapiSpecRequest } from '../../../src/unit/OpenapiSpecRequest.js';
|
|
3
|
+
import User from '../../../test-app/src/app/models/User.js';
|
|
4
|
+
const request = new OpenapiSpecRequest();
|
|
5
|
+
describe('OpenapiSpecSession#put', () => {
|
|
6
|
+
beforeEach(async () => {
|
|
7
|
+
await request.init(PsychicServer);
|
|
8
|
+
});
|
|
9
|
+
it('issues a patch request to a controller endpoint, passing when the status matches', async () => {
|
|
10
|
+
const user = await User.create({ email: 'abc@def' });
|
|
11
|
+
const session = await request.session('get', '/users', 200);
|
|
12
|
+
await session.put('/users/{id}/update-put', 204, {
|
|
13
|
+
id: user.id,
|
|
14
|
+
data: {
|
|
15
|
+
email: 'how@yadoin',
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
await User.findOrFailBy({ email: 'how@yadoin' });
|
|
19
|
+
});
|
|
20
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import fillOpenapiParams from '../../../../src/unit/helpers/fillOpenapiParams.js';
|
|
2
|
+
describe('fillOpenapiUrlParams', () => {
|
|
3
|
+
it('pushes params into a route', () => {
|
|
4
|
+
expect(fillOpenapiParams('/users/{id}/pets/{petId}/posts/{postId}', {
|
|
5
|
+
id: 1,
|
|
6
|
+
petId: 2,
|
|
7
|
+
postId: 3,
|
|
8
|
+
otherId: 4,
|
|
9
|
+
})).toEqual('/users/1/pets/2/posts/3');
|
|
10
|
+
});
|
|
11
|
+
});
|
package/dist/esm/src/index.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// unit spec helpers
|
|
2
2
|
export { default as specRequest } from './unit/SpecRequest.js';
|
|
3
3
|
export { default as createPsychicServer } from './unit/createPsychicServer.js';
|
|
4
|
+
export { OpenapiSpecRequest } from './unit/OpenapiSpecRequest.js';
|
|
5
|
+
export { OpenapiSpecSession } from './unit/OpenapiSpecSession.js';
|
|
4
6
|
export { SpecRequest } from './unit/SpecRequest.js';
|
|
5
7
|
export { SpecSession } from './unit/SpecSession.js';
|
|
6
8
|
// feature spec helpers
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import supertest from 'supertest';
|
|
2
|
+
import { createPsychicServer } from '../index.js';
|
|
3
|
+
import fillOpenapiParams from './helpers/fillOpenapiParams.js';
|
|
4
|
+
import { OpenapiSpecSession } from './OpenapiSpecSession.js';
|
|
5
|
+
import supersession from './supersession.js';
|
|
6
|
+
export class OpenapiSpecRequest {
|
|
7
|
+
// eslint-disable-next-line
|
|
8
|
+
PsychicServer;
|
|
9
|
+
// eslint-disable-next-line
|
|
10
|
+
server;
|
|
11
|
+
// final
|
|
12
|
+
async get(uri, expectedStatus, opts) {
|
|
13
|
+
return (await this.makeRequest('get', fillOpenapiParams(uri, (opts || {})), expectedStatus, opts));
|
|
14
|
+
}
|
|
15
|
+
// final
|
|
16
|
+
async post(uri, expectedStatus, opts = {}) {
|
|
17
|
+
return await this.makeRequest('post', uri, expectedStatus, opts);
|
|
18
|
+
}
|
|
19
|
+
// final
|
|
20
|
+
async put(uri, expectedStatus, opts = {}) {
|
|
21
|
+
return await this.makeRequest('put', fillOpenapiParams(uri, opts || {}), expectedStatus, opts);
|
|
22
|
+
}
|
|
23
|
+
// final
|
|
24
|
+
async patch(uri, expectedStatus, opts = {}) {
|
|
25
|
+
return await this.makeRequest('patch', fillOpenapiParams(uri, opts), expectedStatus, opts);
|
|
26
|
+
}
|
|
27
|
+
// final
|
|
28
|
+
async delete(uri, expectedStatus, opts = {}) {
|
|
29
|
+
return await this.makeRequest('delete', fillOpenapiParams(uri, opts), expectedStatus, opts);
|
|
30
|
+
}
|
|
31
|
+
// eslint-disable-next-line
|
|
32
|
+
async init(PsychicServer) {
|
|
33
|
+
// eslint-disable-next-line
|
|
34
|
+
this.PsychicServer = PsychicServer;
|
|
35
|
+
this.server ||= await createPsychicServer(PsychicServer);
|
|
36
|
+
}
|
|
37
|
+
// final
|
|
38
|
+
async session(httpMethod, uri, expectedStatus, opts = {}) {
|
|
39
|
+
const postOpts = opts;
|
|
40
|
+
const getOpts = opts;
|
|
41
|
+
uri = fillOpenapiParams(uri, (opts || {}));
|
|
42
|
+
return await new Promise((accept, reject) => {
|
|
43
|
+
createPsychicServer(this.PsychicServer)
|
|
44
|
+
.then(server => {
|
|
45
|
+
const session = supersession(server);
|
|
46
|
+
// supersession is borrowed from a non-typescript repo, which
|
|
47
|
+
// does not have strong types around http methods, so we need to any cast
|
|
48
|
+
let req = session[(httpMethod || 'post')](`/${uri.replace(/^\//, '')}`);
|
|
49
|
+
if (postOpts.data) {
|
|
50
|
+
req = req.send(postOpts.data);
|
|
51
|
+
}
|
|
52
|
+
req
|
|
53
|
+
.expect(expectedStatus)
|
|
54
|
+
.query(getOpts.query || {})
|
|
55
|
+
.set(postOpts.headers || {})
|
|
56
|
+
.end((err) => {
|
|
57
|
+
if (err)
|
|
58
|
+
return reject(err);
|
|
59
|
+
return accept(new OpenapiSpecSession(session));
|
|
60
|
+
});
|
|
61
|
+
})
|
|
62
|
+
.catch(err => {
|
|
63
|
+
throw err;
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
async makeRequest(method, uri, expectedStatus, opts = {}) {
|
|
68
|
+
// TODO: find out why this is necessary. Currently, without initializing the server
|
|
69
|
+
// at the beginning of the specs, supertest is unable to use our server to handle requests.
|
|
70
|
+
// it gives the appearance of being an issue with a runaway promise (i.e. missing await)
|
|
71
|
+
// but I can't find it anywhere, so I am putting this init method in as a temporary fix.
|
|
72
|
+
if (!this.server)
|
|
73
|
+
throw new Error(`
|
|
74
|
+
ERROR:
|
|
75
|
+
When making use of the send spec helper, you must first call "await specRequest.init(PsychicServer)"
|
|
76
|
+
from a beforEach hook at the root of your specs.
|
|
77
|
+
`);
|
|
78
|
+
if (expectedStatus === 500) {
|
|
79
|
+
process.env.PSYCHIC_EXPECTING_INTERNAL_SERVER_ERROR = '1';
|
|
80
|
+
}
|
|
81
|
+
// eslint-disable-next-line
|
|
82
|
+
const req = supertest.agent(this.server.expressApp);
|
|
83
|
+
let request = req[method](`/${uri.replace(/^\//, '')}`);
|
|
84
|
+
if (opts.headers)
|
|
85
|
+
request = request.set(opts.headers);
|
|
86
|
+
if (opts.query)
|
|
87
|
+
request = request.query(opts.query);
|
|
88
|
+
if (method !== 'get')
|
|
89
|
+
request = request.send(opts.data);
|
|
90
|
+
try {
|
|
91
|
+
const res = await request.expect(expectedStatus);
|
|
92
|
+
process.env.PSYCHIC_EXPECTING_INTERNAL_SERVER_ERROR = undefined;
|
|
93
|
+
return res;
|
|
94
|
+
}
|
|
95
|
+
catch (err) {
|
|
96
|
+
// without manually console logging, you get no stack trace here
|
|
97
|
+
console.error(err);
|
|
98
|
+
console.trace();
|
|
99
|
+
process.env.PSYCHIC_EXPECTING_INTERNAL_SERVER_ERROR = undefined;
|
|
100
|
+
throw err;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import fillOpenapiParams from './helpers/fillOpenapiParams.js';
|
|
2
|
+
// like SpecRequest, but meant to be bound to an instance
|
|
3
|
+
// of supersession, enabling chained requests to collect cookies
|
|
4
|
+
export class OpenapiSpecSession {
|
|
5
|
+
_session;
|
|
6
|
+
constructor(_session) {
|
|
7
|
+
this._session = _session;
|
|
8
|
+
}
|
|
9
|
+
// final
|
|
10
|
+
async get(uri, expectedStatus, opts) {
|
|
11
|
+
return (await this.makeRequest('get', fillOpenapiParams(uri, (opts || {})), expectedStatus, opts));
|
|
12
|
+
}
|
|
13
|
+
// final
|
|
14
|
+
async post(uri, expectedStatus, opts = {}) {
|
|
15
|
+
return await this.makeRequest('post', uri, expectedStatus, opts);
|
|
16
|
+
}
|
|
17
|
+
// final
|
|
18
|
+
async put(uri, expectedStatus, opts = {}) {
|
|
19
|
+
return await this.makeRequest('put', fillOpenapiParams(uri, opts || {}), expectedStatus, opts);
|
|
20
|
+
}
|
|
21
|
+
// final
|
|
22
|
+
async patch(uri, expectedStatus, opts = {}) {
|
|
23
|
+
return await this.makeRequest('patch', fillOpenapiParams(uri, opts), expectedStatus, opts);
|
|
24
|
+
}
|
|
25
|
+
// final
|
|
26
|
+
async delete(uri, expectedStatus, opts = {}) {
|
|
27
|
+
return await this.makeRequest('delete', fillOpenapiParams(uri, opts), expectedStatus, opts);
|
|
28
|
+
}
|
|
29
|
+
async makeRequest(method, uri, expectedStatus, opts = {}) {
|
|
30
|
+
if (expectedStatus === 500) {
|
|
31
|
+
process.env.PSYCHIC_EXPECTING_INTERNAL_SERVER_ERROR = '1';
|
|
32
|
+
}
|
|
33
|
+
const req = this._session;
|
|
34
|
+
let request = req[method](`/${uri.replace(/^\//, '')}`);
|
|
35
|
+
if (opts.headers)
|
|
36
|
+
request = request.set(opts.headers);
|
|
37
|
+
if (opts.query)
|
|
38
|
+
request = request.query(opts.query);
|
|
39
|
+
if (method !== 'get')
|
|
40
|
+
request = request.send(opts.data);
|
|
41
|
+
try {
|
|
42
|
+
const res = await request.expect(expectedStatus);
|
|
43
|
+
process.env.PSYCHIC_EXPECTING_INTERNAL_SERVER_ERROR = undefined;
|
|
44
|
+
return res;
|
|
45
|
+
}
|
|
46
|
+
catch (err) {
|
|
47
|
+
// without manually console logging, you get no stack trace here
|
|
48
|
+
console.error(err);
|
|
49
|
+
console.trace();
|
|
50
|
+
process.env.PSYCHIC_EXPECTING_INTERNAL_SERVER_ERROR = undefined;
|
|
51
|
+
throw err;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
2
|
+
var useValue = arguments.length > 2;
|
|
3
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
4
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
5
|
+
}
|
|
6
|
+
return useValue ? value : void 0;
|
|
7
|
+
};
|
|
8
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
9
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
10
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
11
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
12
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
13
|
+
var _, done = false;
|
|
14
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
15
|
+
var context = {};
|
|
16
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
17
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
18
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
19
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
20
|
+
if (kind === "accessor") {
|
|
21
|
+
if (result === void 0) continue;
|
|
22
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
23
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
24
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
25
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
26
|
+
}
|
|
27
|
+
else if (_ = accept(result)) {
|
|
28
|
+
if (kind === "field") initializers.unshift(_);
|
|
29
|
+
else descriptor[key] = _;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
33
|
+
done = true;
|
|
34
|
+
};
|
|
35
|
+
import { OpenAPI } from '@rvoh/psychic';
|
|
36
|
+
import User from '../models/User.js';
|
|
37
|
+
import ApplicationController from './ApplicationController.js';
|
|
38
|
+
let UserController = (() => {
|
|
39
|
+
let _classSuper = ApplicationController;
|
|
40
|
+
let _instanceExtraInitializers = [];
|
|
41
|
+
let _create_decorators;
|
|
42
|
+
let _show_decorators;
|
|
43
|
+
let _update_decorators;
|
|
44
|
+
let _updatePut_decorators;
|
|
45
|
+
let _destroy_decorators;
|
|
46
|
+
return class UserController extends _classSuper {
|
|
47
|
+
static {
|
|
48
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
49
|
+
_create_decorators = [OpenAPI(User, {
|
|
50
|
+
status: 201,
|
|
51
|
+
})];
|
|
52
|
+
_show_decorators = [OpenAPI(User)];
|
|
53
|
+
_update_decorators = [OpenAPI(User, { status: 204 })];
|
|
54
|
+
_updatePut_decorators = [OpenAPI(User, { status: 204 })];
|
|
55
|
+
_destroy_decorators = [OpenAPI(User, { status: 204 })];
|
|
56
|
+
__esDecorate(this, null, _create_decorators, { kind: "method", name: "create", static: false, private: false, access: { has: obj => "create" in obj, get: obj => obj.create }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
57
|
+
__esDecorate(this, null, _show_decorators, { kind: "method", name: "show", static: false, private: false, access: { has: obj => "show" in obj, get: obj => obj.show }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
58
|
+
__esDecorate(this, null, _update_decorators, { kind: "method", name: "update", static: false, private: false, access: { has: obj => "update" in obj, get: obj => obj.update }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
59
|
+
__esDecorate(this, null, _updatePut_decorators, { kind: "method", name: "updatePut", static: false, private: false, access: { has: obj => "updatePut" in obj, get: obj => obj.updatePut }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
60
|
+
__esDecorate(this, null, _destroy_decorators, { kind: "method", name: "destroy", static: false, private: false, access: { has: obj => "destroy" in obj, get: obj => obj.destroy }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
61
|
+
if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
62
|
+
}
|
|
63
|
+
create() {
|
|
64
|
+
this.created();
|
|
65
|
+
}
|
|
66
|
+
show() {
|
|
67
|
+
this.ok();
|
|
68
|
+
}
|
|
69
|
+
update() {
|
|
70
|
+
this.noContent();
|
|
71
|
+
}
|
|
72
|
+
updatePut() {
|
|
73
|
+
this.noContent();
|
|
74
|
+
}
|
|
75
|
+
destroy() {
|
|
76
|
+
this.noContent();
|
|
77
|
+
}
|
|
78
|
+
constructor() {
|
|
79
|
+
super(...arguments);
|
|
80
|
+
__runInitializers(this, _instanceExtraInitializers);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
})();
|
|
84
|
+
export default UserController;
|