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_response :redirect
29 assert_redirected_to login_path(:referer => "/account/edit")
31 # Make sure we get the page when we are logged in as the right user
34 assert_response :success
36 assert_select "form#accountForm" do |form|
37 assert_equal "post", form.attr("method").to_s
38 assert_select "input[name='_method']", true
39 assert_equal "/account", form.attr("action").to_s
42 # Updating the description using GET should fail
43 user.description = "new description"
44 user.preferred_editor = "default"
45 get edit_account_path, :params => { :user => user.attributes }
46 assert_response :success
48 assert_not_equal user.description, User.find(user.id).description
50 # Adding external authentication should redirect to the auth provider
51 patch account_path, :params => { :user => user.attributes.merge(:auth_provider => "google") }
52 assert_response :redirect
53 assert_redirected_to auth_path(:provider => "google", :origin => "/account")
55 assert_response :redirect
56 assert_redirected_to %r{^https://accounts.google.com/o/oauth2/auth\?.*}
58 # Changing name to one that exists should fail
59 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name)
60 patch account_path, :params => { :user => new_attributes }
61 assert_response :success
63 assert_select ".alert-success", false
64 assert_select "form#accountForm > div > input.is-invalid#user_display_name"
66 # Changing name to one that exists should fail, regardless of case
67 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name.upcase)
68 patch account_path, :params => { :user => new_attributes }
69 assert_response :success
71 assert_select ".alert-success", false
72 assert_select "form#accountForm > div > input.is-invalid#user_display_name"
74 # Changing name to one that doesn't exist should work
75 new_attributes = user.attributes.dup.merge(:display_name => "new tester")
76 patch account_path, :params => { :user => new_attributes }
77 assert_response :redirect
78 assert_redirected_to edit_account_url
80 assert_response :success
82 assert_select ".alert-success", /^User information updated successfully/
83 assert_select "form#accountForm > div > input#user_display_name[value=?]", "new tester"
85 # Record the change of name
86 user.display_name = "new tester"
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 }
95 assert_response :success
97 assert_select ".alert-success", false
98 assert_select "form#accountForm > div > input.is-invalid#user_new_email"
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 }
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"
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 }
119 assert_response :redirect
120 assert_redirected_to edit_account_url
121 get edit_account_path
122 assert_response :success
123 assert_template :edit
124 assert_select ".alert-success", /^User information updated successfully/
125 assert_select "form#accountForm > div > input#user_new_email[value=?]", user.new_email
126 email = ActionMailer::Base.deliveries.first
127 assert_equal 1, email.to.count
128 assert_equal user.new_email, email.to.first
129 ActionMailer::Base.deliveries.clear
132 def test_private_account
133 user = create(:user, :data_public => false)
135 # Make sure that you are redirected to the login page when
136 # you are not logged in
137 get edit_account_path
138 assert_response :redirect
139 assert_redirected_to login_path(:referer => "/account/edit")
141 # Make sure we get the page when we are logged in as the right user
143 get edit_account_path
144 assert_response :success
145 assert_template :edit
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
152 # Make sure we have a button to "go public"
153 assert_select "form.button_to[action='/user/go_public']", true