]> git.openstreetmap.org Git - rails.git/blob - test/integration/client_applications_test.rb
Remove assert_response when followed by assert_redirected_to
[rails.git] / test / integration / client_applications_test.rb
1 require "test_helper"
2
3 class ClientApplicationsTest < ActionDispatch::IntegrationTest
4   ##
5   # run through the procedure of creating a client application and checking
6   # that it shows up on the user's account page.
7   def test_create_application
8     user = create(:user)
9
10     get "/login"
11     assert_redirected_to login_path(:cookie_test => "true")
12     follow_redirect!
13     assert_response :success
14     post "/login", :params => { "username" => user.email, "password" => "test", :referer => "/user/#{ERB::Util.u(user.display_name)}" }
15     assert_response :redirect
16     follow_redirect!
17     assert_response :success
18     assert_template "users/show"
19     get "/account/edit"
20     assert_response :success
21     assert_template "accounts/edit"
22
23     # check that the form to allow new client application creations exists
24     assert_in_heading do
25       assert_select "ul.nav.nav-tabs li.nav-item a[href='/user/#{ERB::Util.u(user.display_name)}/oauth_clients']"
26     end
27
28     # now we follow the link to the oauth client list
29     get "/user/#{ERB::Util.u(user.display_name)}/oauth_clients"
30     assert_response :success
31     assert_in_body do
32       assert_select "a[href='/user/#{ERB::Util.u(user.display_name)}/oauth_clients/new']"
33     end
34
35     # now we follow the link to the new oauth client page
36     get "/user/#{ERB::Util.u(user.display_name)}/oauth_clients/new"
37     assert_response :success
38     assert_in_heading do
39       assert_select "h1", "Register a new application"
40     end
41     assert_in_body do
42       assert_select "form[action='/user/#{ERB::Util.u(user.display_name)}/oauth_clients']" do
43         [:name, :url, :callback_url, :support_url].each do |inp|
44           assert_select "input[name=?]", "client_application[#{inp}]"
45         end
46         ClientApplication.all_permissions.each do |perm|
47           assert_select "input[name=?]", "client_application[#{perm}]"
48         end
49       end
50     end
51
52     post "/user/#{ERB::Util.u(user.display_name)}/oauth_clients",
53          :params => { "client_application[name]" => "My New App",
54                       "client_application[url]" => "http://my.new.app.org/",
55                       "client_application[callback_url]" => "http://my.new.app.org/callback",
56                       "client_application[support_url]" => "http://my.new.app.org/support" }
57     assert_response :redirect
58     follow_redirect!
59     assert_response :success
60     assert_template "oauth_clients/show"
61     assert_equal "Registered the information successfully", flash[:notice]
62
63     # now go back to the account page and check its listed under this user
64     get "/user/#{ERB::Util.u(user.display_name)}/oauth_clients"
65     assert_response :success
66     assert_template "oauth_clients/index"
67     assert_in_body { assert_select "li>a", "My New App" }
68   end
69
70   ##
71   # fake client workflow.
72   # this acts like a 3rd party client trying to access the site.
73   def test_3rd_party_token
74     # apparently the oauth gem doesn't really support being used inside integration
75     # tests, as its too tied into the HTTP headers and stuff that it signs.
76   end
77
78   private
79
80   ##
81   # utility method to make the HTML screening easier to read.
82   def assert_in_heading(&block)
83     assert_select("div.content-heading", &block)
84   end
85
86   ##
87   # utility method to make the HTML screening easier to read.
88   def assert_in_body(&block)
89     assert_select("div#content", &block)
90   end
91 end