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