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