From d3b2f5e8ffb6e00cdf612fcc9f6a8d8ad76e2db1 Mon Sep 17 00:00:00 2001 From: Michael Ball Date: Mon, 12 Feb 2024 11:02:00 -0800 Subject: [PATCH 01/37] Allow manually triggering specs --- .github/workflows/specs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/specs.yml b/.github/workflows/specs.yml index 5f2f6ec8..970f1f8c 100644 --- a/.github/workflows/specs.yml +++ b/.github/workflows/specs.yml @@ -1,6 +1,6 @@ name: All Specs -on: [push, pull_request] +on: [push, pull_request, workflow_dispatch] jobs: specs: From c343618c1e4624d97df3b96d139e2835451f1cec Mon Sep 17 00:00:00 2001 From: Arush Chhatrapati Date: Wed, 14 Feb 2024 21:16:49 -0800 Subject: [PATCH 02/37] change reason to denial reason --- app/controllers/pages_controller.rb | 2 +- app/controllers/teachers_controller.rb | 4 ++-- app/mailers/teacher_mailer.rb | 10 +++++----- db/seed_data.rb | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 54df8c71..87ad89cd 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -94,7 +94,7 @@ def page_params # teacher_snap: @teacher.snap, # teacher_school_website: @teacher.school.website, # piazza_password: Rails.application.secrets[:piazza_password], - # reason: @reason + # denial_reason: @denial_reason # }.with_indifferent_access # end diff --git a/app/controllers/teachers_controller.rb b/app/controllers/teachers_controller.rb index e178e976..cbcf35ba 100644 --- a/app/controllers/teachers_controller.rb +++ b/app/controllers/teachers_controller.rb @@ -130,7 +130,7 @@ def update def request_info @teacher.info_needed! if !params[:skip_email].present? - TeacherMailer.request_info_email(@teacher, params[:reason]).deliver_now + TeacherMailer.request_info_email(@teacher, params[:denial_reason]).deliver_now end redirect_to root_path end @@ -146,7 +146,7 @@ def deny @teacher.denied! if !params[:skip_email].present? # TODO: Update dropdown to select the email template. - TeacherMailer.deny_email(@teacher, params[:reason]).deliver_now + TeacherMailer.deny_email(@teacher, params[:denial_reason]).deliver_now end redirect_to root_path end diff --git a/app/mailers/teacher_mailer.rb b/app/mailers/teacher_mailer.rb index fe1904ec..4acb1634 100644 --- a/app/mailers/teacher_mailer.rb +++ b/app/mailers/teacher_mailer.rb @@ -16,18 +16,18 @@ def welcome_email(teacher) subject: email_template.subject end - def deny_email(teacher, reason) + def deny_email(teacher, denial_reason) @teacher = teacher - @reason = reason + @denial_reason = denial_reason set_body mail to: @teacher.email_name, cc: CONTACT_EMAIL, subject: email_template.subject end - def request_info_email(teacher, reason) + def request_info_email(teacher, request_reason) @teacher = teacher - @reason = reason + @request_reason = request_reason set_body mail to: @teacher.email_name, cc: CONTACT_EMAIL, @@ -48,7 +48,7 @@ def liquid_assigns base_rules = { bjc_password: Rails.application.secrets[:bjc_password], piazza_password: Rails.application.secrets[:piazza_password], - reason: @reason + denial_reason: @denial_reason } base_rules.merge!(@teacher.email_attributes) base_rules.with_indifferent_access diff --git a/db/seed_data.rb b/db/seed_data.rb index f08a9157..deccbc0d 100644 --- a/db/seed_data.rb +++ b/db/seed_data.rb @@ -27,7 +27,7 @@ module SeedData @basic_email_with_reason = <<-DENY_EMAIL

- {{ reason | strip_tags }} + {{ denial_reason | strip_tags }}

DENY_EMAIL @@ -38,7 +38,7 @@ module SeedData

[Your Name]

Below, you can find the reason as to why it was rejected

- {{ reason | strip_tags}} + {{ denial_reason | strip_tags}}

DENY_EMAIL1 From 754d4c7b847f983d53f4b920e3206df5a43e8159 Mon Sep 17 00:00:00 2001 From: Arush Chhatrapati Date: Wed, 14 Feb 2024 21:27:41 -0800 Subject: [PATCH 03/37] updated relevant deny cucumber test to use denial_reason instead of reason --- app/views/email_templates/_liquid_fields.erb | 2 +- app/views/main/_deny_modal.html.erb | 6 +++--- features/admin.feature | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/views/email_templates/_liquid_fields.erb b/app/views/email_templates/_liquid_fields.erb index d1726afa..715f93d6 100644 --- a/app/views/email_templates/_liquid_fields.erb +++ b/app/views/email_templates/_liquid_fields.erb @@ -24,7 +24,7 @@ (The current piazza sign up password) - {{reason}} + {{denial_reason}} (Only on denial emails.) diff --git a/app/views/main/_deny_modal.html.erb b/app/views/main/_deny_modal.html.erb index cf8b56f7..fa76cc00 100644 --- a/app/views/main/_deny_modal.html.erb +++ b/app/views/main/_deny_modal.html.erb @@ -7,8 +7,8 @@ <%= form_tag deny_teacher_path(0), method: :post, class: "form-horizontal" do %> diff --git a/app/views/main/dashboard.html.erb b/app/views/main/dashboard.html.erb index ca4d6840..bb83db7c 100644 --- a/app/views/main/dashboard.html.erb +++ b/app/views/main/dashboard.html.erb @@ -1,44 +1,44 @@ <% provide(:title, "BJC Teacher Dashboard") %>

New Requests

- - - - <%= render 'teachers/table_headers' %> - - - - - <% @unreviewed_teachers.each do |teacher| %> - - <%= render 'teachers/teacher', teacher: teacher %> - + <% end %> + +
Actions
-
- <%= button_to("✔️", validate_teacher_path(teacher.id), - class: 'btn-group btn btn-outline-success', type: 'button') %> - + + + + <%= render 'teachers/table_headers' %> + + + + + <% @unreviewed_teachers.each do |teacher| %> + + <%= render 'teachers/teacher', teacher: teacher %> + - - <% end %> - -
Actions
+
+ <%= button_to("✔️", validate_teacher_path(teacher.id), + class: 'btn-group btn btn-outline-success', type: 'button') %> + - + -
-
- <% if @unreviewed_teachers.empty? %> - +
+
+ <% if @unreviewed_teachers.empty? %> + + <% end %>
<%= render 'deny_modal' %> @@ -52,65 +52,65 @@

Course Statistics

- + - + - <% @statuses.each do |key, value| %> + <% @statuses.each do |key, value| %> - <% end %> + <% end %> -
Status Count
<%= key.humanize.titlecase %> <%= value %>
+

School Statistics

