Skip to content

Commit 2ab8865

Browse files
committed
refactor: Cleans tests, improves coverage
1 parent 59d54ed commit 2ab8865

22 files changed

+693
-76
lines changed

app/controllers/manage/application_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ def logged_in
99
end
1010

1111
def require_director
12+
return redirect_to manage_root_path if current_user.staff? unless current_user.try(:director?)
1213
return redirect_to root_path unless current_user.try(:director?)
1314
end
1415

1516
def require_director_or_organizer
17+
return redirect_to manage_root_path if current_user.staff? unless current_user.try(:director?) || current_user.try(:organizer?)
1618
return redirect_to root_path unless current_user.try(:director?) || current_user.try(:organizer?)
1719
end
1820

app/controllers/manage/checkins_controller.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class Manage::CheckinsController < Manage::ApplicationController
2-
before_action :require_director_or_organizer
32
before_action :set_questionnaire, only: [:show]
43

54
respond_to :html, :json

app/controllers/manage/dashboard_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class Manage::DashboardController < Manage::ApplicationController
2+
before_action :require_director_or_organizer
23

34
def index
45
end

app/controllers/manage/messages_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class Manage::MessagesController < Manage::ApplicationController
2+
before_action :require_director_or_organizer
23
before_action :set_message, only: [:show, :edit, :update, :destroy, :deliver, :preview, :duplicate]
34
before_action :check_message_access, only: [:edit, :update, :destroy]
45
before_action :limit_template_access_to_directors, only: [:template, :template_preview, :template_update, :template_replace_with_default]

app/controllers/manage/stats_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class Manage::StatsController < Manage::ApplicationController
2+
before_action :require_director_or_organizer
23

34
respond_to :html, :json
45

app/models/user.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ def full_name
5656
"#{first_name} #{last_name}"
5757
end
5858

59+
def staff?
60+
self.director? || self.organizer? || self.volunteer?
61+
end
62+
63+
def organizing_staff?
64+
self.director? || self.organizer?
65+
end
66+
5967
def self.from_omniauth(auth)
6068
matching_provider = where(provider: auth.provider, uid: auth.uid)
6169
matching_email = where(email: auth.info.email)

app/views/layouts/_header.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
= btn_link_to "Home", homepage_url
1111
.header-nav
1212
- if user_signed_in?
13-
- if current_user.director? or current_user.organizer? or current_user.volunteer?
13+
- if current_user.staff?
1414
= btn_link_to "Manage", manage_root_path
1515
= btn_link_to "Sign Out", destroy_user_session_path, method: :delete

app/views/layouts/manage/application.html.haml

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,32 @@
2828
%h6.sidebar-heading.d-flex.justify-content-between.align-items-center.px-3.mt-4.mb-1.text-muted
2929
%span
3030
= t(:overview, scope: 'layouts.manage.navigation')
31-
%ul.nav.flex-column.mb-2
32-
%li.nav-item
33-
= active_link_to manage_root_path, class: "nav-link", active_children: false do
34-
.fa.fa-area-chart.fa-fw.icon-space-r-half
35-
= t(:title, scope: 'pages.manage.dashboard')
36-
%li.nav-item
37-
= active_link_to manage_questionnaires_path, class: "nav-link" do
38-
.fa.fa-inbox.fa-fw.icon-space-r-half
39-
= t(:title, scope: 'pages.manage.questionnaires')
40-
%li.nav-item
41-
= active_link_to manage_messages_path, class: "nav-link" do
42-
.fa.fa-bullhorn.fa-fw.icon-space-r-half
43-
= t(:title, scope: 'pages.manage.messages')
31+
- if current_user.director?
32+
%ul.nav.flex-column.mb-2
33+
%li.nav-item
34+
= active_link_to manage_root_path, class: "nav-link", active_children: false do
35+
.fa.fa-area-chart.fa-fw.icon-space-r-half
36+
= t(:title, scope: 'pages.manage.dashboard')
37+
%li.nav-item
38+
= active_link_to manage_questionnaires_path, class: "nav-link" do
39+
.fa.fa-inbox.fa-fw.icon-space-r-half
40+
= t(:title, scope: 'pages.manage.questionnaires')
41+
%li.nav-item
42+
= active_link_to manage_messages_path, class: "nav-link" do
43+
.fa.fa-bullhorn.fa-fw.icon-space-r-half
44+
= t(:title, scope: 'pages.manage.messages')
45+
- else
46+
%ul.nav.flex-column.mb-2
47+
%li.nav-item
48+
= active_link_to manage_checkins_path, class: "nav-link" do
49+
.fa.fa-drivers-license-o.fa-fw.icon-space-r-half
50+
= t(:title, scope: 'pages.manage.check-in')
51+
%li.nav-item
52+
= active_link_to manage_questionnaires_path, class: "nav-link" do
53+
.fa.fa-inbox.fa-fw.icon-space-r-half
54+
= t(:title, scope: 'pages.manage.questionnaires')
4455
56+
- if current_user.organizing_staff?
4557
%h6.sidebar-heading.d-flex.justify-content-between.align-items-center.px-3.mt-4.mb-1.text-muted
4658
%span
4759
= t(:logistics, scope: 'layouts.manage.navigation')
@@ -59,18 +71,18 @@
5971
.fa.fa-tag.fa-fw.icon-space-r-half
6072
= t(:title, scope: 'pages.manage.trackable-tags')
6173
62-
%h6.sidebar-heading.d-flex.justify-content-between.align-items-center.px-3.mt-4.mb-1.text-muted
63-
%span
64-
= t(:travel, scope: 'layouts.manage.navigation')
65-
%ul.nav.flex-column.mb-2
66-
%li.nav-item
67-
= active_link_to manage_bus_lists_path, class: "nav-link" do
68-
.fa.fa-bus.fa-fw.icon-space-r-half
69-
= t(:title, scope: 'pages.manage.bus-lists')
70-
%li.nav-item
71-
= active_link_to manage_schools_path, class: "nav-link" do
72-
.fa.fa-home.fa-fw.icon-space-r-half
73-
= t(:title, scope: 'pages.manage.schools')
74+
%h6.sidebar-heading.d-flex.justify-content-between.align-items-center.px-3.mt-4.mb-1.text-muted
75+
%span
76+
= t(:travel, scope: 'layouts.manage.navigation')
77+
%ul.nav.flex-column.mb-2
78+
%li.nav-item
79+
= active_link_to manage_bus_lists_path, class: "nav-link" do
80+
.fa.fa-bus.fa-fw.icon-space-r-half
81+
= t(:title, scope: 'pages.manage.bus-lists')
82+
%li.nav-item
83+
= active_link_to manage_schools_path, class: "nav-link" do
84+
.fa.fa-home.fa-fw.icon-space-r-half
85+
= t(:title, scope: 'pages.manage.schools')
7486
7587
- if current_user.director?
7688
%h6.sidebar-heading.d-flex.justify-content-between.align-items-center.px-3.mt-4.mb-1.text-muted

