]> git.openstreetmap.org Git - rails.git/blob - test/controllers/api/permissions_controller_test.rb
Merge remote-tracking branch 'upstream/pull/4705'
[rails.git] / test / controllers / api / permissions_controller_test.rb
1 require "test_helper"
2
3 module Api
4   class PermissionsControllerTest < ActionDispatch::IntegrationTest
5     ##
6     # test all routes which lead to this controller
7     def test_routes
8       assert_routing(
9         { :path => "/api/0.6/permissions", :method => :get },
10         { :controller => "api/permissions", :action => "show" }
11       )
12       assert_routing(
13         { :path => "/api/0.6/permissions.json", :method => :get },
14         { :controller => "api/permissions", :action => "show", :format => "json" }
15       )
16     end
17
18     def test_permissions_anonymous
19       get permissions_path
20       assert_response :success
21       assert_select "osm > permissions", :count => 1 do
22         assert_select "permission", :count => 0
23       end
24
25       # Test json
26       get permissions_path(:format => "json")
27       assert_response :success
28       assert_equal "application/json", @response.media_type
29
30       js = ActiveSupport::JSON.decode(@response.body)
31       assert_not_nil js
32       assert_equal 0, js["permissions"].count
33     end
34
35     def test_permissions_basic_auth
36       auth_header = basic_authorization_header create(:user).email, "test"
37       get permissions_path, :headers => auth_header
38       assert_response :success
39       assert_select "osm > permissions", :count => 1 do
40         assert_select "permission", :count => ClientApplication.all_permissions.size
41         ClientApplication.all_permissions.each do |p|
42           assert_select "permission[name='#{p}']", :count => 1
43         end
44       end
45
46       # Test json
47       get permissions_path(:format => "json"), :headers => auth_header
48       assert_response :success
49       assert_equal "application/json", @response.media_type
50
51       js = ActiveSupport::JSON.decode(@response.body)
52       assert_not_nil js
53       assert_equal ClientApplication.all_permissions.size, js["permissions"].count
54       ClientApplication.all_permissions.each do |p|
55         assert_includes js["permissions"], p.to_s
56       end
57     end
58
59     def test_permissions_oauth1
60       token = create(:access_token,
61                      :allow_read_prefs => true,
62                      :allow_write_api => true,
63                      :allow_read_gpx => false)
64       signed_get permissions_path, :oauth => { :token => token }
65       assert_response :success
66       assert_select "osm > permissions", :count => 1 do
67         assert_select "permission", :count => 2
68         assert_select "permission[name='allow_read_prefs']", :count => 1
69         assert_select "permission[name='allow_write_api']", :count => 1
70         assert_select "permission[name='allow_read_gpx']", :count => 0
71       end
72     end
73
74     def test_permissions_oauth2
75       user = create(:user)
76       token = create(:oauth_access_token,
77                      :resource_owner_id => user.id,
78                      :scopes => %w[read_prefs write_api])
79       get permissions_path, :headers => bearer_authorization_header(token.token)
80       assert_response :success
81       assert_select "osm > permissions", :count => 1 do
82         assert_select "permission", :count => 2
83         assert_select "permission[name='allow_read_prefs']", :count => 1
84         assert_select "permission[name='allow_write_api']", :count => 1
85         assert_select "permission[name='allow_read_gpx']", :count => 0
86       end
87     end
88   end
89 end