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