Files
project-work/worker-toolkit-stocks-in-the-future/repo/test/controllers/teachers_controller_test.rb
Eric Bell 2854619bc9 chore: init commit
in worker.../repo/GITFOLDER.zip is the .git folder.
2026-08-11 14:44:09 -04:00

177 lines
4.3 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
class TeachersControllerTest < ActionDispatch::IntegrationTest
test "index" do
admin = create(:admin)
sign_in(admin)
get admin_teachers_path
assert_response :success
end
test "new" do
admin = create(:admin)
sign_in(admin)
get new_admin_teacher_path
assert_response :success
end
test "show" do
teacher = create(:teacher)
admin = create(:admin)
sign_in(admin)
get admin_teacher_path(teacher)
assert_response :success
end
test "edit" do
teacher = create(:teacher)
admin = create(:admin)
sign_in(admin)
get edit_admin_teacher_path(teacher)
assert_response :success
end
test "update" do
teacher = create(:teacher)
admin = create(:admin)
sign_in(admin)
params = { teacher: { name: "Jane Smith" } }
assert_changes "teacher.reload.updated_at" do
patch admin_teacher_path(teacher), params: params
end
assert_redirected_to admin_teacher_path(teacher)
end
test "create creates a teacher, associates classroom, sends reset email, and redirects with notice" do
admin = create(:admin)
classroom = create(:classroom)
sign_in(admin)
ActionMailer::Base.deliveries.clear
params = {
teacher: {
email: "t1@example.com",
username: "teacher1",
classroom_ids: [classroom.id]
}
}
assert_difference %w[Teacher.count ActionMailer::Base.deliveries.size], 1 do
post admin_teachers_path, params: params
end
teacher = Teacher.find_by!(email: "t1@example.com")
assert_redirected_to admin_teacher_path(teacher)
assert_not_nil flash[:notice]
assert_includes teacher.classrooms, classroom
teacher.reload
assert_not_nil teacher.reset_password_sent_at, "reset_password_instructions should be sent"
assert teacher.encrypted_password.present?, "temp password should be set"
end
test "create with no classroom id still creates teacher, and does not associate classroom" do
admin = create(:admin)
sign_in(admin)
ActionMailer::Base.deliveries.clear
params = {
teacher: {
email: "t2@example.com",
username: "teacher2"
}
}
assert_difference %w[Teacher.count ActionMailer::Base.deliveries.size], 1 do
post admin_teachers_path, params: params
end
teacher = Teacher.find_by!(email: "t2@example.com")
assert_redirected_to admin_teacher_path(teacher)
assert_equal 0, teacher.classrooms.count, "no classroom should be associated"
end
test "create sends email with password reset link" do
admin = create(:admin)
sign_in(admin)
params = {
teacher: {
email: "newteacher@example.com",
username: "newteacher"
}
}
emails = capture_emails do
post admin_teachers_path, params: params
end
assert_equal 1, emails.size
email = emails.first
assert_equal ["newteacher@example.com"], email.to
assert_match(/reset_password_token=/, email.body.encoded)
assert_match(/Change my password/, email.body.encoded)
end
test "teacher can set password via reset link from creation email" do
admin = create(:admin)
sign_in(admin)
ActionMailer::Base.deliveries.clear
post admin_teachers_path, params: {
teacher: { email: "resettest@example.com", username: "resettest" }
}
email = ActionMailer::Base.deliveries.last
token = email.body.encoded.match(/reset_password_token=([^"]+)/)[1]
sign_out(admin)
# Teacher follows the link from the email
get edit_user_password_path(reset_password_token: token)
assert_response :success
# Teacher sets a new password
put user_password_path, params: {
user: {
reset_password_token: token,
password: "newpassword123",
password_confirmation: "newpassword123"
}
}
teacher = Teacher.find_by!(email: "resettest@example.com")
assert teacher.valid_password?("newpassword123")
end
test "create with invalid params renders unprocessable_entity and does not create teacher" do
admin = create(:admin)
sign_in(admin)
params = {
teacher: {
email: ""
}
}
assert_no_difference "Teacher.count" do
post admin_teachers_path, params: params
end
assert_response :unprocessable_entity
end
end