]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/users/traces_controller_test.rb
Add frozen_string_literal comments to ruby files
[rails.git] / test / controllers / api / users / traces_controller_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 module Api
6   module Users
7     class TracesControllerTest < ActionDispatch::IntegrationTest
8       ##
9       # test all routes which lead to this controller
10       def test_routes
11         assert_routing(
12           { :path => "/api/0.6/user/gpx_files", :method => :get },
13           { :controller => "api/users/traces", :action => "index" }
14         )
15         assert_routing(
16           { :path => "/api/0.6/user/gpx_files.json", :method => :get },
17           { :controller => "api/users/traces", :action => "index", :format => "json" }
18         )
19       end
20
21       def test_index
22         user = create(:user)
23         trace1 = create(:trace, :user => user) do |trace|
24           create(:tracetag, :trace => trace, :tag => "London")
25         end
26         trace2 = create(:trace, :user => user) do |trace|
27           create(:tracetag, :trace => trace, :tag => "Birmingham")
28         end
29
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
35
36         # check the data that is returned
37         assert_select "gpx_file[id='#{trace1.id}']", 1 do
38           assert_select "tag", "London"
39         end
40         assert_select "gpx_file[id='#{trace2.id}']", 1 do
41           assert_select "tag", "Birmingham"
42         end
43
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)
50         assert_not_nil js
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]
55       end
56
57       def test_index_anonymous
58         get api_user_traces_path
59         assert_response :unauthorized
60       end
61
62       def test_index_no_scope
63         user = create(:user)
64         bad_auth = bearer_authorization_header user, :scopes => %w[]
65
66         get api_user_traces_path, :headers => bad_auth
67         assert_response :forbidden
68       end
69     end
70   end
71 end