config/routes.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@
3838
end
3939

4040
namespace :manage do
41-
root to: "dashboard#index"
41+
authenticate :user, ->(u) { u.director? } do
42+
root to: "dashboard#index"
43+
end
44+
authenticate :user, ->(u) { u.organizer? } do
45+
root to: "dashboard#index"
46+
end
47+
authenticate :user, ->(u) { u.volunteer? } do
48+
root to: "checkins#index"
49+
end
4250
resources :dashboard do
4351
get :map_data, on: :collection
4452
get :todays_activity_data, on: :collection

test/controllers/manage/bus_lists_controller_test.rb

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,76 @@ class Manage::BusListsControllerTest < ActionController::TestCase
137137
end
138138
end
139139

140+
context "while authenticated as a volunteer" do
141+
setup do
142+
@user = create(:volunteer)
143+
@request.env["devise.mapping"] = Devise.mappings[:user]
144+
sign_in @user
145+
end
146+
147+
should "allow access to manage_bus_lists#index" do
148+
get :index
149+
assert_response :success
150+
end
151+
152+
should "allow access to manage_bus_lists#show" do
153+
get :show, params: { id: @bus_list }
154+
assert_response :success
155+
end
156+
157+
should "not allow access to manage_bus_lists#new" do
158+
get :new
159+
assert_response :redirect
160+
assert_redirected_to manage_bus_lists_path
161+
end
162+
163+
should "not allow access to manage_bus_lists#edit" do
164+
get :edit, params: { id: @bus_list }
165+
assert_response :redirect
166+
assert_redirected_to manage_bus_lists_path
167+
end
168+
169+
should "not allow access to manage_bus_lists#create" do
170+
post :create, params: { bus_list: { email: "[email protected]" } }
171+
assert_response :redirect
172+
assert_redirected_to manage_bus_lists_path
173+
end
174+
175+
should "not allow access to manage_bus_lists#update" do
176+
patch :update, params: { id: @bus_list, bus_list: { email: "[email protected]" } }
177+
assert_response :redirect
178+
assert_redirected_to manage_bus_lists_path
179+
end
180+
181+
should "not allow access to manage_bus_lists#toggle_bus_captain" do
182+
questionnaire = create(:questionnaire)
183+
assert_difference "enqueued_jobs.size", 0 do
184+
patch :toggle_bus_captain, params: { id: @bus_list, questionnaire_id: questionnaire.id, bus_captain: "1" }
185+
end
186+
assert_equal false, questionnaire.reload.is_bus_captain
187+
assert_response :redirect
188+
assert_redirected_to manage_bus_lists_path
189+
end
190+
191+
should "not allow access to manage_bus_lists#send_update_email" do
192+
assert_difference "enqueued_jobs.size", 0 do
193+
patch :send_update_email, params: { id: @bus_list }
194+
end
195+
assert_response :redirect
196+
assert_redirected_to manage_bus_lists_path
197+
end
198+
199+
should "not allow access to manage_bus_lists#destroy" do
200+
patch :destroy, params: { id: @bus_list }
201+
assert_response :redirect
202+
assert_redirected_to manage_bus_lists_path
203+
end
204+
end
205+
140206
context "while authenticated as an organizer" do
141207
setup do
142208
@user = create(:organizer)
143-
@request.env["devise.mapping"] = Devise.mappings[:director]
209+
@request.env["devise.mapping"] = Devise.mappings[:user]
144210
sign_in @user
145211
end
146212

@@ -203,7 +269,7 @@ class Manage::BusListsControllerTest < ActionController::TestCase
203269
end
204270
end
205271

206-
context "while authenticated as an admin" do
272+
context "while authenticated as a director" do
207273
setup do
208274
@user = create(:director)
209275
@request.env["devise.mapping"] = Devise.mappings[:user]

0 commit comments

Comments
 (0)