1 # frozen_string_literal: true
 
   7     class TracesControllerTest < ActionDispatch::IntegrationTest
 
   9       # test all routes which lead to this controller
 
  12           { :path => "/api/0.6/user/gpx_files", :method => :get },
 
  13           { :controller => "api/users/traces", :action => "index" }
 
  16           { :path => "/api/0.6/user/gpx_files.json", :method => :get },
 
  17           { :controller => "api/users/traces", :action => "index", :format => "json" }
 
  23         trace1 = create(:trace, :user => user) do |trace|
 
  24           create(:tracetag, :trace => trace, :tag => "London")
 
  26         trace2 = create(:trace, :user => user) do |trace|
 
  27           create(:tracetag, :trace => trace, :tag => "Birmingham")
 
  30         # check that we get a response when logged in
 
  31         auth_header = bearer_authorization_header user, :scopes => %w[read_gpx]
 
  32         get api_user_traces_path, :headers => auth_header
 
  33         assert_response :success
 
  34         assert_equal "application/xml", response.media_type
 
  36         # check the data that is returned
 
  37         assert_select "gpx_file[id='#{trace1.id}']", 1 do
 
  38           assert_select "tag", "London"
 
  40         assert_select "gpx_file[id='#{trace2.id}']", 1 do
 
  41           assert_select "tag", "Birmingham"
 
  44         # check that we get a response when logged in with json
 
  45         auth_header = bearer_authorization_header user, :scopes => %w[read_gpx]
 
  46         get api_user_traces_path(:format => "json"), :headers => auth_header
 
  47         assert_response :success
 
  48         assert_equal "application/json", response.media_type
 
  49         js = ActiveSupport::JSON.decode(@response.body)
 
  51         assert_equal trace1.id, js["traces"][0]["id"]
 
  52         assert_equal "London", js["traces"][0]["tags"][0]
 
  53         assert_equal trace2.id, js["traces"][1]["id"]
 
  54         assert_equal "Birmingham", js["traces"][1]["tags"][0]
 
  57       def test_index_anonymous
 
  58         get api_user_traces_path
 
  59         assert_response :unauthorized
 
  62       def test_index_no_scope
 
  64         bad_auth = bearer_authorization_header user, :scopes => %w[]
 
  66         get api_user_traces_path, :headers => bad_auth
 
  67         assert_response :forbidden