diff --git a/dashboard/app/controllers/admin_search_controller.rb b/dashboard/app/controllers/admin_search_controller.rb index 4b10332269c8a..25b0db164cbcf 100644 --- a/dashboard/app/controllers/admin_search_controller.rb +++ b/dashboard/app/controllers/admin_search_controller.rb @@ -17,7 +17,7 @@ def find_students users = users.where("name LIKE ?", "%#{params[:studentNameFilter]}%") end if params[:studentEmailFilter].present? - hashed_email = Digest::MD5.hexdigest(params[:studentEmailFilter]) + hashed_email = User.hash_email params[:studentEmailFilter] users = users.where(hashed_email: hashed_email) end if params[:teacherNameFilter].present? || params[:teacherEmailFilter].present? @@ -26,8 +26,7 @@ def find_students where("email LIKE ?", "%#{params[:teacherEmailFilter]}%"). all if teachers.count > 1 - # TODO(asher): Display a warning to the admin that multiple teachers - # matched. + flash[:alert] = 'Multiple teachers matched the name and email search criteria.' end if teachers.first array_of_student_ids = teachers.first.students.pluck(:id) @@ -35,8 +34,16 @@ def find_students end end if params[:sectionFilter].present? - array_of_student_ids = Section.find_by_code(params[:sectionFilter]).students.pluck(:id) - users = users.where(id: array_of_student_ids) + section = Section.with_deleted.find_by_code params[:sectionFilter] + if section.nil? + flash[:alert] = 'Section not found.' + elsif section.deleted? + flash[:alert] = 'Section is deleted.' + end + if section + array_of_student_ids = section.students.pluck(:id) + users = users.where(id: array_of_student_ids) + end end @users = users.page(params[:page]).per(MAX_PAGE_SIZE) diff --git a/dashboard/test/controllers/admin_search_controller_test.rb b/dashboard/test/controllers/admin_search_controller_test.rb index 5068b15f3adcc..39d6cf96d336f 100644 --- a/dashboard/test/controllers/admin_search_controller_test.rb +++ b/dashboard/test/controllers/admin_search_controller_test.rb @@ -17,6 +17,40 @@ class AdminSearchControllerTest < ActionController::TestCase 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}