Commit 5e008aa

mo <mo.khan@gmail.com>
2018-09-02 20:29:35
extract navbar.
1 parent 7d29b6c
Changed files (7)
app/controllers/my/mfas_controller.rb
@@ -2,6 +2,10 @@
 
 module My
   class MfasController < ApplicationController
+    def show
+      redirect_to current_user.tfa.setup? ? edit_my_mfa_path : new_my_mfa_path
+    end
+
     def new
       return redirect_to edit_my_mfa_path if current_user.tfa.setup?
       current_user.tfa.build_secret
app/controllers/application_controller.rb
@@ -4,7 +4,7 @@ class ApplicationController < ActionController::Base
   include SamlRespondable
   protect_from_forgery with: :exception
   before_action :authenticate!
-  helper_method :current_user
+  helper_method :current_user, :current_user?
   add_flash_types :error, :warning
 
   def render_error(status, model: nil)
app/views/application/_navbar.html.erb
@@ -0,0 +1,15 @@
+<div class="container">
+  <nav class="navbar navbar-expand-lg navbar-light bg-light">
+    <%= link_to "Proof", my_dashboard_path, class: 'navbar-brand' %>
+    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
+      <span class="navbar-toggler-icon"></span>
+    </button>
+    <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
+      <div class="navbar-nav">
+        <%= link_to "Home", my_dashboard_path, class: 'nav-item nav-link active' %>
+        <%= link_to "MFA", my_mfa_path, class: 'nav-item nav-link' %>
+        <%= button_to "Logout", session_path, method: :delete, class: 'nav-item nav-link' %>
+      </div>
+    </div>
+  </nav>
+</div>
app/views/layouts/application.html.erb
@@ -10,6 +10,7 @@
     <%= stylesheet_pack_tag 'vendor' %>
   </head>
   <body>
+    <%= render partial: 'navbar' if current_user? %>
     <%= render partial: 'flash' %>
     <%= yield %>
   </body>
app/views/my/dashboards/show.html.erb
@@ -2,12 +2,6 @@
   <div class="row">
     <div class="col">
       <h1>Dashboard</h1>
-      <% if current_user.tfa.setup? %>
-        <%= link_to "TFA", edit_my_mfa_path %>
-      <% else %>
-        <%= link_to "Setup TFA", new_my_mfa_path %>
-      <% end %>
-      <%= button_to "Logout", session_path, method: :delete %>
       <table class="table">
         <thead>
           <th>Entity ID</th>
config/routes.rb
@@ -7,7 +7,7 @@ Rails.application.routes.draw do
   resources :registrations, only: [:new, :create]
   namespace :my do
     resource :dashboard, only: [:show]
-    resource :mfa, only: [:new, :edit, :create, :destroy]
+    resource :mfa, only: [:show, :new, :edit, :create, :destroy]
   end
   namespace :scim do
     namespace :v2, defaults: { format: :scim } do
spec/requests/my/mfas_spec.rb
@@ -6,6 +6,19 @@ RSpec.describe '/my/mfa' do
     before { http_login(current_user) }
 
     describe "GET /my/mfa" do
+      context "when MFA is set up" do
+        let(:current_user) { create(:user, :mfa_configured) }
+        before { get '/my/mfa' }
+        specify { expect(response).to redirect_to(edit_my_mfa_path) }
+      end
+
+      context "when MFA is not set up" do
+        before { get '/my/mfa' }
+        specify { expect(response).to redirect_to(new_my_mfa_path) }
+      end
+    end
+
+    describe "GET /my/mfa/new" do
       context "when mfa has not been set up yet" do
         before { get '/my/mfa/new' }