]> git.openstreetmap.org Git - rails.git/blob - test/controllers/oauth_clients_controller_test.rb
Remove assert_response when followed by assert_redirected_to
[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_redirected_to login_path(:referer => oauth_clients_path(:display_name => user.display_name))
44
45     session_for(user)
46
47     get oauth_clients_path(:display_name => user.display_name)
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(:display_name => user.display_name)
57     assert_redirected_to login_path(:referer => new_oauth_client_path(:display_name => user.display_name))
58
59     session_for(user)
60
61     get new_oauth_client_path(:display_name => user.display_name)
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(:display_name => user.display_name)
80       assert_redirected_to login_path(:referer => new_oauth_client_path(:display_name => user.display_name))
81
82       session_for(user)
83
84       get new_oauth_client_path(:display_name => user.display_name)
85       assert_redirected_to oauth_clients_path(:display_name => user.display_name)
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(:display_name => user.display_name)
94     end
95     assert_response :forbidden
96
97     session_for(user)
98
99     assert_difference "ClientApplication.count", 0 do
100       post oauth_clients_path(:display_name => user.display_name,
101                               :client_application => { :name => "Test Application" })
102     end
103     assert_response :success
104     assert_template "new"
105
106     assert_difference "ClientApplication.count", 1 do
107       post oauth_clients_path(:display_name => user.display_name,
108                               :client_application => { :name => "Test Application",
109                                                        :url => "http://test.example.com/" })
110     end
111     assert_redirected_to oauth_client_path(:id => ClientApplication.find_by(:name => "Test Application").id)
112   end
113
114   def test_show
115     user = create(:user)
116     client = create(:client_application, :user => user)
117     other_client = create(:client_application)
118
119     get oauth_client_path(:display_name => user.display_name, :id => client)
120     assert_redirected_to login_path(:referer => oauth_client_path(:display_name => user.display_name, :id => client.id))
121
122     session_for(user)
123
124     get oauth_client_path(:display_name => user.display_name, :id => other_client)
125     assert_response :not_found
126     assert_template "not_found"
127
128     get oauth_client_path(:display_name => user.display_name, :id => client)
129     assert_response :success
130     assert_template "show"
131   end
132
133   def test_edit
134     user = create(:user)
135     client = create(:client_application, :user => user)
136     other_client = create(:client_application)
137
138     get edit_oauth_client_path(:display_name => user.display_name, :id => client)
139     assert_redirected_to login_path(:referer => edit_oauth_client_path(:display_name => user.display_name, :id => client.id))
140
141     session_for(user)
142
143     get edit_oauth_client_path(:display_name => user.display_name, :id => other_client)
144     assert_response :not_found
145     assert_template "not_found"
146
147     get edit_oauth_client_path(:display_name => user.display_name, :id => client)
148     assert_response :success
149     assert_template "edit"
150     assert_select "form", 1 do
151       assert_select "input#client_application_name", 1
152       assert_select "input#client_application_url", 1
153       assert_select "input#client_application_callback_url", 1
154       assert_select "input#client_application_support_url", 1
155       ClientApplication.all_permissions.each do |perm|
156         assert_select "input#client_application_#{perm}", 1
157       end
158     end
159   end
160
161   def test_update
162     user = create(:user)
163     client = create(:client_application, :user => user)
164     other_client = create(:client_application)
165
166     put oauth_client_path(:display_name => user.display_name, :id => client)
167     assert_response :forbidden
168
169     session_for(user)
170
171     put oauth_client_path(:display_name => user.display_name, :id => other_client)
172     assert_response :not_found
173     assert_template "not_found"
174
175     put oauth_client_path(:display_name => user.display_name, :id => client,
176                           :client_application => { :name => "New Name", :url => nil })
177     assert_response :success
178     assert_template "edit"
179
180     put oauth_client_path(:display_name => user.display_name, :id => client,
181                           :client_application => { :name => "New Name", :url => "http://new.example.com/url" })
182     assert_redirected_to oauth_client_path(:id => client.id)
183   end
184
185   def test_destroy
186     user = create(:user)
187     client = create(:client_application, :user => user)
188     other_client = create(:client_application)
189
190     assert_difference "ClientApplication.count", 0 do
191       delete oauth_client_path(:display_name => user.display_name, :id => client)
192     end
193     assert_response :forbidden
194
195     session_for(user)
196
197     assert_difference "ClientApplication.count", 0 do
198       delete oauth_client_path(:display_name => user.display_name, :id => other_client)
199     end
200     assert_response :not_found
201     assert_template "not_found"
202
203     assert_difference "ClientApplication.count", -1 do
204       delete oauth_client_path(:display_name => user.display_name, :id => client)
205     end
206     assert_redirected_to oauth_clients_path(:display_name => user.display_name)
207   end
208 end