@@ -12,14 +12,15 @@ class AuthenticationTest extends \Codeception\TestCase\WPTestCase {
12
12
13
13
public $ admin ;
14
14
public $ login_mutation ;
15
+ public $ admin_username ;
16
+ public $ admin_password ;
15
17
16
18
/**
17
19
* This function is run before each method
18
20
* @since 0.0.5
19
21
*/
20
22
public function setUp (): void {
21
23
22
- $ _SERVER ['HTTP_AUTHORIZATION ' ] = 'Bearer goo ' ;
23
24
24
25
add_filter ( 'graphql_debug_enabled ' , '__return_true ' );
25
26
add_filter ( 'graphql_jwt_auth_secret_key ' , function () {
@@ -28,10 +29,13 @@ public function setUp(): void {
28
29
29
30
parent ::setUp ();
30
31
32
+ $ this ->admin_password = 'testPassword ' ;
33
+ $ this ->admin_username = 'testuser ' ;
34
+
31
35
$ this ->admin = $ this ->factory ->user ->create ( [
32
36
'role ' => 'administrator ' ,
33
- 'user_login ' => ' testuser ' ,
34
- 'user_pass ' => ' testPassword ' ,
37
+ 'user_login ' => $ this -> admin_username ,
38
+ 'user_pass ' => $ this -> admin_password ,
35
39
] );
36
40
37
41
@@ -44,9 +48,13 @@ public function setUp(): void {
44
48
pages{
45
49
edges{
46
50
node{
47
- id
48
51
title
49
52
content
53
+ author {
54
+ node {
55
+ databaseId
56
+ }
57
+ }
50
58
}
51
59
}
52
60
}
@@ -77,9 +85,9 @@ public function testLoginWithBadCredentials() {
77
85
'query ' => $ this ->login_mutation ,
78
86
'variables ' => [
79
87
'input ' => [
80
- 'username ' => ' testuser ' ,
88
+ 'username ' => $ this -> admin_username ,
81
89
'password ' => 'badPassword ' ,
82
- 'clientMutationId ' => uniqid (),
90
+ 'clientMutationId ' => uniqid ( '' , true ),
83
91
]
84
92
]
85
93
]);
@@ -125,9 +133,8 @@ public function testLoginWithPage() {
125
133
'query ' => $ this ->login_mutation ,
126
134
'variables ' => [
127
135
'input ' => [
128
- 'username ' => 'testuser ' ,
129
- 'password ' => 'testPassword ' ,
130
- 'clientMutationId ' => uniqid (),
136
+ 'username ' => $ this ->admin_username ,
137
+ 'password ' => $ this ->admin_password ,
131
138
]
132
139
]
133
140
]);
@@ -138,14 +145,18 @@ public function testLoginWithPage() {
138
145
* Establish the expectation for the output of the query
139
146
*/
140
147
$ expected_user = [
141
- 'username ' => ' testuser ' ,
148
+ 'username ' => $ this -> admin_username ,
142
149
'pages ' => [
143
150
'edges ' => [
144
151
[
145
152
'node ' => [
146
- 'id ' => $ global_id ,
147
153
'title ' => 'Test Page Title ' ,
148
154
'content ' => apply_filters ( 'the_content ' , $ args ['post_content ' ] ),
155
+ 'author ' => [
156
+ 'node ' => [
157
+ 'databaseId ' => $ this ->admin
158
+ ],
159
+ ],
149
160
],
150
161
],
151
162
],
@@ -175,9 +186,9 @@ public function testLoginWithNoSecretKeyConfigured() {
175
186
'query ' => $ this ->login_mutation ,
176
187
'variables ' => [
177
188
'input ' => [
178
- 'username ' => ' testuser ' ,
179
- 'password ' => ' testPassword ' ,
180
- 'clientMutationId ' => uniqid (),
189
+ 'username ' => $ this -> admin_username ,
190
+ 'password ' => $ this -> admin_password ,
191
+ 'clientMutationId ' => uniqid ( '' , true ),
181
192
]
182
193
]
183
194
] );
@@ -189,14 +200,16 @@ public function testLoginWithNoSecretKeyConfigured() {
189
200
190
201
}
191
202
203
+ public function filter_authentication () {
204
+ return 'goo ' ;
205
+ }
206
+
192
207
public function testLoginWithValidUserThatWasJustDeleted () {
193
208
194
209
/**
195
210
* Filter the authentication to make sure it returns an error
196
211
*/
197
- add_filter ( 'authenticate ' , function () {
198
- return 'goo ' ;
199
- }, 9999 );
212
+ add_filter ( 'authenticate ' , [ $ this , 'filter_authentication ' ], 9999 );
200
213
201
214
/**
202
215
* Run the GraphQL query
@@ -205,9 +218,9 @@ public function testLoginWithValidUserThatWasJustDeleted() {
205
218
'query ' => $ this ->login_mutation ,
206
219
'variables ' => [
207
220
'input ' => [
208
- 'username ' => ' testuser ' ,
209
- 'password ' => ' testPassword ' ,
210
- 'clientMutationId ' => uniqid (),
221
+ 'username ' => $ this -> admin_username ,
222
+ 'password ' => $ this -> admin_password ,
223
+ 'clientMutationId ' => uniqid ( '' , true ),
211
224
]
212
225
]
213
226
]);
@@ -217,6 +230,8 @@ public function testLoginWithValidUserThatWasJustDeleted() {
217
230
*/
218
231
$ this ->assertArrayHasKey ( 'errors ' , $ actual );
219
232
233
+ remove_filter ( 'authenticate ' , [ $ this , 'filter_authentication ' ], 9999 );
234
+
220
235
}
221
236
222
237
public function testNonAuthenticatedRequest () {
@@ -267,6 +282,8 @@ public function testRequestWithNoToken() {
267
282
268
283
public function testRequestWithInvalidToken () {
269
284
285
+ wp_set_current_user ( $ this ->admin );
286
+
270
287
add_filter ( 'graphql_jwt_auth_token_before_sign ' , function ( $ token ) {
271
288
$ token ['iss ' ] = null ;
272
289
return $ token ;
@@ -279,11 +296,15 @@ public function testRequestWithInvalidToken() {
279
296
return 'Bearer ' . $ token ;
280
297
} );
281
298
299
+ codecept_debug ( [ 'invalidToken ' => $ token ]);
300
+
282
301
/**
283
302
* Validate the token (should not work because we filtered the iss to make it invalid)
284
303
*/
285
304
$ token = \WPGraphQL \JWT_Authentication \Auth::validate_token ( $ token );
286
305
306
+ codecept_debug ( $ token );
307
+
287
308
/**
288
309
* Validate token should return nothing if it can't be validated properly
289
310
*/
@@ -296,27 +317,40 @@ public function testRequestWithInvalidToken() {
296
317
*/
297
318
public function testNoSecretKey () {
298
319
320
+ // $_SERVER['HTTP_AUTHORIZATION'] = 'Bearer goo';
321
+
299
322
/**
300
323
* Filter the secret key to return null, which should cause an exception to be thrown
301
324
*/
302
325
add_filter ( 'graphql_jwt_auth_secret_key ' , function () {
303
326
return null ;
304
327
} );
305
328
306
- /**
307
- * Set our expected exception
308
- */
309
- $ this ->expectException ( 'Exception ' , 'JWT is not configured properly ' );
310
329
311
330
/**
312
331
* Run the function to determine the current user
313
332
*/
314
333
$ user = \WPGraphQL \JWT_Authentication \Auth::filter_determine_current_user ( 0 );
315
334
335
+ codecept_debug ( [ 'user ' => $ user ] );
336
+
337
+ $ actual = graphql ([
338
+ 'query ' => $ this ->login_mutation ,
339
+ 'variables ' => [
340
+ 'input ' => [
341
+ 'username ' => $ this ->admin_username ,
342
+ 'password ' => $ this ->admin_password ,
343
+ ]
344
+ ]
345
+ ]);
346
+
347
+ codecept_debug ( $ actual );
348
+
316
349
/**
317
350
* Ensure that the Exception prevented any user from being authenticated
318
351
*/
319
- $ this ->assertEquals ( 0 , $ user );
352
+ $ this ->assertNull ( $ actual ['data ' ]['login ' ] );
353
+ $ this ->assertArrayHasKey ( 'errors ' , $ actual );
320
354
321
355
}
322
356
0 commit comments