]> git.openstreetmap.org Git - rails.git/blob - test/controllers/oauth_clients_controller_test.rb
Add links to reveal/hide redacted version data
[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(:display_name => user.display_name)
43     assert_response :redirect
44     assert_redirected_to login_path(:referer => oauth_clients_path(:display_name => user.display_name))
45
46     session_for(user)
47
48     get oauth_clients_path(:display_name => user.display_name)
49     assert_response :success
50     assert_template "index"
51     assert_select "li.client_application", 2
52   end
53
54   def test_new
55     user = create(:user)
56
57     get new_oauth_client_path(:display_name => user.display_name)
58     assert_response :redirect
59     assert_redirected_to login_path(:referer => new_oauth_client_path(:display_name => user.display_name))
60
61     session_for(user)
62
63     get new_oauth_client_path(:display_name => user.display_name)
64     assert_response :success
65     assert_template "new"
66     assert_select "form", 1 do
67       assert_select "input#client_application_name", 1
68       assert_select "input#client_application_url", 1
69       assert_select "input#client_application_callback_url", 1
70       assert_select "input#client_application_support_url", 1
71       ClientApplication.all_permissions.each do |perm|
72         assert_select "input#client_application_#{perm}", 1
73       end
74     end
75   end
76
77   def test_new_disabled
78     user = create(:user)
79
80     with_settings(:oauth_10_registration => false) do
81       get new_oauth_client_path(:display_name => user.display_name)
82       assert_response :redirect
83       assert_redirected_to login_path(:referer => new_oauth_client_path(:display_name => user.display_name))
84
85       session_for(user)
86
87       get new_oauth_client_path(:display_name => user.display_name)
88       assert_response :redirect
89       assert_redirected_to oauth_clients_path(:display_name => user.display_name)
90     end
91   end
92
93   def test_create
94     user = create(:user)
95
96     assert_difference "ClientApplication.count", 0 do
97       post oauth_clients_path(:display_name => user.display_name)
98     end
99     assert_response :forbidden
100
101     session_for(user)
102
103     assert_difference "ClientApplication.count", 0 do
104       post oauth_clients_path(:display_name => user.display_name,
105                               :client_application => { :name => "Test Application" })
106     end
107     assert_response :success
108     assert_template "new"
109
110     assert_difference "ClientApplication.count", 1 do
111       post oauth_clients_path(:display_name => user.display_name,
112                               :client_application => { :name => "Test Application",
113                                                        :url => "http://test.example.com/" })
114     end
115     assert_response :redirect
116     assert_redirected_to oauth_client_path(:id => ClientApplication.find_by(:name => "Test Application").id)
117   end
118
119   def test_show
120     user = create(:user)
121     client = create(:client_application, :user => user)
122     other_client = create(:client_application)
123
124     get oauth_client_path(:display_name => user.display_name, :id => client)
125     assert_response :redirect
126     assert_redirected_to login_path(:referer => oauth_client_path(:display_name => user.display_name, :id => client.id))
127
128     session_for(user)
129
130     get oauth_client_path(:display_name => user.display_name, :id => other_client)
131     assert_response :not_found
132     assert_template "not_found"
133
134     get oauth_client_path(:display_name => user.display_name, :id => client)
135     assert_response :success
136     assert_template "show"
137   end
138
139   def test_edit
140     user = create(:user)
141     client = create(:client_application, :user => user)
142     other_client = create(:client_application)
143
144     get edit_oauth_client_path(:display_name => user.display_name, :id => client)
145     assert_response :redirect
146     assert_redirected_to login_path(:referer => edit_oauth_client_path(:display_name => user.display_name, :id => client.id))
147
148     session_for(user)
149
150     get edit_oauth_client_path(:display_name => user.display_name, :id => other_client)
151     assert_response :not_found
152     assert_template "not_found"
153
154     get edit_oauth_client_path(:display_name => user.display_name, :id => client)
155     assert_response :success
156     assert_template "edit"
157     assert_select "form", 1 do
158       assert_select "input#client_application_name", 1
159       assert_select "input#client_application_url", 1
160       assert_select "input#client_application_callback_url", 1
161       assert_select "input#client_application_support_url", 1
162       ClientApplication.all_permissions.each do |perm|
163         assert_select "input#client_application_#{perm}", 1
164       end
165     end
166   end
167
168   def test_update
169     user = create(:user)
170     client = create(:client_application, :user => user)
171     other_client = create(:client_application)
172
173     put oauth_client_path(:display_name => user.display_name, :id => client)
174     assert_response :forbidden
175
176     session_for(user)
177
178     put oauth_client_path(:display_name => user.display_name, :id => other_client)
179     assert_response :not_found
180     assert_template "not_found"
181
182     put oauth_client_path(:display_name => user.display_name, :id => client,
183                           :client_application => { :name => "New Name", :url => nil })
184     assert_response :success
185     assert_template "edit"
186
187     put oauth_client_path(:display_name => user.display_name, :id => client,
188                           :client_application => { :name => "New Name", :url => "http://new.example.com/url" })
189     assert_response :redirect
190     assert_redirected_to oauth_client_path(:id => client.id)
191   end
192
193   def test_destroy
194     user = create(:user)
195     client = create(:client_application, :user => user)
196     other_client = create(:client_application)
197
198     assert_difference "ClientApplication.count", 0 do
199       delete oauth_client_path(:display_name => user.display_name, :id => client)
200     end
201     assert_response :forbidden
202
203     session_for(user)
204
205     assert_difference "ClientApplication.count", 0 do
206       delete oauth_client_path(:display_name => user.display_name, :id => other_client)
207     end
208     assert_response :not_found
209     assert_template "not_found"
210
211     assert_difference "ClientApplication.count", -1 do
212       delete oauth_client_path(:display_name => user.display_name, :id => client)
213     end
214     assert_response :redirect
215     assert_redirected_to oauth_clients_path(:display_name => user.display_name)
216   end
217 end