Created
February 8, 2012 16:37
-
-
Save cgriego/1770903 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe LoginRequest do | |
it_should_behave_like "ActiveModel" | |
it { should_not be_persisted } | |
describe "email" do | |
it { should respond_to :email } | |
it { should respond_to :email= } | |
it { should_not allow_value(nil).for(:email) } | |
end | |
describe "password" do | |
it { should respond_to :password } | |
it { should respond_to :password= } | |
it { should_not allow_value(nil).for(:password) } | |
it { should_not allow_value("1234").for(:password).with_message(/is too short/) } | |
it "with a blank password should only return one error" do | |
subject.valid? | |
subject.errors[:password].should have(1).item | |
end | |
end | |
describe "remember_me" do | |
it { should respond_to :remember_me } | |
it { should respond_to :remember_me= } | |
it { subject.remember_me.should be_true } | |
it { expect { subject.remember_me = "" }.to change { subject.remember_me? }.from(true).to(false) } | |
end | |
describe "#initialize" do | |
it "mass assigns email" do | |
described_class.new(:email => "[email protected]").email.should == "[email protected]" | |
end | |
it "mass assigns password" do | |
described_class.new(:password => "12345").password.should == "12345" | |
end | |
it "mass assigns remember_me" do | |
described_class.new(:remember_me => false).remember_me.should == false | |
end | |
it "doesn't raise if it receives an unknown attribute" do | |
expect { described_class.new(:unknown => "attribute") }.not_to raise_error | |
end | |
end | |
describe "#authenticate" do | |
context "when the record is invalid" do | |
before { subject.stub(:valid?).and_return(false) } | |
it { subject.authenticate.should be_false } | |
it "logs a warning about the failed login" do | |
Rails.logger.should_receive(:warn).with(/Failed signin/) | |
subject.authenticate | |
end | |
end | |
context "when the record is valid" do | |
before { subject.stub(:valid?).and_return(true) } | |
it { subject.authenticate.should be_true } | |
it "does not log a warning" do | |
Rails.logger.should_not_receive(:warn) | |
subject.authenticate | |
end | |
end | |
end | |
describe "#user" do | |
subject { described_class.new(:email => email, :password => password) } | |
let(:email) { FactoryGirl.generate(:email) } | |
let(:password) { FactoryGirl.build(:user).password } | |
context "when the user exists" do | |
let(:user) { FactoryGirl.build(:user, :email => email, :password => password) } | |
it "asks the User repository for the user" do | |
User.stub(:authenticate).with(email, password).and_return(user) | |
subject.user.should == user | |
end | |
it "memoizes the result" do | |
User.should_receive(:authenticate).once.and_return(user) | |
2.times { subject.user } | |
end | |
end | |
context "when the user does not exist" do | |
it "returns nil" do | |
User.stub(:authenticate).with(email, password).and_return(nil) | |
subject.user.should be_nil | |
end | |
it "memoizes the result" do | |
User.should_receive(:authenticate).once.and_return(nil) | |
2.times { subject.user } | |
end | |
end | |
end | |
describe "#valid?" do | |
context "a fully populated login request" do | |
subject { described_class.new(:email => email, :password => password) } | |
let(:email) { FactoryGirl.generate(:email) } | |
let(:password) { FactoryGirl.build(:user).password } | |
context "when the user exists" do | |
before { User.stub(:authenticate).with(email, password).and_return(user) } | |
let(:user) { FactoryGirl.build(:user, :email => email, :password => password) } | |
it { should be_valid } | |
end | |
context "when the user does not exist" do | |
before { User.stub(:authenticate).with(email, password).and_return(nil) } | |
it { should_not be_valid } | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment