Browse Source

Merge pull request #27 from rossta/integration_spec_for_send_code

Adds integration spec to ensure authentication code is sent on sign in
master
Dmitrii Golub 11 years ago
parent
commit
f20c290f2e
  1. 16
      spec/features/two_factor_authenticatable_spec.rb
  2. 6
      spec/rails_app/app/models/user.rb
  3. 1
      spec/rails_app/config/application.rb
  4. 17
      spec/rails_app/lib/sms_provider.rb
  5. 6
      spec/support/features_spec_helper.rb
  6. 5
      spec/support/sms_provider.rb

16
spec/features/two_factor_authenticatable_spec.rb

@ -1,6 +1,7 @@
require 'spec_helper'
feature "User of two factor authentication" do
let(:user) { create_user }
scenario "must be logged in" do
visit user_two_factor_authentication_path
@ -9,8 +10,21 @@ feature "User of two factor authentication" do
expect(page).to have_content("You are signed out")
end
scenario "sends two factor authentication code after sign in" do
SMSProvider.messages.should be_empty
visit new_user_session_path
complete_sign_in_form_for(user)
expect(page).to have_content "Enter your personal code"
SMSProvider.messages.size.should eq(1)
message = SMSProvider.last_message
expect(message.to).to eq(user.phone_number)
expect(message.body).to eq(user.otp_code)
end
context "when logged in" do
let(:user) { create_user }
background do
login_as user

6
spec/rails_app/app/models/user.rb

@ -6,6 +6,10 @@ class User < ActiveRecord::Base
has_one_time_password
def send_two_factor_authentication_code
# No op
SMSProvider.send_message(to: phone_number, body: otp_code)
end
def phone_number
'14159341234'
end
end

1
spec/rails_app/config/application.rb

@ -16,6 +16,7 @@ module Dummy
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
config.autoload_paths += %W(#{config.root}/lib)
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named.

17
spec/rails_app/lib/sms_provider.rb

@ -0,0 +1,17 @@
require 'ostruct'
class SMSProvider
Message = Class.new(OpenStruct)
class_attribute :messages
self.messages = []
def self.send_message(opts = {})
self.messages << Message.new(opts)
end
def self.last_message
self.messages.last
end
end

6
spec/support/features_spec_helper.rb

@ -4,6 +4,12 @@ module FeaturesSpecHelper
def warden
request.env['warden']
end
def complete_sign_in_form_for(user)
fill_in "Email", with: user.email
fill_in "Password", with: 'password'
click_button "Sign in"
end
end
RSpec.configure do |config|

5
spec/support/sms_provider.rb

@ -0,0 +1,5 @@
RSpec.configure do |c|
c.before(:each) do
SMSProvider.messages.clear
end
end
Loading…
Cancel
Save