3 class AccountsControllerTest < ActionDispatch::IntegrationTest
5 # test all routes which lead to this controller
8 { :path => "/account/edit", :method => :get },
9 { :controller => "accounts", :action => "edit" }
12 { :path => "/account", :method => :put },
13 { :controller => "accounts", :action => "update" }
18 # Get a user to work with - note that this user deliberately
19 # conflicts with uppercase_user in the email and display name
20 # fields to test that we can change other fields without any
21 # validation errors being reported
22 user = create(:user, :languages => [])
23 _uppercase_user = build(:user, :email => user.email.upcase, :display_name => user.display_name.upcase).tap { |u| u.save(:validate => false) }
25 # Make sure that you are redirected to the login page when
26 # you are not logged in
28 assert_redirected_to login_path(:referer => "/account/edit")
30 # Make sure we get the page when we are logged in as the right user
33 assert_response :success
35 assert_select "form#accountForm" do |form|
36 assert_equal "post", form.attr("method").to_s
37 assert_select "input[name='_method']", true
38 assert_equal "/account", form.attr("action").to_s
41 # Updating the description using GET should fail
42 user.description = "new description"
43 user.preferred_editor = "default"
44 get edit_account_path, :params => { :user => user.attributes }
45 assert_response :success
47 assert_not_equal user.description, User.find(user.id).description
49 # Adding external authentication should redirect to the auth provider
50 patch account_path, :params => { :user => user.attributes.merge(:auth_provider => "google") }
51 assert_redirected_to auth_path(:provider => "google", :origin => "/account")
53 assert_redirected_to %r{^https://accounts.google.com/o/oauth2/auth\?.*}
55 # Changing name to one that exists should fail
56 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name)
57 patch account_path, :params => { :user => new_attributes }
58 assert_response :success
60 assert_select ".alert-success", false
61 assert_select "form#accountForm > div > input.is-invalid#user_display_name"
63 # Changing name to one that exists should fail, regardless of case
64 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name.upcase)
65 patch account_path, :params => { :user => new_attributes }
66 assert_response :success
68 assert_select ".alert-success", false
69 assert_select "form#accountForm > div > input.is-invalid#user_display_name"
71 # Changing name to one that doesn't exist should work
72 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
73 patch account_path, :params => { :user => new_attributes }
74 assert_redirected_to edit_account_url
76 assert_response :success
78 assert_select ".alert-success", /^User information updated successfully/
79 assert_select "form#accountForm > div > input#user_display_name[value=?]", "new tester"
81 # Record the change of name
82 user.display_name = "new tester"
84 # Changing email to one that exists should fail
85 user.new_email = create(:user).email
86 assert_no_difference "ActionMailer::Base.deliveries.size" do
87 perform_enqueued_jobs do
88 patch account_path, :params => { :user => user.attributes }
91 assert_response :success
93 assert_select ".alert-success", false
94 assert_select "form#accountForm > div > input.is-invalid#user_new_email"
96 # Changing email to one that exists should fail, regardless of case
97 user.new_email = create(:user).email.upcase
98 assert_no_difference "ActionMailer::Base.deliveries.size" do
99 perform_enqueued_jobs do
100 patch account_path, :params => { :user => user.attributes }
103 assert_response :success
104 assert_template :edit
105 assert_select ".alert-success", false
106 assert_select "form#accountForm > div > input.is-invalid#user_new_email"
108 # Changing email to one that doesn't exist should work
109 user.new_email = "new_tester@example.com"
110 assert_difference "ActionMailer::Base.deliveries.size", 1 do
111 perform_enqueued_jobs do
112 patch account_path, :params => { :user => user.attributes }
115 assert_redirected_to edit_account_url
116 get edit_account_path
117 assert_response :success
118 assert_template :edit
119 assert_select ".alert-success", /^User information updated successfully/
120 assert_select "form#accountForm > div > input#user_new_email[value=?]", user.new_email
121 email = ActionMailer::Base.deliveries.first
122 assert_equal 1, email.to.count
123 assert_equal user.new_email, email.to.first
124 ActionMailer::Base.deliveries.clear
127 def test_private_account
128 user = create(:user, :data_public => false)
130 # Make sure that you are redirected to the login page when
131 # you are not logged in
132 get edit_account_path
133 assert_redirected_to login_path(:referer => "/account/edit")
135 # Make sure we get the page when we are logged in as the right user
137 get edit_account_path
138 assert_response :success
139 assert_template :edit
140 assert_select "form#accountForm" do |form|
141 assert_equal "post", form.attr("method").to_s
142 assert_select "input[name='_method']", true
143 assert_equal "/account", form.attr("action").to_s
146 # Make sure we have a button to "go public"
147 assert_select "form.button_to[action='/user/go_public']", true
150 def test_destroy_allowed
155 assert_response :redirect
158 def test_destroy_not_allowed
159 with_user_account_deletion_delay(24) do
161 create(:changeset, :user => user, :created_at => Time.now.utc)
165 assert_response :bad_request