]> git.openstreetmap.org Git - rails.git/blob - test/controllers/oauth2_authorized_applications_controller_test.rb
Merge pull request #6394 from openstreetmap/dependabot/github_actions/ruby/setup...
[rails.git] / test / controllers / oauth2_authorized_applications_controller_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4
5 class Oauth2AuthorizedApplicationsControllerTest < ActionDispatch::IntegrationTest
6   ##
7   # test all routes which lead to this controller
8   def test_routes
9     assert_routing(
10       { :path => "/oauth2/authorized_applications", :method => :get },
11       { :controller => "oauth2_authorized_applications", :action => "index" }
12     )
13     assert_routing(
14       { :path => "/oauth2/authorized_applications/1", :method => :delete },
15       { :controller => "oauth2_authorized_applications", :action => "destroy", :id => "1" }
16     )
17   end
18
19   def test_index
20     user = create(:user)
21     application1 = create(:oauth_application)
22     create(:oauth_access_grant, :user => user, :application => application1)
23     create(:oauth_access_token, :user => user, :application => application1)
24     application2 = create(:oauth_application)
25     create(:oauth_access_grant, :user => user, :application => application2)
26     create(:oauth_access_token, :user => user, :application => application2)
27     create(:oauth_application)
28
29     get oauth_authorized_applications_path
30     assert_redirected_to login_path(:referer => oauth_authorized_applications_path)
31
32     session_for(user)
33
34     get oauth_authorized_applications_path
35     assert_response :success
36     assert_template "oauth2_authorized_applications/index"
37     assert_select "tbody tr", 2
38   end
39
40   def test_index_scopes
41     user = create(:user)
42     application1 = create(:oauth_application, :scopes => %w[read_prefs write_prefs write_diary read_gpx write_gpx])
43     create(:oauth_access_grant, :user => user, :application => application1, :scopes => %w[read_prefs write_prefs])
44     create(:oauth_access_token, :user => user, :application => application1, :scopes => %w[read_prefs write_prefs])
45     create(:oauth_access_grant, :user => user, :application => application1, :scopes => %w[read_prefs write_diary])
46     create(:oauth_access_token, :user => user, :application => application1, :scopes => %w[read_prefs write_diary])
47
48     get oauth_authorized_applications_path
49     assert_redirected_to login_path(:referer => oauth_authorized_applications_path)
50
51     session_for(user)
52
53     get oauth_authorized_applications_path
54     assert_response :success
55     assert_template "oauth2_authorized_applications/index"
56     assert_select "tbody tr", 1
57     assert_select "tbody tr td ul" do
58       assert_select "li", :count => 3
59       assert_select "li", :text => "Read user preferences"
60       assert_select "li", :text => "Modify user preferences"
61       assert_select "li", :text => "Create diary entries and comments"
62     end
63   end
64
65   def test_destroy
66     user = create(:user)
67     application1 = create(:oauth_application)
68     create(:oauth_access_grant, :user => user, :application => application1)
69     create(:oauth_access_token, :user => user, :application => application1)
70     application2 = create(:oauth_application)
71     create(:oauth_access_grant, :user => user, :application => application2)
72     create(:oauth_access_token, :user => user, :application => application2)
73     create(:oauth_application)
74
75     delete oauth_authorized_application_path(:id => application1.id)
76     assert_response :forbidden
77
78     session_for(user)
79
80     delete oauth_authorized_application_path(:id => application1.id)
81     assert_redirected_to oauth_authorized_applications_path
82
83     get oauth_authorized_applications_path
84     assert_response :success
85     assert_template "oauth2_authorized_applications/index"
86     assert_select "tbody tr", 1
87   end
88 end