Ross Kaffenberger
11 years ago
15 changed files with 134 additions and 9 deletions
@ -1,4 +1,5 @@
|
||||
en: |
||||
devise: |
||||
two_factor_authentication: |
||||
success: "Two factor authentication successful." |
||||
attempt_failed: "Attempt failed." |
||||
|
@ -0,0 +1,42 @@
|
||||
require 'spec_helper' |
||||
|
||||
feature "User of two factor authentication" do |
||||
|
||||
scenario "must be logged in" do |
||||
visit user_two_factor_authentication_path |
||||
|
||||
page.should have_content("Welcome Home") |
||||
end |
||||
|
||||
context "when logged in" do |
||||
let(:user) { create_user } |
||||
|
||||
background do |
||||
login_as user |
||||
end |
||||
|
||||
scenario "can fill in TFA code" do |
||||
visit user_two_factor_authentication_path |
||||
|
||||
page.should have_content("Enter your personal code") |
||||
|
||||
fill_in "code", with: user.otp_code |
||||
click_button "Submit" |
||||
|
||||
within(".flash.notice") do |
||||
expect(page).to have_content("Two factor authentication successful.") |
||||
end |
||||
end |
||||
|
||||
scenario "is redirected to TFA when path requires authentication" do |
||||
visit dashboard_path |
||||
|
||||
expect(page).to_not have_content("Your Personal Dashboard") |
||||
|
||||
fill_in "code", with: user.otp_code |
||||
click_button "Submit" |
||||
|
||||
expect(page).to have_content("Your Personal Dashboard") |
||||
end |
||||
end |
||||
end |
@ -1,4 +1,16 @@
|
||||
class HomeController < ApplicationController |
||||
prepend_before_filter :store_location, only: :dashboard |
||||
before_filter :authenticate_user!, only: :dashboard |
||||
|
||||
def index |
||||
end |
||||
|
||||
def dashboard |
||||
end |
||||
|
||||
private |
||||
|
||||
def store_location |
||||
store_location_for(:user, dashboard_path) |
||||
end |
||||
end |
||||
|
@ -0,0 +1,5 @@
|
||||
<h1>Your Personal Dashboard</h1> |
||||
|
||||
<p>Your email is <%= current_user.email %></p> |
||||
|
||||
<p>You will only be able to see this page after successfully completing two factor authentication</p> |
@ -1,2 +1,3 @@
|
||||
<h1>Home#index</h1> |
||||
<h1>Welcome Home</h1> |
||||
|
||||
<p>Find me in app/views/home/index.html.erb</p> |
||||
|
@ -1,18 +1,48 @@
|
||||
module AuthenticatedModelHelper |
||||
|
||||
class UserWithOverrides < User |
||||
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 |
||||
User.new |
||||
POROUser.new |
||||
end |
||||
|
||||
def create_new_user_with_overrides |
||||
UserWithOverrides.new |
||||
end |
||||
|
||||
def create_user(attributes={}) |
||||
User.create!(valid_attributes(attributes)) |
||||
end |
||||
|
||||
def valid_attributes(attributes={}) |
||||
{ |
||||
email: generate_unique_email, |
||||
password: 'password', |
||||
password_confirmation: 'password' |
||||
}.merge(attributes) |
||||
end |
||||
|
||||
def generate_unique_email |
||||
@@email_count ||= 0 |
||||
@@email_count += 1 |
||||
"user#{@@email_count}@example.com" |
||||
end |
||||
|
||||
end |
||||
|
||||
RSpec.configuration.send(:include, AuthenticatedModelHelper) |
||||
|
@ -0,0 +1,9 @@
|
||||
require 'capybara/rspec' |
||||
|
||||
Capybara.app = Dummy::Application |
||||
|
||||
RSpec.configure do |config| |
||||
config.before(:each, :feature) do |
||||
|
||||
end |
||||
end |
@ -0,0 +1,13 @@
|
||||
require 'warden' |
||||
|
||||
module FeaturesSpecHelper |
||||
def warden |
||||
request.env['warden'] |
||||
end |
||||
end |
||||
|
||||
RSpec.configure do |config| |
||||
config.include Warden::Test::Helpers, type: :feature |
||||
config.include FeaturesSpecHelper, type: :feature |
||||
end |
||||
|
Loading…
Reference in new issue