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,49 +47,32 @@ 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 ) {
50
+ async function runGqlOk ( authType , endpoint , query , variables , operationName , expected ) {
51
+ let headers = new GQLHeaders ( ) ;
49
52
switch ( authType ) {
50
53
case authTypes . adminKey :
51
- authValue = adminKey ;
54
+ headers . withAPIKey ( adminKey ) ;
52
55
break ;
53
56
case authTypes . apiKey :
54
- authValue = apiKey ;
57
+ headers . withAPIKey ( apiKey ) ;
55
58
break ;
56
59
// Have not implemented jwt and noAuth yet
57
60
case authTypes . jwt :
58
61
case authTypes . noAuth :
59
62
default :
60
63
authValue = "" ;
61
64
}
62
- return fetch ( endpoint , {
63
- method : "POST" ,
64
- headers : {
65
- "Content-Type" : "application/json" ,
66
- Authorization : authValue ,
67
- } ,
68
- body : JSON . stringify ( {
65
+ await executeOK ( {
66
+ test : this ,
67
+ endpoint,
68
+ headers,
69
+ request : {
69
70
query : query ,
70
71
variables : variables ,
71
72
operationName : operationName ,
72
- } ) ,
73
+ } ,
74
+ expected,
73
75
} )
74
- . then ( function ( result ) {
75
- expect ( result . status ) . to . equal ( 200 ) ;
76
- return result ;
77
- } )
78
- . then ( function ( result ) {
79
- return result . json ( ) ;
80
- } )
81
- . then ( function ( response ) {
82
- expect ( response . errors , `no errors should exist: ${ JSON . stringify ( response . errors ) } ` ) . to . be . undefined ;
83
- return response ;
84
- } ) ;
85
- }
86
-
87
- // tests that the data key in a GraphQL response
88
- // is equal to value.
89
- function expectData ( response , value ) {
90
- expect ( response . data ) . to . eql ( value ) ;
91
76
}
92
77
93
78
// deploys graphql schema located in dirname to the test endpoint provided by the environment (process.env.STEPZEN_ENDPOINT),
@@ -101,19 +86,19 @@ function deployAndRun(dirname, tests) {
101
86
return deployEndpoint ( endpoint , dirname ) ;
102
87
} ) ;
103
88
89
+ afterEach ( 'log-failure' , logOnFail )
104
90
tests . forEach (
105
91
( { label, query, variables, operationName, expected, authType } ) => {
106
- it ( label , function ( ) {
92
+ it ( label , async function ( ) {
107
93
this . timeout ( 4000 ) ; // Occasional requests take > 2s
108
- return runGqlOk (
94
+ return await runGqlOk (
109
95
authType ,
110
96
endpoint ,
111
97
query ,
112
98
variables ,
113
- operationName
114
- ) . then ( function ( response ) {
115
- expectData ( response , expected ) ;
116
- } ) ;
99
+ operationName ,
100
+ expected ,
101
+ ) ;
117
102
} ) ;
118
103
}
119
104
) ;
@@ -127,8 +112,6 @@ function getTestDescription(testRoot, fullDirName) {
127
112
return segments . slice ( rootIndex + 1 , - 1 ) . join ( "/" ) ;
128
113
}
129
114
130
- exports . runGqlOk = runGqlOk ;
131
- exports . expectData = expectData ;
132
115
exports . deployAndRun = deployAndRun ;
133
116
exports . authTypes = authTypes ;
134
117
exports . getTestDescription = getTestDescription ;
0 commit comments