require "rails_helper" describe GenerateInterviewFilesZipJob do let(:project) { create(:project) } let(:download) { create(:download, project: project, release_type: "CastingCallInterview", name: "my-title_john-doe") } let(:casting_call) { create(:casting_call, project: project, title: "My Title") } let(:casting_call_interview) { create(:casting_call_interview, casting_call: casting_call, performer_name: "John Doe") } before do dir = Rails.root.join("spec", "fixtures", "files") files = ["contract.pdf", "AppearanceRelease.pdf"] # Attachments in the test environment do not persist to cloud storage # Therefore we want to stub calls to `open` with a cloud storage URL allow_any_instance_of(InterviewFilesCollectionService).to receive(:open).and_return(StringIO.new("file data")) allow_any_instance_of(InterviewFilesCollectionService).to receive(:build).and_yield(dir, files) end describe ".perform_later" do it "enqueues a background job for generating zip file" do expect { GenerateInterviewFilesZipJob.perform_later(project, download, casting_call_interview) }.to have_enqueued_job end end describe ".perform_now" do it "updates a download record and creates attachment for it" do GenerateInterviewFilesZipJob.perform_now(project, download, casting_call_interview) expect(download.project).to eq project expect(download.release_type).to eq "CastingCallInterview" expect(download.name).to eq "my-title_john-doe" expect(download.status).to eq "success" expect(download.file).to be_attached end context "When there are errors" do let(:error) { StandardError.new("Casting Call Interview files not found.") } before do allow(ProjectsChannel).to receive(:broadcast_download_generation_update).with(download, I18n.t("interview_downloads.download.failure")) allow_any_instance_of(InterviewFilesCollectionService).to receive(:build).and_raise(StandardError, "Casting Call Interview files not found.") end it "updates status to 'failure' and sends user a notification" do GenerateInterviewFilesZipJob.perform_now(project, download, casting_call_interview) expect(download.status).to eq "failure" expect(ProjectsChannel).to have_received(:broadcast_download_generation_update).with(download, I18n.t("interview_downloads.download.failure")) end end end after do # Delete the file created in fixture. # Or the tests will fail on next run due to already existing files in existing zip. path = Rails.root.join("spec", "fixtures", "files") File.delete("#{path}/my-title_john-doe.zip") if File.exists? "#{path}/my-title_john-doe.zip" end end