Skip to content

Commit

Permalink
refactoring. "confirmed" attribute was getting confusing when talking…
Browse files Browse the repository at this point in the history
… about password confirmation, so it became "email_confirmed". pulling out some logical test groups into shoulda macros (getting a good start on a should_validate_confirmation_of, yet depends on Factory Girl). changing generated factory file to be more than one Factory definition for different states of Users
  • Loading branch information
Dan Croak committed Jan 18, 2009
1 parent 1af586a commit fbca4ec
Show file tree
Hide file tree
Showing 21 changed files with 107 additions and 105 deletions.
2 changes: 1 addition & 1 deletion README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ The tests use "Shoulda":http://thoughtbot.com/projects/shoulda >= 2.0.6 and "Fac
include Clearance::Test::TestHelper
end

The generator will create a user factory in test/factories/clearance_user.rb unless
The generator will create a user factory in test/factories/clearance.rb unless
you have it defined somewhere else.

h2. Usage: basic workflow
Expand Down
2 changes: 1 addition & 1 deletion clearance.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ files:
- generators/clearance/templates/README
- generators/clearance/templates/test
- generators/clearance/templates/test/factories
- generators/clearance/templates/test/factories/clearance_user.rb
- generators/clearance/templates/test/factories/clearance.rb
- generators/clearance/templates/test/functional
- generators/clearance/templates/test/functional/confirmations_controller_test.rb
- generators/clearance/templates/test/functional/passwords_controller_test.rb
Expand Down
2 changes: 1 addition & 1 deletion generators/clearance/clearance_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def manifest
end

m.directory File.join("test", "factories")
["test/factories/clearance_user.rb"].each do |file|
["test/factories/clearance.rb"].each do |file|
m.file file, file
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def self.up
t.string :salt, :limit => 40
t.string :remember_token
t.datetime :remember_token_expires_at
t.boolean :confirmed, :default => false, :null => false
t.boolean :email_confirmed, :default => false, :null => false
end

add_index :users, [:email, :encrypted_password]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def self.up
[:salt, 't.string :salt, :limit => 40'],
[:remember_token, 't.string :remember_token'],
[:remember_token_expires_at, 't.datetime :remember_token_expires_at'],
[:confirmed, 't.boolean :confirmed, :default => false, :null => false']
[:email_confirmed, 't.boolean :email_confirmed, :default => false, :null => false']
].delete_if {|c| existing_columns.include?(c.first.to_s)}
-%>
change_table(:users) do |t|
Expand Down
9 changes: 9 additions & 0 deletions generators/clearance/templates/test/factories/clearance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Factory.sequence :email do |n|
"user#{n}@example.com"
end

Factory.define :authorized_user, :class => 'user' do |user|
user.email { Factory.next :email }
user.password { "password" }
user.password_confirmation { "password" }
end

This file was deleted.

2 changes: 1 addition & 1 deletion lib/clearance/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def user_from_session
if session[:user_id] && session[:salt]
user = User.find_by_id_and_salt(session[:user_id], session[:salt])
end
user && user.confirmed? ? user : nil
user && user.email_confirmed? ? user : nil
end

def user_from_cookie
Expand Down
2 changes: 1 addition & 1 deletion lib/clearance/app/controllers/confirmations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def new
end

def create
@user.confirm!
@user.confirm_email!
log_user_in(@user)
redirect_to url_after_create
end
Expand Down
8 changes: 4 additions & 4 deletions lib/clearance/app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ def create
if @user.nil?
login_failure
else
if @user.confirmed?
if @user.email_confirmed?
remember(@user) if remember?
log_user_in(@user)
login_successful
else
ClearanceMailer.deliver_confirmation(@user)
deny_access("Account not confirmed. Confirmation email sent.")
deny_access("User has not confirmed email. Confirmation email sent.")
end
end
end
Expand All @@ -35,8 +35,8 @@ def destroy

private

def login_successful
flash[:notice] = "Logged in successfully"
def login_successful(message = "Logged in successfully")
flash[:notice] = message
redirect_back_or url_after_create
end

Expand Down
4 changes: 2 additions & 2 deletions lib/clearance/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def forget_me!
self.update_attribute :remember_token, nil
end

def confirm!
self.update_attribute :confirmed, true
def confirm_email!
self.update_attribute :email_confirmed, true
end

protected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ def self.included(controller_test)

should_filter_params :salt

context "Given a user" do
setup { @user = Factory(:clearance_user) }
context "Given a user whose email has not been confirmed" do
setup { @user = Factory(:authorized_user) }

context "on GET to #new with correct id and salt" do
setup do
get :new, :user_id => @user.to_param, :salt => @user.salt
end

should_be_logged_in_and_confirmed_as { @user }
should_be_logged_in_and_email_confirmed_as { @user }
should_redirect_to_url_after_create
end

Expand Down
2 changes: 1 addition & 1 deletion lib/clearance/test/functional/passwords_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.included(controller_test)
:action => 'edit', :user_id => '1'

context 'with a user' do
setup { @user = Factory(:clearance_user) }
setup { @user = Factory(:authorized_user) }

