-
Notifications
You must be signed in to change notification settings - Fork 482
/
admin_search_controller_test.rb
119 lines (93 loc) · 3.43 KB
/
admin_search_controller_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
require 'test_helper'
class AdminSearchControllerTest < ActionController::TestCase
include Devise::Test::ControllerHelpers
setup do
@admin = create(:admin)
sign_in(@admin)
@not_admin = create(:user, username: 'notadmin')
@teacher = create(:teacher)
@teacher_section = create(:section, user: @teacher)
end
# Confirm the permissioning on these pages is admin-only.
generate_admin_only_tests_for :find_students
generate_admin_only_tests_for :lookup_section
#
# find_student tests
#
test 'find_students flashes warning if multiple teachers match' do
create_list :teacher, 2, name: 'multiple'
get :find_students, params: {teacherNameFilter: 'multiple'}
assert_response :success
assert_select '.container .alert-danger', 'Multiple teachers matched the name and email search criteria.'
end
test 'find_students flashes warning for nil section' do
get :find_students, params: {sectionFilter: 'AAAAAA'}
assert_response :success
assert_select '.container .alert-danger', 'Section not found.'
end
test 'find_students flashes warning for deleted section' do
section = create :section
section.destroy
get :find_students, params: {sectionFilter: section.code}
assert_response :success
assert_select '.container .alert-danger', 'Section is deleted.'
end
#
# undelete_section tests
#
test "undelete_section is admin only" do
sign_in(@not_admin)
post :undelete_section, params: {section_code: @teacher_section.code}
assert_response :forbidden
end
test "undelete_section should undelete deleted section" do
@teacher_section.destroy
post :undelete_section, params: {section_code: @teacher_section.code}
assert_response :redirect
refute @teacher_section.reload.deleted?
assert_equal "Section (CODE: #{@teacher_section.code}) undeleted!",
flash[:alert]
end
test "undelete_section should noop for non-deleted section" do
code = @teacher_section.code
post :undelete_section, params: {section_code: code}
assert_response :redirect
refute @teacher_section.reload.deleted?
assert_equal "Section (CODE: #{code}) not found or undeleted.",
flash[:alert]
end
test "undelete_section should recursively undelete" do
follower = create :follower, section: @teacher_section
follower.destroy
@teacher_section.destroy
assert_creates(Section, Follower) do
assert_no_change("Section.with_deleted.count", "Follower.with_deleted.count") do
post :undelete_section, params: {section_code: @teacher_section.code}
refute follower.reload.deleted?
refute @teacher_section.reload.deleted?
end
end
end
#
# lookup_section tests
#
test "should lookup_section" do
post :lookup_section, params: {section_code: @teacher_section.code}
assert_select '#section_owner', 'Owner: ' + @teacher.email
end
test "should lookup_section error if not found" do
post :lookup_section, params: {section_code: 'ZZZZ'}
assert_response :success
assert_select '.container .alert-danger', 'Section code not found'
end
test "should not lookup_section if not admin" do
sign_in @not_admin
post :lookup_section, params: {section_code: @teacher_section.code}
assert_response :forbidden
end
test "should not lookup_section if not signed in" do
sign_out @admin
post :lookup_section, params: {section_code: @teacher_section.code}
assert_redirected_to_sign_in
end
end