From 957e6b144b1092c2e894beb8faf03590c234a327 Mon Sep 17 00:00:00 2001 From: Ross Kaffenberger Date: Mon, 7 Apr 2014 17:08:30 -0400 Subject: [PATCH] extract GuestUser for unit specs --- .../models/two_factor_authenticatable_spec.rb | 22 ++++++++-------- spec/rails_app/app/models/guest_user.rb | 10 ++++++++ spec/support/authenticated_model_helper.rb | 25 ++----------------- 3 files changed, 24 insertions(+), 33 deletions(-) create mode 100644 spec/rails_app/app/models/guest_user.rb diff --git a/spec/lib/two_factor_authentication/models/two_factor_authenticatable_spec.rb b/spec/lib/two_factor_authentication/models/two_factor_authenticatable_spec.rb index ea529e0..efa8e37 100644 --- a/spec/lib/two_factor_authentication/models/two_factor_authenticatable_spec.rb +++ b/spec/lib/two_factor_authentication/models/two_factor_authenticatable_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' include AuthenticatedModelHelper describe Devise::Models::TwoFactorAuthenticatable, '#otp_code' do - let(:instance) { AuthenticatedModelHelper.create_new_user } + let(:instance) { build_guest_user } subject { instance.otp_code(time) } let(:time) { 1392852456 } @@ -32,7 +32,7 @@ describe Devise::Models::TwoFactorAuthenticatable, '#otp_code' do end describe Devise::Models::TwoFactorAuthenticatable, '#authenticate_otp' do - let(:instance) { AuthenticatedModelHelper.create_new_user } + let(:instance) { build_guest_user } before :each do instance.otp_secret_key = "2z6hxkdwi3uvrnpn" @@ -54,22 +54,24 @@ describe Devise::Models::TwoFactorAuthenticatable, '#authenticate_otp' do end describe Devise::Models::TwoFactorAuthenticatable, '#send_two_factor_authentication_code' do + let(:instance) { build_guest_user } it "should raise an error by default" do - instance = AuthenticatedModelHelper.create_new_user expect { instance.send_two_factor_authentication_code }.to raise_error(NotImplementedError) end it "should be overrideable" do - instance = AuthenticatedModelHelper.create_new_user_with_overrides + def instance.send_two_factor_authentication_code + "Code sent" + end expect(instance.send_two_factor_authentication_code).to eq("Code sent") end end describe Devise::Models::TwoFactorAuthenticatable, '#provisioning_uri' do - let(:instance) { AuthenticatedModelHelper.create_new_user } + let(:instance) { build_guest_user } before do instance.email = "houdini@example.com" @@ -99,7 +101,7 @@ describe Devise::Models::TwoFactorAuthenticatable, '#provisioning_uri' do end describe Devise::Models::TwoFactorAuthenticatable, '#populate_otp_column' do - let(:instance) { AuthenticatedModelHelper.create_new_user } + let(:instance) { build_guest_user } it "populates otp_column on create" do expect(instance.otp_secret_key).to be_nil @@ -121,14 +123,14 @@ describe Devise::Models::TwoFactorAuthenticatable, '#populate_otp_column' do end describe Devise::Models::TwoFactorAuthenticatable, '#max_login_attempts' do - let(:instance) { AuthenticatedModelHelper.create_new_user } + let(:instance) { build_guest_user } before do - @original_max_login_attempts = User.max_login_attempts - User.max_login_attempts = 3 + @original_max_login_attempts = GuestUser.max_login_attempts + GuestUser.max_login_attempts = 3 end - after { User.max_login_attempts = @original_max_login_attempts } + after { GuestUser.max_login_attempts = @original_max_login_attempts } it "returns class setting" do expect(instance.max_login_attempts).to eq(3) diff --git a/spec/rails_app/app/models/guest_user.rb b/spec/rails_app/app/models/guest_user.rb new file mode 100644 index 0000000..84049fe --- /dev/null +++ b/spec/rails_app/app/models/guest_user.rb @@ -0,0 +1,10 @@ +class GuestUser + extend ActiveModel::Callbacks + include ActiveModel::Validations + include Devise::Models::TwoFactorAuthenticatable + + define_model_callbacks :create + attr_accessor :otp_secret_key, :email, :second_factor_attempts_count + + has_one_time_password +end diff --git a/spec/support/authenticated_model_helper.rb b/spec/support/authenticated_model_helper.rb index b469ea6..7195d97 100644 --- a/spec/support/authenticated_model_helper.rb +++ b/spec/support/authenticated_model_helper.rb @@ -1,28 +1,7 @@ module AuthenticatedModelHelper - class POROUser - extend ActiveModel::Callbacks - include ActiveModel::Validations - include Devise::Models::TwoFactorAuthenticatable - - define_model_callbacks :create - attr_accessor :otp_secret_key, :email, :second_factor_attempts_count - - has_one_time_password - end - - class UserWithOverrides < POROUser - def send_two_factor_authentication_code - "Code sent" - end - end - - def create_new_user - POROUser.new - end - - def create_new_user_with_overrides - UserWithOverrides.new + def build_guest_user + GuestUser.new end def create_user(attributes={})