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 put account_path, :params => { :user => user.attributes.merge(:auth_provider => "openid", :auth_uid => "gmail.com") }
52 assert_response :redirect
53 assert_redirected_to auth_path(:provider => "openid", :openid_url => "https://www.google.com/accounts/o8/id", :origin => "/account")
55 # Changing name to one that exists should fail
56 new_attributes = user.attributes.dup.merge(:display_name => create(:user).display_name)
57 put account_path, :params => { :user => new_attributes }
58 assert_response :success
60 assert_select ".notice", false
61 assert_select "form#accountForm > div.form-group > 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 put account_path, :params => { :user => new_attributes }
66 assert_response :success
68 assert_select ".notice", false
69 assert_select "form#accountForm > div.form-group > 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 put account_path, :params => { :user => new_attributes }
74 assert_response :redirect
75 assert_redirected_to edit_account_url
77 assert_response :success
79 assert_select ".notice", /^User information updated successfully/
80 assert_select "form#accountForm > div.form-group > input#user_display_name[value=?]", "new tester"
82 # Record the change of name
83 user.display_name = "new tester"
85 # Changing email to one that exists should fail
86 user.new_email = create(:user).email
87 assert_no_difference "ActionMailer::Base.deliveries.size" do
88 perform_enqueued_jobs do
89 put account_path, :params => { :user => user.attributes }
92 assert_response :success
94 assert_select ".notice", false
95 assert_select "form#accountForm > div.form-group > input.is-invalid#user_new_email"
97 # Changing email to one that exists should fail, regardless of case
98 user.new_email = create(:user).email.upcase
99 assert_no_difference "ActionMailer::Base.deliveries.size" do
100 perform_enqueued_jobs do
101 put account_path, :params => { :user => user.attributes }
104 assert_response :success
105 assert_template :edit
106 assert_select ".notice", false
107 assert_select "form#accountForm > div.form-group > input.is-invalid#user_new_email"
109 # Changing email to one that doesn't exist should work
110 user.new_email = "new_tester@example.com"
111 assert_difference "ActionMailer::Base.deliveries.size", 1 do
112 perform_enqueued_jobs do
113 put account_path, :params => { :user => user.attributes }
116 assert_response :redirect
117 assert_redirected_to edit_account_url
118 get edit_account_path
119 assert_response :success
120 assert_template :edit
121 assert_select ".notice", /^User information updated successfully/
122 assert_select "form#accountForm > div.form-group > input#user_new_email[value=?]", user.new_email
123 email = ActionMailer::Base.deliveries.first
124 assert_equal 1, email.to.count
125 assert_equal user.new_email, email.to.first
126 ActionMailer::Base.deliveries.clear