From e4601a70d5358d8283274c3ba391f684251c6b2a Mon Sep 17 00:00:00 2001 From: Anderson Guimaraes Moura Date: Tue, 19 Sep 2023 01:17:15 -0300 Subject: [PATCH] Update CaseCourtReportsController & AllCasaAdmins::SessionsController controller specs to request specs (#5210) * Move controller specs for CaseCourtReportsController to request specs * Move controller specs for AllCasaAdmin::SessionsController to request specs --- .../sessions_controller_spec.rb | 26 -- .../case_court_reports_controller_spec.rb | 346 ------------------ .../requests/all_casa_admins/sessions_spec.rb | 36 ++ spec/requests/case_court_reports_spec.rb | 235 ++++++------ 4 files changed, 145 insertions(+), 498 deletions(-) delete mode 100644 spec/controllers/all_casa_admins/sessions_controller_spec.rb delete mode 100644 spec/controllers/case_court_reports_controller_spec.rb create mode 100644 spec/requests/all_casa_admins/sessions_spec.rb diff --git a/spec/controllers/all_casa_admins/sessions_controller_spec.rb b/spec/controllers/all_casa_admins/sessions_controller_spec.rb deleted file mode 100644 index 36a63f9b88..0000000000 --- a/spec/controllers/all_casa_admins/sessions_controller_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -# frozen_string_literal: true - -require "rails_helper" - -RSpec.describe AllCasaAdmins::SessionsController, type: :controller do - let(:all_casa_admin) { create(:all_casa_admin) } - - describe "Implements Devise's actions" do - before { @request.env["devise.mapping"] = Devise.mappings[:all_casa_admin] } - - it "GET new" do - get :new - expect(response).to be_successful - end - - it "POST create" do - post :create, params: {email: all_casa_admin.email, password: all_casa_admin.password} - expect(response).to be_successful - end - - it "GET destroy" do - get :destroy - expect(response).to have_http_status(:redirect) - end - end -end diff --git a/spec/controllers/case_court_reports_controller_spec.rb b/spec/controllers/case_court_reports_controller_spec.rb deleted file mode 100644 index a839b7e764..0000000000 --- a/spec/controllers/case_court_reports_controller_spec.rb +++ /dev/null @@ -1,346 +0,0 @@ -require "rails_helper" - -RSpec.describe CaseCourtReportsController, type: :controller do - include DownloadHelpers - describe "GET index" do - context "when volunteer" do - it "successfully accesses 'Generate Court Report' page" do - current_user = create(:volunteer, :with_cases_and_contacts, :with_assigned_supervisor) - - sign_in current_user - - get :index - - expect(response).to have_http_status(:ok) - expect(response).to render_template :index - expect(assigns(:assigned_cases)).not_to be_empty - end - end - - context "when supervisor" do - it "successfully accesses 'Generate Court Report' page" do - current_user = create(:volunteer, :with_cases_and_contacts, :with_assigned_supervisor) - - sign_in current_user - - get :index - - expect(response).to have_http_status(:ok) - expect(response).to render_template :index - expect(assigns(:assigned_cases)).not_to be_empty - end - - context "when there's no case in the organization" do - it "successfully accesses 'Generate Court Report' page" do - current_user = create(:supervisor, casa_org: create(:casa_org)) - - sign_in current_user - - get :index - - expect(response).to have_http_status(:ok) - expect(response).to render_template :index - expect(assigns(:assigned_cases)).to be_empty - end - end - end - end - - describe "GET show" do - context "when the case is valid" do - let(:volunteer) { create(:volunteer, :with_cases_and_contacts, :with_assigned_supervisor) } - let(:casa_case) { volunteer.casa_cases.first } - - before do - Tempfile.create do |t| - casa_case.court_reports.attach( - io: File.open(t.path), filename: "#{casa_case.case_number}.docx" - ) - end - end - - context "when volunteer" do - it "sends DOCX file in response with a success status" do - sign_in volunteer - - get :show, params: {id: casa_case.case_number, format: "docx"} - - expect(response.content_type).to eq Mime::Type.lookup_by_extension(:docx) - expect(response).to have_http_status(:ok) - end - end - - context "when supervisor" do - it "sends DOCX file in response with aa success status" do - sign_in volunteer.supervisor - - get :show, params: {id: casa_case.case_number, format: "docx"} - - expect(response.content_type).to eq Mime::Type.lookup_by_extension(:docx) - expect(response).to have_http_status(:ok) - end - end - end - - context "when the case is invalid" do - let(:volunteer) { create(:volunteer, :with_cases_and_contacts, :with_assigned_supervisor) } - - let(:invalid_casa_case) { build_stubbed(:casa_case) } - - context "when volunteer" do - it "redirects back to 'Generate Court Report' page" do - sign_in volunteer - - get :show, params: {id: invalid_casa_case.case_number, format: "docx"} - - expect(response).to redirect_to(case_court_reports_path) - expect(response.content_type).to eq "text/html; charset=utf-8" - end - end - - context "when supervisor" do - it "redirects back to 'Generate Court Report' page" do - sign_in volunteer.supervisor - - get :show, params: {id: invalid_casa_case.case_number, format: "docx"} - - expect(response).to redirect_to(case_court_reports_path) - expect(response.content_type).to eq "text/html; charset=utf-8" - end - end - end - end - - describe "POST generate" do - context "when case is valid" do - let(:volunteer) { create(:volunteer, :with_cases_and_contacts, :with_assigned_supervisor) } - let(:casa_case) { volunteer.casa_cases.first } - - context "when volunteer" do - context "when a custom template is not set" do - before do - sign_in volunteer - - request.headers["ACCEPT"] = "application/json" - - post :generate, params: { - case_court_report: {case_number: casa_case.case_number.to_s} - } - end - - it "sends response as a JSON string" do - expect(response.content_type).to eq("application/json; charset=utf-8") - expect(JSON.parse(response.body)).to be_instance_of Hash - end - - it "has keys ['link', 'status'] in JSON string" do - body_hash = JSON.parse(response.body) - - expect(body_hash).to have_key "link" - expect(body_hash).to have_key "status" - end - - it "sends response with status :ok" do - expect(response).to have_http_status(:ok) - end - - it "contains a link ending with .DOCX extension" do - body_hash = JSON.parse(response.body) - - expect(body_hash["link"]).to end_with(".docx") - end - - it "uses the default template" do - link_parts = JSON.parse(response.body)["link"].split("/") - case_number = link_parts[link_parts.length - 1].sub(".docx", "") - - get :show, params: {id: case_number, format: "docx"} - - docx_response = Docx::Document.open(StringIO.new(response.body)) - - expect(header_text(docx_response)).to include("YOUR CASA ORG’S NUMBER") - end - end - context "when a custom template is set" do - before do - stub_twillio - volunteer.casa_org.court_report_template.attach(io: File.new(Rails.root.join("app", "documents", "templates", "montgomery_report_template.docx")), filename: "montgomery_report_template.docx") - - sign_in volunteer - - request.headers["ACCEPT"] = "application/json" - - post :generate, params: { - case_court_report: {case_number: casa_case.case_number.to_s} - } - end - - it "uses the custom template" do - link_parts = JSON.parse(response.body)["link"].split("/") - case_number = link_parts[link_parts.length - 1].sub(".docx", "") - - get :show, params: {id: case_number, format: "docx"} - - download_docx = Docx::Document.open(StringIO.new(response.body)) - - expect(download_docx.paragraphs.map(&:to_s)).to include("Did you forget to enter your court orders?") - end - end - end - - context "when supervisor" do - context "when a custom template is not set" do - before do - sign_in volunteer.supervisor - - request.headers["ACCEPT"] = "application/json" - - post :generate, params: { - case_court_report: {case_number: casa_case.case_number.to_s} - } - end - - it "sends response as a JSON string" do - expect(response.content_type).to eq("application/json; charset=utf-8") - expect(JSON.parse(response.body)).to be_instance_of Hash - end - - it "has keys ['link', 'status'] in JSON string" do - body_hash = JSON.parse(response.body) - - expect(body_hash).to have_key "link" - expect(body_hash).to have_key "status" - end - - it "sends response with status :ok" do - expect(response).to have_http_status(:ok) - end - - it "contains a link ending with .DOCX extension" do - body_hash = JSON.parse(response.body) - - expect(body_hash["link"]).to end_with(".docx") - end - - it "uses the default template" do - link_parts = JSON.parse(response.body)["link"].split("/") - case_number = link_parts[link_parts.length - 1].sub(".docx", "") - - get :show, params: {id: case_number, format: "docx"} - - docx = Docx::Document.open(StringIO.new(response.body)) - expect(header_text(docx)).to include("YOUR CASA ORG’S NUMBER") - end - end - context "when a custom template is set" do - before do - stub_twillio - volunteer.casa_org.court_report_template.attach(io: File.new(Rails.root.join("app", "documents", "templates", "montgomery_report_template.docx")), filename: "montgomery_report_template.docx") - - sign_in volunteer.supervisor - - request.headers["ACCEPT"] = "application/json" - - post :generate, params: { - case_court_report: {case_number: casa_case.case_number.to_s} - } - end - - it "uses the custom template" do - link_parts = JSON.parse(response.body)["link"].split("/") - case_number = link_parts[link_parts.length - 1].sub(".docx", "") - - get :show, params: {id: case_number, format: "docx"} - - download_docx = Docx::Document.open(StringIO.new(response.body)) - - expect(download_docx.paragraphs.map(&:to_s)).to include("Did you forget to enter your court orders?") - end - end - end - end - - context "when case is invalid" do - let(:volunteer) { create(:volunteer, :with_cases_and_contacts, :with_assigned_supervisor) } - let(:casa_case) { build_stubbed(:casa_case) } - - context "when volunteer" do - before do - sign_in volunteer - - request.headers["ACCEPT"] = "application/json" - - post :generate, params: { - case_court_report: {case_number: casa_case.case_number.to_s} - } - end - - it "sends response as a JSON string" do - expect(response.content_type).to eq("application/json; charset=utf-8") - expect(JSON.parse(response.body)).to be_instance_of Hash - end - - it "has keys ['link','status','error_messages'] in JSON string" do - body_hash = JSON.parse(response.body) - - expect(body_hash).to have_key "link" - expect(body_hash).to have_key "status" - expect(body_hash).to have_key "error_messages" - end - - it "sends response with status :not_found" do - expect(response).to have_http_status(:not_found) - end - - it "contains a empty link" do - body_hash = JSON.parse(response.body) - - expect(body_hash["link"].length).to be 0 - end - end - - context "when supervisor" do - before do - sign_in volunteer.supervisor - - request.headers["ACCEPT"] = "application/json" - - post :generate, params: { - case_court_report: {case_number: casa_case.case_number.to_s} - } - end - - it "sends response as a JSON string" do - expect(response.content_type).to eq("application/json; charset=utf-8") - expect(JSON.parse(response.body)).to be_instance_of Hash - end - - it "has keys ['link','status','error_messages'] in JSON string" do - body_hash = JSON.parse(response.body) - - expect(body_hash).to have_key "link" - expect(body_hash).to have_key "status" - expect(body_hash).to have_key "error_messages" - end - - it "sends response with status :not_found" do - expect(response).to have_http_status(:not_found) - end - - it "contains a empty link" do - body_hash = JSON.parse(response.body) - - expect(body_hash["link"].length).to be 0 - end - end - end - end -end - -def stub_twillio - twillio_client = instance_double(Twilio::REST::Client) - messages = instance_double(Twilio::REST::Api::V2010::AccountContext::MessageList) - allow(Twilio::REST::Client).to receive(:new).with("Aladdin", "open sesame", "articuno34").and_return(twillio_client) - allow(twillio_client).to receive(:messages).and_return(messages) - allow(messages).to receive(:list).and_return([]) -end diff --git a/spec/requests/all_casa_admins/sessions_spec.rb b/spec/requests/all_casa_admins/sessions_spec.rb new file mode 100644 index 0000000000..95192286a9 --- /dev/null +++ b/spec/requests/all_casa_admins/sessions_spec.rb @@ -0,0 +1,36 @@ +require "rails_helper" + +RSpec.describe "AllCasaAdmin::SessionsController", type: :request do + let(:all_casa_admin) { create(:all_casa_admin) } + + describe "GET /new" do + subject(:request) do + get new_all_casa_admin_session_path + + response + end + + it { is_expected.to be_successful } + end + + describe "POST /create" do + let(:params) { {email: all_casa_admin.email, password: all_casa_admin.password} } + subject(:request) do + post all_casa_admin_session_path + + response + end + + it { is_expected.to be_successful } + end + + describe "GET /destroy" do + subject(:request) do + get destroy_all_casa_admin_session_path + + response + end + + it { is_expected.to have_http_status(:redirect) } + end +end diff --git a/spec/requests/case_court_reports_spec.rb b/spec/requests/case_court_reports_spec.rb index 42ca28a2b0..0a793308f2 100644 --- a/spec/requests/case_court_reports_spec.rb +++ b/spec/requests/case_court_reports_spec.rb @@ -11,7 +11,7 @@ # case_court_reports#index describe "GET /case_court_reports" do context "as volunteer" do - it "can view 'Generate Court Report' page" do + it "can view 'Generate Court Report' page", :aggregate_failures do get case_court_reports_path expect(response).to be_successful expect(assigns(:assigned_cases)).to_not be_empty @@ -25,7 +25,7 @@ sign_in supervisor end - it "can view the 'Generate Court Report' page" do + it "can view the 'Generate Court Report' page", :aggregate_failures do get case_court_reports_path expect(response).to be_successful expect(assigns(:assigned_cases)).to_not be_empty @@ -34,7 +34,7 @@ context "with no cases in the organization" do let(:supervisor) { create(:supervisor, casa_org: create(:casa_org)) } - it "can view 'Generate Court Report page" do + it "can view 'Generate Court Report page", :aggregate_failures do get case_court_reports_path expect(response).to be_successful expect(assigns(:assigned_cases)).to be_empty @@ -54,15 +54,25 @@ io: File.open(t.path), filename: "#{casa_case.case_number}.docx" ) end + end + + subject(:request) do get case_court_report_path(casa_case.case_number, format: "docx") + + response + end + + it "authorizes action" do + expect_any_instance_of(CaseCourtReportsController).to receive(:authorize).with(CaseCourtReport).and_call_original + request end it "send response as a .DOCX file" do - expect(response.content_type).to eq Mime::Type.lookup_by_extension(:docx) + expect(request.content_type).to eq Mime::Type.lookup_by_extension(:docx) end it "send response with a status :ok" do - expect(response).to have_http_status(:ok) + expect(request).to have_http_status(:ok) end end @@ -74,182 +84,155 @@ get case_court_report_path(invalid_casa_case.case_number, format: "docx") end - it "redirects back to 'Generate Court Report' page", js: true do + it "redirects back to 'Generate Court Report' page", :aggregate_failures, js: true do expect(response).to redirect_to(case_court_reports_path) expect(response.content_type).to eq "text/html; charset=utf-8" + end - follow_redirect! - - expect(response.content_type).to eq "text/html; charset=utf-8" - expect(response.body).to match(/
Generate Court Report<\/h6>/) - expect(response.request.flash.to_h).to have_key("alert") - expect(response.body).to match(/