Skip to content

Commit

Permalink
Finishing touches on specs:
Browse files Browse the repository at this point in the history
- Better spec descriptions
- Use path/url helpers in feature specs
- Rename routes
  • Loading branch information
christiannelson committed Dec 27, 2012
1 parent b0f959f commit 294f4ba
Show file tree
Hide file tree
Showing 18 changed files with 77 additions and 87 deletions.
5 changes: 3 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
* Basic usage information.
* Added guard-livereload to Guardfile.
* Gemfile, rvmrc, rbenv files configured with the ruby version used to generate the app.
* Better specs for auth flows (register, password reset, sign in).
* Hardcode 1.9.3-p327 so that app_prototype is executable without futzing.
* Better specs for auth flows (register, password reset, sign in) (~98% coverage).
* Hardcode 1.9.3-p327 so that app_prototype is executable without futzing.
* Consistent hostnames across environments.
4 changes: 2 additions & 2 deletions app_prototype/app/controllers/password_resets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def create
end

def edit
@user = User.load_from_reset_password_token(params[:id])
@token = params[:id]
@token = params[:token]
@user = User.load_from_reset_password_token(@token)
not_authenticated if !@user
end

Expand Down
4 changes: 2 additions & 2 deletions app_prototype/app/views/password_resets/edit.html.slim
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
.page-header
h1 Reset Your Password

= simple_form_for(@user, url: password_reset_path(@user)) do |f|
= simple_form_for(@user, url: reset_password_path(@user)) do |f|
= f.error_notification

.form-inputs
= f.input :email, disabled: true
= f.input :password, required: true, autofocus: true
= f.input :password, label: 'New Password', required: true, autofocus: true
= hidden_field_tag :token, @token

.form-actions
Expand Down
4 changes: 2 additions & 2 deletions app_prototype/app/views/password_resets/new.html.slim
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
.page-header
h1 Password Reset

= form_tag(password_resets_path, method: :post) do
= form_tag(forgotten_password_path, method: :post) do
.control-group
= label_tag :email, nil, class: 'control-label'
.controls
= text_field_tag :email, nil, placeholder: 'joe@example.com'

.form-actions
= submit_tag "Reset My Password!", class: 'btn btn-primary'
= submit_tag 'Reset My Password', class: 'btn btn-primary'
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
your username is: <%= @user.email %>.<br/>
</p>
<p>
To login to the site, just follow this link: <%= link_to activate_registration_url(@user.activation_token), activate_registration_url(@user.activation_token) %>.
To login to the site, just follow this link: <%= link_to activation_url(@user.activation_token), activation_url(@user.activation_token) %>.
</p>
<p>Thanks for joining and have a great day!</p>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ Welcome to example.com, <%= @user.email %>
You have successfully signed up to example.com,
your username is: <%= @user.email %>.

To login to the site, just follow this link: <%= activate_registration_url(@user.activation_token) %>
To login to the site, just follow this link: <%= activation_url(@user.activation_token) %>

Thanks for joining and have a great day!
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
You have requested to reset your password.
</p>
<p>
To choose a new password, just follow this link: <%= link_to edit_password_reset_url(@user.reset_password_token), edit_password_reset_url(@user.reset_password_token) %>.
To choose a new password, just follow this link: <%= link_to reset_password_url(@user.reset_password_token), reset_password_url(@user.reset_password_token) %>.
</p>
<p>Have a great day!</p>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ Hello, <%= @user.email %>

You have requested to reset your password.

To choose a new password, just follow this link: edit_password_reset_url(@user.reset_password_token)
To choose a new password, just follow this link: <%= reset_password_url(@user.reset_password_token) %>

Have a great day!
2 changes: 1 addition & 1 deletion app_prototype/app/views/user_sessions/new.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
= f.input :password

ul.unstyled
li= link_to 'Reset forgotten password', new_password_reset_path
li= link_to 'Sign Up', sign_up_path
li= link_to 'Reset Password', forgotten_password_path

.form-actions
= f.button :submit, 'Sign In', class: 'btn btn-primary'
2 changes: 1 addition & 1 deletion app_prototype/app/views/users/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ dl

