2019-08-28 15:21:20 +02:00
|
|
|
require 'rails_helper'
|
|
|
|
|
# require 'bcrypt'
|
|
|
|
|
|
2023-03-19 19:57:53 +01:00
|
|
|
feature 'edit user profile settings', type: :system, js: true do
|
2019-08-28 15:21:20 +02:00
|
|
|
let(:user) { FactoryBot.create(:user) }
|
|
|
|
|
|
2021-01-26 23:42:36 +01:00
|
|
|
before(:each) do
|
2019-08-28 15:21:20 +02:00
|
|
|
user.confirm # devise helper to confirm user account
|
|
|
|
|
sign_in user # devise helper to login user
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
scenario 'edit full name field' do
|
|
|
|
|
new_full_name = 'My new full name'
|
|
|
|
|
|
|
|
|
|
expect(user.full_name).not_to eq(new_full_name)
|
|
|
|
|
|
|
|
|
|
visit edit_user_registration_path
|
|
|
|
|
fill_in 'Full name', with: new_full_name
|
|
|
|
|
fill_in 'Current password', with: user.password
|
|
|
|
|
click_button 'Update profile'
|
|
|
|
|
|
|
|
|
|
expect(page).to have_css('.notice')
|
2023-03-19 19:57:53 +01:00
|
|
|
expect(user.reload.full_name).to eq(new_full_name)
|
2019-08-28 15:21:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
scenario 'edit email field' do
|
|
|
|
|
new_email = 'newemail@example.com'
|
|
|
|
|
|
|
|
|
|
expect(user.email).not_to eq(new_email)
|
|
|
|
|
|
|
|
|
|
visit edit_user_registration_path
|
|
|
|
|
fill_in 'Email', with: new_email
|
|
|
|
|
fill_in 'Current password', with: user.password
|
|
|
|
|
click_button 'Update profile'
|
|
|
|
|
|
|
|
|
|
expect(page).to have_css('.notice')
|
2023-03-19 19:57:53 +01:00
|
|
|
user.reload; user.confirm
|
|
|
|
|
expect(user.email).to eq(new_email)
|
|
|
|
|
|
2019-08-28 15:21:20 +02:00
|
|
|
end
|
|
|
|
|
|
2021-01-26 23:42:36 +01:00
|
|
|
scenario 'turns on notifications' do
|
|
|
|
|
user.update(notifications_enabled: false)
|
|
|
|
|
|
|
|
|
|
visit edit_user_registration_path
|
|
|
|
|
check 'Notifications enabled'
|
|
|
|
|
fill_in 'Current password', with: user.password
|
|
|
|
|
click_button 'Update profile'
|
|
|
|
|
|
2023-03-19 19:57:53 +01:00
|
|
|
expect(page).to have_css('.notice')
|
|
|
|
|
expect(user.reload.notifications_enabled).to eq(true)
|
2021-01-26 23:42:36 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
scenario 'turns off notifications' do
|
|
|
|
|
user.update(notifications_enabled: user)
|
|
|
|
|
|
|
|
|
|
visit edit_user_registration_path
|
|
|
|
|
uncheck 'Notifications enabled'
|
|
|
|
|
fill_in 'Current password', with: user.password
|
|
|
|
|
click_button 'Update profile'
|
|
|
|
|
|
2023-03-19 19:57:53 +01:00
|
|
|
expect(page).to have_css('.notice')
|
|
|
|
|
expect(user.reload.notifications_enabled).to eq(false)
|
2021-01-26 23:42:36 +01:00
|
|
|
end
|
|
|
|
|
|
2019-08-28 15:21:20 +02:00
|
|
|
# Remember that 'password' is just a virtual attribute (i.e. it is not stored in the db)
|
|
|
|
|
# so updating the user account password does not update it, but only the
|
|
|
|
|
# 'encrypted_password' attribute in the db
|
|
|
|
|
scenario 'edit password' do
|
|
|
|
|
new_password = 'newpassword'
|
|
|
|
|
encrypted_password = user.encrypted_password
|
|
|
|
|
|
|
|
|
|
expect(user.password).not_to eq(new_password)
|
|
|
|
|
|
|
|
|
|
visit edit_user_registration_path
|
|
|
|
|
fill_in 'Password', with: new_password
|
|
|
|
|
fill_in 'Password confirmation', with: new_password
|
|
|
|
|
fill_in 'Current password', with: user.password
|
|
|
|
|
click_button 'Update profile'
|
|
|
|
|
|
|
|
|
|
# I don't know why the following line doesn't work
|
|
|
|
|
# (maybe Devise uses a different BCrypt config?)
|
|
|
|
|
# expect(User.find(user.id).encrypted_password).to eq(BCrypt::Password.create(new_password))
|
|
|
|
|
|
|
|
|
|
# Because the previous line does not work, I decided to use this
|
|
|
|
|
# expectation, which is weaker (it just checks that the
|
|
|
|
|
# encrypted password is different after updating the profile)
|
|
|
|
|
expect(page).to have_css('.notice')
|
2023-03-19 19:57:53 +01:00
|
|
|
expect(user.reload.encrypted_password).not_to eq(encrypted_password)
|
2019-08-28 15:21:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
scenario 'edit field with invalid current password' do
|
|
|
|
|
full_name = user.full_name
|
|
|
|
|
|
|
|
|
|
visit edit_user_registration_path
|
|
|
|
|
fill_in 'Full name', with: 'New fantastic full name'
|
|
|
|
|
# do not fill current password textbox
|
|
|
|
|
click_button 'Update profile'
|
|
|
|
|
|
|
|
|
|
expect(page).to have_css('#error_explanation')
|
|
|
|
|
expect(page).to have_css('.field_with_errors')
|
2023-03-19 19:57:53 +01:00
|
|
|
expect(user.reload.full_name).to eq(full_name)
|
2019-08-28 15:21:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
scenario 'cancel account', js: true do
|
2023-03-19 19:57:53 +01:00
|
|
|
expect(user.status).to eq('active')
|
2019-08-28 15:21:20 +02:00
|
|
|
|
|
|
|
|
visit edit_user_registration_path
|
2023-03-19 19:57:53 +01:00
|
|
|
click_button 'Cancel account'
|
2019-08-28 15:21:20 +02:00
|
|
|
page.driver.browser.switch_to.alert.accept # accepts js pop up
|
2021-01-26 23:42:36 +01:00
|
|
|
|
2019-08-28 15:21:20 +02:00
|
|
|
expect(page).to have_current_path(root_path)
|
|
|
|
|
expect(page).to have_css('.notice')
|
2023-03-19 19:57:53 +01:00
|
|
|
expect(user.reload.status).to eq('deleted')
|
2019-08-28 15:21:20 +02:00
|
|
|
end
|
2021-01-26 23:42:36 +01:00
|
|
|
end
|