diff --git a/app/controllers/devise/two_factor_authentication_controller.rb b/app/controllers/devise/two_factor_authentication_controller.rb index 5f5b4b4..05c6027 100644 --- a/app/controllers/devise/two_factor_authentication_controller.rb +++ b/app/controllers/devise/two_factor_authentication_controller.rb @@ -17,10 +17,10 @@ class Devise::TwoFactorAuthenticationController < DeviseController else resource.second_factor_attempts_count += 1 resource.save - set_flash_message :error, :attempt_failed + flash.now[:error] = find_message(:attempt_failed) if resource.max_login_attempts? sign_out(resource) - render :template => 'devise/two_factor_authentication/max_login_attempts_reached' and return + render :max_login_attempts_reached else render :show end @@ -37,6 +37,7 @@ class Devise::TwoFactorAuthenticationController < DeviseController redirect_to :root and return if resource.nil? @limit = resource.class.max_login_attempts if resource.max_login_attempts? + binding.pry sign_out(resource) render :template => 'devise/two_factor_authentication/max_login_attempts_reached' and return end diff --git a/spec/features/two_factor_authenticatable_spec.rb b/spec/features/two_factor_authenticatable_spec.rb index b16f6fb..899cd5b 100644 --- a/spec/features/two_factor_authenticatable_spec.rb +++ b/spec/features/two_factor_authenticatable_spec.rb @@ -5,7 +5,8 @@ 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") + expect(page).to have_content("Welcome Home") + expect(page).to have_content("You are signed out") end context "when logged in" do @@ -18,7 +19,8 @@ feature "User of two factor authentication" do scenario "can fill in TFA code" do visit user_two_factor_authentication_path - page.should have_content("Enter your personal code") + expect(page).to have_content("You are signed in as Marissa") + expect(page).to have_content("Enter your personal code") fill_in "code", with: user.otp_code click_button "Submit" @@ -37,6 +39,23 @@ feature "User of two factor authentication" do click_button "Submit" expect(page).to have_content("Your Personal Dashboard") + expect(page).to have_content("You are signed in as Marissa") + end + + scenario "is locked out after 3 failed attempts" do + visit user_two_factor_authentication_path + + 3.times do + fill_in "code", with: "incorrect#{rand(100)}" + click_button "Submit" + + within(".flash.error") do + expect(page).to have_content("Attempt failed") + end + end + + expect(page).to have_content("Access completely denied") + expect(page).to have_content("You are signed out") end end end diff --git a/spec/rails_app/app/helpers/application_helper.rb b/spec/rails_app/app/helpers/application_helper.rb index de6be79..b5e2e4e 100644 --- a/spec/rails_app/app/helpers/application_helper.rb +++ b/spec/rails_app/app/helpers/application_helper.rb @@ -1,2 +1,8 @@ module ApplicationHelper + + def render_flash + flash.map do |name, message| + content_tag(:p, message, class: "flash #{name}") + end.join.html_safe + end end diff --git a/spec/rails_app/app/views/home/dashboard.html.erb b/spec/rails_app/app/views/home/dashboard.html.erb index d48f903..13e63b8 100644 --- a/spec/rails_app/app/views/home/dashboard.html.erb +++ b/spec/rails_app/app/views/home/dashboard.html.erb @@ -1,5 +1,7 @@

Your Personal Dashboard

-

Your email is <%= current_user.email %>

+

Hi <%= current_user.nickname %>

-

You will only be able to see this page after successfully completing two factor authentication

+

Your registered email address is <%= current_user.email %>

+ +

You can only see this page after successfully completing two factor authentication

diff --git a/spec/rails_app/app/views/layouts/application.html.erb b/spec/rails_app/app/views/layouts/application.html.erb index 8d56308..5d58281 100644 --- a/spec/rails_app/app/views/layouts/application.html.erb +++ b/spec/rails_app/app/views/layouts/application.html.erb @@ -7,8 +7,14 @@ <%= csrf_meta_tags %> -

<%= notice %>

-

<%= alert %>

+ + <%= render_flash %> <%= yield %> diff --git a/spec/rails_app/config/database.yml b/spec/rails_app/config/database.yml index 51a4dd4..1902f92 100644 --- a/spec/rails_app/config/database.yml +++ b/spec/rails_app/config/database.yml @@ -17,9 +17,3 @@ test: database: db/test.sqlite3 pool: 5 timeout: 5000 - -production: - adapter: sqlite3 - database: db/production.sqlite3 - pool: 5 - timeout: 5000 diff --git a/spec/rails_app/db/migrate/20140407215513_add_nickanme_to_users.rb b/spec/rails_app/db/migrate/20140407215513_add_nickanme_to_users.rb new file mode 100644 index 0000000..ee3fa8f --- /dev/null +++ b/spec/rails_app/db/migrate/20140407215513_add_nickanme_to_users.rb @@ -0,0 +1,7 @@ +class AddNickanmeToUsers < ActiveRecord::Migration + def change + change_table :users do |t| + t.column :nickname, :string, limit: 64 + end + end +end diff --git a/spec/rails_app/db/schema.rb b/spec/rails_app/db/schema.rb index e378f2b..76bbd49 100644 --- a/spec/rails_app/db/schema.rb +++ b/spec/rails_app/db/schema.rb @@ -11,23 +11,24 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20140407172619) do +ActiveRecord::Schema.define(:version => 20140407215513) do create_table "users", :force => true do |t| - t.string "email", :default => "", :null => false - t.string "encrypted_password", :default => "", :null => false + t.string "email", :default => "", :null => false + t.string "encrypted_password", :default => "", :null => false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" - t.integer "sign_in_count", :default => 0, :null => false + t.integer "sign_in_count", :default => 0, :null => false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.string "otp_secret_key" - t.integer "second_factor_attempts_count", :default => 0 + t.integer "second_factor_attempts_count", :default => 0 + t.string "nickname", :limit => 64 end add_index "users", ["email"], :name => "index_users_on_email", :unique => true diff --git a/spec/support/authenticated_model_helper.rb b/spec/support/authenticated_model_helper.rb index 7195d97..d6205a3 100644 --- a/spec/support/authenticated_model_helper.rb +++ b/spec/support/authenticated_model_helper.rb @@ -10,6 +10,7 @@ module AuthenticatedModelHelper def valid_attributes(attributes={}) { + nickname: 'Marissa', email: generate_unique_email, password: 'password', password_confirmation: 'password' diff --git a/spec/support/capybara.rb b/spec/support/capybara.rb index c112d19..c5eff30 100644 --- a/spec/support/capybara.rb +++ b/spec/support/capybara.rb @@ -1,9 +1,3 @@ require 'capybara/rspec' Capybara.app = Dummy::Application - -RSpec.configure do |config| - config.before(:each, :feature) do - - end -end