Initial commit

This commit is contained in:
Senad Uka
2020-05-31 22:38:19 +02:00
commit 858fafc3c5
1280 changed files with 65918 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
require "rails_helper"
describe AddHeadshotCollectionUidToProjectJob do
describe ".perform_now" do
it "calls API with headshot collection data" do
releases = build_stubbed_list(:appearance_release, 2)
project = build_stubbed(:project, appearance_releases: releases)
stub_collection_create_api_call(collection_uid: "123abc")
mock_project_update(project)
collection = double("collection")
allow(HeadshotCollection).to receive(:new).and_return(collection)
AddHeadshotCollectionUidToProjectJob.perform_now(project)
expect(BrayniacAI::Collection).to(
have_received(:create!).
with(collection)
)
end
it "saves the ID of the created collection" do
project = build_stubbed(:project)
stub_collection_create_api_call(collection_uid: "123abc")
mock_project_update(project)
AddHeadshotCollectionUidToProjectJob.perform_now(project)
expect(project).to(
have_received(:update_attribute).
with(:headshot_collection_uid, "123abc")
)
end
end
private
def stub_collection_create_api_call(collection_uid:)
api_response = double("response", collection_id: collection_uid)
allow(BrayniacAI::Collection).to receive(:create!).and_return(api_response)
end
def mock_project_update(project)
allow(project).to receive(:update_attribute)
end
end

View File

@@ -0,0 +1,55 @@
require 'rails_helper'
RSpec.describe AnalyzeAudioJob, type: :job do
describe ".perform_now" do
it "calls API" do
video = build_stubbed(:video)
audio_analysis = instance_double(AudioAnalysis, "audio_analysis")
expect(AudioAnalysis).to receive(:new).with(video).and_return(audio_analysis)
mock_audio_recognition_create_api_call
mock_video_update(video)
AnalyzeAudioJob.perform_now video
expect(BrayniacAI::AudioRecognition).to(
have_received(:create!).
with(audio_analysis)
)
end
it "saves data about the analysis" do
video = build_stubbed(:video)
mock_audio_recognition_create_api_call(job_uid: "123abc")
mock_video_update(video)
freeze_time do
AnalyzeAudioJob.perform_now video
expect(video).to(
have_received(:update!).
with({
audio_analysis_uid: "123abc",
audio_analysis_started_at: Time.zone.now,
audio_analysis_status: :pending
})
)
end
end
end
private
def mock_audio_recognition_create_api_call(job_uid: "test")
double("response", job_id: job_uid).tap do |api_response|
allow(BrayniacAI::AudioRecognition).to(
receive(:create!).
and_return(api_response)
)
end
end
def mock_video_update(video)
allow(video).to receive(:update!)
end
end

View File

@@ -0,0 +1,71 @@
require "rails_helper"
describe AnalyzeVideoJob do
describe ".perform_now" do
it "calls API with video data" do
video = build_stubbed(:video)
video_analysis = instance_double(VideoAnalysis, "video_analysis")
expect(VideoAnalysis).to receive(:new).with(video, false).and_return(video_analysis)
mock_facial_recognition_create_api_call
mock_video_update(video)
AnalyzeVideoJob.perform_now video
expect(BrayniacAI::FacialRecognition).to(
have_received(:create!).
with(video_analysis)
)
end
it "calls API with video data and reanalysis flag" do
video = build_stubbed(:video)
video_analysis = instance_double(VideoAnalysis, "video_analysis")
expect(VideoAnalysis).to receive(:new).with(video, true).and_return(video_analysis)
mock_facial_recognition_create_api_call
mock_video_update(video)
AnalyzeVideoJob.perform_now video, reanalysis: true
expect(BrayniacAI::FacialRecognition).to(
have_received(:create!).
with(video_analysis)
)
end
it "saves data about the analysis" do
video = build_stubbed(:video)
mock_facial_recognition_create_api_call(job_uid: "123abc")
mock_video_update(video)
freeze_time do
AnalyzeVideoJob.perform_now video
expect(video).to(
have_received(:update!).
with({
analysis_uid: "123abc",
analysis_started_at: Time.zone.now,
analysis_status: :pending
})
)
end
end
end
private
def mock_facial_recognition_create_api_call(job_uid: "test")
double("response", job_id: job_uid).tap do |api_response|
allow(BrayniacAI::FacialRecognition).to(
receive(:create!).
and_return(api_response)
)
end
end
def mock_video_update(video)
allow(video).to receive(:update!)
end
end