- + - + - <% @schools.each do |school| %> + <% @schools.each do |school| %> - <% end %> + <% end %>
School Location Teachers
<%= link_to(school.name, school_path(school)) %> <%= school.location %> <%= school.teachers_count %>
diff --git a/features/admin.feature b/features/admin.feature index 15bae961..3c066a9f 100644 --- a/features/admin.feature +++ b/features/admin.feature @@ -4,34 +4,34 @@ Feature: basic admin functionality So that I can see how many people are teaching BJC I can login in an see the dashboard -Background: Has an Admin in DB - Given the following teachers exist: - | first_name | last_name | admin | email | - | Joseph | Mamoa | true | testadminuser@berkeley.edu | + Background: Has an Admin in DB + Given the following teachers exist: + | first_name | last_name | admin | email | + | Joseph | Mamoa | true | testadminuser@berkeley.edu | -Scenario: Logging in as an admin - Given I am on the BJC home page - Given I have an admin email - And I follow "Log In" - Then I can log in with Google - And I should see "BJC Teacher Dashboard" + Scenario: Logging in as an admin + Given I am on the BJC home page + Given I have an admin email + And I follow "Log In" + Then I can log in with Google + And I should see "BJC Teacher Dashboard" -Scenario: Logging out as an admin - Given I am on the BJC home page - Given I have an admin email - And I follow "Log In" - Then I can log in with Google - And I follow "Logout" - Then I should see "Request Access to BJC Teacher Materials" - Then I should see "Log In" + Scenario: Logging out as an admin + Given I am on the BJC home page + Given I have an admin email + And I follow "Log In" + Then I can log in with Google + And I follow "Logout" + Then I should see "Request Access to BJC Teacher Materials" + Then I should see "Log In" -Scenario: Viewing Teachers as an admin - Given I am on the BJC home page - Given I have an admin email - And I follow "Log In" - Then I can log in with Google - When I go to the teachers page - Then I should see "BJC Teachers" + Scenario: Viewing Teachers as an admin + Given I am on the BJC home page + Given I have an admin email + And I follow "Log In" + Then I can log in with Google + When I go to the teachers page + Then I should see "BJC Teachers" # TODO: Add steps to check basic dashboard functions # A search field should be visible on the index pages @@ -39,198 +39,221 @@ Scenario: Viewing Teachers as an admin # TODO: Checks for validation and deny belong here. -Scenario: Logging in with non-admin, unregistered Google Account should fail - Given I have a non-admin, unregistered Google email - Given I am on the BJC home page - And I follow "Log In" - Then I can log in with Google - And I should see "Please submit a new request." + Scenario: Logging in with non-admin, unregistered Google Account should fail + Given I have a non-admin, unregistered Google email + Given I am on the BJC home page + And I follow "Log In" + Then I can log in with Google + And I should see "Please submit a new request." -Scenario: Logging in with non-admin, unregistered Microsoft Account should fail - Given I have a non-admin, unregistered Microsoft email - Given I am on the BJC home page - And I follow "Log In" - Then I can log in with Microsoft - And I should see "Please submit a new request." + Scenario: Logging in with non-admin, unregistered Microsoft Account should fail + Given I have a non-admin, unregistered Microsoft email + Given I am on the BJC home page + And I follow "Log In" + Then I can log in with Microsoft + And I should see "Please submit a new request." -Scenario: Logging in with non-admin, unregistered Snap Account should fail - Given I have a non-admin, unregistered Snap email - Given I am on the BJC home page - And I follow "Log In" - Then I can log in with Snap - And I should see "Please submit a new request." + Scenario: Logging in with non-admin, unregistered Snap Account should fail + Given I have a non-admin, unregistered Snap email + Given I am on the BJC home page + And I follow "Log In" + Then I can log in with Snap + And I should see "Please submit a new request." -Scenario: Logging in with non-admin, unregistered Clever Account should fail - Given I have a non-admin, unregistered Clever email - Given I am on the BJC home page - And I follow "Log In" - Then I can log in with Clever - And I should see "Please submit a new request." + Scenario: Logging in with non-admin, unregistered Clever Account should fail + Given I have a non-admin, unregistered Clever email + Given I am on the BJC home page + And I follow "Log In" + Then I can log in with Clever + And I should see "Please submit a new request." -Scenario: Non-admin, unregistered user should not be able to see admin-only pages - Given I have a non-admin, unregistered Google email - Given I am on the BJC home page - When I go to the dashboard page - Then I should see "Only admins can access this page" - And I should be on the new teachers page + Scenario: Non-admin, unregistered user should not be able to see admin-only pages + Given I have a non-admin, unregistered Google email + Given I am on the BJC home page + When I go to the dashboard page + Then I should see "Only admins can access this page" + And I should be on the new teachers page -Scenario: Edit teacher info as an admin - Given the following schools exist: - | name | city | state | website | grade_level | school_type | - | UC Berkeley | Berkeley | CA | https://www.berkeley.edu | university | public | - Given the following teachers exist: - | first_name | last_name | admin | email | school | snap | - | Joseph | Mamoa | false | testteacher@berkeley.edu | UC Berkeley | alonzo | - Given I am on the BJC home page - Given I have an admin email - And I follow "Log In" - Then I can log in with Google - When I go to the teachers page - When I go to the edit page for Joseph Mamoa - Then I should see "Joseph" - And I enter my "First Name" as "Joe" - And I set my status as "Other - Please specify below." - And I set my education level target as "College" - And I press "Update" - Then I see a confirmation "Saved" + Scenario: Edit teacher info as an admin + Given the following schools exist: + | name | city | state | website | grade_level | school_type | + | UC Berkeley | Berkeley | CA | https://www.berkeley.edu | university | public | + Given the following teachers exist: + | first_name | last_name | admin | email | school | snap | + | Joseph | Mamoa | false | testteacher@berkeley.edu | UC Berkeley | alonzo | + Given I am on the BJC home page + Given I have an admin email + And I follow "Log In" + Then I can log in with Google + When I go to the teachers page + When I go to the edit page for Joseph Mamoa + Then I should see "Joseph" + And I enter my "First Name" as "Joe" + And I set my status as "Other - Please specify below." + And I set my education level target as "College" + And I press "Update" + Then I see a confirmation "Saved" -Scenario: Deny teacher as an admin - Given the following schools exist: - | name | city | state | website | grade_level | school_type | - | UC Berkeley | Berkeley | CA | https://www.berkeley.edu | university | public | - Given the following teachers exist: - | first_name | last_name | admin | email | school | - | Joseph | Mamoa | false | testteacher@berkeley.edu | UC Berkeley | - Given I am on the BJC home page - Given I have an admin email - When I follow "Log In" - Then I can log in with Google - And I press "❌" within "#DataTables_Table_0 > tbody > tr:nth-child(1)" - Then I should see "Reason" - And I should see "Deny Joseph Mamoa" - And I fill in "denial_reason" with "Test" - And I press "Cancel" - And I press "❌" within "#DataTables_Table_0 > tbody > tr:nth-child(1)" - Then the "denial_reason" field should not contain "Test" - And I fill in "denial_reason" with "Denial Reason" - And I press "Submit" - Then I can send a deny email + Scenario: Deny teacher as an admin + Given the following schools exist: + | name | city | state | website | grade_level | school_type | + | UC Berkeley | Berkeley | CA | https://www.berkeley.edu | university | public | + Given the following teachers exist: + | first_name | last_name | admin | email | school | + | Joseph | Mamoa | false | testteacher@berkeley.edu | UC Berkeley | + Given I am on the BJC home page + Given I have an admin email + When I follow "Log In" + Then I can log in with Google + And I press "❌" within "#DataTables_Table_0 > tbody > tr:nth-child(1)" + Then I should see "Reason" + And I should see "Deny Joseph Mamoa" + And I fill in "denial_reason" with "Test" + And I press "Cancel" + And I press "❌" within "#DataTables_Table_0 > tbody > tr:nth-child(1)" + Then the "denial_reason" field should not contain "Test" + And I fill in "denial_reason" with "Denial Reason" + And I press "Submit" + Then I can send a deny email -Scenario: Not logged in should not have access to edit - Given the following schools exist: - | name | city | state | website | grade_level | school_type | - | UC Berkeley | Berkeley | CA | https://www.berkeley.edu | university | public | - Given the following teachers exist: - | first_name | last_name | admin | email | school | - | Joseph | Mamoa | false | testteacher@berkeley.edu | UC Berkeley | - When I go to the edit page for Joseph Mamoa - Then should see "You need to log in to access this." + Scenario: Not logged in should not have access to edit + Given the following schools exist: + | name | city | state | website | grade_level | school_type | + | UC Berkeley | Berkeley | CA | https://www.berkeley.edu | university | public | + Given the following teachers exist: + | first_name | last_name | admin | email | school | + | Joseph | Mamoa | false | testteacher@berkeley.edu | UC Berkeley | + When I go to the edit page for Joseph Mamoa + Then should see "You need to log in to access this." -Scenario: Filter all teacher info as an admin - Given the following schools exist: - | name | city | state | website | grade_level | school_type | - | UC Berkeley | Berkeley | CA | https://www.berkeley.edu | university | public | - Given the following teachers exist: - | first_name | last_name | admin | email | school | application_status | - | Victor | Validateme | false | testteacher1@berkeley.edu | UC Berkeley | Validated | - | Danny | Denyme | false | testteacher2@berkeley.edu | UC Berkeley | Denied | - | Peter | Pendme | false | testteacher3@berkeley.edu | UC Berkeley | Not Reviewed | - Given I am on the BJC home page - Given I have an admin email - And I follow "Log In" - Then I can log in with Google - When I go to the teachers page - And I check "Not Reviewed" - And I uncheck "Validated" - Then I should see "Peter" - Then I should not see "Victor" - Then I should not see "Danny" - And I check "Validated" - Then I should see "Peter" - Then I should see "Victor" - Then I should not see "Danny" + Scenario: Filter all teacher info as an admin + Given the following schools exist: + | name | city | state | website | grade_level | school_type | + | UC Berkeley | Berkeley | CA | https://www.berkeley.edu | university | public | + Given the following teachers exist: + | first_name | last_name | admin | email | school | application_status | + | Victor | Validateme | false | testteacher1@berkeley.edu | UC Berkeley | Validated | + | Danny | Denyme | false | testteacher2@berkeley.edu | UC Berkeley | Denied | + | Peter | Pendme | false | testteacher3@berkeley.edu | UC Berkeley | Not Reviewed | + Given I am on the BJC home page + Given I have an admin email + And I follow "Log In" + Then I can log in with Google + When I go to the teachers page + And I check "Not Reviewed" + And I uncheck "Validated" + Then I should see "Peter" + Then I should not see "Victor" + Then I should not see "Danny" + And I check "Validated" + Then I should see "Peter" + Then I should see "Victor" + Then I should not see "Danny" -Scenario: View teacher info as an admin - Given the following schools exist: - | name | city | state | website | grade_level | school_type | - | UC Berkeley | Berkeley | CA | https://www.berkeley.edu | university | public | - Given the following teachers exist: - | first_name | last_name | admin | email | school | snap | - | Joseph | Test | false | testteacher@berkeley.edu | UC Berkeley | alonzo | - Given I am on the BJC home page - Given I have an admin email - And I follow "Log In" - Then I can log in with Google - When I go to the teachers page - And I uncheck "Validated" - When I follow "Joseph Test" - Then I should see "Joseph Test" - And I should see "Edit Information" - And I should see "School" - And I should see "School Location" - And I should see "Email" - And I should see "Personal or Course Website" + Scenario: View teacher info as an admin + Given the following schools exist: + | name | city | state | website | grade_level | school_type | + | UC Berkeley | Berkeley | CA | https://www.berkeley.edu | university | public | + Given the following teachers exist: + | first_name | last_name | admin | email | school | snap | + | Joseph | Test | false | testteacher@berkeley.edu | UC Berkeley | alonzo | + Given I am on the BJC home page + Given I have an admin email + And I follow "Log In" + Then I can log in with Google + When I go to the teachers page + And I uncheck "Validated" + When I follow "Joseph Test" + Then I should see "Joseph Test" + And I should see "Edit Information" + And I should see "School" + And I should see "School Location" + And I should see "Email" + And I should see "Personal or Course Website" -Scenario: Edit teacher info as an admin navigating from view only page to edit page - Given the following schools exist: - | name | city | state | website | grade_level | school_type | - | UC Berkeley | Berkeley | CA | https://www.berkeley.edu | university | public | - Given the following teachers exist: - | first_name | last_name | admin | email | school | snap | - | Joseph | Mamoa New | false | testteacher@berkeley.edu | UC Berkeley | alonzo | - Given I am on the BJC home page - Given I have an admin email - And I follow "Log In" - Then I can log in with Google - When I go to the teachers page - And I uncheck "Validated" - When I follow "Joseph Mamoa New" - Then I should see "Joseph Mamoa" - And I should see "Edit Information" - And I follow "Edit Information" - And I should see "Joseph" - And I enter my "First Name" as "Joe" - And I set my status as "Other - Please specify below." - And I set my education level target as "College" - And I press "Update" - Then I see a confirmation "Saved" + Scenario: Edit teacher info as an admin navigating from view only page to edit page + Given the following schools exist: + | name | city | state | website | grade_level | school_type | + | UC Berkeley | Berkeley | CA | https://www.berkeley.edu | university | public | + Given the following teachers exist: + | first_name | last_name | admin | email | school | snap | + | Joseph | Mamoa New | false | testteacher@berkeley.edu | UC Berkeley | alonzo | + Given I am on the BJC home page + Given I have an admin email + And I follow "Log In" + Then I can log in with Google + When I go to the teachers page + And I uncheck "Validated" + When I follow "Joseph Mamoa New" + Then I should see "Joseph Mamoa" + And I should see "Edit Information" + And I follow "Edit Information" + And I should see "Joseph" + And I enter my "First Name" as "Joe" + And I set my status as "Other - Please specify below." + And I set my education level target as "College" + And I press "Update" + Then I see a confirmation "Saved" -Scenario: Should be able to resend welcome email - Given the following schools exist: - | name | city | state | website | grade_level | school_type | - | UC Berkeley | Berkeley | CA | https://www.berkeley.edu | university | public | - Given the following teachers exist: - | first_name | last_name | admin | email | school | snap | application_status | - | Joseph | Mamoa | false | testteacher@berkeley.edu | UC Berkeley | alonzo | validated | - Given I am on the BJC home page - Given I have an admin email - And I follow "Log In" - Then I can log in with Google - When I go to the teachers page - When I go to the edit page for Joseph Mamoa - Then I should see a button named "Resend Welcome Email" + Scenario: Should be able to resend welcome email + Given the following schools exist: + | name | city | state | website | grade_level | school_type | + | UC Berkeley | Berkeley | CA | https://www.berkeley.edu | university | public | + Given the following teachers exist: + | first_name | last_name | admin | email | school | snap | application_status | + | Joseph | Mamoa | false | testteacher@berkeley.edu | UC Berkeley | alonzo | validated | + Given I am on the BJC home page + Given I have an admin email + And I follow "Log In" + Then I can log in with Google + When I go to the teachers page + When I go to the edit page for Joseph Mamoa + Then I should see a button named "Resend Welcome Email" Scenario: Admin can access new teacher button at teacher index page - Given I am on the BJC home page - Given I have an admin email - And I follow "Log In" - Then I can log in with Google - And I should see "BJC Teacher Dashboard" - Given I follow "Teachers" - Then I should see "BJC Teachers" - And I press "New Teacher" - Then I should see "Request Access to BJC Teacher Materials" + Given I am on the BJC home page + Given I have an admin email + And I follow "Log In" + Then I can log in with Google + And I should see "BJC Teacher Dashboard" + Given I follow "Teachers" + Then I should see "BJC Teachers" + And I press "New Teacher" + Then I should see "Request Access to BJC Teacher Materials" Scenario: Admin can access new school button at teacher index page - Given I am on the BJC home page - Given I have an admin email - And I follow "Log In" - Then I can log in with Google - And I should see "BJC Teacher Dashboard" - Given I follow "Schools" - Then I should see "BJC Schools" - And I press "New School" - Then I should see "Add a School" + Given I am on the BJC home page + Given I have an admin email + And I follow "Log In" + Then I can log in with Google + And I should see "BJC Teacher Dashboard" + Given I follow "Schools" + Then I should see "BJC Schools" + And I press "New School" + Then I should see "Add a School" + + Scenario: Request information from a teacher as an admin + Given the following schools exist: + | name | city | state | website | grade_level | school_type | + | UC Berkeley | Berkeley | CA | https://www.berkeley.edu | university | public | + And the following teachers exist: + | first_name | last_name | admin | email | school | + | Joseph | Mamoa | false | testteacher@berkeley.edu | UC Berkeley | + And I am on the BJC home page + And I have an admin email + When I follow "Log In" + Then I can log in with Google + And I press "❓" within "#DataTables_Table_0 > tbody > tr:nth-child(1)" + Then I should see "Reason" + And I should see "Request Info from Joseph Mamoa" + And I fill in "request_reason" with "Need more details on qualifications" + And I press "Cancel" + And I press "❓" within "#DataTables_Table_0 > tbody > tr:nth-child(1)" + Then the "request_reason" field should not contain "Need more details on qualifications" + And I fill in "request_reason" with "Complete your profile details" + And I press "Submit" + Then I can send a request info email + # Scenario: Admin can import csv file. The loader should filter invalid record and create associate school. # Given the following schools exist: diff --git a/features/step_definitions/admin_steps.rb b/features/step_definitions/admin_steps.rb index 0777c8dd..d878a923 100644 --- a/features/step_definitions/admin_steps.rb +++ b/features/step_definitions/admin_steps.rb @@ -12,32 +12,32 @@ # Returns a OAuth2 token associated with email "testadminuser@berkeley.edu" Given(/I have an admin email/) do OmniAuth.config.mock_auth[:google_oauth2] = OmniAuth::AuthHash.new({ - provider: "google_oauth2", - uid: "123545", - info: { - name: "Admin User", - first_name: "Admin", - last_name: "User", - email: "testadminuser@berkeley.edu", - school: "UC Berkeley", - } - }) + provider: "google_oauth2", + uid: "123545", + info: { + name: "Admin User", + first_name: "Admin", + last_name: "User", + email: "testadminuser@berkeley.edu", + school: "UC Berkeley", + } + }) end # Returns a OAuth2 token associated with email "randomemail@gmail.com" Given(/I have a non-admin, unregistered (.*) email/) do |login| service = LOGIN_SERVICE[login] OmniAuth.config.mock_auth[service] = OmniAuth::AuthHash.new({ - provider: service, - uid: "123545", - info: { - name: "Random User", - first_name: "Random", - last_name: "User", - email: "randomemail@berkeley.edu", - school: "UC Berkeley", - } - }) + provider: service, + uid: "123545", + info: { + name: "Random User", + first_name: "Random", + last_name: "User", + email: "randomemail@berkeley.edu", + school: "UC Berkeley", + } + }) end # A wrapper around the omniauth link. @@ -64,7 +64,7 @@ last_email = ActionMailer::Base.deliveries.last last_email.to[0].should eq "testteacher@berkeley.edu" last_email.subject.should eq "Request Info Email" - last_email.body.encoded.should include "Request Info Reason" + last_email.body.encoded.should include "We kindly ask you to provide the following details to complete your application:" end Then(/I attach the csv "([^"]*)"$/) do |path| From c64216f2f663879d90ac341828b9ca33d77beae9 Mon Sep 17 00:00:00 2001 From: JacksonXu33 Date: Tue, 27 Feb 2024 10:52:52 -0800 Subject: [PATCH 25/37] edit redirect to use current path --- app/controllers/teachers_controller.rb | 7 +++---- spec/controllers/teachers_controller_spec.rb | 7 ++++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/controllers/teachers_controller.rb b/app/controllers/teachers_controller.rb index 12214763..d6f0a5a1 100644 --- a/app/controllers/teachers_controller.rb +++ b/app/controllers/teachers_controller.rb @@ -160,13 +160,12 @@ def destroy def resend_welcome_email if @teacher.validated? || @is_admin - TeacherMailer.welcome_email(@teacher).deliver_now flash[:success] = "Welcome email resent successfully!" + TeacherMailer.welcome_email(@teacher).deliver_now else - flash[:alert] = "Error resending welcome email. \ - Please ensure that your account has been validated by an administrator." + flash[:alert] = "Error resending welcome email. Please ensure that your account has been validated by an administrator." end - redirect_to edit_teacher_path(@teacher) + redirect_back(fallback_location: dashboard_path) end def import diff --git a/spec/controllers/teachers_controller_spec.rb b/spec/controllers/teachers_controller_spec.rb index 99a91370..56bf3b17 100644 --- a/spec/controllers/teachers_controller_spec.rb +++ b/spec/controllers/teachers_controller_spec.rb @@ -188,6 +188,7 @@ it "succeeds when teacher is validated, sets success" do ApplicationController.any_instance.stub(:current_user).and_return(Teacher.find_by(first_name: "Validated")) validated_teacher = Teacher.find_by(first_name: "Validated") + request.env["HTTP_REFERER"] = edit_teacher_path(validated_teacher.id) post :resend_welcome_email, params: { id: validated_teacher.id } expect(flash[:success]).to eq("Welcome email resent successfully!") expect(response).to redirect_to(edit_teacher_path(validated_teacher.id)) @@ -196,10 +197,10 @@ it "fails when teacher is not validated, sets alert" do ApplicationController.any_instance.stub(:current_user).and_return(Teacher.find_by(first_name: "Short")) nonvalidated_teacher = Teacher.find_by(first_name: "Short") + request.env["HTTP_REFERER"] = teacher_path(nonvalidated_teacher.id) post :resend_welcome_email, params: { id: nonvalidated_teacher.id } - expect(flash[:alert]).to eq("Error resending welcome email. \ - Please ensure that your account has been validated by an administrator.") - expect(response).to redirect_to(edit_teacher_path(nonvalidated_teacher.id)) + expect(flash[:alert]).to eq("Error resending welcome email. Please ensure that your account has been validated by an administrator.") + expect(response).to redirect_to(teacher_path(nonvalidated_teacher.id)) end end end From 9eeb0dc52413b2553f189813dd95422e9d38d209 Mon Sep 17 00:00:00 2001 From: Jingchao Zhong <92573736+perryzjc@users.noreply.github.com> Date: Wed, 28 Feb 2024 11:28:10 -0800 Subject: [PATCH 26/37] Fix email template reseed problem --- db/seeds.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/db/seeds.rb b/db/seeds.rb index 319a30dd..10512ca9 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -2,7 +2,8 @@ require_relative "seed_data" -SeedData.emails.each { |email| EmailTemplate.find_or_create_by(email) } +EmailTemplate.delete_all +SeedData.emails.each { |email| EmailTemplate.create!(email) } SeedData.create_schools From 82c5475906074b86138097bc8b06ed460a502913 Mon Sep 17 00:00:00 2001 From: Jingchao Zhong <92573736+perryzjc@users.noreply.github.com> Date: Wed, 28 Feb 2024 11:28:41 -0800 Subject: [PATCH 27/37] Add rspec test for email reseed behavior --- .../email_templates_reseed_spec.rb | 113 ++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 spec/controllers/email_templates_reseed_spec.rb diff --git a/spec/controllers/email_templates_reseed_spec.rb b/spec/controllers/email_templates_reseed_spec.rb new file mode 100644 index 00000000..19960390 --- /dev/null +++ b/spec/controllers/email_templates_reseed_spec.rb @@ -0,0 +1,113 @@ +# frozen_string_literal: true + +require "rails_helper" + +# This test suite verifies the reseeding behavior of EmailTemplate models to ensure +# that email templates are correctly handled (i.e., updated or removed) without duplication +# upon reseeding the database with new seed data. It tests the initial seeding, updates to +# templates, and the removal of templates that are no longer present in the seed data. +RSpec.describe "EmailTemplate Reseed Behavior", type: :model do + # Define email content for initial seeding and subsequent updates. These blocks + # provide a flexible way to manage test data, allowing us to simulate changes in seed data + # across different test scenarios. + let(:form_submission_old) do + <<-FORM_SUBMISSION +

