1+ <?php
2+ $ I = new FunctionalTester ($ scenario );
3+ $ I ->wantTo ('Make an authenticated request to generate a Refresh token ' );
4+
5+ $ username = uniqid ();
6+ $ user = $ I ->haveUserInDatabase ( $ username , 'administrator ' , [ 'user_pass ' => 'password ' ] );
7+
8+ /**
9+ * Login with username and password to get the authToken for use in the subsequent Authenticated request
10+ */
11+ $ I ->sendPOST ( 'http://wp.localhost/graphql ' , json_encode ([
12+ 'query ' => '
13+ mutation Login($input: LoginInput!) {
14+ login( input: $input ) {
15+ authToken
16+ refreshToken
17+ user {
18+ username
19+ }
20+ }
21+ }
22+ ' ,
23+ 'variables ' => [
24+ 'input ' => [
25+ 'username ' => $ username ,
26+ 'password ' => 'password ' ,
27+ 'clientMutationId ' => uniqid (),
28+ ]
29+ ],
30+ ], true ));
31+
32+ $ I ->seeResponseCodeIs ( 200 );
33+ $ I ->seeResponseIsJson ();
34+
35+ $ response = $ I ->grabResponse ();
36+ $ response_array = json_decode ( $ response , true );
37+ $ I ->assertArrayNotHasKey ( 'errors ' , $ response_array );
38+ $ I ->assertArrayHasKey ( 'data ' , $ response_array );
39+
40+ $ authToken = $ response_array ['data ' ]['login ' ]['authToken ' ];
41+ $ refreshToken = $ response_array ['data ' ]['login ' ]['refreshToken ' ];
42+
43+ /**
44+ * Set the Authorization header using the authToken retrieved in the previous request.
45+ * The authToken can be used to access resources (or mutate data) on behalf of the user it was issued for.
46+ * Here we will make a request to get data about the user, using the authToken as the mechanism for setting
47+ * the current user.
48+ */
49+ $ I ->setHeader ( 'Authorization ' , 'Bearer ' . $ authToken );
50+ $ I ->sendPOST ( 'http://wp.localhost/graphql ' , json_encode ([
51+ 'query ' => '
52+ {
53+ viewer {
54+ username
55+ jwtAuthToken
56+ jwtUserSecret
57+ jwtRefreshToken
58+ jwtAuthExpiration
59+ isJwtAuthSecretRevoked
60+ }
61+ }
62+ ' ,
63+ ], true ));
64+
65+ /**
66+ * The repsonse code should be 200
67+ */
68+ $ I ->seeResponseCodeIs ( 200 );
69+
70+ /**
71+ * Grab the Refresh header. Because the request was properly authenticated, there should
72+ * be a valid refresh header in the response
73+ */
74+ $ refreshTokenHeader = $ I ->grabHttpHeader ('X-JWT-Refresh ' );
75+ $ I ->assertNotEmpty ( $ refreshTokenHeader );
76+
77+ /**
78+ * The response should be JSON
79+ */
80+ $ I ->seeResponseIsJson ();
81+
82+ /**
83+ * Get the JSON response
84+ */
85+ $ response = $ I ->grabResponse ();
86+
87+ /**
88+ * Convert the response to JSON for making assertions
89+ */
90+ $ response_array = json_decode ( $ response , true );
91+
92+ /**
93+ * The request should be valid, so we expect no errors
94+ */
95+ $ I ->assertArrayNotHasKey ( 'errors ' , $ response_array );
96+
97+ /**
98+ * A valid request should contain the data in the response
99+ */
100+ $ I ->assertArrayHasKey ( 'data ' , $ response_array );
101+
102+ /**
103+ * The username of the viewer should match the username for the Token we retrieved and sent a request with
104+ */
105+ $ I ->assertEquals ( $ username , $ response_array ['data ' ]['viewer ' ]['username ' ] );
106+
107+ /**
108+ * The request should provide a new jwtAuthToken that we can use for future requests
109+ */
110+ $ I ->assertNotEmpty ( $ response_array ['data ' ]['viewer ' ]['jwtAuthToken ' ] );
111+
112+ /**
113+ * The request should provide the secret for the user, because the user has access to see their own JWT secret
114+ */
115+ $ I ->assertNotEmpty ( $ response_array ['data ' ]['viewer ' ]['jwtUserSecret ' ] );
116+
117+ /**
118+ * The request should provide a new JWT Refresh Token that can be used for future requests to get a new AccessToken
119+ */
120+ $ I ->assertNotEmpty ( $ response_array ['data ' ]['viewer ' ]['jwtRefreshToken ' ] );
121+
122+ /**
123+ * The request should provide info on the auth expiration for the user. This field is useful for building an interface
124+ * where the user can see how long their expiration is and can then mutate the expiration timeframe, should there be
125+ * per-user customization of expiration settings.
126+ */
127+ $ I ->assertNotEmpty ( $ response_array ['data ' ]['viewer ' ]['jwtAuthExpiration ' ] );
128+
129+ /**
130+ * The JWT should not be revoked for this user, so this assertion should be false
131+ */
132+ $ I ->assertFalse ( $ response_array ['data ' ]['viewer ' ]['isJwtAuthSecretRevoked ' ] );
0 commit comments