View File

@@ -0,0 +1,48 @@
require "rails_helper"
describe AttachContractToReleasableJob do
describe ".perform_now" do
it "generates contract PDF" do
release = build_stubbed(:appearance_release)
contract = mock_contract_new
pdf = mock_contract_to_pdf(contract)
allow(release.contract).to receive(:attach)
AttachContractToReleasableJob.perform_now release
expect(Contract).to have_received(:new).with(release)
end
it "attaches it to a releaseable record" do
release = build_stubbed(:appearance_release)
contract = mock_contract_new(filename: "contract.pdf")
pdf = mock_contract_to_pdf(contract)
allow(release.contract).to receive(:attach)
AttachContractToReleasableJob.perform_now release
expect(release.contract).to(
have_received(:attach).
with({
io: pdf,
filename: "contract.pdf",
content_type: "application/pdf",
})
)
end
end
private
def mock_contract_new(filename: "")
double("contract", filename: filename).tap do |contract|
allow(Contract).to receive(:new).and_return(contract)
end
end
def mock_contract_to_pdf(contract)
double("pdf").tap do |pdf|
allow(contract).to receive(:to_pdf).and_return(pdf)
end
end
end

View File

@@ -0,0 +1,45 @@
require "rails_helper"
describe AttachRecordingToZoomMeetingJob do
let(:project) { create(:project) }
let(:zoom_meeting) { create(:zoom_meeting, project: project) }
let(:recording_hash) { {'id' => 'recording-id', 'download_url' => 'http://download.url', 'recording_start' => '2020-05-22 16:33:50 UTC', 'recording_end' => '2020-05-22 17:34:06 UTC'} }
let(:download_token) { 'download_token' }
before :each do
allow_any_instance_of(ZoomGateway).to receive(:delete_recording)
end
describe ".perform_now" do
it "downloads the video" do
allow(zoom_meeting.recording).to receive(:attach)
expect(URI).to receive(:open).with("http://download.url?access_token=download_token")
AttachRecordingToZoomMeetingJob.perform_now zoom_meeting, recording_hash, download_token
end
it "deletes the recording through the API" do
stub_uri_open
allow(zoom_meeting.recording).to receive(:attach).and_return(true)
expect_any_instance_of(ZoomGateway).to receive(:delete_recording).with(zoom_meeting.api_meeting_id, 'recording-id')
AttachRecordingToZoomMeetingJob.perform_now zoom_meeting, recording_hash, download_token
end
it "attaches downloaded video to recording" do
allow(zoom_meeting.recording).to receive(:attach)
stub_uri_open
AttachRecordingToZoomMeetingJob.perform_now zoom_meeting, recording_hash, download_token
expect(zoom_meeting.recording).to have_received(:attach).with(hash_including(content_type: 'video/mp4'))
end
end
private
def stub_uri_open
url = "http://download.url?access_token=download_token"
file = double(:file, read: 'stubbed read')
allow(URI).to receive(:open).with(url).and_return(file)
end
end

View File

