@@ -21,7 +21,8 @@ def app
2121 def test_auth_post
2222 data_to_post = {
2323 username : 'aadmin' ,
24- password : 'password'
24+ password : 'password' ,
25+ remember : true
2526 }
2627 # Get response back for logging in with username 'aadmin' password 'password'
2728 post_json '/api/auth.json' , data_to_post
@@ -50,6 +51,30 @@ def test_auth_post
5051
5152 # User has the token - count of matching tokens for that user is 1
5253 assert_equal 1 , expected_auth . auth_tokens . select { |t | t . authentication_token == actual_auth [ 'auth_token' ] } . count
54+
55+ # Check we got a refresh token
56+ assert last_response . cookies [ 'refresh_token' ] , 'Expect refresh token to be set'
57+ assert last_response . cookies [ 'username' ] , 'Expect username to be set'
58+
59+ refresh_token = User . first . auth_tokens . where ( token_type : :refresh_token ) . last
60+ assert refresh_token . present?
61+ assert_match ( /refresh_token=#{ refresh_token . authentication_token } ;/ , last_response . cookies [ 'refresh_token' ] . to_s , 'Expect refresh token to be set' )
62+ assert_match ( /username=#{ User . first . username } ;/ , last_response . cookies [ 'username' ] . to_s , 'Expect username to be set' )
63+ end
64+
65+ def test_auth_no_remember
66+ data_to_post = {
67+ username : 'aadmin' ,
68+ password : 'password' ,
69+ remember : false
70+ }
71+ # Get response back for logging in with username 'aadmin' password 'password'
72+ post_json '/api/auth.json' , data_to_post
73+
74+ assert_equal 201 , last_response . status
75+
76+ assert_match ( /refresh_token=;/ , last_response . cookies [ 'refresh_token' ] . to_s , 'Expect refresh token to be deleted' )
77+ assert_match ( /username=;/ , last_response . cookies [ 'username' ] . to_s , 'Expect username to be deleted' )
5378 end
5479
5580 # Test auth when username is invalid
@@ -152,101 +177,58 @@ def test_auth_roles
152177 # End POST tests
153178 # --------------------------------------------------------------------------- #
154179
155- # --------------------------------------------------------------------------- #
156- # PUT tests
157-
158- # Test put for authentication token
159- def test_auth_put
160- add_auth_header_for ( user : User . first )
161- put_json "/api/auth" , nil
162-
163- actual_auth = last_response_body [ 'auth_token' ]
164- expected_auth = auth_token
165- # Check to see if the response auth token matches the auth token that was sent through in put
166- assert_equal expected_auth , actual_auth
167- end
168-
169- def test_auth_using_query_string
170- put_json "/api/auth?Username=#{ User . first . username } &Auth-Token=#{ auth_token ( User . first ) } " , nil
171- assert_equal 200 , last_response . status , last_response_body
172- end
180+ # # --------------------------------------------------------------------------- #
181+ # # DELETE tests
173182
174- # Test invalid authentication token
175- def test_fail_auth_put
176- # Override data to set custom username or token in header
183+ # Test for deleting authentication token
184+ def test_auth_delete
177185 # Add authentication token to header
178- add_auth_header_for ( user : User . first , auth_token : '1234' )
179- put_json "/api/auth" , nil
180- actual_auth = last_response_body
181- expected_auth = auth_token
186+ add_auth_header_for ( user : User . first )
182187
183- # 404 response code means invalid token
184- assert_equal 404 , last_response . status
188+ delete "/api/auth" , { remember : false }
189+ # 204 response code means success!
190+ assert_equal 204 , last_response . status
185191
186- # Check to see if the response is invalid
187- assert actual_auth . key? 'error'
192+ assert_match ( /username=;/ , last_response . cookies [ 'username' ] . to_s )
193+ assert_match ( /refresh_token=;/ , last_response . cookies [ 'refresh_token' ] . to_s )
188194 end
189195
190- # Test invalid username for valid authentication token
191- def test_fail_username_put
192- # Add authentication token to header
193- add_auth_header_for ( user : User . first , username : 'acain123' )
194- put_json "/api/auth" , nil
195- actual_auth = last_response_body
196- expected_auth = auth_token
197-
198- # 404 response code means invalid token
199- assert_equal 404 , last_response . status
200-
201- # Check to see if the response is invalid
202- assert actual_auth . key? 'error'
203- end
196+ def test_refresh_token
197+ user = FactoryBot . create ( :user )
198+ token = user . generate_authentication_token! ( token_type : :refresh_token )
204199
205- # Test valid username for empty authentication token
206- def test_fail_empty_authKey_put
207- # Add authentication token to header
208- add_auth_header_for ( user : User . first )
200+ count = user . auth_tokens . count
209201
210- # Overwrite header for empty auth_token
211- header 'auth_token' , ''
202+ set_cookie "username= #{ user . username } "
203+ set_cookie "refresh_token= #{ token . authentication_token } "
212204
213- put_json "/api/auth/" , nil
214- actual_auth = last_response_body
215- expected_auth = auth_token
205+ post '/api/auth/access-token' , { remember : true }
216206
217- # 404 response code means invalid token
218- assert_equal 404 , last_response . status
207+ assert_equal 201 , last_response . status
208+ assert_equal count + 1 , user . auth_tokens . count
219209
220- # Check to see if the response is invalid
221- assert actual_auth . key? 'error'
222- end
210+ new_token = user . auth_tokens . last
223211
224- # Test empty request
225- def test_fail_empty_body_put
226- put_json "/api/auth" , nil
227- actual_auth = last_response_body
228- expected_auth = auth_token
212+ assert_equal :general , new_token . token_type . to_sym
213+ assert_equal last_response_body [ 'auth_token' ] , new_token . authentication_token
229214
230- # 400 response code means empty body
231- assert_equal 404 , last_response . status
215+ # Test using to refresh the auth token
216+ add_auth_header_for ( user : user , auth_token : new_token . authentication_token )
232217
233- # Check to see if the response is invalid
234- assert actual_auth . key? 'error'
235- end
236- # # End PUT tests
237- # # --------------------------------------------------------------------------- #
218+ # Test it returns existing and does not delete old asked not to
219+ post '/api/auth/access-token' , { remember : true , delete_auth_token : false }
220+ assert_equal count + 1 , user . auth_tokens . count
221+ assert AuthToken . exists? ( new_token . id )
238222
239- # # --------------------------------------------------------------------------- #
240- # # DELETE tests
223+ # Test it adds one and deletes the old token
224+ post '/api/auth/access-token' , { remember : true , delete_auth_token : true }
225+ assert_equal count + 1 , user . auth_tokens . count
226+ assert_not AuthToken . exists? ( new_token . id )
241227
242- # Test for deleting authentication token
243- def test_auth_delete
244- # Add authentication token to header
245- add_auth_header_for ( user : User . first )
228+ new_new_token = user . auth_tokens . last
246229
247- delete "/api/auth" , nil
248- # 204 response code means success!
249- assert_equal 204 , last_response . status
230+ assert_not_equal last_response_body [ 'auth_token' ] , new_token . authentication_token
231+ assert_equal last_response_body [ 'auth_token' ] , new_new_token . authentication_token
250232 end
251233
252234 def test_token_signout_works_with_multiple
@@ -260,10 +242,10 @@ def test_token_signout_works_with_multiple
260242 add_auth_header_for ( username : user . username , auth_token : t1 . authentication_token )
261243
262244 # Sign out one
263- delete " /api/auth.json"
245+ delete ' /api/auth.json' , { remember : false }
264246
265247 t2 . reload
266- refute t2 . destroyed?
248+ assert_not t2 . destroyed?
267249
268250 assert_raises ( ActiveRecord ::RecordNotFound ) { t1 . reload }
269251 end
0 commit comments