context 'A GET to #new' do
setup { get :new, :user_id => @user.to_param }
Expand Down
4 changes: 2 additions & 2 deletions lib/clearance/test/functional/sessions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def self.included(controller_test)

context "Given an unconfirmed user" do
setup do
@user = Factory(:clearance_user, :confirmed => false)
@user = Factory(:authorized_user, :email_confirmed => false)
end

context "a POST to #create with good credentials" do
Expand All @@ -53,7 +53,7 @@ def self.included(controller_test)
end

context "Given a confirmed user" do
setup { @user = Factory(:clearance_user, :confirmed => true) }
setup { @user = Factory(:authorized_user, :email_confirmed => true) }

context "a POST to #create with good credentials" do
setup do
Expand Down
2 changes: 1 addition & 1 deletion lib/clearance/test/functional/users_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def self.included(controller_test)

context "on POST to /users" do
setup do
user_attributes = Factory.attributes_for(:clearance_user,
user_attributes = Factory.attributes_for(:authorized_user,
:password => "secret",
:password_confirmation => "secret")
post :create, :user => user_attributes
Expand Down
4 changes: 2 additions & 2 deletions lib/clearance/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ def self.included(test_helper)

def login_as(user = nil)
unless user
user = Factory(:clearance_user)
user.confirm!
user = Factory(:authorized_user)
user.confirm_email!
end
@request.session[:user_id] = user.id
@request.session[:salt] = user.salt
Expand Down
4 changes: 2 additions & 2 deletions lib/clearance/test/unit/clearance_mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def self.included(mailer_test)

context "A change password email" do
setup do
@user = Factory(:clearance_user)
@user = Factory(:authorized_user)
@email = ClearanceMailer.create_change_password @user
end

Expand All @@ -33,7 +33,7 @@ def self.included(mailer_test)

context "A confirmation email" do
setup do
@user = Factory(:clearance_user)
@user = Factory(:authorized_user)
@email = ClearanceMailer.create_confirmation @user
end

Expand Down
54 changes: 20 additions & 34 deletions lib/clearance/test/unit/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def self.included(unit_test)
unit_test.class_eval do

context "a User" do
setup { @user = Factory(:clearance_user) }
setup { @user = Factory(:authorized_user) }

should_require_attributes :email
should_require_unique_attributes :email
Expand All @@ -16,40 +16,26 @@ def self.included(unit_test)
should_not_allow_values_for :email, "example.com"
end

# registering
# registering - confirmation branches
# enrypting the user's password
# not confirming password
# confirming account
# remember me
# forget me
# recovering password
# updating password

context "password is confirmed but encrypted_password is blank" do
setup do
@user = Factory(:clearance_user, :encrypted_password => nil)
end

context "creating a User" do
should_validate_confirmation_of :authorized_user, :password

should "encrypt password" do
assert_not_nil @user.encrypted_password
assert_not_nil Factory(:authorized_user).encrypted_password
end
end

context "password is not confirmed on create" do
setup do
@user = Factory.build(:clearance_user,
:password => "password",
:password_confirmation => "unconfirmed_password")
assert ! @user.save
end

should "raise error on password" do
assert_match(/confirmation/i, @user.errors.on(:password))
end
end


context "password is not confirmed on update" do
setup do
@user = Factory(:clearance_user)
@user = Factory(:authorized_user)
@user.update_attributes(
:password => "password",
:password_confirmation => "unconfirmed_password")
Expand All @@ -61,7 +47,7 @@ def self.included(unit_test)
end

context "An existing User" do
setup { @user = Factory(:clearance_user) }
setup { @user = Factory(:authorized_user) }

context "who changes and confirms password" do
setup do
Expand All @@ -79,7 +65,7 @@ def self.included(unit_test)
@salt = "salt"
User.any_instance.stubs(:initialize_salt)

@user = Factory(:clearance_user, :salt => @salt)
@user = Factory(:authorized_user, :salt => @salt)
@password = @user.password
end

Expand Down Expand Up @@ -111,7 +97,7 @@ def self.included(unit_test)

context "remember_me!" do
setup do
@user = Factory(:clearance_user)
@user = Factory(:authorized_user)
assert_nil @user.remember_token
assert_nil @user.remember_token_expires_at
@user.remember_me!
Expand Down Expand Up @@ -155,7 +141,7 @@ def self.included(unit_test)
@salt = "salt"
User.any_instance.stubs(:initialize_salt)

@user = Factory(:clearance_user, :salt => @salt)
@user = Factory(:authorized_user, :salt => @salt)
@password = @user.password

@user.encrypt(@password)
Expand All @@ -168,20 +154,20 @@ def self.included(unit_test)
end
end

context "An unconfirmed user" do
context "A user who has not confirmed their email" do
setup do
@user = Factory(:clearance_user)
assert ! @user.confirmed?
@user = Factory(:authorized_user)
assert ! @user.email_confirmed?
end

context "after #confirm!" do
context "after #confirm_email!" do
setup do
assert @user.confirm!
assert @user.confirm_email!
@user.reload
end

should "be confirmed" do
assert @user.confirmed?
should "have confirmed their email" do
assert @user.email_confirmed?
end
end
end
Expand Down
Loading

0 comments on commit fbca4ec

Please sign in to comment.