@@ -0,0 +1,61 @@
require "rails_helper"
describe GenerateContractsZipJob do
let(:project) { create(:project) }
let(:download) { create(:download, project: project) }
let(:contract_template) { create(:contract_template, :project) }
before do
create(:appearance_release_with_contract_template, :native, project: project, person_name: "John Doe")
create(:appearance_release_with_contract_template, :non_native, project: project)
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(ReleaseContractCollectionService).to receive(:open).and_return(StringIO.new("file data"))
allow_any_instance_of(ReleaseContractCollectionService).to receive(:build).and_yield(dir, files)
end
describe ".perform_later" do
it "enqueues a background job for generating zip file" do
expect {
GenerateContractsZipJob.perform_later(project, download, "AppearanceRelease", project.appearance_releases.ids)
}.to have_enqueued_job
end
end
describe ".perform_now" do
it "updates a download record and creates attachment for it" do
GenerateContractsZipJob.perform_now(project, download, "AppearanceRelease", project.appearance_releases.ids)
expect(download.project).to eq project
expect(download.release_type).to eq "AppearanceRelease"
expect(download.name).to eq "my-video-project_appearance-releases"
expect(download.status).to eq "success"
expect(download.file).to be_attached
end
context "When there are errors" do
let(:error) { StandardError.new("Contracts or contract templates not found.") }
before do
allow(ProjectsChannel).to receive(:broadcast_download_generation_update).with(download, I18n.t("contract_downloads.download.failure"))
allow_any_instance_of(ReleaseContractCollectionService).to receive(:build).and_raise(StandardError, "Contracts or contract templates not found")
end
it "updates status to 'failure' and sends user a notification" do
GenerateContractsZipJob.perform_now(project, download, "AppearanceRelease", project.appearance_releases.ids)
expect(download.status).to eq "failure"
expect(ProjectsChannel).to have_received(:broadcast_download_generation_update).with(download, I18n.t("contract_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-video-project_appearance-releases.zip") if File.exists? "#{path}/my-video-project_appearance-releases.zip"
end
end

View File

@@ -0,0 +1,54 @@
require "rails_helper"
describe GenerateReportsZipJob do
let(:project) { create(:project) }
let(:download) { create(:download, project: project) }
before do
dir = Rails.root.join("spec", "fixtures", "files")
files = ["bray_edl_100319-mp4_big-cue-sheet.xlsx",
"bray_edl_100319-mp4_discovery-cue-sheet.xlsx",
"bray_edl_100319-mp4_gfx-cue-list.xlsx",
"bray_edl_100319-mp4_issues-and-concerns.xlsx",
"bray_edl_100319-mp4_production-elements-log.xlsx"]
@zip_file_name = "#{project.name.parameterize}_#{Time.now.strftime('%Y-%m-%d_%H-%M-%S')}"
allow_any_instance_of(ReportCollectionService).to receive(:build).and_yield(dir, files)
end
describe ".perform_later" do
it "enqueues a background job for generating zip file" do
expect {
GenerateReportsZipJob.perform_later(project, download, @zip_file_name)
}.to have_enqueued_job
end
end
describe ".perform_now" do
it "updates a download record and creates attachment for it" do
GenerateReportsZipJob.perform_now(project, download, @zip_file_name)
expect(download.project).to eq project
expect(download.status).to eq "success"
expect(download.file).to be_attached
end
context "When there are errors" do
before do
allow(ProjectsChannel).to receive(:broadcast_download_generation_update).with(download, I18n.t("report_downloads.download.failure"))
allow_any_instance_of(ReportCollectionService).to receive(:build).and_raise(StandardError, "Reports not found")
end
it "updates status to 'failure' and sends user a notification" do
GenerateReportsZipJob.perform_now(project, download, @zip_file_name)
expect(download.status).to eq "failure"
expect(ProjectsChannel).to have_received(:broadcast_download_generation_update).with(download, I18n.t("report_downloads.download.failure"))
end
end
end
after do
path = Rails.root.join("spec", "fixtures", "files")
File.delete("#{path}/#{@zip_file_name}.zip") if File.exists? "#{path}/#{@zip_file_name}.zip"
end
end

View File

@@ -0,0 +1,36 @@
require "rails_helper"
RSpec.describe SetTagsForReleasableJob do
context "for releasables with a single photo" do
it "detects tags for the photo and adds them to the release" do
releasable = build(:appearance_release)
tags = %w(Female Adult)
stub_brayniac_ai_api_tag_create(releasable.photo, tags)
SetTagsForReleasableJob.perform_now(releasable)
expect(releasable.internal_tag_list).to eq(tags)
end
end
context "for releasables with multiple photos" do
it "detects tags for each photo and adds them all to the release" do
releasable = build(:location_release_with_photos)
stub_brayniac_ai_api_tag_create(releasable.photos.first, %w(Restaurant Fireplace))
stub_brayniac_ai_api_tag_create(releasable.photos.last, %w(Bar Fireplace))
SetTagsForReleasableJob.perform_now(releasable)
expect(releasable.internal_tag_list).to eq(%w(Restaurant Fireplace Bar))
end
end
private
def stub_brayniac_ai_api_tag_create(photo, tags)
allow(BrayniacAI::Tag).to \
receive(:create).
with(bucket_name: ENV['AWS_BUCKET'], object_name: photo.key).
and_return(tags)
end
end

View File

@@ -0,0 +1,51 @@
require 'rails_helper'
RSpec.describe SubmitHubspotFormJob, type: :job do
describe '#perform_now' do
before do
ENV["HUBSPOT_FORM_GUID"] = "hubspot_form_guid"
end
it 'submits to the Hubspot API with the right params' do
form = double(:form)
allow(Hubspot::Form).to receive(:new).and_return(form)
allow(form).to receive(:submit).and_return(true)
SubmitHubspotFormJob.perform_now("email@test.com", "My Account")
expect(Hubspot::Form).to have_received(:new).with("guid" => "hubspot_form_guid")
expect(form).to have_received(:submit).with(
email: "email@test.com",
company: "My Account"
)
end
it 'allows additional params to be submitted' do
form = double(:form)
allow(Hubspot::Form).to receive(:new).and_return(form)
allow(form).to receive(:submit).and_return(true)
SubmitHubspotFormJob.perform_now("email@test.com", "My Account", additional_param_one: "Foo", additional_param_two: "Bar")
expect(form).to have_received(:submit).with(
email: "email@test.com",
company: "My Account",
additional_param_one: "Foo",
additional_param_two: "Bar",
)
end
context 'when HUBSPOT_FORM_GUID is not available' do
it 'does not submit to the API' do
ENV["HUBSPOT_FORM_GUID"] = nil
form = double(:form)
allow(Hubspot::Form).to receive(:new).and_return(form)
allow(form).to receive(:submit)
SubmitHubspotFormJob.perform_now("email@test.com", "My Account")
expect(form).not_to have_received(:submit)
end
end
end
end

View File

@@ -0,0 +1,49 @@
require "rails_helper"
RSpec.describe TrackAnalyticsJob, type: :job do
let(:user) { create(:user) }
let(:account) { user.primary_account }
describe ".perform_now" do
it "uses the given user and account to build an analytics tracker" do
analytics = instance_double(Analytics)
allow(Analytics).to receive(:new).and_return(analytics)
allow(analytics).to receive(:track_video_upload)
TrackAnalyticsJob.perform_now user, account, :track_video_upload, user_agent: "Mozilla Firefox", user_ip: "0.0.0.0"
expect(Analytics).to have_received(:new).with(user, account)
end
it "calls the right analytics tracking method" do
analytics = instance_double(Analytics)
allow(Analytics).to receive(:new).and_return(analytics)
allow(analytics).to receive(:track_video_upload)
TrackAnalyticsJob.perform_now user, account, :track_video_upload, user_agent: "Mozilla Firefox", user_ip: "0.0.0.0"
expect(analytics).to have_received(:track_video_upload)
end
it "passes arguments to the analytics tracking call" do
account = build(:account)
analytics = instance_double(Analytics)
allow(Analytics).to receive(:new).and_return(analytics)
allow(analytics).to receive(:track_create_native_release)
TrackAnalyticsJob.perform_now(
user, account,
:track_create_native_release,
release_type: "AppearanceRelease",
account: account,
user_agent: "Mozilla Firefox",
user_ip: "0.0.0.0",
)
expect(analytics).to(
have_received(:track_create_native_release)
.with(release_type: "AppearanceRelease", account: account, user_agent: "Mozilla Firefox", user_ip: "0.0.0.0")
)
end
end
end