- if can?(:destroy, @user)
'
= link_to'Destroy', user_path(@user), method: :delete, data: { confirm: "Are you sure?" }, class: 'btn btn-danger'
= link_to 'Destroy', user_path(@user), method: :delete, data: { confirm: "Are you sure?" }, class: 'btn btn-danger'
14 changes: 9 additions & 5 deletions app_prototype/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@

resources :user_sessions, only: [:new, :create, :destroy]

resources :registrations, only: [:new, :create, :activate]
match 'sign_up' => 'registrations#new', via: :get
match 'sign_up' => 'registrations#create', via: :post
match 'sign_up/:token/activate' => 'registrations#activate', via: :get, as: :activate_registration
#resources :registrations, only: [:new, :create, :activate]
match 'sign_up' => 'registrations#new', via: :get, as: :sign_up
match 'sign_up' => 'registrations#create', via: :post, as: :sign_up
match 'activate/:token' => 'registrations#activate', via: :get, as: :activation

resources :password_resets, only: [:new, :create, :edit, :update]
#resources :password_resets, only: [:new, :create, :edit, :update]
match 'forgotten_password' => 'password_resets#new', via: :get, as: :forgotten_password
match 'forgotten_password' => 'password_resets#create', via: :post, as: :forgotten_password
match 'reset_password/:token' => 'password_resets#edit', via: :get, as: :reset_password
match 'reset_password/:id' => 'password_resets#update', via: :put

resources :users

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

describe UserSessionsController do

describe "GET new" do
describe "#new" do
it "assigns a new user as @user" do
get :new
expect(assigns(:user_session)).to_not be_nil
end
end

describe "POST create" do
describe "#create" do
describe "with valid params" do
it "redirect to the target page" do
subject.stub(:login) { build_stubbed :user }
Expand All @@ -27,7 +27,7 @@
end
end

describe "DELETE destroy" do
describe "#destroy" do
it "destroys the requested user session" do
subject.should_receive(:logout)
delete :destroy
Expand Down
16 changes: 8 additions & 8 deletions app_prototype/spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def valid_attributes
attributes_for :user
end

# expect(This).to expect(return the minimal set of values that).to be in the session
# This returns the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# UsersController. Be sure to keep this updated too.
def valid_session
Expand All @@ -20,38 +20,38 @@ def valid_session
login_user build :admin
end

describe "GET index" do
describe "#index" do
it "assigns all users as @users" do
user = User.create! valid_attributes
get :index, {}, valid_session
expect(assigns(:users)).to eq([user])
end
end

describe "GET show" do
describe "#show" do
it "assigns the requested user as @user" do
user = User.create! valid_attributes
get :show, { id: user.to_param }, valid_session
expect(assigns(:user)).to eq(user)
end
end

describe "GET new" do
describe "#new" do
it "assigns a new user as @user" do
get :new, {}, valid_session
expect(assigns(:user)).to be_a_new(User)
end
end

describe "GET edit" do
describe "#edit" do
it "assigns the requested user as @user" do
user = User.create! valid_attributes
get :edit, { id: user.to_param }, valid_session
expect(assigns(:user)).to eq(user)
end
end

