Skip to content

Commit

Permalink
RSpec testing for user sign-up and sign-in
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrawford13 committed Apr 3, 2015
1 parent 4b6ec8b commit cd2f5c2
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@
/log/*
!/log/.keep
/tmp

# Ignore application configuration
/config/application.yml
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
source 'https://rubygems.org'

ruby '2.2.0'
gem 'rails', '4.2.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
Expand All @@ -22,6 +22,7 @@ gem 'puma'
gem 'bootstrap-sass'
gem 'faker'
gem 'devise'
gem 'figaro'

group :development do
gem 'sqlite3', '~> 1.3.10'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ GEM
railties (>= 3.0.0)
faker (1.4.3)
i18n (~> 0.5)
figaro (1.0.0)
thor (~> 0.14)
globalid (0.3.3)
activesupport (>= 4.1.0)
hike (1.2.3)
Expand Down Expand Up @@ -210,6 +212,7 @@ DEPENDENCIES
devise
factory_girl_rails (~> 4.0)
faker
figaro
jbuilder (~> 2.0)
jquery-rails
pg
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
module ApplicationHelper
end
end
19 changes: 12 additions & 7 deletions app/views/devise/registrations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>

<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, autofocus: true, class: 'form-control' %>
</div>

<div class="form-group">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
<%= f.email_field :email, class: 'form-control' %>
</div>

<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
Expand All @@ -14,26 +19,26 @@

<div class="form-group">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "off" %>
<%= f.password_field :password, autocomplete: "off", class: 'form-control' %>
</div>

<div class="form-group">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
<%= f.password_field :password_confirmation, autocomplete: "off", class: 'form-control' %>
</div>

<div class="form-group">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %>
<%= f.password_field :current_password, autocomplete: "off", class: 'form-control' %>
</div>

<div class="form-group">
<%= f.submit "Update" %>
<%= f.submit "Update" , class: 'btn btn-success'%>
</div>
<% end %>

<h3>Cancel my account</h3>

<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>
<%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete, class: 'btn btn-danger' %></p>

<%= link_to "Back", :back %>
<%= link_to "Back", :back, class: 'btn btn-primary' %>
1 change: 1 addition & 0 deletions app/views/devise/registrations/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<%= f.label :name %>
<%= f.text_field :name, autofocus: true, class: 'form-control', placeholder: "Enter name" %>
</div>

<div class="form-group">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, class: 'form-control', placeholder: "Enter email" %>
Expand Down
11 changes: 5 additions & 6 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@

<div class="pull-right user-info"
<% if current_user %>
Hello <%= link_to (current_user.name || current_user.email), current_user %>!
Hello <%= link_to (current_user.name || current_user.email) %>!
<%= link_to edit_user_registration_path, class: 'btn btn-primary btn-sm' do %>
<span class="glyphicon glyphicon-user"></span>
<% end %>
<%= link_to destroy_user_session_path, method: :delete, class: 'btn btn-primary btn-sm' do %>
<span class="glyphicon glyphicon-log-out"></span>
<% end %>
<% end %>
<%= link_to "Sign out", destroy_user_session_path, method: :delete, class: 'btn btn-primary btn-sm' %>
<% else %>
<%= link_to "Sign In", new_user_session_path %> or
<%= link_to "Sign In", new_user_session_path %> |
<%= link_to "Sign Up", new_user_registration_path %>
<% end %>
</div>
Expand Down
2 changes: 2 additions & 0 deletions config/application.example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SENDGRID_PASSWORD:

This comment has been minimized.

Copy link
@felixclack

felixclack Apr 15, 2015

The extension of this file should be .yml rather than .rb

SENDGRID_USERNAME:
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true

end

56 changes: 56 additions & 0 deletions spec/features/edit_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'rails_helper'

describe "Edit user attributes" do

describe "change user password" do
before do
@user = create(:user)

#Sign in User
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)
visit edit_user_registration_path
end

it "is not updated without current password" do

fill_in "Password", with: 'newpassword'
fill_in "Password confirmation", with: 'newpassword'
fill_in "Current password", with: ''

This comment has been minimized.

Copy link
@felixclack

felixclack Apr 15, 2015

Filling the field with a blank string is the same as not filling it in at all, so you could leave this line out.

click_button "Update"
expect( page ).to have_content("Current password can't be blank")
end

it "is not updated if 'password confirmation' does not mach password" do

fill_in "Password", with: 'newpassword'
fill_in "Password confirmation", with: 'password'
fill_in "Current password", with: @user.password

This comment has been minimized.

Copy link
@felixclack

felixclack Apr 15, 2015

Does this work? Usually the #password method on a Devise model is not a readable field. It stores an encrypted version of the password and not the actual password.

click_button "Update"
expect( page ).to have_content("Password confirmation doesn't match Password")
end

it "is not updated if password is shorter than 8 characters" do

fill_in "Password", with: 'word'
fill_in "Password confirmation", with: 'word'
fill_in "Current password", with: @user.password
click_button "Update"
expect( page ).to have_content("Password is too short")
end

it "updates password with all fields correctly entered" do
fill_in "Password", with: 'newpassword'
fill_in "Password confirmation", with: 'newpassword'
fill_in "Current password", with: @user.password
click_button "Update"
expect( current_path ).to eq(root_path)
end

end
end

48 changes: 48 additions & 0 deletions spec/features/sign_in_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'rails_helper'

describe 'User sign-in' do

describe "successful sign-in" do
before do
@user = create(:user)
end

it "allows confirmed user to sign-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

describe "navigation changes to reflect #current_user" do
before do
@user = create(:user)
end

it "shows #user.name when signed in" do

visit new_user_session_path

fill_in "Email", with: @user.email
fill_in "Password", with: @user.password
click_button "Log in"
expect( page ).to have_content(@user.name)
end

it "shows link to 'sign-in' when no user is signed in" do

visit root_path

within '.user-info' do
expect( page ).to have_content("Sign In")
end
end
end
end



54 changes: 44 additions & 10 deletions spec/features/sign_up_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,61 @@
describe 'User sign up' do

describe "successful sign-up" do
before do
@user = create(:user)
@user1 = create(:user)
end

it "sends confirmation email following successful sign-up" do
before do
@user = create(:user)
end

visit root_path
within '.user-info' do
click_link 'Sign Up'
end

sign_up_nav

fill_in "Name", with: @user.name
fill_in "Email", with: @user.email
fill_in "Password", with: @user.password
fill_in "Password confirmation", with: @user.password_confirmation
click_button 'Sign up'
expect( @user.confirmed_at ).not_to be nil
end
end

describe "unsuccessful sign-up" do
before do
@user = create(:user)
end

it "prohibits user from signing up with duplicate email" do

sign_up_nav

fill_in "Name", with: @user.name
fill_in "Email", with: @user.email
fill_in "Password", with: @user.password
fill_in "Password confirmation", with: @user.password_confirmation
click_button 'Sign up'
expect(@user.confirmation_sent_at).to eq(Time.now)
expect( page ).to have_content('Email has already been taken')
end

describe "unsuccessful sign-up" do
it "prohibits user from signing up with invalid email" do

@user1 = create(:user)

sign_up_nav

fill_in "Name", with: @user1.name
fill_in "Email", with: 'chad@123'
fill_in "Password", with: @user1.password
fill_in "Password confirmation", with: @user1.password_confirmation
click_button 'Sign up'
expect( page ).to have_content('Email is invalid')
end
end
end

def sign_up_nav
visit root_path
within '.user-info' do
click_link 'Sign Up'
end
end

Expand Down

0 comments on commit cd2f5c2

Please sign in to comment.