]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/users/traces_controller_test.rb
Localisation updates from https://translatewiki.net.
[rails.git] / test / controllers / api / users / traces_controller_test.rb
1 require "test_helper"
2
3 module Api
4   module Users
5     class TracesControllerTest < ActionDispatch::IntegrationTest
6       ##
7       # test all routes which lead to this controller
8       def test_routes
9         assert_routing(
10           { :path => "/api/0.6/user/gpx_files", :method => :get },
11           { :controller => "api/users/traces", :action => "index" }
12         )
13         assert_routing(
14           { :path => "/api/0.6/user/gpx_files.json", :method => :get },
15           { :controller => "api/users/traces", :action => "index", :format => "json" }
16         )
17       end
18
19       def test_index
20         user = create(:user)
21         trace1 = create(:trace, :user => user) do |trace|
22           create(:tracetag, :trace => trace, :tag => "London")
23         end
24         trace2 = create(:trace, :user => user) do |trace|
25           create(:tracetag, :trace => trace, :tag => "Birmingham")
26         end
27
28         # check that we get a response when logged in
29         auth_header = bearer_authorization_header user, :scopes => %w[read_gpx]
30         get api_user_traces_path, :headers => auth_header
31         assert_response :success
32         assert_equal "application/xml", response.media_type
33
34         # check the data that is returned
35         assert_select "gpx_file[id='#{trace1.id}']", 1 do
36           assert_select "tag", "London"
37         end
38         assert_select "gpx_file[id='#{trace2.id}']", 1 do
39           assert_select "tag", "Birmingham"
40         end
41
42         # check that we get a response when logged in with json
43         auth_header = bearer_authorization_header user, :scopes => %w[read_gpx]
44         get api_user_traces_path(:format => "json"), :headers => auth_header
45         assert_response :success
46         assert_equal "application/json", response.media_type
47         js = ActiveSupport::JSON.decode(@response.body)
48         assert_not_nil js
49         assert_equal trace1.id, js["traces"][0]["id"]
50         assert_equal "London", js["traces"][0]["tags"][0]
51         assert_equal trace2.id, js["traces"][1]["id"]
52         assert_equal "Birmingham", js["traces"][1]["tags"][0]
53       end
54
55       def test_index_anonymous
56         get api_user_traces_path
57         assert_response :unauthorized
58       end
59
60       def test_index_no_scope
61         user = create(:user)
62         bad_auth = bearer_authorization_header user, :scopes => %w[]
63
64         get api_user_traces_path, :headers => bad_auth
65         assert_response :forbidden
66       end
67     end
68   end
69 end