]> git.openstreetmap.org Git - rails.git/blob - test/controllers/oauth_clients_controller_test.rb
Merge remote-tracking branch 'upstream/pull/4637'
[rails.git] / test / controllers / oauth_clients_controller_test.rb
1 require "test_helper"
2
3 class OauthClientsControllerTest < ActionDispatch::IntegrationTest
4   ##
5   # test all routes which lead to this controller
6   def test_routes
7     assert_routing(
8       { :path => "/user/username/oauth_clients", :method => :get },
9       { :controller => "oauth_clients", :action => "index", :display_name => "username" }
10     )
11     assert_routing(
12       { :path => "/user/username/oauth_clients/new", :method => :get },
13       { :controller => "oauth_clients", :action => "new", :display_name => "username" }
14     )
15     assert_routing(
16       { :path => "/user/username/oauth_clients", :method => :post },
17       { :controller => "oauth_clients", :action => "create", :display_name => "username" }
18     )
19     assert_routing(
20       { :path => "/user/username/oauth_clients/1", :method => :get },
21       { :controller => "oauth_clients", :action => "show", :display_name => "username", :id => "1" }
22     )
23     assert_routing(
24       { :path => "/user/username/oauth_clients/1/edit", :method => :get },
25       { :controller => "oauth_clients", :action => "edit", :display_name => "username", :id => "1" }
26     )
27     assert_routing(
28       { :path => "/user/username/oauth_clients/1", :method => :put },
29       { :controller => "oauth_clients", :action => "update", :display_name => "username", :id => "1" }
30     )
31     assert_routing(
32       { :path => "/user/username/oauth_clients/1", :method => :delete },
33       { :controller => "oauth_clients", :action => "destroy", :display_name => "username", :id => "1" }
34     )
35   end
36
37   def test_index
38     user = create(:user)
39     create_list(:client_application, 2, :user => user)
40     create_list(:access_token, 2, :user => user)
41
42     get oauth_clients_path(user)
43     assert_redirected_to login_path(:referer => oauth_clients_path(user))
44
45     session_for(user)
46
47     get oauth_clients_path(user)
48     assert_response :success
49     assert_template "index"
50     assert_select "li.client_application", 2
51   end
52
53   def test_new
54     user = create(:user)
55
56     get new_oauth_client_path(user)
57     assert_redirected_to login_path(:referer => new_oauth_client_path(user))
58
59     session_for(user)
60
61     get new_oauth_client_path(user)
62     assert_response :success
63     assert_template "new"
64     assert_select "form", 1 do
65       assert_select "input#client_application_name", 1
66       assert_select "input#client_application_url", 1
67       assert_select "input#client_application_callback_url", 1
68       assert_select "input#client_application_support_url", 1
69       ClientApplication.all_permissions.each do |perm|
70         assert_select "input#client_application_#{perm}", 1
71       end
72     end
73   end
74
75   def test_new_disabled
76     user = create(:user)
77
78     with_settings(:oauth_10_registration => false) do
79       get new_oauth_client_path(user)
80       assert_redirected_to login_path(:referer => new_oauth_client_path(user))
81
82       session_for(user)
83
84       get new_oauth_client_path(user)
85       assert_redirected_to oauth_clients_path(user)
86     end
87   end
88
89   def test_create
90     user = create(:user)
91
92     assert_difference "ClientApplication.count", 0 do
93       post oauth_clients_path(user)
94     end
95     assert_response :forbidden
96
97     session_for(user)
98
99     assert_difference "ClientApplication.count", 0 do
100       post oauth_clients_path(user, :client_application => { :name => "Test Application" })
101     end
102     assert_response :success
103     assert_template "new"
104
105     assert_difference "ClientApplication.count", 1 do
106       post oauth_clients_path(user, :client_application => { :name => "Test Application",
107                                                              :url => "http://test.example.com/" })
108     end
109     assert_redirected_to oauth_client_path(:id => ClientApplication.find_by(:name => "Test Application").id)
110   end
111
112   def test_show
113     user = create(:user)
114     client = create(:client_application, :user => user)
115     other_client = create(:client_application)
116
117     get oauth_client_path(user, client)
118     assert_redirected_to login_path(:referer => oauth_client_path(user, client.id))
119
120     session_for(user)
121
122     get oauth_client_path(user, other_client)
123     assert_response :not_found
124     assert_template "not_found"
125
126     get oauth_client_path(user, client)
127     assert_response :success
128     assert_template "show"
129   end
130
131   def test_edit
132     user = create(:user)
133     client = create(:client_application, :user => user)
134     other_client = create(:client_application)
135
136     get edit_oauth_client_path(user, client)
137     assert_redirected_to login_path(:referer => edit_oauth_client_path(user, client.id))
138
139     session_for(user)
140
141     get edit_oauth_client_path(user, other_client)
142     assert_response :not_found
143     assert_template "not_found"
144
145     get edit_oauth_client_path(user, client)
146     assert_response :success
147     assert_template "edit"
148     assert_select "form", 1 do
149       assert_select "input#client_application_name", 1
150       assert_select "input#client_application_url", 1
151       assert_select "input#client_application_callback_url", 1
152       assert_select "input#client_application_support_url", 1
153       ClientApplication.all_permissions.each do |perm|
154         assert_select "input#client_application_#{perm}", 1
155       end
156     end
157   end
158
159   def test_update
160     user = create(:user)
161     client = create(:client_application, :user => user)
162     other_client = create(:client_application)
163
164     put oauth_client_path(user, client)
165     assert_response :forbidden
166
167     session_for(user)
168
169     put oauth_client_path(user, other_client)
170     assert_response :not_found
171     assert_template "not_found"
172
173     put oauth_client_path(user, client, :client_application => { :name => "New Name", :url => nil })
174     assert_response :success
175     assert_template "edit"
176
177     put oauth_client_path(user, client, :client_application => { :name => "New Name", :url => "http://new.example.com/url" })
178     assert_redirected_to oauth_client_path(:id => client.id)
179   end
180
181   def test_destroy
182     user = create(:user)
183     client = create(:client_application, :user => user)
184     other_client = create(:client_application)
185
186     assert_difference "ClientApplication.count", 0 do
187       delete oauth_client_path(user, client)
188     end
189     assert_response :forbidden
190
191     session_for(user)
192
193     assert_difference "ClientApplication.count", 0 do
194       delete oauth_client_path(user, other_client)
195     end
196     assert_response :not_found
197     assert_template "not_found"
198
199     assert_difference "ClientApplication.count", -1 do
200       delete oauth_client_path(user, client)
201     end
202     assert_redirected_to oauth_clients_path(user)
203   end
204 end