1
1
require ( "dotenv" ) . config ( ) ;
2
- const fetch = require ( "node-fetch" ) ;
3
- const { expect } = require ( "chai" ) ;
4
2
const { execSync } = require ( "child_process" ) ;
5
3
const { URL } = require ( "url" ) ;
6
4
const path = require ( "node:path" ) ;
7
5
6
+ const {
7
+ GQLHeaders,
8
+ executeOK,
9
+ logOnFail,
10
+ } = require ( 'gqltest/packages/gqltest/gqltest.js' )
11
+
8
12
const authTypes = {
9
13
adminKey : 1 ,
10
14
apiKey : 2 ,
@@ -15,10 +19,8 @@ Object.freeze(authTypes);
15
19
16
20
// We use admin key to test because there is a cache optimization for apikey's that is not conducive
17
21
// to rapid deploy and run cycles that occur with this type of testing
18
- const adminKey =
19
- `apikey ` + execSync ( `stepzen whoami --adminkey` ) . toString ( ) . trim ( ) ;
20
- const apiKey =
21
- `apikey ` + execSync ( `stepzen whoami --apikey` ) . toString ( ) . trim ( ) ;
22
+ const adminKey = execSync ( `stepzen whoami --adminkey` ) . toString ( ) . trim ( ) ;
23
+ const apiKey = execSync ( `stepzen whoami --apikey` ) . toString ( ) . trim ( ) ;
22
24
23
25
const endpoint = process . env . STEPZEN_ENDPOINT ;
24
26
@@ -45,56 +47,31 @@ function deployEndpoint(endpoint, dirname) {
45
47
// as a test returning the response.
46
48
// The test will fail if the request does not
47
49
// have status 200 or has any GraphQL errors.
48
- function runGqlOk ( authType , endpoint , query , variables , operationName ) {
49
- let authValue ;
50
+ async function runGqlOk ( authType , endpoint , query , variables , operationName , expected ) {
51
+ let headers = new GQLHeaders ( ) ;
50
52
switch ( authType ) {
51
53
case authTypes . adminKey :
52
- authValue = adminKey ;
54
+ headers . withAPIKey ( adminKey ) ;
53
55
break ;
54
56
case authTypes . apiKey :
55
- authValue = apiKey ;
57
+ headers . withAPIKey ( apiKey ) ;
56
58
break ;
57
- // Have not implemented jwt and noAuth yet
59
+ // Have not implemented jwt yet
58
60
case authTypes . jwt :
59
61
case authTypes . noAuth :
60
62
default :
61
- authValue = "" ;
62
- }
63
-
64
- let headers = {
65
- "Content-Type" : "application/json" ,
66
- } ;
67
-
68
- if ( authValue ) {
69
- headers . Authorization = authValue ;
70
63
}
71
-
72
- return fetch ( endpoint , {
73
- method : "POST" ,
74
- headers : headers ,
75
- body : JSON . stringify ( {
64
+ await executeOK ( {
65
+ test : this ,
66
+ endpoint ,
67
+ headers,
68
+ request : {
76
69
query : query ,
77
70
variables : variables ,
78
71
operationName : operationName ,
79
- } ) ,
72
+ } ,
73
+ expected,
80
74
} )
81
- . then ( function ( result ) {
82
- expect ( result . status ) . to . equal ( 200 ) ;
83
- return result ;
84
- } )
85
- . then ( function ( result ) {
86
- return result . json ( ) ;
87
- } )
88
- . then ( function ( response ) {
89
- expect ( response . errors , `no errors should exist: ${ JSON . stringify ( response . errors ) } ` ) . to . be . undefined ;
90
- return response ;
91
- } ) ;
92
- }
93
-
94
- // tests that the data key in a GraphQL response
95
- // is equal to value.
96
- function expectData ( response , value ) {
97
- expect ( response . data ) . to . eql ( value ) ;
98
75
}
99
76
100
77
// deploys graphql schema located in dirname to the test endpoint provided by the environment (process.env.STEPZEN_ENDPOINT),
@@ -108,19 +85,19 @@ function deployAndRun(dirname, tests) {
108
85
return deployEndpoint ( endpoint , dirname ) ;
109
86
} ) ;
110
87
88
+ afterEach ( 'log-failure' , logOnFail )
111
89
tests . forEach (
112
90
( { label, query, variables, operationName, expected, authType } ) => {
113
- it ( label , function ( ) {
91
+ it ( label , async function ( ) {
114
92
this . timeout ( 4000 ) ; // Occasional requests take > 2s
115
- return runGqlOk (
93
+ return await runGqlOk (
116
94
authType ,
117
95
endpoint ,
118
96
query ,
119
97
variables ,
120
- operationName
121
- ) . then ( function ( response ) {
122
- expectData ( response , expected ) ;
123
- } ) ;
98
+ operationName ,
99
+ expected ,
100
+ ) ;
124
101
} ) ;
125
102
}
126
103
) ;
@@ -134,8 +111,6 @@ function getTestDescription(testRoot, fullDirName) {
134
111
return segments . slice ( rootIndex + 1 , - 1 ) . join ( "/" ) ;
135
112
}
136
113
137
- exports . runGqlOk = runGqlOk ;
138
- exports . expectData = expectData ;
139
114
exports . deployAndRun = deployAndRun ;
140
115
exports . authTypes = authTypes ;
141
116
exports . getTestDescription = getTestDescription ;
0 commit comments