Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User story9 #101

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ def new
def create
@user = User.new(strong_params)
if password_confirmation != true
flash[:notice] = "Those passwords don't match."
render :new
flash[:notice] = "Those passwords don't match"
elsif @user.save
elsif email_confirmation != true
flash[:notice] = "That email address is already taken."
render :new
elsif @user.save!
session[:user_id] = @user.id
flash[:notice] = "You are now registered and logged in."
redirect_to profile_path
Expand All @@ -31,6 +34,12 @@ def edit

private

def email_confirmation
User.email_string.all? do |email|
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you just use an .include? here?

params[:user][:email] != email
end
end

def password_confirmation
if params["user"]["password"] == params["user"]["confirm_password"]
true
Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ class User < ApplicationRecord
has_secure_password

enum role: ["default", "merchant", "admin"]

def self.email_string
pluck(:email)
end
end
62 changes: 58 additions & 4 deletions spec/features/users/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
describe 'When I visit the register new register link' do

before :each do
@user_1 = User.create!(name: "default_user", role: 0, active: true, password_digest: "8320280282", address: "333", city: "Denver", zip: "80000", email: "[email protected]", state: 'IL' )
@user_1 = User.create!(name: "default_user", role: 0, active: true, password_digest: "8320280282", address: "333", city: "Denver", state: "CO", zip: "80000", email: "[email protected]" )
end

it 'I can register as a new user' do
Expand Down Expand Up @@ -45,8 +45,58 @@

end

it 'Can not use an already used email address' do
user_2 = User.create!(name: "User_1", role: 0, active: true, password_digest: "8320280282", address: "333", city: "Denver", zip: "80000", email: "[email protected]", state: 'IL' )
it 'I throws flash message when password isnt the same' do


visit root_path

within '.register-link' do
click_link('Register')
end

expect(current_path).to eq(register_path)

fill_in 'Name', with: 'User'
fill_in 'Address', with: '1111 South One St.'
fill_in 'City', with: 'Denver'
fill_in 'State', with: 'CO'
fill_in 'Zip', with: '80000'
fill_in 'Email', with: '[email protected]'
fill_in 'Password', with: 'password'
fill_in 'Confirm password', with: 'password3'

click_button 'Create User'


new_user = User.last


expect(current_path).to eq(register_path)

expect(page).to have_content("Those passwords don't match.")

end

it 'Can not use a duplicate email address' do

visit root_path

within '.register-link' do
click_link('Register')
end


fill_in 'Name', with: 'User_1'
fill_in 'Address', with: '1111 South One St.'
fill_in 'City', with: 'Denver'
fill_in 'State', with: 'CO'
fill_in 'Zip', with: '80000'
fill_in 'Email', with: '[email protected]'
fill_in 'Password', with: 'password'
fill_in 'Confirm password', with: 'password'

click_button 'Create User'

visit root_path

within '.navbar' do
Expand All @@ -65,9 +115,13 @@
click_button 'Create User'

new_user = User.last

expect(page).to have_content("That email address is already taken.")
expect(new_user.name).to eq('User_1')
end
end
end
end

# My details are not saved and I am not logged in
# The form is filled in with all previous data except the email field and password fields
# I see a flash message telling me the email address is already in use
11 changes: 11 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,15 @@

it { should validate_uniqueness_of :email }
end

describe 'class methods' do

it 'email_string' do
user_1 = User.create!(name: "default_user", role: 0, active: true, password_digest: "8320280282", address: "333", city: "Denver", state: "CO", zip: "80000", email: "[email protected]" )
user_2 = User.create!(name: "default_user1", role: 0, active: true, password_digest: "8320280282", address: "333", city: "Denver", state: "CO", zip: "80000", email: "[email protected]" )
user_3 = User.create!(name: "default_user2", role: 0, active: true, password_digest: "8320280282", address: "333", city: "Denver", state: "CO", zip: "80000", email: "[email protected]" )

expect(User.email_string).not_to eq(["[email protected]", "[email protected]", "[email protected]"])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a positive expectation as well?

end
end
end