Submitted Information:

+

I am the original content of the form submission email.

+ FORM_SUBMISSION + end + + let(:request_info_email_old) do + <<-REQUEST_INFO_EMAIL +

Dear {{teacher_first_name}},

+

I am the original content of the request info email.

+ REQUEST_INFO_EMAIL + end + + let(:request_info_email_new) do + <<-REQUEST_INFO_EMAIL +

Dear {{teacher_first_name}},

+

I am NEW Template Content of the request info email.

+ REQUEST_INFO_EMAIL + end + + # Setup phase: Mocks the SeedData.emails call to return predetermined email content. + # This mimics the initial seeding process with specific email templates before each test. + # The `load Rails.root.join("db/seeds.rb")` line simulates the seeding action. + before do + allow(SeedData).to receive(:emails).and_return( + [ + { + to: "lmock@berkeley.edu, contact@bjc.berkeley.edu", + body: form_submission_old, + path: "teacher_mailer/form_submission", + locale: nil, + handler: "liquid", + partial: false, + format: "html", + title: "Form Submission", + required: true, + subject: "Form Submission" + }, + { + body: request_info_email_old, + to: "{{teacher_email}}, {{teacher_personal_email}}", + path: "teacher_mailer/request_info_email", + locale: nil, + handler: "liquid", + partial: false, + format: "html", + title: "Request Info Email", + required: true, + subject: "Request Info Email" + } + ]) + # Initial seed with original data + load Rails.root.join("db/seeds.rb") + end + + # This test verifies the entire reseed process by first checking the state after initial seeding, + # then mocking updated seed data, reseeding, and finally asserting the expected outcomes. + # It checks for the correct handling of both updated and removed email templates. + it "correctly handles email templates on reseed without duplication" do + # Verify original content + expect(EmailTemplate.count).to eq(2) + request_info_email = EmailTemplate.find_by(title: "Request Info Email") + expect(request_info_email.title).to eq("Request Info Email") + expect(request_info_email.body).to include("I am the original content of the request info email") + + form_submission_email = EmailTemplate.find_by(title: "Form Submission") + expect(form_submission_email.title).to eq("Form Submission") + expect(form_submission_email.body).to include("I am the original content of the form submission email") + + # Simulate an update to the seed data by changing the content of one email template + # and removing another. This prepares for the reseed to test how the application handles + # updated and removed templates. + allow(SeedData).to receive(:emails).and_return( + [ + { + body: request_info_email_new, + to: "{{teacher_email}}, {{teacher_personal_email}}", + path: "teacher_mailer/request_info_email", + locale: nil, + handler: "liquid", + partial: false, + format: "html", + title: "Request Info Email", + required: true, + subject: "Request Info Email" + } + ]) + load Rails.root.join("db/seeds.rb") + + # Final assertions to verify that after reseeding, the email templates in the database + # reflect the updated seed data: updated content is present, and removed templates are absent. + expect(EmailTemplate.count).to eq(1) + + request_info_email = EmailTemplate.find_by(title: "Request Info Email") + expect(request_info_email.title).to eq("Request Info Email") + expect(request_info_email.body).to include("NEW Template Content of the request info email") + + expect(EmailTemplate.find_by(title: "Form Submission")).to be_nil + end +end From d00c8896f6533b1581def7fd935975d0a26a155a Mon Sep 17 00:00:00 2001 From: Jingchao Zhong <92573736+perryzjc@users.noreply.github.com> Date: Wed, 28 Feb 2024 11:50:41 -0800 Subject: [PATCH 28/37] Refract rspec test of email reseed to be more modular --- .../email_templates_reseed_spec.rb | 72 +++++++++++++++---- 1 file changed, 57 insertions(+), 15 deletions(-) diff --git a/spec/controllers/email_templates_reseed_spec.rb b/spec/controllers/email_templates_reseed_spec.rb index 19960390..f5644694 100644 --- a/spec/controllers/email_templates_reseed_spec.rb +++ b/spec/controllers/email_templates_reseed_spec.rb @@ -33,7 +33,6 @@ # Setup phase: Mocks the SeedData.emails call to return predetermined email content. # This mimics the initial seeding process with specific email templates before each test. - # The `load Rails.root.join("db/seeds.rb")` line simulates the seeding action. before do allow(SeedData).to receive(:emails).and_return( [ @@ -62,29 +61,39 @@ subject: "Request Info Email" } ]) - # Initial seed with original data + # Simulates the seeding action. load Rails.root.join("db/seeds.rb") - end - # This test verifies the entire reseed process by first checking the state after initial seeding, - # then mocking updated seed data, reseeding, and finally asserting the expected outcomes. - # It checks for the correct handling of both updated and removed email templates. - it "correctly handles email templates on reseed without duplication" do # Verify original content expect(EmailTemplate.count).to eq(2) request_info_email = EmailTemplate.find_by(title: "Request Info Email") + expect(request_info_email).to be_present expect(request_info_email.title).to eq("Request Info Email") expect(request_info_email.body).to include("I am the original content of the request info email") form_submission_email = EmailTemplate.find_by(title: "Form Submission") + expect(form_submission_email).to be_present expect(form_submission_email.title).to eq("Form Submission") expect(form_submission_email.body).to include("I am the original content of the form submission email") + end - # Simulate an update to the seed data by changing the content of one email template - # and removing another. This prepares for the reseed to test how the application handles - # updated and removed templates. + it "does not duplicate email templates on reseed with updated content" do + # Mock the SeedData.emails call to return updated email content for the "Request Info Email" template. + # This simulates the reseeding process with updated email content. allow(SeedData).to receive(:emails).and_return( [ + { + to: "lmock@berkeley.edu, contact@bjc.berkeley.edu", + body: form_submission_old, + path: "teacher_mailer/form_submission", + locale: nil, + handler: "liquid", + partial: false, + format: "html", + title: "Form Submission", + required: true, + subject: "Form Submission" + }, { body: request_info_email_new, to: "{{teacher_email}}, {{teacher_personal_email}}", @@ -100,14 +109,47 @@ ]) load Rails.root.join("db/seeds.rb") - # Final assertions to verify that after reseeding, the email templates in the database - # reflect the updated seed data: updated content is present, and removed templates are absent. - expect(EmailTemplate.count).to eq(1) - + # Verify updated content: Email count is still 2, with only updated content for the request info email. + expect(EmailTemplate.count).to eq(2) request_info_email = EmailTemplate.find_by(title: "Request Info Email") + expect(request_info_email).to be_present expect(request_info_email.title).to eq("Request Info Email") - expect(request_info_email.body).to include("NEW Template Content of the request info email") + expect(request_info_email.body).to include("I am NEW Template Content of the request info email") + + form_submission_email = EmailTemplate.find_by(title: "Form Submission") + expect(form_submission_email).to be_present + expect(form_submission_email.title).to eq("Form Submission") + expect(form_submission_email.body).to include("I am the original content of the form submission email") + end + it "removes email templates not present in updated seed data" do + # Mock the SeedData.emails call to only return the "Request Info Email" template, + # simulating the removal of the "Form Submission" template from the seed data. + allow(SeedData).to receive(:emails).and_return( + [ + { + body: request_info_email_old, + to: "{{teacher_email}}, {{teacher_personal_email}}", + path: "teacher_mailer/request_info_email", + locale: nil, + handler: "liquid", + partial: false, + format: "html", + title: "Request Info Email", + required: true, + subject: "Request Info Email" + } + ]) + load Rails.root.join("db/seeds.rb") + + # Verify that the "Form Submission" email template is removed + expect(EmailTemplate.count).to eq(1) expect(EmailTemplate.find_by(title: "Form Submission")).to be_nil + + # Additionally, verify that the "Request Info Email" template remains and is unchanged + request_info_email = EmailTemplate.find_by(title: "Request Info Email") + expect(request_info_email).to be_present + expect(request_info_email.title).to eq("Request Info Email") + expect(request_info_email.body).to include("I am the original content of the request info email") end end From 9fb64e9b70c9f3f8e00bc39f887065f62afdf50c Mon Sep 17 00:00:00 2001 From: Jingchao Zhong <92573736+perryzjc@users.noreply.github.com> Date: Wed, 28 Feb 2024 12:58:08 -0800 Subject: [PATCH 29/37] Add RSpec db check to ensure tests use bjc_teachers_test for safety --- spec/controllers/email_templates_reseed_spec.rb | 5 +++++ spec/rails_helper.rb | 2 ++ spec/support/db_verification.rb | 13 +++++++++++++ 3 files changed, 20 insertions(+) create mode 100644 spec/support/db_verification.rb diff --git a/spec/controllers/email_templates_reseed_spec.rb b/spec/controllers/email_templates_reseed_spec.rb index f5644694..f1926010 100644 --- a/spec/controllers/email_templates_reseed_spec.rb +++ b/spec/controllers/email_templates_reseed_spec.rb @@ -7,6 +7,8 @@ # upon reseeding the database with new seed data. It tests the initial seeding, updates to # templates, and the removal of templates that are no longer present in the seed data. RSpec.describe "EmailTemplate Reseed Behavior", type: :model do + # To ensure that the tests run against the correct database. + include_context "database verification" # Define email content for initial seeding and subsequent updates. These blocks # provide a flexible way to manage test data, allowing us to simulate changes in seed data # across different test scenarios. @@ -34,6 +36,9 @@ # Setup phase: Mocks the SeedData.emails call to return predetermined email content. # This mimics the initial seeding process with specific email templates before each test. before do + # Clean the database for email templates before each test. + # This is safe to do due to the `database verification` context + EmailTemplate.delete_all allow(SeedData).to receive(:emails).and_return( [ { diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 175c3543..1783dbcb 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -16,6 +16,8 @@ require "spec_helper" +require "support/db_verification" + # Requires supporting ruby files with custom matchers and macros, etc, in # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are # run as spec files by default. This means that files in spec/support that end diff --git a/spec/support/db_verification.rb b/spec/support/db_verification.rb new file mode 100644 index 00000000..437aae3b --- /dev/null +++ b/spec/support/db_verification.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +RSpec.shared_context "database verification" do + before(:all) do + # Use before(:all) to ensure this check runs once before all tests in the suite + db_name = ActiveRecord::Base.connection.current_database + expected_db_name = "bjc_teachers_test" + + if db_name != expected_db_name + raise "Tests are running against the wrong database: #{db_name}. Expected: #{expected_db_name}." + end + end +end From 2f30dc1b858f763e662d1c801645a5e2465c2fa8 Mon Sep 17 00:00:00 2001 From: Jingchao Zhong <92573736+perryzjc@users.noreply.github.com> Date: Wed, 28 Feb 2024 13:11:50 -0800 Subject: [PATCH 30/37] Revert "Add RSpec db check to ensure tests use bjc_teachers_test for safety" This reverts commit 9fb64e9b70c9f3f8e00bc39f887065f62afdf50c. --- spec/controllers/email_templates_reseed_spec.rb | 5 ----- spec/rails_helper.rb | 2 -- spec/support/db_verification.rb | 13 ------------- 3 files changed, 20 deletions(-) delete mode 100644 spec/support/db_verification.rb diff --git a/spec/controllers/email_templates_reseed_spec.rb b/spec/controllers/email_templates_reseed_spec.rb index f1926010..f5644694 100644 --- a/spec/controllers/email_templates_reseed_spec.rb +++ b/spec/controllers/email_templates_reseed_spec.rb @@ -7,8 +7,6 @@ # upon reseeding the database with new seed data. It tests the initial seeding, updates to # templates, and the removal of templates that are no longer present in the seed data. RSpec.describe "EmailTemplate Reseed Behavior", type: :model do - # To ensure that the tests run against the correct database. - include_context "database verification" # Define email content for initial seeding and subsequent updates. These blocks # provide a flexible way to manage test data, allowing us to simulate changes in seed data # across different test scenarios. @@ -36,9 +34,6 @@ # Setup phase: Mocks the SeedData.emails call to return predetermined email content. # This mimics the initial seeding process with specific email templates before each test. before do - # Clean the database for email templates before each test. - # This is safe to do due to the `database verification` context - EmailTemplate.delete_all allow(SeedData).to receive(:emails).and_return( [ { diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 1783dbcb..175c3543 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -16,8 +16,6 @@ require "spec_helper" -require "support/db_verification" - # Requires supporting ruby files with custom matchers and macros, etc, in # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are # run as spec files by default. This means that files in spec/support that end diff --git a/spec/support/db_verification.rb b/spec/support/db_verification.rb deleted file mode 100644 index 437aae3b..00000000 --- a/spec/support/db_verification.rb +++ /dev/null @@ -1,13 +0,0 @@ -# frozen_string_literal: true - -RSpec.shared_context "database verification" do - before(:all) do - # Use before(:all) to ensure this check runs once before all tests in the suite - db_name = ActiveRecord::Base.connection.current_database - expected_db_name = "bjc_teachers_test" - - if db_name != expected_db_name - raise "Tests are running against the wrong database: #{db_name}. Expected: #{expected_db_name}." - end - end -end From 7464961911c98304a7b4bea5f5ab447e89c13ef1 Mon Sep 17 00:00:00 2001 From: Jingchao Zhong <92573736+perryzjc@users.noreply.github.com> Date: Wed, 28 Feb 2024 13:22:29 -0800 Subject: [PATCH 31/37] Adjust Rspec test to avoid purge old email --- .../email_templates_reseed_spec.rb | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/spec/controllers/email_templates_reseed_spec.rb b/spec/controllers/email_templates_reseed_spec.rb index f5644694..642e0684 100644 --- a/spec/controllers/email_templates_reseed_spec.rb +++ b/spec/controllers/email_templates_reseed_spec.rb @@ -3,9 +3,9 @@ require "rails_helper" # This test suite verifies the reseeding behavior of EmailTemplate models to ensure -# that email templates are correctly handled (i.e., updated or removed) without duplication +# that email templates are correctly handled (i.e., updated or not-removal) without duplication # upon reseeding the database with new seed data. It tests the initial seeding, updates to -# templates, and the removal of templates that are no longer present in the seed data. +# templates, and the non-removal of templates that are no longer present in the seed data. RSpec.describe "EmailTemplate Reseed Behavior", type: :model do # Define email content for initial seeding and subsequent updates. These blocks # provide a flexible way to manage test data, allowing us to simulate changes in seed data @@ -31,9 +31,10 @@ REQUEST_INFO_EMAIL end - # Setup phase: Mocks the SeedData.emails call to return predetermined email content. - # This mimics the initial seeding process with specific email templates before each test. before do + # Clear the EmailTemplate table to ensure a clean slate for each test. + # This is safe to do because it's under test environment, not production + EmailTemplate.delete_all allow(SeedData).to receive(:emails).and_return( [ { @@ -122,9 +123,12 @@ expect(form_submission_email.body).to include("I am the original content of the form submission email") end - it "removes email templates not present in updated seed data" do + # Test the special needs from Professor Ball: I would not have the seeds file delete templates + it "does not remove email templates not present in updated seed data" do # Mock the SeedData.emails call to only return the "Request Info Email" template, # simulating the removal of the "Form Submission" template from the seed data. + # Expected behavior: The "Form Submission" template should not be removed from the **database**, + # even though it's no longer present in the seed data. allow(SeedData).to receive(:emails).and_return( [ { @@ -142,14 +146,16 @@ ]) load Rails.root.join("db/seeds.rb") - # Verify that the "Form Submission" email template is removed - expect(EmailTemplate.count).to eq(1) - expect(EmailTemplate.find_by(title: "Form Submission")).to be_nil - - # Additionally, verify that the "Request Info Email" template remains and is unchanged + # Verify that both email templates still present and remain same + expect(EmailTemplate.count).to eq(2) request_info_email = EmailTemplate.find_by(title: "Request Info Email") expect(request_info_email).to be_present expect(request_info_email.title).to eq("Request Info Email") expect(request_info_email.body).to include("I am the original content of the request info email") + + form_submission_email = EmailTemplate.find_by(title: "Form Submission") + expect(form_submission_email).to be_present + expect(form_submission_email.title).to eq("Form Submission") + expect(form_submission_email.body).to include("I am the original content of the form submission email") end end From 755a96bd73f93b992ca8353113445b3f909f0bc0 Mon Sep 17 00:00:00 2001 From: Jingchao Zhong <92573736+perryzjc@users.noreply.github.com> Date: Wed, 28 Feb 2024 13:23:08 -0800 Subject: [PATCH 32/37] Update email reseed implementation to avoid purge old email templates --- db/seeds.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/db/seeds.rb b/db/seeds.rb index 10512ca9..8c6d7945 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -2,8 +2,11 @@ require_relative "seed_data" -EmailTemplate.delete_all -SeedData.emails.each { |email| EmailTemplate.create!(email) } +SeedData.emails.each do |email_attrs| + # to, body fields are updated through development, should not be used as part of the uniqueness check + email_template = EmailTemplate.find_or_initialize_by(email_attrs.except(:to, :body)) + email_template.update(email_attrs) +end SeedData.create_schools From dad60c56ada6f26d6a3c638d12efd96ed13f5e96 Mon Sep 17 00:00:00 2001 From: Jingchao Zhong <92573736+perryzjc@users.noreply.github.com> Date: Sat, 2 Mar 2024 10:33:38 -0800 Subject: [PATCH 33/37] Refract Rspec test for email reseed --- .../email_templates_reseed_spec.rb | 87 ++++++------------- 1 file changed, 27 insertions(+), 60 deletions(-) diff --git a/spec/controllers/email_templates_reseed_spec.rb b/spec/controllers/email_templates_reseed_spec.rb index 642e0684..9ec989e4 100644 --- a/spec/controllers/email_templates_reseed_spec.rb +++ b/spec/controllers/email_templates_reseed_spec.rb @@ -31,36 +31,33 @@ REQUEST_INFO_EMAIL end + def email_template_hash(body:, title:, subject:, path:) + { + to: title == "Form Submission" ? + "lmock@berkeley.edu, contact@bjc.berkeley.edu" + : "{{teacher_email}}, {{teacher_personal_email}}", + body:, + path:, + locale: nil, + handler: "liquid", + partial: false, + format: "html", + title:, + required: true, + subject: + } + end + before do # Clear the EmailTemplate table to ensure a clean slate for each test. # This is safe to do because it's under test environment, not production EmailTemplate.delete_all allow(SeedData).to receive(:emails).and_return( [ - { - to: "lmock@berkeley.edu, contact@bjc.berkeley.edu", - body: form_submission_old, - path: "teacher_mailer/form_submission", - locale: nil, - handler: "liquid", - partial: false, - format: "html", - title: "Form Submission", - required: true, - subject: "Form Submission" - }, - { - body: request_info_email_old, - to: "{{teacher_email}}, {{teacher_personal_email}}", - path: "teacher_mailer/request_info_email", - locale: nil, - handler: "liquid", - partial: false, - format: "html", - title: "Request Info Email", - required: true, - subject: "Request Info Email" - } + email_template_hash(body: form_submission_old, title: "Form Submission", subject: "Form Submission", + path: "teacher_mailer/form_submission"), + email_template_hash(body: request_info_email_old, title: "Request Info Email", subject: "Request Info Email", + path: "teacher_mailer/request_info_email") ]) # Simulates the seeding action. load Rails.root.join("db/seeds.rb") @@ -83,30 +80,10 @@ # This simulates the reseeding process with updated email content. allow(SeedData).to receive(:emails).and_return( [ - { - to: "lmock@berkeley.edu, contact@bjc.berkeley.edu", - body: form_submission_old, - path: "teacher_mailer/form_submission", - locale: nil, - handler: "liquid", - partial: false, - format: "html", - title: "Form Submission", - required: true, - subject: "Form Submission" - }, - { - body: request_info_email_new, - to: "{{teacher_email}}, {{teacher_personal_email}}", - path: "teacher_mailer/request_info_email", - locale: nil, - handler: "liquid", - partial: false, - format: "html", - title: "Request Info Email", - required: true, - subject: "Request Info Email" - } + email_template_hash(body: form_submission_old, title: "Form Submission", subject: "Form Submission", + path: "teacher_mailer/form_submission"), + email_template_hash(body: request_info_email_new, title: "Request Info Email", subject: "Request Info Email", + path: "teacher_mailer/request_info_email") ]) load Rails.root.join("db/seeds.rb") @@ -131,18 +108,8 @@ # even though it's no longer present in the seed data. allow(SeedData).to receive(:emails).and_return( [ - { - body: request_info_email_old, - to: "{{teacher_email}}, {{teacher_personal_email}}", - path: "teacher_mailer/request_info_email", - locale: nil, - handler: "liquid", - partial: false, - format: "html", - title: "Request Info Email", - required: true, - subject: "Request Info Email" - } + email_template_hash(body: request_info_email_old, title: "Request Info Email", subject: "Request Info Email", + path: "teacher_mailer/request_info_email") ]) load Rails.root.join("db/seeds.rb") From 54bd3fde046ecd24dcb43aafa74a8c071977be74 Mon Sep 17 00:00:00 2001 From: MT Date: Sat, 2 Mar 2024 11:44:13 -0800 Subject: [PATCH 34/37] finish refract email reseed test cases --- .../email_templates_reseed_spec.rb | 199 +++++++++++------- 1 file changed, 127 insertions(+), 72 deletions(-) diff --git a/spec/controllers/email_templates_reseed_spec.rb b/spec/controllers/email_templates_reseed_spec.rb index 9ec989e4..8d6b2509 100644 --- a/spec/controllers/email_templates_reseed_spec.rb +++ b/spec/controllers/email_templates_reseed_spec.rb @@ -7,6 +7,44 @@ # upon reseeding the database with new seed data. It tests the initial seeding, updates to # templates, and the non-removal of templates that are no longer present in the seed data. RSpec.describe "EmailTemplate Reseed Behavior", type: :model do + + RSpec.shared_context "setup initial email templates" do + before do + # Clear the EmailTemplate table to ensure a clean slate for each test. + # This is safe to do because it's under test environment, not production + EmailTemplate.delete_all + allow(SeedData).to receive(:emails).and_return( + [ + email_template_hash(body: form_submission_old, title: "Form Submission", subject: "Form Submission", + path: "teacher_mailer/form_submission"), + email_template_hash(body: request_info_email_old, title: "Request Info Email", subject: "Request Info Email", + path: "teacher_mailer/request_info_email") + ]) + # Simulates the seeding action. + load Rails.root.join("db/seeds.rb") + end + end + + RSpec.shared_examples "verify initial email templates" do + it "has the correct number of email templates" do + expect(EmailTemplate.count).to eq(2) + end + + it "verifies the Request Info Email template content" do + request_info_email = EmailTemplate.find_by(title: "Request Info Email") + expect(request_info_email).to be_present + expect(request_info_email.title).to eq("Request Info Email") + expect(request_info_email.body).to include("I am the original content of the request info email") + end + + it "verifies the Form Submission Email template content" do + form_submission_email = EmailTemplate.find_by(title: "Form Submission") + expect(form_submission_email).to be_present + expect(form_submission_email.title).to eq("Form Submission") + expect(form_submission_email.body).to include("I am the original content of the form submission email") + end + end + # Define email content for initial seeding and subsequent updates. These blocks # provide a flexible way to manage test data, allowing us to simulate changes in seed data # across different test scenarios. @@ -48,81 +86,98 @@ def email_template_hash(body:, title:, subject:, path:) } end - before do - # Clear the EmailTemplate table to ensure a clean slate for each test. - # This is safe to do because it's under test environment, not production - EmailTemplate.delete_all - allow(SeedData).to receive(:emails).and_return( - [ - email_template_hash(body: form_submission_old, title: "Form Submission", subject: "Form Submission", - path: "teacher_mailer/form_submission"), - email_template_hash(body: request_info_email_old, title: "Request Info Email", subject: "Request Info Email", - path: "teacher_mailer/request_info_email") - ]) - # Simulates the seeding action. - load Rails.root.join("db/seeds.rb") - - # Verify original content - expect(EmailTemplate.count).to eq(2) - request_info_email = EmailTemplate.find_by(title: "Request Info Email") - expect(request_info_email).to be_present - expect(request_info_email.title).to eq("Request Info Email") - expect(request_info_email.body).to include("I am the original content of the request info email") - - form_submission_email = EmailTemplate.find_by(title: "Form Submission") - expect(form_submission_email).to be_present - expect(form_submission_email.title).to eq("Form Submission") - expect(form_submission_email.body).to include("I am the original content of the form submission email") - end + context "when reseeded with updated content" do + include_context "setup initial email templates" + include_examples "verify initial email templates" + + context "after initial setup" do + before do + allow(SeedData).to receive(:emails).and_return( + [ + email_template_hash(body: form_submission_old, title: "Form Submission", subject: "Form Submission", + path: "teacher_mailer/form_submission"), + email_template_hash(body: request_info_email_new, title: "Request Info Email", subject: "Request Info Email", + path: "teacher_mailer/request_info_email") + ]) + load Rails.root.join("db/seeds.rb") + end + + it "maintains the expected number of email templates" do + expect(EmailTemplate.count).to eq(2) + end + + context "with the updated Request Info Email template" do + subject(:request_info_email) { EmailTemplate.find_by(title: "Request Info Email") } + + it "is present in the database" do + expect(request_info_email).to be_present + end - it "does not duplicate email templates on reseed with updated content" do - # Mock the SeedData.emails call to return updated email content for the "Request Info Email" template. - # This simulates the reseeding process with updated email content. - allow(SeedData).to receive(:emails).and_return( - [ - email_template_hash(body: form_submission_old, title: "Form Submission", subject: "Form Submission", - path: "teacher_mailer/form_submission"), - email_template_hash(body: request_info_email_new, title: "Request Info Email", subject: "Request Info Email", - path: "teacher_mailer/request_info_email") - ]) - load Rails.root.join("db/seeds.rb") - - # Verify updated content: Email count is still 2, with only updated content for the request info email. - expect(EmailTemplate.count).to eq(2) - request_info_email = EmailTemplate.find_by(title: "Request Info Email") - expect(request_info_email).to be_present - expect(request_info_email.title).to eq("Request Info Email") - expect(request_info_email.body).to include("I am NEW Template Content of the request info email") - - form_submission_email = EmailTemplate.find_by(title: "Form Submission") - expect(form_submission_email).to be_present - expect(form_submission_email.title).to eq("Form Submission") - expect(form_submission_email.body).to include("I am the original content of the form submission email") + it "reflects the updated content" do + expect(request_info_email.title).to eq("Request Info Email") + expect(request_info_email.body).to include("I am NEW Template Content of the request info email") + end + end + + context "with the unchanged Form Submission Email template" do + subject(:form_submission_email) { EmailTemplate.find_by(title: "Form Submission") } + + it "is present in the database" do + expect(form_submission_email).to be_present + end + + it "retains its original content" do + expect(form_submission_email.title).to eq("Form Submission") + expect(form_submission_email.body).to include("I am the original content of the form submission email") + end + end + end end # Test the special needs from Professor Ball: I would not have the seeds file delete templates - it "does not remove email templates not present in updated seed data" do - # Mock the SeedData.emails call to only return the "Request Info Email" template, - # simulating the removal of the "Form Submission" template from the seed data. - # Expected behavior: The "Form Submission" template should not be removed from the **database**, - # even though it's no longer present in the seed data. - allow(SeedData).to receive(:emails).and_return( - [ - email_template_hash(body: request_info_email_old, title: "Request Info Email", subject: "Request Info Email", - path: "teacher_mailer/request_info_email") - ]) - load Rails.root.join("db/seeds.rb") - - # Verify that both email templates still present and remain same - expect(EmailTemplate.count).to eq(2) - request_info_email = EmailTemplate.find_by(title: "Request Info Email") - expect(request_info_email).to be_present - expect(request_info_email.title).to eq("Request Info Email") - expect(request_info_email.body).to include("I am the original content of the request info email") - - form_submission_email = EmailTemplate.find_by(title: "Form Submission") - expect(form_submission_email).to be_present - expect(form_submission_email.title).to eq("Form Submission") - expect(form_submission_email.body).to include("I am the original content of the form submission email") + context "when reseed does not remove email templates not present in updated seed data" do + include_context "setup initial email templates" + include_examples "verify initial email templates" + + context "after initial setup" do + before do + allow(SeedData).to receive(:emails).and_return( + [ + email_template_hash(body: request_info_email_old, title: "Request Info Email", subject: "Request Info Email", + path: "teacher_mailer/request_info_email") + ]) + load Rails.root.join("db/seeds.rb") + end + + it "maintains the expected number of email templates" do + expect(EmailTemplate.count).to eq(2) + end + + context "with the Request Info Email template" do + subject(:request_info_email) { EmailTemplate.find_by(title: "Request Info Email") } + + it "is present in the database" do + expect(request_info_email).to be_present + end + + it "has unchanged content" do + expect(request_info_email.title).to eq("Request Info Email") + expect(request_info_email.body).to include("I am the original content of the request info email") + end + end + + context "with the Form Submission Email template" do + subject(:form_submission_email) { EmailTemplate.find_by(title: "Form Submission") } + + it "is present in the database" do + expect(form_submission_email).to be_present + end + + it "has unchanged content" do + expect(form_submission_email.title).to eq("Form Submission") + expect(form_submission_email.body).to include("I am the original content of the form submission email") + end + end + end end end From 5bffa763a71793625470a8950d19d1c5e9c1585c Mon Sep 17 00:00:00 2001 From: MT Date: Sat, 2 Mar 2024 11:45:20 -0800 Subject: [PATCH 35/37] move email reseed test cases to models folder --- spec/{controllers => models}/email_templates_reseed_spec.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename spec/{controllers => models}/email_templates_reseed_spec.rb (100%) diff --git a/spec/controllers/email_templates_reseed_spec.rb b/spec/models/email_templates_reseed_spec.rb similarity index 100% rename from spec/controllers/email_templates_reseed_spec.rb rename to spec/models/email_templates_reseed_spec.rb From ba4a96bb1ca8429a29a932e7c8f9dc9e10210711 Mon Sep 17 00:00:00 2001 From: Jingchao Zhong <92573736+perryzjc@users.noreply.github.com> Date: Sat, 2 Mar 2024 11:49:27 -0800 Subject: [PATCH 36/37] Fix rubocop check --- spec/models/email_templates_reseed_spec.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/models/email_templates_reseed_spec.rb b/spec/models/email_templates_reseed_spec.rb index 8d6b2509..f8ded296 100644 --- a/spec/models/email_templates_reseed_spec.rb +++ b/spec/models/email_templates_reseed_spec.rb @@ -7,7 +7,6 @@ # upon reseeding the database with new seed data. It tests the initial seeding, updates to # templates, and the non-removal of templates that are no longer present in the seed data. RSpec.describe "EmailTemplate Reseed Behavior", type: :model do - RSpec.shared_context "setup initial email templates" do before do # Clear the EmailTemplate table to ensure a clean slate for each test. From 0fe0be57948f62a826d80ff6a3060a5a7574fe8c Mon Sep 17 00:00:00 2001 From: Jingchao Zhong <92573736+perryzjc@users.noreply.github.com> Date: Sat, 2 Mar 2024 11:57:23 -0800 Subject: [PATCH 37/37] Change the way of hashing for updating email template seed --- db/seeds.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/db/seeds.rb b/db/seeds.rb index 8c6d7945..15bd64ef 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -3,8 +3,9 @@ require_relative "seed_data" SeedData.emails.each do |email_attrs| - # to, body fields are updated through development, should not be used as part of the uniqueness check - email_template = EmailTemplate.find_or_initialize_by(email_attrs.except(:to, :body)) + # Based on what Professor Ball mentioned, 'title' is the unique identifier for each EmailTemplate + unique_identifier = { title: email_attrs[:title] } + email_template = EmailTemplate.find_or_initialize_by(unique_identifier) email_template.update(email_attrs) end