describe "POST create" do
describe "#create" do
describe "with valid params" do
it "creates a new User" do
expect {
Expand Down Expand Up @@ -88,7 +88,7 @@ def valid_session
end
end

describe "PUT update" do
describe "#update" do
describe "with valid params" do
it "updates the requested user" do
user = User.create! valid_attributes
Expand Down Expand Up @@ -132,7 +132,7 @@ def valid_session
end
end

describe "DELETE destroy" do
describe "#destroy" do
it "destroys the requested user" do
user = User.create! valid_attributes
expect {
Expand Down
8 changes: 4 additions & 4 deletions app_prototype/spec/features/activation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
require 'spec_helper'

feature "Activation" do
scenario "a successful activation" do
scenario "with a valid token should activate the user and sign them in" do
@user = create(:user)
visit "/sign_up/#{@user.activation_token}/activate" # FIXME
visit activation_path(@user.activation_token)

expect(current_path).to eq sign_in_path
expect(page).to have_content "Your account has been activated and you're now signed in."
end

scenario "an unsuccessful activation" do
visit "/sign_up/bogus_activation_token/activate" # FIXME
scenario "with an invalid token should send the user to sign in" do
visit activation_path('BOGUS')

expect(current_path).to eq sign_in_path
expect(page).to have_content "Please sign in first."
Expand Down
14 changes: 7 additions & 7 deletions app_prototype/spec/features/password_reset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
@user.activate!

visit sign_in_path
click_link 'Reset forgotten password'
click_link 'Reset Password'

fill_in 'Email', with: @user.email
click_button 'Reset My Password!'
click_button 'Reset My Password'

@user.reload
end
Expand All @@ -24,15 +24,15 @@
expect(current_path).to eq sign_in_path
end

scenario "the password reset email is sent with a reset url" do
scenario "sends a password reset email with url" do
expect(open_email(@user.email)).to_not be_nil
expect(current_email).to have_content edit_password_reset_path(@user.reset_password_token) # FIXME
expect(current_email).to have_content reset_password_path(@user.reset_password_token)
end

scenario "password can be reset" do
visit edit_password_reset_path(@user.reset_password_token)
scenario "resets the password" do
visit reset_password_path(@user.reset_password_token)

fill_in 'Password', with: 'some_good_password'
fill_in 'New Password', with: 'som3_g00d_p@ssword'
click_button 'Reset Password'

expect(page).to have_content "Password was successfully updated."
Expand Down
59 changes: 22 additions & 37 deletions app_prototype/spec/features/registration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,37 @@
# http://www.elabs.se/blog/51-simple-tricks-to-clean-up-your-capybara-tests

feature "Registration" do
context "successful registration" do
background do
clear_emails
background do
clear_emails

@email = 'stan@example.com'
@email = 'stan@example.com'

visit sign_up_path
visit sign_up_path

within '#new_user' do
fill_in 'Email', with: @email
fill_in 'Name', with: 'Stan'
fill_in 'Password', with: 'p@ssword'
click_button 'Sign Up'
end

@user = User.find_by_email(@email)
within '#new_user' do
fill_in 'Email', with: @email
fill_in 'Name', with: 'Stan'
fill_in 'Password', with: 'p@ssword'
click_button 'Sign Up'
end

after do
@user.destroy
end
@user = User.find_by_email(@email)
end

it "creates a new user" do
expect(@user).to_not be_nil
end
after do
@user.destroy
end

it "displays a message about activation" do
expect(find('.alert')).to have_content "Thanks for signing up. Please check your email for activation instructions."
end
it "creates a new user" do
expect(@user).to_not be_nil
end

it "sends the activation email" do
expect(open_email(@email)).to_not be_nil
expect(current_email).to have_content "/sign_up/#{@user.activation_token}/activate" # FIXME
end
it "displays a message about activation" do
expect(find('.alert')).to have_content "Thanks for signing up. Please check your email for activation instructions."
end

context "unsuccessful registration" do
it "redirects to new" do
visit sign_up_path
within '#new_user' do
fill_in 'Email', with: 'INVALID EMAIL'
fill_in 'Name', with: 'Stan'
fill_in 'Password', with: 'p@ssword'
click_button 'Sign Up'
end
expect(current_path).to eq sign_up_path
end
it "sends the activation email with url" do
expect(open_email(@email)).to_not be_nil
expect(current_email).to have_content activation_path(@user.activation_token)
end
end
6 changes: 3 additions & 3 deletions app_prototype/spec/features/sign_in_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
@user.destroy
end

scenario "Sign in with valid credentials" do
scenario "authenticates with valid credentials" do
sign_in(@user.email, 'password')

expect(find('.alert')).to have_content("Successfully signed in")
end

scenario "Sign in with an invalid email" do
scenario "displays a generic error message with an invalid email" do
sign_in('this is not valid', 'password')

expect(find('.alert')).to have_content("Sign in failed")
end

scenario "Sign in with an invalid password" do
scenario "displays a generic error message with an invalid password" do
sign_in(@user.email, 'this is not valid')

expect(find('.alert')).to have_content("Sign in failed")
Expand Down
Loading

0 comments on commit 294f4ba

Please sign in to comment.