@@ -12,14 +12,15 @@ class AuthenticationTest extends \Codeception\TestCase\WPTestCase {
1212
1313 public $ admin ;
1414 public $ login_mutation ;
15+ public $ admin_username ;
16+ public $ admin_password ;
1517
1618 /**
1719 * This function is run before each method
1820 * @since 0.0.5
1921 */
2022 public function setUp (): void {
2123
22- $ _SERVER ['HTTP_AUTHORIZATION ' ] = 'Bearer goo ' ;
2324
2425 add_filter ( 'graphql_debug_enabled ' , '__return_true ' );
2526 add_filter ( 'graphql_jwt_auth_secret_key ' , function () {
@@ -28,10 +29,13 @@ public function setUp(): void {
2829
2930 parent ::setUp ();
3031
32+ $ this ->admin_password = 'testPassword ' ;
33+ $ this ->admin_username = 'testuser ' ;
34+
3135 $ this ->admin = $ this ->factory ->user ->create ( [
3236 'role ' => 'administrator ' ,
33- 'user_login ' => ' testuser ' ,
34- 'user_pass ' => ' testPassword ' ,
37+ 'user_login ' => $ this -> admin_username ,
38+ 'user_pass ' => $ this -> admin_password ,
3539 ] );
3640
3741
@@ -44,9 +48,13 @@ public function setUp(): void {
4448 pages{
4549 edges{
4650 node{
47- id
4851 title
4952 content
53+ author {
54+ node {
55+ databaseId
56+ }
57+ }
5058 }
5159 }
5260 }
@@ -77,9 +85,9 @@ public function testLoginWithBadCredentials() {
7785 'query ' => $ this ->login_mutation ,
7886 'variables ' => [
7987 'input ' => [
80- 'username ' => ' testuser ' ,
88+ 'username ' => $ this -> admin_username ,
8189 'password ' => 'badPassword ' ,
82- 'clientMutationId ' => uniqid (),
90+ 'clientMutationId ' => uniqid ( '' , true ),
8391 ]
8492 ]
8593 ]);
@@ -125,9 +133,8 @@ public function testLoginWithPage() {
125133 'query ' => $ this ->login_mutation ,
126134 'variables ' => [
127135 'input ' => [
128- 'username ' => 'testuser ' ,
129- 'password ' => 'testPassword ' ,
130- 'clientMutationId ' => uniqid (),
136+ 'username ' => $ this ->admin_username ,
137+ 'password ' => $ this ->admin_password ,
131138 ]
132139 ]
133140 ]);
@@ -138,14 +145,18 @@ public function testLoginWithPage() {
138145 * Establish the expectation for the output of the query
139146 */
140147 $ expected_user = [
141- 'username ' => ' testuser ' ,
148+ 'username ' => $ this -> admin_username ,
142149 'pages ' => [
143150 'edges ' => [
144151 [
145152 'node ' => [
146- 'id ' => $ global_id ,
147153 'title ' => 'Test Page Title ' ,
148154 'content ' => apply_filters ( 'the_content ' , $ args ['post_content ' ] ),
155+ 'author ' => [
156+ 'node ' => [
157+ 'databaseId ' => $ this ->admin
158+ ],
159+ ],
149160 ],
150161 ],
151162 ],
@@ -175,9 +186,9 @@ public function testLoginWithNoSecretKeyConfigured() {
175186 'query ' => $ this ->login_mutation ,
176187 'variables ' => [
177188 'input ' => [
178- 'username ' => ' testuser ' ,
179- 'password ' => ' testPassword ' ,
180- 'clientMutationId ' => uniqid (),
189+ 'username ' => $ this -> admin_username ,
190+ 'password ' => $ this -> admin_password ,
191+ 'clientMutationId ' => uniqid ( '' , true ),
181192 ]
182193 ]
183194 ] );
@@ -189,14 +200,16 @@ public function testLoginWithNoSecretKeyConfigured() {
189200
190201 }
191202
203+ public function filter_authentication () {
204+ return 'goo ' ;
205+ }
206+
192207 public function testLoginWithValidUserThatWasJustDeleted () {
193208
194209 /**
195210 * Filter the authentication to make sure it returns an error
196211 */
197- add_filter ( 'authenticate ' , function () {
198- return 'goo ' ;
199- }, 9999 );
212+ add_filter ( 'authenticate ' , [ $ this , 'filter_authentication ' ], 9999 );
200213
201214 /**
202215 * Run the GraphQL query
@@ -205,9 +218,9 @@ public function testLoginWithValidUserThatWasJustDeleted() {
205218 'query ' => $ this ->login_mutation ,
206219 'variables ' => [
207220 'input ' => [
208- 'username ' => ' testuser ' ,
209- 'password ' => ' testPassword ' ,
210- 'clientMutationId ' => uniqid (),
221+ 'username ' => $ this -> admin_username ,
222+ 'password ' => $ this -> admin_password ,
223+ 'clientMutationId ' => uniqid ( '' , true ),
211224 ]
212225 ]
213226 ]);
@@ -217,6 +230,8 @@ public function testLoginWithValidUserThatWasJustDeleted() {
217230 */
218231 $ this ->assertArrayHasKey ( 'errors ' , $ actual );
219232
233+ remove_filter ( 'authenticate ' , [ $ this , 'filter_authentication ' ], 9999 );
234+
220235 }
221236
222237 public function testNonAuthenticatedRequest () {
@@ -267,6 +282,8 @@ public function testRequestWithNoToken() {
267282
268283 public function testRequestWithInvalidToken () {
269284
285+ wp_set_current_user ( $ this ->admin );
286+
270287 add_filter ( 'graphql_jwt_auth_token_before_sign ' , function ( $ token ) {
271288 $ token ['iss ' ] = null ;
272289 return $ token ;
@@ -279,11 +296,15 @@ public function testRequestWithInvalidToken() {
279296 return 'Bearer ' . $ token ;
280297 } );
281298
299+ codecept_debug ( [ 'invalidToken ' => $ token ]);
300+
282301 /**
283302 * Validate the token (should not work because we filtered the iss to make it invalid)
284303 */
285304 $ token = \WPGraphQL \JWT_Authentication \Auth::validate_token ( $ token );
286305
306+ codecept_debug ( $ token );
307+
287308 /**
288309 * Validate token should return nothing if it can't be validated properly
289310 */
@@ -296,27 +317,40 @@ public function testRequestWithInvalidToken() {
296317 */
297318 public function testNoSecretKey () {
298319
320+ // $_SERVER['HTTP_AUTHORIZATION'] = 'Bearer goo';
321+
299322 /**
300323 * Filter the secret key to return null, which should cause an exception to be thrown
301324 */
302325 add_filter ( 'graphql_jwt_auth_secret_key ' , function () {
303326 return null ;
304327 } );
305328
306- /**
307- * Set our expected exception
308- */
309- $ this ->expectException ( 'Exception ' , 'JWT is not configured properly ' );
310329
311330 /**
312331 * Run the function to determine the current user
313332 */
314333 $ user = \WPGraphQL \JWT_Authentication \Auth::filter_determine_current_user ( 0 );
315334
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+
316349 /**
317350 * Ensure that the Exception prevented any user from being authenticated
318351 */
319- $ this ->assertEquals ( 0 , $ user );
352+ $ this ->assertNull ( $ actual ['data ' ]['login ' ] );
353+ $ this ->assertArrayHasKey ( 'errors ' , $ actual );
320354
321355 }
322356
0 commit comments