]> git.openstreetmap.org Git - rails.git/blob - test/controllers/accounts_controller_test.rb
Merge remote-tracking branch 'upstream/pull/5702'
[rails.git] / test / controllers / accounts_controller_test.rb
1 require "test_helper"
2
3 class AccountsControllerTest < ActionDispatch::IntegrationTest
4   ##
5   # test all routes which lead to this controller
6   def test_routes
7     assert_routing(
8       { :path => "/account", :method => :get },
9       { :controller => "accounts", :action => "show" }
10     )
11     assert_routing(
12       { :path => "/account", :method => :put },
13       { :controller => "accounts", :action => "update" }
14     )
15     assert_routing(
16       { :path => "/account", :method => :delete },
17       { :controller => "accounts", :action => "destroy" }
18     )
19
20     get "/account/edit"
21     assert_redirected_to "/account"
22   end
23
24   def test_show_and_update
25     # Get a user to work with - note that this user deliberately
26     # conflicts with uppercase_user in the email and display name
27     # fields to test that we can change other fields without any
28     # validation errors being reported
29     user = create(:user, :languages => [])
30     _uppercase_user = build(:user, :email => user.email.upcase, :display_name => user.display_name.upcase).tap { |u| u.save(:validate => false) }
31
32     # Make sure that you are redirected to the login page when
33     # you are not logged in
34     get account_path
35     assert_redirected_to login_path(:referer => account_path)
36
37     # Make sure we get the page when we are logged in as the right user
38     session_for(user)
39     get account_path
40     assert_response :success
41     assert_template :show
42     assert_select "form#accountForm" do |form|
43       assert_equal "post", form.attr("method").to_s
44       assert_select "input[name='_method']", true
45       assert_equal "/account", form.attr("action").to_s
46     end
47
48     # Updating the description using GET should fail
49     user.description = "new description"
50     user.preferred_editor = "default"
51     get account_path, :params => { :user => user.attributes }
52     assert_response :success
53     assert_template :show
54     assert_not_equal user.description, User.find(user.id).description
55
56     # Adding external authentication should redirect to the auth provider
57     patch account_path, :params => { :user => user.attributes.merge(:auth_provider => "google") }
58     assert_redirected_to auth_path(:provider => "google", :origin => "/account")
59     follow_redirect!
60     assert_redirected_to %r{^https://accounts.google.com/o/oauth2/auth\?.*}
61
62     # Changing name to one that exists should fail
63     new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name)
64     patch account_path, :params => { :user => new_attributes }
65     assert_response :success
66     assert_template :show
67     assert_select ".alert-success", false
68     assert_select "form#accountForm > div > input.is-invalid#user_display_name"
69
70     # Changing name to one that exists should fail, regardless of case
71     new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name.upcase)
72     patch account_path, :params => { :user => new_attributes }
73     assert_response :success
74     assert_template :show
75     assert_select ".alert-success", false
76     assert_select "form#accountForm > div > input.is-invalid#user_display_name"
77
78     # Changing name to one that doesn't exist should work
79     new_attributes = user.attributes.dup.merge(:display_name => "new tester")
80     patch account_path, :params => { :user => new_attributes }
81     assert_redirected_to account_path
82     follow_redirect!
83     assert_response :success
84     assert_template :show
85     assert_select ".alert-success", /^User information updated successfully/
86     assert_select "form#accountForm > div > input#user_display_name[value=?]", "new tester"
87
88     # Record the change of name
89     user.display_name = "new tester"
90
91     # Changing email to one that exists should fail
92     user.new_email = create(:user).email
93     assert_no_difference "ActionMailer::Base.deliveries.size" do
94       perform_enqueued_jobs do
95         patch account_path, :params => { :user => user.attributes }
96       end
97     end
98     assert_response :success
99     assert_template :show
100     assert_select ".alert-success", false
101     assert_select "form#accountForm > div > input.is-invalid#user_new_email"
102
103     # Changing email to one that exists should fail, regardless of case
104     user.new_email = create(:user).email.upcase
105     assert_no_difference "ActionMailer::Base.deliveries.size" do
106       perform_enqueued_jobs do
107         patch account_path, :params => { :user => user.attributes }
108       end
109     end
110     assert_response :success
111     assert_template :show
112     assert_select ".alert-success", false
113     assert_select "form#accountForm > div > input.is-invalid#user_new_email"
114
115     # Changing email to one that doesn't exist should work
116     user.new_email = "new_tester@example.com"
117     assert_difference "ActionMailer::Base.deliveries.size", 1 do
118       perform_enqueued_jobs do
119         patch account_path, :params => { :user => user.attributes }
120       end
121     end
122     assert_redirected_to account_path
123     follow_redirect!
124     assert_response :success
125     assert_template :show
126     assert_select ".alert-success", /^User information updated successfully/
127     assert_select "form#accountForm > div > input#user_new_email[value=?]", user.new_email
128     email = ActionMailer::Base.deliveries.first
129     assert_equal 1, email.to.count
130     assert_equal user.new_email, email.to.first
131   end
132
133   def test_show_private_account
134     user = create(:user, :data_public => false)
135
136     # Make sure that you are redirected to the login page when
137     # you are not logged in
138     get account_path
139     assert_redirected_to login_path(:referer => account_path)
140
141     # Make sure we get the page when we are logged in as the right user
142     session_for(user)
143     get account_path
144     assert_response :success
145     assert_template :show
146     assert_select "form#accountForm" do |form|
147       assert_equal "post", form.attr("method").to_s
148       assert_select "input[name='_method']", true
149       assert_equal "/account", form.attr("action").to_s
150     end
151
152     # Make sure we have a button to "go public"
153     assert_select "form.button_to[action='/user/go_public']", true
154   end
155
156   def test_destroy_allowed
157     user = create(:user)
158     session_for(user)
159
160     delete account_path
161     assert_response :redirect
162   end
163
164   def test_destroy_not_allowed
165     with_user_account_deletion_delay(24) do
166       user = create(:user)
167       create(:changeset, :user => user, :created_at => Time.now.utc)
168       session_for(user)
169
170       delete account_path
171       assert_response :bad_request
172     end
173   end
174 end