1
1
import * as path from 'path' ;
2
2
import * as Serverless from 'serverless' ;
3
3
import { DefinitionGenerator } from '../DefinitionGenerator' ;
4
+ import { merge } from '../utils' ;
4
5
5
6
class ServerlessInterface extends Serverless {
6
7
public service : any = { } ;
@@ -11,10 +12,12 @@ class ServerlessInterface extends Serverless {
11
12
}
12
13
13
14
describe ( 'OpenAPI Documentation Generator' , ( ) => {
14
- it ( 'Generates OpenAPI document' , async ( ) => {
15
+ let sls : ServerlessInterface ;
16
+
17
+ beforeEach ( async ( ) => {
15
18
const servicePath = path . join ( __dirname , '../../test/project' ) ;
16
19
const serverlessYamlPath = path . join ( servicePath , './serverless.yml' ) ;
17
- const sls : ServerlessInterface = new Serverless ( ) ;
20
+ sls = new Serverless ( ) ;
18
21
19
22
sls . config . update ( {
20
23
servicePath,
@@ -26,12 +29,67 @@ describe('OpenAPI Documentation Generator', () => {
26
29
await sls . service . load ( config ) ;
27
30
await sls . variables . populateService ( ) ;
28
31
29
- if ( 'documentation' in sls . service . custom ) {
30
- const docGen = new DefinitionGenerator ( sls . service . custom . documentation ) ;
31
-
32
- expect ( docGen ) . not . toBeNull ( ) ;
33
- } else {
32
+ if ( ! ( 'documentation' in sls . service . custom ) ) {
34
33
throw new Error ( 'Cannot find "documentation" in custom section of "serverless.yml"' ) ;
35
34
}
36
35
} ) ;
36
+
37
+ it ( 'Generates OpenAPI document' , async ( ) => {
38
+ const docGen = new DefinitionGenerator ( sls . service . custom . documentation ) ;
39
+ expect ( docGen ) . not . toBeNull ( ) ;
40
+ } ) ;
41
+
42
+ it ( 'adds paths to OpenAPI output from function configuration' , async ( ) => {
43
+ const docGen = new DefinitionGenerator ( sls . service . custom . documentation ) ;
44
+
45
+ // implementation copied from ServerlessOpenApiDocumentation.ts
46
+ docGen . parse ( ) ;
47
+
48
+ const funcConfigs = sls . service . getAllFunctions ( ) . map ( ( functionName ) => {
49
+ const func = sls . service . getFunction ( functionName ) ;
50
+ return merge ( { _functionName : functionName } , func ) ;
51
+ } ) ;
52
+
53
+ docGen . readFunctions ( funcConfigs ) ;
54
+
55
+ // get the parameters from the `/create POST' endpoint
56
+ const actual = docGen . definition . paths [ '/create' ] . post . parameters ;
57
+ const expected = [
58
+ {
59
+ description : 'The username for a user to create' ,
60
+ in : 'path' ,
61
+ name : 'username' ,
62
+ required : true ,
63
+ schema : {
64
+ pattern : '^[-a-z0-9_]+$' ,
65
+ type : 'string' ,
66
+ } ,
67
+ } ,
68
+ {
69
+ allowEmptyValue : false ,
70
+ description : `The user's Membership Type` ,
71
+ in : 'query' ,
72
+ name : 'membershipType' ,
73
+ required : false ,
74
+ schema : {
75
+ enum : [
76
+ 'premium' ,
77
+ 'standard' ,
78
+ ] ,
79
+ type : 'string' ,
80
+ } ,
81
+ } ,
82
+ {
83
+ description : 'A Session ID variable' ,
84
+ in : 'cookie' ,
85
+ name : 'SessionId' ,
86
+ required : false ,
87
+ schema : {
88
+ type : 'string' ,
89
+ } ,
90
+ } ,
91
+ ] ;
92
+
93
+ expect ( actual ) . toEqual ( expected ) ;
94
+ } ) ;
37
95
} ) ;
0 commit comments