lib/generators/authkit/templates/spec/models/user_spec.rb in authkit-0.4.0 vs lib/generators/authkit/templates/spec/models/user_spec.rb in authkit-0.5.0

- old
+ new

@@ -1,264 +1,258 @@ -require 'spec_helper' +require 'rails_helper' describe User do let(:user_params) { attributes_for(:user) } it "has secure password support" do - User.new.should respond_to(:authenticate) + expect(User.new).to respond_to(:authenticate) end it "has one time password support" do - User.new.should respond_to(:otp_secret_key) + expect(User.new).to respond_to(:otp_secret_key) end it "accepts a password confirmation" do - User.new.should respond_to(:password_confirmation=) + expect(User.new).to respond_to(:password_confirmation=) end it "downcases the email address" do user = User.new user.email = "SIR@CAPSALOCK.COM" user.valid? - user.email.should == "sir@capsalock.com" + expect(user.email).to eq("sir@capsalock.com") end - it "knows if the password was set" do - user = User.new - user.send(:password_set?).should == false - user.password = "example" - user.send(:password_set?).should == true - end - describe "validations" do describe "unique" do before(:each) do create(:user) end - it { should validate_uniqueness_of(:username) } - it { should validate_uniqueness_of(:email) } + <% if username? %>it { should validate_uniqueness_of(:username) } + <% end %>it { should validate_uniqueness_of(:email) } it "validates the uniqueness of the the confirmation email" do - user = User.new(user_params.merge(email: "old@example.com", username: "old")) + existing_user = create(:user) + user = build(:user, email: "old@example.com") user.confirmation_email = "new@example.com" - user.should be_valid - user.confirmation_email = "test@example.com" - user.should_not be_valid + expect(user).to be_valid + user.confirmation_email = existing_user.email + expect(user).to_not be_valid end end it { should validate_presence_of(:confirmation_email) } - it { should validate_presence_of(:username) } - it { should validate_presence_of(:password) } + <% if username? %>it { should validate_presence_of(:username) } + <% end %>it { should validate_presence_of(:password) } it { should validate_confirmation_of(:password) } end describe "tokens" do it "sets the remember token" do user = User.new - user.should_receive(:save!).and_return(true) + expect(user).to receive(:save!).and_return(true) user.set_remember_token - user.remember_token.should_not be_blank - user.remember_token_created_at.should_not be_blank + expect(user.remember_token).to_not be_blank + expect(user.remember_token_created_at).to_not be_blank end it "clears the remember token" do user = User.new - user.should_receive(:save!).and_return(true) + expect(user).to receive(:save!).and_return(true) user.remember_token = "TOKEN" user.remember_token_created_at = Time.now user.clear_remember_token - user.remember_token.should be_nil - user.remember_token_created_at.should be_nil + expect(user.remember_token).to be_nil + expect(user.remember_token_created_at).to be_nil end end describe "token expiry" do it "should expire reset password tokens" do user = User.new - user.reset_password_token_expired?.should == true + expect(user.reset_password_token_expired?).to eq(true) user.reset_password_token_created_at = 10.minutes.ago - user.reset_password_token_expired?.should == false + expect(user.reset_password_token_expired?).to eq(false) user.reset_password_token_created_at = 1.day.ago - user.reset_password_token_expired?.should == true + expect(user.reset_password_token_expired?).to eq(true) end it "should expire confirmation tokens" do user = User.new - user.confirmation_token_expired?.should == true + expect(user.confirmation_token_expired?).to eq(true) user.confirmation_token_created_at = 2.days.ago - user.confirmation_token_expired?.should == false + expect(user.confirmation_token_expired?).to eq(false) user.confirmation_token_created_at = 3.days.ago - user.confirmation_token_expired?.should == true + expect(user.confirmation_token_expired?).to eq(true) end it "should expire remember tokens" do user = User.new - user.remember_token_expired?.should == true + expect(user.remember_token_expired?).to eq(true) user.remember_token_created_at = 30.days.ago - user.remember_token_expired?.should == false + expect(user.remember_token_expired?).to eq(false) user.remember_token_created_at = 1.years.ago - user.remember_token_expired?.should == true + expect(user.remember_token_expired?).to eq(true) end end describe "display name" do it "has a display name" do user = User.new(first_name: "Boss", last_name: "Hogg") - user.display_name.should == "Boss Hogg" + expect(user.full_name).to eq("Boss Hogg") user.first_name = nil - user.display_name.should == "Hogg" + expect(user.full_name).to eq("Hogg") end end describe "tracking" do let(:user) do user = User.new - user.should_receive(:save).and_return(true) + expect(user).to receive(:save).and_return(true) user end it "tracks sign in count" do expect { user.track_sign_in(nil) }.to change(user, :sign_in_count).by(1) end it "tracks current sign in" do - Time.stub(:now).and_return(time = Time.now) + allow(Time).to receive(:now).and_return(time = Time.now) user.track_sign_in(nil) - user.current_sign_in_at.should == time + expect(user.current_sign_in_at).to eq(time) end it "tracks last sign in" do time = Time.now user.current_sign_in_at = time user.track_sign_in(nil) - user.last_sign_in_at.should == time + expect(user.last_sign_in_at).to eq(time) end it "tracks current and last ip" do user.track_sign_in(ip = "123.456.789.001") - user.current_sign_in_ip.should == ip + expect(user.current_sign_in_ip).to eq(ip) end it "tracks current and last ip" do ip = "123.456.789.001" user.current_sign_in_ip = ip user.track_sign_in(nil) - user.last_sign_in_ip.should == ip + expect(user.last_sign_in_ip).to eq(ip) end end describe "emails" do let(:user) { build(:user) } describe "with valid params" do it "confirms the email" do user = User.new - user.should_receive(:save!).and_return(true) - Time.stub(:now).and_return(time = Time.now) + expect(user).to receive(:save!).and_return(true) + allow(Time).to receive(:now).and_return(time = Time.now) user.send_confirmation - user.confirmation_token_created_at.should == time - user.confirmation_token.should_not be_blank + expect(user.confirmation_token_created_at).to eq(time) + expect(user.confirmation_token).to_not be_blank end it "generates a token before it sends confirmation email instructions" do user = User.new - user.should_receive(:save!).and_return(true) + expect(user).to receive(:save!).and_return(true) user.send_confirmation - user.confirmation_token.should_not be_blank - user.confirmation_token_created_at.should_not be_blank + expect(user.confirmation_token).to_not be_blank + expect(user.confirmation_token_created_at).to_not be_blank end it "sends confirmation email instructions" do user = User.new - user.should_receive(:save!).and_return(true) + expect(user).to receive(:save!).and_return(true) user.send_confirmation end it "handles confirmed emails" do - user.should_receive(:save).and_return(true) + expect(user).to receive(:save).and_return(true) user.confirmation_email = "new@example.com" user.confirmation_token = "TOKEN" - user.email_confirmed.should == true - user.confirmation_email.should == user.email - user.confirmation_token.should be_nil - user.confirmation_token_created_at.should be_nil - user.email.should == "new@example.com" + expect(user.email_confirmed).to eq(true) + expect(user.confirmation_email).to eq(user.email) + expect(user.confirmation_token).to be_nil + expect(user.confirmation_token_created_at).to be_nil + expect(user.email).to eq("new@example.com") end end it "does not confirm if there is no confirmation token" do user.confirmation_email = "new@example.com" user.confirmation_token = nil - user.email_confirmed.should == false + expect(user.email_confirmed).to eq(false) end it "does not confirm if there is no confirmation email" do user.confirmation_email = "" user.confirmation_token = "TOKEN" - user.email_confirmed.should == false + expect(user.email_confirmed).to eq(false) end it "does not confirm emails if they are already used" do - create(:user, email: "new@example.com", username: "newuser") + create(:user, email: "new@example.com") user.confirmation_email = "new@example.com" user.confirmation_token = "TOKEN" - user.email_confirmed.should == false - user.should have(1).errors_on(:email) + expect(user.email_confirmed).to eq(false) + expect(user.errors[:email].size).to eq(1) end it "is pending confirmation if there is a confirmation token" do user = build(:user, confirmation_token: "TOKEN") - user.should be_pending_confirmation + expect(user).to be_pending_confirmation end it "there is no pending confirmation if there is not a confirmation token" do user = build(:user, confirmation_token: nil) - user.should_not be_pending_confirmation + expect(user).to_not be_pending_confirmation end end describe "passwords" do it "changes the password if it matches" do user = build(:user) - user.should_receive(:save).and_return(true) + expect(user).to receive(:save).and_return(true) user.change_password("password", "password") - user.password_digest.should_not be_blank - user.remember_token.should be_nil - user.remember_token_created_at.should be_nil + expect(user.password_digest).to_not be_blank + expect(user.remember_token).to be_nil + expect(user.remember_token_created_at).to be_nil end it "doesn't change the password if it doesn't match" do user = User.new user.remember_token = "token" user.change_password("password", "typotypo") - user.should_not be_valid - user.remember_token.should == "token" + expect(user).to_not be_valid + expect(user.remember_token).to eq("token") end it "resets the password" do user = User.new - user.should_receive(:save!).and_return(true) - Time.stub(:now).and_return(time = Time.now) + expect(user).to receive(:save!).and_return(true) + allow(Time).to receive(:now).and_return(time = Time.now) user.send_reset_password - user.reset_password_token_created_at.should == time - user.reset_password_token.should_not be_blank + expect(user.reset_password_token_created_at).to eq(time) + expect(user.reset_password_token).to_not be_blank end it "generates a token before it sends reset password instructions" do user = User.new - user.should_receive(:save!).and_return(true) + expect(user).to receive(:save!).and_return(true) user.send_reset_password - user.reset_password_token.should_not be_blank - user.reset_password_token_created_at.should_not be_blank + expect(user.reset_password_token).to_not be_blank + expect(user.reset_password_token_created_at).to_not be_blank end it "sends reset password instructions" do user = User.new - user.should_receive(:save!).and_return(true) + expect(user).to receive(:save!).and_return(true) user.send_reset_password end end end