mirror of
https://github.com/astuto/astuto.git
synced 2025-12-16 11:47:56 +01:00
Add system tests for user signup, login and edit profile
This commit is contained in:
@@ -47,7 +47,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="actions">
|
<div class="actions">
|
||||||
<%= f.submit "Update", class: "btn btn-primary" %>
|
<%= f.submit "Update profile", class: "btn btn-primary" %>
|
||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,7 @@
|
|||||||
</li>
|
</li>
|
||||||
<% else %>
|
<% else %>
|
||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<%= link_to 'Sign in / Sign up', new_user_session_path, class: 'nav-link' %>
|
<%= link_to 'Log in / Sign up', new_user_session_path, class: 'nav-link' %>
|
||||||
</li>
|
</li>
|
||||||
<% end %>
|
<% end %>
|
||||||
</ul>
|
</ul>
|
||||||
|
|||||||
@@ -1,31 +0,0 @@
|
|||||||
require 'rails_helper'
|
|
||||||
|
|
||||||
RSpec.describe 'Session', type: :system do
|
|
||||||
|
|
||||||
let(:user) { FactoryBot.create(:user) }
|
|
||||||
|
|
||||||
before(:each) { user.confirm } # devise helper to confirm user email
|
|
||||||
|
|
||||||
it 'allows guests to sign in' do
|
|
||||||
user_count = User.count
|
|
||||||
|
|
||||||
visit new_user_registration_path
|
|
||||||
fill_in 'Full name', with: 'Test Test'
|
|
||||||
fill_in 'Email', with: 'test@test.com'
|
|
||||||
fill_in 'Password', with: 'password'
|
|
||||||
fill_in 'Password confirmation', with: 'password'
|
|
||||||
click_button 'Sign up'
|
|
||||||
|
|
||||||
expect(User.count).to eq(user_count + 1)
|
|
||||||
end
|
|
||||||
|
|
||||||
# it 'allows users to log in' do
|
|
||||||
# visit new_user_session_path
|
|
||||||
# fill_in "Email", with: user.email
|
|
||||||
# fill_in "Password", with: user.password
|
|
||||||
# click_button "Log in"
|
|
||||||
|
|
||||||
# expect(current_path).to eq(root_path)
|
|
||||||
# end
|
|
||||||
|
|
||||||
end
|
|
||||||
99
spec/system/user_edit_profile_spec.rb
Normal file
99
spec/system/user_edit_profile_spec.rb
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
# require 'bcrypt'
|
||||||
|
|
||||||
|
feature 'edit user profile settings', type: :system do
|
||||||
|
let(:user) { FactoryBot.create(:user) }
|
||||||
|
|
||||||
|
before(:each) do
|
||||||
|
user.confirm # devise helper to confirm user account
|
||||||
|
sign_in user # devise helper to login user
|
||||||
|
|
||||||
|
# check that user is confirmed and saved in the db
|
||||||
|
expect(user.confirmed_at).not_to be_nil
|
||||||
|
expect(User.count).to eq(1)
|
||||||
|
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'
|
||||||
|
|
||||||
|
user.reload
|
||||||
|
expect(user.full_name).to eq(new_full_name)
|
||||||
|
expect(page).to have_css('.notice')
|
||||||
|
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'
|
||||||
|
|
||||||
|
user.reload
|
||||||
|
user.confirm
|
||||||
|
expect(user.email).to eq(new_email)
|
||||||
|
expect(page).to have_css('.notice')
|
||||||
|
end
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
user.reload
|
||||||
|
expect(user.encrypted_password).not_to eq(encrypted_password)
|
||||||
|
expect(page).to have_css('.notice')
|
||||||
|
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'
|
||||||
|
|
||||||
|
user.reload
|
||||||
|
expect(user.full_name).to eq(full_name)
|
||||||
|
expect(page).to have_css('#error_explanation')
|
||||||
|
expect(page).to have_css('.field_with_errors')
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario 'cancel account', js: true do
|
||||||
|
user_count = User.count
|
||||||
|
|
||||||
|
visit edit_user_registration_path
|
||||||
|
click_button 'Cancel my account'
|
||||||
|
page.driver.browser.switch_to.alert.accept # accepts js pop up
|
||||||
|
|
||||||
|
expect(page).to have_current_path(root_path)
|
||||||
|
expect(User.count).to eq(user_count - 1)
|
||||||
|
expect(page).to have_css('.notice')
|
||||||
|
end
|
||||||
|
end
|
||||||
51
spec/system/user_log_in_spec.rb
Normal file
51
spec/system/user_log_in_spec.rb
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
feature 'log in', type: :system do
|
||||||
|
let(:user) { FactoryBot.create(:user) }
|
||||||
|
|
||||||
|
before(:each) do
|
||||||
|
user.confirm # devise helper to confirm user account
|
||||||
|
|
||||||
|
# check that user is confirmed and saved in the db
|
||||||
|
expect(user.confirmed_at).not_to be_nil
|
||||||
|
expect(User.count).to eq(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
def log_in_as(user)
|
||||||
|
visit new_user_session_path
|
||||||
|
fill_in 'Email', with: user.email
|
||||||
|
fill_in 'Password', with: user.password
|
||||||
|
click_button 'Log in'
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario 'with valid credentials' do
|
||||||
|
log_in_as user
|
||||||
|
|
||||||
|
expect(page).not_to have_current_path(new_user_session_path)
|
||||||
|
expect(page).to have_no_content('Log in')
|
||||||
|
expect(page).to have_css('.notice')
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario 'with invalid credentials' do
|
||||||
|
visit new_user_session_path
|
||||||
|
fill_in 'Email', with: user.email + 'a' # wrong email
|
||||||
|
fill_in 'Password', with: user.password
|
||||||
|
click_button 'Log in'
|
||||||
|
|
||||||
|
expect(page).to have_current_path(new_user_session_path)
|
||||||
|
expect(page).to have_content('Log in')
|
||||||
|
expect(page).to have_css('.alert')
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario 'log out' do
|
||||||
|
sign_in user
|
||||||
|
|
||||||
|
visit root_path
|
||||||
|
click_link 'Sign out'
|
||||||
|
|
||||||
|
expect(page).to have_current_path(root_path)
|
||||||
|
expect(page).to have_content('Log in / Sign up')
|
||||||
|
expect(page).to have_css('.notice')
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
78
spec/system/user_sign_up_spec.rb
Normal file
78
spec/system/user_sign_up_spec.rb
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
feature 'sign up', type: :system do
|
||||||
|
let(:user) { FactoryBot.build(:user) }
|
||||||
|
|
||||||
|
def sign_up_as(user)
|
||||||
|
visit new_user_registration_path
|
||||||
|
fill_in 'Full name', with: user.full_name
|
||||||
|
fill_in 'Email', with: user.email
|
||||||
|
fill_in 'Password', with: user.password
|
||||||
|
fill_in 'Password confirmation', with: user.password
|
||||||
|
click_button 'Sign up'
|
||||||
|
end
|
||||||
|
|
||||||
|
def expect_to_be_on_sign_up_page
|
||||||
|
expect(page).to have_current_path(user_registration_path)
|
||||||
|
expect(page).to have_content('Sign up')
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario 'with valid fields' do
|
||||||
|
user_count = User.count
|
||||||
|
|
||||||
|
sign_up_as user
|
||||||
|
|
||||||
|
expect(User.count).to eq(user_count + 1)
|
||||||
|
expect(page).to have_current_path(root_path)
|
||||||
|
expect(page).to have_css('.notice')
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario 'with invalid Full Name' do
|
||||||
|
user_count = User.count
|
||||||
|
|
||||||
|
user.full_name = 'a'
|
||||||
|
sign_up_as user
|
||||||
|
|
||||||
|
expect(User.count).to eq(user_count)
|
||||||
|
expect_to_be_on_sign_up_page
|
||||||
|
expect(page).to have_css('.alert')
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario 'with invalid email' do
|
||||||
|
user_count = User.count
|
||||||
|
|
||||||
|
user.email = 'a'
|
||||||
|
sign_up_as user
|
||||||
|
|
||||||
|
expect(User.count).to eq(user_count)
|
||||||
|
expect_to_be_on_sign_up_page
|
||||||
|
expect(page).to have_css('.alert')
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario 'with invalid password' do
|
||||||
|
user_count = User.count
|
||||||
|
|
||||||
|
user.password = 'a'
|
||||||
|
sign_up_as user
|
||||||
|
|
||||||
|
expect(User.count).to eq(user_count)
|
||||||
|
expect_to_be_on_sign_up_page
|
||||||
|
expect(page).to have_css('.alert')
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario 'with mismatching passwords' do
|
||||||
|
user_count = User.count
|
||||||
|
|
||||||
|
user.email = 'a'
|
||||||
|
visit new_user_registration_path
|
||||||
|
fill_in 'Full name', with: user.full_name
|
||||||
|
fill_in 'Email', with: user.email
|
||||||
|
fill_in 'Password', with: user.password
|
||||||
|
fill_in 'Password confirmation', with: user.password + 'a'
|
||||||
|
click_button 'Sign up'
|
||||||
|
|
||||||
|
expect(User.count).to eq(user_count)
|
||||||
|
expect_to_be_on_sign_up_page
|
||||||
|
expect(page).to have_css('.alert')
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user