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