Initial commit
This commit is contained in:
101
spec/controllers/api/acquired_media_releases_controller_spec.rb
Normal file
101
spec/controllers/api/acquired_media_releases_controller_spec.rb
Normal file
@@ -0,0 +1,101 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Api::AcquiredMediaReleasesController, type: :controller do
|
||||
let(:current_user) { create(:user) }
|
||||
let(:project) { create(:project, name: 'first', account: current_user.primary_account) }
|
||||
|
||||
describe '#index' do
|
||||
it 'returns a succesful response' do
|
||||
create(:acquired_media_release, name: 'ct1', project_id: project.id)
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
get :index, params: { project_id: project.id }
|
||||
|
||||
expect(response).to be_successful
|
||||
old_body = response.body
|
||||
|
||||
get :index, params: { project_id: project.id, updated_since: (DateTime.current + 10.days).to_date.to_s }
|
||||
expect(response.body).not_to eq(old_body)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#show' do
|
||||
it 'returns a succesful response' do
|
||||
tested_release = create(:acquired_media_release, name: 'ct1', project_id: project.id)
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
get :show, params: { id: tested_release.id }
|
||||
|
||||
expect(response).to be_successful
|
||||
end
|
||||
end
|
||||
|
||||
describe '#create' do
|
||||
before do
|
||||
template = create(:contract_template, name: 'ct1', project_id: project.id)
|
||||
signature = signature_base64
|
||||
@parameters = {
|
||||
contract_template_id: template.id,
|
||||
acquired_media_release: {
|
||||
signature: signature,
|
||||
person_phone: '123',
|
||||
name: 'aaa',
|
||||
signed_at: '2020-01-01 16:50:26 UTC'
|
||||
}
|
||||
}
|
||||
sign_in_to_api(current_user)
|
||||
end
|
||||
|
||||
it 'returns 201 - created response' do
|
||||
post :create, params: @parameters
|
||||
expect(response.status).to eq 201
|
||||
|
||||
expect(AcquiredMediaRelease.last.signature).to be_attached
|
||||
end
|
||||
|
||||
it 'saves signed on date correctly' do
|
||||
post :create, params: @parameters
|
||||
expect(response.status).to eq 201
|
||||
expect(AcquiredMediaRelease.last.signed_on).to eq '01/01/20'
|
||||
end
|
||||
|
||||
it 'runs attach contract to background job' do
|
||||
expect do
|
||||
post :create, params: @parameters
|
||||
end.to have_enqueued_job(AttachContractToReleasableJob).with(AcquiredMediaRelease.last)
|
||||
end
|
||||
|
||||
it 'runs set tags for background releasable job' do
|
||||
expect do
|
||||
post :create, params: @parameters
|
||||
end.to have_enqueued_job(SetTagsForReleasableJob).with(AcquiredMediaRelease.last)
|
||||
end
|
||||
|
||||
it 'fails if there is no signature' do
|
||||
template = create(:contract_template, name: 'ct1', project_id: project.id)
|
||||
parameters = {
|
||||
contract_template_id: template.id,
|
||||
acquired_media_release: {
|
||||
name: 'aaa'
|
||||
}
|
||||
}
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
expect do
|
||||
post :create, params: parameters
|
||||
end.to raise_error(ActiveRecord::RecordInvalid)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def signature_base64
|
||||
@signature_base64 ||= Base64Image.from_image(file_fixture('signature.png')).data_uri
|
||||
end
|
||||
|
||||
def response_body_data_attributes
|
||||
JSON.parse(response.body).dig('data', 'attributes')
|
||||
end
|
||||
end
|
||||
219
spec/controllers/api/appearance_releases_controller_spec.rb
Normal file
219
spec/controllers/api/appearance_releases_controller_spec.rb
Normal file
@@ -0,0 +1,219 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Api::AppearanceReleasesController, type: :controller do
|
||||
let(:current_user) { create(:user) }
|
||||
let(:project) { create(:project, name: 'first', account: current_user.primary_account) }
|
||||
|
||||
before do
|
||||
allow(BrayniacAI::Validation).to receive(:create).and_return(double(:validation, valid: true))
|
||||
end
|
||||
|
||||
describe '#index' do
|
||||
it 'returns a succesful response' do
|
||||
create(:appearance_release, person_first_name: 'ct1', person_last_name: 'ct12', project_id: project.id)
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
get :index, params: { project_id: project.id }
|
||||
|
||||
expect(response).to be_successful
|
||||
old_body = response.body
|
||||
|
||||
get :index, params: { project_id: project.id, updated_since: (DateTime.current + 10.days).to_date.to_s }
|
||||
expect(response.body).not_to eq(old_body)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#show' do
|
||||
it 'returns a succesful response' do
|
||||
tested_release = create(:appearance_release, person_first_name: 'ct1', person_last_name: 'ct12', project_id: project.id)
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
get :show, params: { id: tested_release.id }
|
||||
|
||||
expect(response).to be_successful
|
||||
end
|
||||
|
||||
it 'includes photos' do
|
||||
project = create(:project, account_id: current_user.primary_account.id)
|
||||
appearance_release = create(:appearance_release, person_first_name: 'Release', person_last_name: 'Name', project: project)
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
get :show, params: { id: appearance_release.id }
|
||||
|
||||
photo = appearance_release.person_photo
|
||||
data = {
|
||||
'id' => photo.id.to_s,
|
||||
'type' => 'active_storage_attachment',
|
||||
'attributes' => {
|
||||
'content_type' => photo.content_type,
|
||||
'filename' => photo.filename.to_s,
|
||||
'url' => photo_path_for(photo),
|
||||
'thumbnail_url' => photo_variant_path_for(photo, '150x150')
|
||||
}
|
||||
}
|
||||
expect(response_body_included_attributes).to include(data)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#create' do
|
||||
before do
|
||||
template = create(:contract_template, name: 'ct1', project_id: project.id)
|
||||
small_photo = photo_base64
|
||||
guardian_photo = guardian_photo_base64
|
||||
signature = signature_base64
|
||||
@parameters = {
|
||||
contract_template_id: template.id,
|
||||
appearance_release: {
|
||||
signature: signature,
|
||||
person_phone: '123',
|
||||
person_first_name: 'aaa',
|
||||
person_last_name: 'bbb',
|
||||
minor: true,
|
||||
guardian_first_name: 'John',
|
||||
guardian_last_name: 'Doe',
|
||||
guardian_phone: '9191919191',
|
||||
signed_at: '2020-01-01 16:50:26 UTC',
|
||||
person_photo: {
|
||||
io: small_photo,
|
||||
filename: 'filenameee.jpg'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@guardian_info = {
|
||||
minor: true,
|
||||
guardian_name: 'Guardian',
|
||||
guardian_phone: '101010'
|
||||
}
|
||||
|
||||
@guardian_photo = {
|
||||
guardian_photo: {
|
||||
io: guardian_photo,
|
||||
filename: 'guardianPhoto.jpeg'
|
||||
}
|
||||
}
|
||||
sign_in_to_api(current_user)
|
||||
end
|
||||
|
||||
it 'returns 201 - created response' do
|
||||
post :create, params: @parameters
|
||||
expect(response.status).to eq 201
|
||||
|
||||
expect(AppearanceRelease.last.signature).to be_attached
|
||||
end
|
||||
|
||||
it 'saves signed on date correctly' do
|
||||
post :create, params: @parameters
|
||||
expect(response.status).to eq 201
|
||||
expect(AppearanceRelease.last.signed_on).to eq '01/01/20'
|
||||
end
|
||||
|
||||
|
||||
it 'runs attach contract to background job' do
|
||||
expect do
|
||||
post :create, params: @parameters
|
||||
end.to have_enqueued_job(AttachContractToReleasableJob).with(AppearanceRelease.last)
|
||||
end
|
||||
|
||||
it 'runs add headshot collection uid background job' do
|
||||
expect do
|
||||
post :create, params: @parameters
|
||||
end.to have_enqueued_job(AddHeadshotCollectionUidToProjectJob).with(project)
|
||||
end
|
||||
|
||||
it 'runs set tags for background releasable job' do
|
||||
expect do
|
||||
post :create, params: @parameters
|
||||
end.to have_enqueued_job(SetTagsForReleasableJob).with(AppearanceRelease.last)
|
||||
end
|
||||
|
||||
it 'fails if there is no signature' do
|
||||
template = create(:contract_template, name: 'ct1', project_id: project.id)
|
||||
small_photo = photo_base64
|
||||
parameters = {
|
||||
contract_template_id: template.id,
|
||||
appearance_release: {
|
||||
person_name: 'aaa'
|
||||
}
|
||||
}
|
||||
|
||||
parameters[:appearance_release][:person_photo] = {
|
||||
io: small_photo,
|
||||
filename: 'filenameee.jpg'
|
||||
}
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
expect do
|
||||
post :create, params: parameters
|
||||
end.to raise_error(ActiveRecord::RecordInvalid)
|
||||
end
|
||||
|
||||
it 'returns 201 for minor without guardian photo' do
|
||||
params_without_guardian_photo = @parameters
|
||||
params_without_guardian_photo[:appearance_release].merge!(@guardian_info)
|
||||
|
||||
post :create, params: params_without_guardian_photo
|
||||
expect(response.status).to eq 201
|
||||
expect(AppearanceRelease.last.signature).to be_attached
|
||||
expect(AppearanceRelease.last.guardian_photo).not_to be_attached
|
||||
end
|
||||
|
||||
it 'returns 201 for minor with guardian photo' do
|
||||
params_with_guardian_photo = @parameters
|
||||
params_with_guardian_photo[:appearance_release].merge!(@guardian_info, @guardian_photo)
|
||||
|
||||
post :create, params: params_with_guardian_photo
|
||||
expect(response.status).to eq 201
|
||||
|
||||
appearance_release = AppearanceRelease.last
|
||||
photo = appearance_release.guardian_photo
|
||||
data = {
|
||||
'id' => photo.id.to_s,
|
||||
'type' => 'active_storage_attachment',
|
||||
'attributes' => {
|
||||
'content_type' => photo.content_type,
|
||||
'filename' => photo.filename.to_s,
|
||||
'url' => photo_path_for(photo),
|
||||
'thumbnail_url' => photo_variant_path_for(photo, '150x150')
|
||||
}
|
||||
}
|
||||
expect(response_body_included_attributes).to include(data)
|
||||
expect(appearance_release.signature).to be_attached
|
||||
expect(appearance_release.guardian_photo).to be_attached
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def photo_base64
|
||||
@photo_base64 ||= Base64Image.from_image(file_fixture('person_photo.png')).data_uri
|
||||
end
|
||||
|
||||
def guardian_photo_base64
|
||||
@guardian_photo_base64 ||= Base64Image.from_image(file_fixture('pratt.jpg')).data_uri
|
||||
end
|
||||
|
||||
def signature_base64
|
||||
@signature_base64 ||= Base64Image.from_image(file_fixture('signature.png')).data_uri
|
||||
end
|
||||
|
||||
def photo_path_for(attachment)
|
||||
Rails.application.routes.url_helpers.rails_blob_url(attachment, host: AppHost.new.domain_with_port)
|
||||
end
|
||||
|
||||
def photo_variant_path_for(attachment, _size)
|
||||
Rails.application.routes.url_helpers.rails_representation_url(attachment.variant(resize: '150x150'), host: AppHost.new.domain_with_port)
|
||||
end
|
||||
|
||||
def response_body_data_attributes
|
||||
JSON.parse(response.body).dig('data', 'attributes')
|
||||
end
|
||||
|
||||
def response_body_included_attributes
|
||||
JSON.parse(response.body).dig('included')
|
||||
end
|
||||
end
|
||||
156
spec/controllers/api/broadcasts_controller_spec.rb
Normal file
156
spec/controllers/api/broadcasts_controller_spec.rb
Normal file
@@ -0,0 +1,156 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Api::BroadcastsController, type: :controller do
|
||||
let(:current_user) { create(:user, :admin) }
|
||||
|
||||
describe '#index' do
|
||||
before do
|
||||
allow(MuxLiveStream).to receive(:new).and_return(double(id: 'id', key: 'key', playback_id: 'playback_id'))
|
||||
end
|
||||
|
||||
it 'returns a list of all broadcasts ready for streaming in the project' do
|
||||
project = create(:project, name: 'first', account_id: current_user.primary_account.id)
|
||||
br1 = create(:broadcast, project: project, name: 'Created Bc', status: 'created')
|
||||
br2 = create(:broadcast, project: project, name: 'Idle Bc', status: 'idle')
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
get :index, params: { project_id: project.id }
|
||||
|
||||
expect(response).to be_successful
|
||||
|
||||
expect(response.body).to have_content('Created Bc')
|
||||
expect(response.body).to have_content('Idle Bc')
|
||||
expect(response.body).to have_content('created')
|
||||
expect(response.body).to have_content('idle')
|
||||
expect(response.body).to have_content('created_at')
|
||||
end
|
||||
|
||||
it 'returns empty list if there are no broadcasts' do
|
||||
project = create(:project, name: 'empty', account_id: current_user.primary_account.id)
|
||||
sign_in_to_api(current_user)
|
||||
get :index, params: { project_id: project.id }
|
||||
expect(response).to be_successful
|
||||
|
||||
expect(response.body).to have_content('[]')
|
||||
end
|
||||
|
||||
it 'does not return broadcasts that are not ready to stream' do
|
||||
project = create(:project, name: 'first', account_id: current_user.primary_account.id)
|
||||
br1 = create(:broadcast, project: project, name: 'Created Bc', status: 'created')
|
||||
br2 = create(:broadcast, project: project, name: 'Active Bc', status: 'active')
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
get :index, params: { project_id: project.id }
|
||||
|
||||
expect(response).to be_successful
|
||||
|
||||
expect(response.body).to have_content('Created Bc')
|
||||
expect(response.body).to have_content('created')
|
||||
expect(response.body).not_to have_content('Active Bc')
|
||||
expect(response.body).not_to have_content('active')
|
||||
end
|
||||
|
||||
it 'does not return broadcasts from another project' do
|
||||
project = create(:project, name: 'empty', account_id: current_user.primary_account.id)
|
||||
project2 = create(:project, name: 'with broadcasts', account_id: current_user.primary_account.id)
|
||||
create(:broadcast, project: project2, name: 'Created Bc', status: 'created')
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
get :index, params: { project_id: project.id }
|
||||
expect(response).to be_successful
|
||||
|
||||
expect(response.body).to have_content('[]')
|
||||
end
|
||||
|
||||
it 'does not return broadcasts from another account' do
|
||||
second_account = create(:account, name: 'second')
|
||||
project = create(:project, name: 'empty', account_id: current_user.primary_account.id)
|
||||
project2 = create(:project, name: 'with broadcasts', account_id: second_account.id)
|
||||
create(:broadcast, project: project2, name: 'Created Bc', status: 'created')
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
get :index, params: { project_id: project.id }
|
||||
expect(response).to be_successful
|
||||
|
||||
expect(response.body).to have_content('[]')
|
||||
end
|
||||
end
|
||||
|
||||
describe '#show' do
|
||||
it 'returns a single broadcast' do
|
||||
project = create(:project, name: 'first', account_id: current_user.primary_account.id)
|
||||
br1 = create(:broadcast, :with_stream, skip_create_callback: true, project: project, name: 'Created Bc', status: 'created')
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
get :show, params: { project_id: project.id, id: br1.id }
|
||||
|
||||
expect(response).to be_successful
|
||||
|
||||
expect(response.body).to have_content('Created Bc')
|
||||
expect(response.body).to have_content('created')
|
||||
expect(response.body).to have_content('created_at')
|
||||
end
|
||||
|
||||
it 'includes files relationship' do
|
||||
project = create(:project, name: 'first', account_id: current_user.primary_account.id)
|
||||
broadcast = create(:broadcast, :with_stream, :with_files, skip_create_callback: true, project: project)
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
get :show, params: { project_id: project.id, id: broadcast.id }
|
||||
|
||||
expect(response).to be_successful
|
||||
|
||||
relationships = JSON.parse(response.body).dig('data', 'relationships')
|
||||
included = JSON.parse(response.body).dig('included')
|
||||
|
||||
expect(relationships.keys).to include('files')
|
||||
expect(included.size).to eq 1
|
||||
expect(included.first.dig("id")).to eq broadcast.files.first.id.to_s
|
||||
expect(included.first.dig("type")).to eq 'active_storage_attachment'
|
||||
end
|
||||
end
|
||||
|
||||
describe "#update" do
|
||||
it "uploads files to broadcast" do
|
||||
project = create(:project, name: 'first', account_id: current_user.primary_account.id)
|
||||
broadcast = create(:broadcast, :with_stream, skip_create_callback: true, project: project, name: 'Created Bc', status: 'created')
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
patch :update, params: file_params(project, broadcast)
|
||||
|
||||
relationships = JSON.parse(response.body).dig('data', 'relationships')
|
||||
included = JSON.parse(response.body).dig('included')
|
||||
|
||||
expect(relationships.keys).to include('files')
|
||||
expect(included.size).to eq 1
|
||||
expect(included.first.dig("id")).to eq broadcast.files.first.id.to_s
|
||||
expect(included.first.dig("type")).to eq 'active_storage_attachment'
|
||||
end
|
||||
end
|
||||
|
||||
after do
|
||||
# Set the callback again or it will affect other test cases where the callback is required
|
||||
Broadcast.set_callback(:create, :after, :create_mux_live_stream)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def file_params(project, broadcast)
|
||||
{
|
||||
:project_id => project.id,
|
||||
:id => broadcast.id,
|
||||
:broadcast => {
|
||||
files: [{
|
||||
io: file_base64,
|
||||
filename: "person_photo.png"
|
||||
}]
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
def file_base64
|
||||
Base64Image.from_image(file_fixture('person_photo.png')).data_uri
|
||||
end
|
||||
end
|
||||
44
spec/controllers/api/contract_templates_controller_spec.rb
Normal file
44
spec/controllers/api/contract_templates_controller_spec.rb
Normal file
@@ -0,0 +1,44 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Api::ContractTemplatesController, type: :controller do
|
||||
let(:current_user) { create(:user) }
|
||||
let(:project) { create(:project, name: 'first', account: current_user.primary_account) }
|
||||
|
||||
describe '#index' do
|
||||
it 'returns a succesful response' do
|
||||
create(:contract_template, name: 'ct1', project_id: project.id)
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
get :index, params: { project_id: project.id }
|
||||
|
||||
expect(response).to be_successful
|
||||
old_body = response.body
|
||||
|
||||
get :index, params: { project_id: project.id, updated_since: (DateTime.current + 10.days).to_date.to_s }
|
||||
expect(response.body).not_to eq(old_body)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#show' do
|
||||
it 'returns a succesful response' do
|
||||
tested_release = create(:contract_template, name: 'ct1', project_id: project.id)
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
get :show, params: { id: tested_release.id }
|
||||
|
||||
expect(response).to be_successful
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def response_body_data_attributes
|
||||
JSON.parse(response.body).dig('data', 'attributes')
|
||||
end
|
||||
|
||||
def response_body_included_attributes
|
||||
JSON.parse(response.body).dig('included')
|
||||
end
|
||||
end
|
||||
48
spec/controllers/api/notes_controller_spec.rb
Normal file
48
spec/controllers/api/notes_controller_spec.rb
Normal file
@@ -0,0 +1,48 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
releases = [
|
||||
{
|
||||
type: :appearance_release,
|
||||
obligatory_attribute: :person_name
|
||||
},
|
||||
{
|
||||
type: :talent_release,
|
||||
obligatory_attribute: :person_name
|
||||
},
|
||||
{
|
||||
type: :location_release,
|
||||
obligatory_attribute: :name
|
||||
},
|
||||
{
|
||||
type: :material_release,
|
||||
obligatory_attribute: :name
|
||||
}
|
||||
]
|
||||
|
||||
releases.each do |release|
|
||||
RSpec.describe Api::NotesController, type: :controller do
|
||||
let(:current_user) { create(:user, :admin) }
|
||||
|
||||
describe '#create' do
|
||||
it 'creates a note and returns a created response' do
|
||||
project = create(:project, name: 'first', account_id: current_user.primary_account.id)
|
||||
tested_release = create(release[:type], release[:obligatory_attribute] => 'contract template 1', :project_id => project.id)
|
||||
sign_in_to_api(current_user)
|
||||
post :create, params: { "#{release[:type]}_id": tested_release.id, note: { content: 'blah blah' } }
|
||||
expect(response.status).to eq 201
|
||||
end
|
||||
end
|
||||
|
||||
describe '#index' do
|
||||
it 'returns all notes for the release' do
|
||||
project = create(:project, name: 'first', account_id: current_user.primary_account.id)
|
||||
tested_release = create(release[:type], release[:obligatory_attribute] => 'contract template 1', :project_id => project.id)
|
||||
sign_in_to_api(current_user)
|
||||
get :index, params: { "#{release[:type]}_id": tested_release.id }
|
||||
expect(response).to be_successful
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
29
spec/controllers/api/profiles_controller_spec.rb
Normal file
29
spec/controllers/api/profiles_controller_spec.rb
Normal file
@@ -0,0 +1,29 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Api::ProfilesController, type: :controller do
|
||||
let(:current_user) { create(:user) }
|
||||
|
||||
describe '#show' do
|
||||
it 'responds with profile info for the current user' do
|
||||
sign_in_to_api(current_user)
|
||||
|
||||
get :show
|
||||
|
||||
expect(response).to be_successful
|
||||
expect(response_body_data).to include('id' => current_user.to_param, 'type' => 'user')
|
||||
expect(response_body_data_attributes).to include('email' => current_user.email)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def response_body_data
|
||||
JSON.parse(response.body).dig('data')
|
||||
end
|
||||
|
||||
def response_body_data_attributes
|
||||
response_body_data.dig('attributes')
|
||||
end
|
||||
end
|
||||
22
spec/controllers/api/projects_controller_spec.rb
Normal file
22
spec/controllers/api/projects_controller_spec.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Api::ProjectsController, type: :controller do
|
||||
let(:current_user) { create(:user, :admin) }
|
||||
|
||||
describe '#index' do
|
||||
it 'returns a succesful response with and without updated_since param' do
|
||||
create(:project, name: 'first', account_id: current_user.primary_account.id, updated_at: 10.days.ago)
|
||||
create(:project, name: 'second', account_id: current_user.primary_account.id, updated_at: 10.days.ago)
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
get :index
|
||||
|
||||
expect(response).to be_successful
|
||||
|
||||
expect(response.body).to have_content('first')
|
||||
expect(response.body).to have_content('second')
|
||||
end
|
||||
end
|
||||
end
|
||||
204
spec/controllers/api/releases_controller_spec.rb
Normal file
204
spec/controllers/api/releases_controller_spec.rb
Normal file
@@ -0,0 +1,204 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
releases = [
|
||||
{
|
||||
type: :location_release,
|
||||
obligatory_attribute: :name
|
||||
},
|
||||
{
|
||||
type: :material_release,
|
||||
obligatory_attribute: :name
|
||||
}
|
||||
]
|
||||
|
||||
releases.each do |release|
|
||||
controller = "Api::#{release[:type].to_s.pluralize.camelize}Controller".constantize
|
||||
|
||||
RSpec.describe controller, type: :controller do
|
||||
let(:current_user) { create(:user) }
|
||||
let(:project) { create(:project, name: 'first', account: current_user.primary_account) }
|
||||
|
||||
describe '#index' do
|
||||
it 'returns a succesful response' do
|
||||
r = create(release[:type], release[:obligatory_attribute] => 'contract template 1', :project_id => project.id)
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
get :index, params: { project_id: project.id }
|
||||
|
||||
expect(response).to be_successful
|
||||
old_body = response.body
|
||||
|
||||
get :index, params: { project_id: project.id, updated_since: (DateTime.current + 10.days).to_date.to_s }
|
||||
expect(response.body).not_to eq(old_body)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#show' do
|
||||
it 'returns a succesful response' do
|
||||
tested_release = create(release[:type], release[:obligatory_attribute] => 'contract template 1', :project_id => project.id)
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
get :show, params: { id: tested_release.id }
|
||||
|
||||
expect(response).to be_successful
|
||||
end
|
||||
|
||||
it 'includes photos' do
|
||||
tested_release = create("#{release[:type]}_with_photo", release[:obligatory_attribute] => 'Release Name', project: project)
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
get :show, params: { id: tested_release.id }
|
||||
|
||||
tested_release.photos.each do |photo|
|
||||
data = {
|
||||
'id' => photo.id.to_s,
|
||||
'type' => 'active_storage_attachment',
|
||||
'attributes' => {
|
||||
'content_type' => photo.content_type,
|
||||
'filename' => photo.filename.to_s,
|
||||
'url' => photo_path_for(photo),
|
||||
'thumbnail_url' => photo_variant_path_for(photo, '150x150')
|
||||
}
|
||||
}
|
||||
|
||||
expect(response_body_included_attributes).to include(data)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#create' do
|
||||
before do
|
||||
template = create(:contract_template, name: 'contract template 1', project_id: project.id)
|
||||
small_photo = photo_base64
|
||||
signature = signature_base64
|
||||
@parameters = {
|
||||
contract_template_id: template.id,
|
||||
release[:type] => {
|
||||
release[:obligatory_attribute] => 'aaa',
|
||||
:signature => signature,
|
||||
:person_phone => '123',
|
||||
:person_first_name => 'aaa',
|
||||
:person_last_name => 'aaa',
|
||||
:signed_at => '2020-01-01 16:50:26 UTC',
|
||||
:photos => [{
|
||||
io: small_photo,
|
||||
filename: 'photo1.jpg'
|
||||
}]
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
end
|
||||
|
||||
it 'returns 201 - created response' do
|
||||
post :create, params: @parameters
|
||||
releasable = release[:type].to_s.classify.constantize.last
|
||||
|
||||
expect(response.status).to eq 201
|
||||
|
||||
expect(releasable.signature).to be_attached
|
||||
end
|
||||
|
||||
it 'saves signed on date correctly' do
|
||||
post :create, params: @parameters
|
||||
expect(response.status).to eq 201
|
||||
|
||||
expect(release[:type].to_s.classify.constantize.last.signed_on).to eq '01/01/20'
|
||||
end
|
||||
|
||||
it 'runs attach contract to background job' do
|
||||
expect do
|
||||
post :create, params: @parameters
|
||||
end.to have_enqueued_job(AttachContractToReleasableJob).with(release[:type].to_s.classify.constantize.last)
|
||||
end
|
||||
|
||||
it 'runs set tags for releasable background job' do
|
||||
expect do
|
||||
post :create, params: @parameters
|
||||
end.to have_enqueued_job(SetTagsForReleasableJob).with(release[:type].to_s.classify.constantize.last)
|
||||
end
|
||||
|
||||
it 'fails if there is no signature' do
|
||||
template = create(:contract_template, name: 'ct1', project_id: project.id)
|
||||
small_photo = photo_base64
|
||||
parameters = {
|
||||
contract_template_id: template.id,
|
||||
release[:type] => {
|
||||
release[:obligatory_attribute] => 'aaa bbb'
|
||||
}
|
||||
}
|
||||
|
||||
if release[:type] == :appearance_release
|
||||
parameters[release[:type]][:person_photo] = {
|
||||
io: small_photo,
|
||||
filename: 'filenameee.jpg'
|
||||
}
|
||||
else
|
||||
parameters[release[:type]][:photos] = [{
|
||||
io: small_photo,
|
||||
filename: 'photo1.jpg'
|
||||
}]
|
||||
end
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
expect do
|
||||
post :create, params: parameters
|
||||
end.to raise_error(ActiveRecord::RecordInvalid)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#update' do
|
||||
it 'returns 200 - successful response' do
|
||||
tested_release = create(release[:type], release[:obligatory_attribute] => 'contract template 1',
|
||||
:project_id => project.id,
|
||||
:person_phone => '123',
|
||||
:person_name => 'aaa bbb')
|
||||
|
||||
small_photo = photo_base64
|
||||
|
||||
parameters = {
|
||||
:id => tested_release.id,
|
||||
release[:type] => {
|
||||
photos: [{
|
||||
io: small_photo,
|
||||
filename: 'photo1.jpg'
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
put :update, params: parameters
|
||||
expect(response.status).to eq 200
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def photo_base64
|
||||
@photo_base64 ||= Base64Image.from_image(file_fixture('person_photo.png')).data_uri
|
||||
end
|
||||
|
||||
def signature_base64
|
||||
@signature_base64 ||= Base64Image.from_image(file_fixture('signature.png')).data_uri
|
||||
end
|
||||
|
||||
def photo_path_for(attachment)
|
||||
Rails.application.routes.url_helpers.rails_blob_url(attachment, host: AppHost.new.domain_with_port)
|
||||
end
|
||||
|
||||
def photo_variant_path_for(attachment, _size)
|
||||
Rails.application.routes.url_helpers.rails_representation_url(attachment.variant(resize: '150x150'), host: AppHost.new.domain_with_port)
|
||||
end
|
||||
|
||||
def response_body_data_attributes
|
||||
JSON.parse(response.body).dig('data', 'attributes')
|
||||
end
|
||||
|
||||
def response_body_included_attributes
|
||||
JSON.parse(response.body).dig('included')
|
||||
end
|
||||
end
|
||||
141
spec/controllers/api/sync_controller_spec.rb
Normal file
141
spec/controllers/api/sync_controller_spec.rb
Normal file
@@ -0,0 +1,141 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Api::SyncController, type: :controller do
|
||||
let(:current_user) { create(:user, :admin) }
|
||||
|
||||
before do
|
||||
sign_in_to_api(current_user)
|
||||
end
|
||||
|
||||
describe '#index' do
|
||||
it 'dumps all the data when parameter since not suplied' do
|
||||
create_default_data
|
||||
|
||||
get :index
|
||||
|
||||
expect(response).to be_successful
|
||||
end
|
||||
|
||||
it 'does not leak data from other accounts' do
|
||||
another_user = create(:user)
|
||||
my_project = create(:project, account: current_user.primary_account)
|
||||
their_project = create(:project, account: another_user.primary_account)
|
||||
my_contract_template = create(:contract_template, project: my_project)
|
||||
their_contract_template = create(:contract_template, project: their_project)
|
||||
my_appearance_release = create(:appearance_release, project: my_project)
|
||||
their_appearance_release = create(:appearance_release, project: their_project)
|
||||
my_talent_release = create(:talent_release, project: my_project)
|
||||
their_talent_release = create(:talent_release, project: their_project)
|
||||
my_location_release = create(:location_release, project: my_project)
|
||||
their_location_release = create(:location_release, project: their_project)
|
||||
my_material_release = create(:material_release, project: my_project)
|
||||
their_material_release = create(:material_release, project: their_project)
|
||||
my_note = create(:note, notable: my_appearance_release, user: current_user)
|
||||
their_note = create(:note, notable: their_appearance_release, user: another_user)
|
||||
|
||||
get :index
|
||||
|
||||
account_ids = ids_for_type('accounts')
|
||||
project_ids = ids_for_type('projects')
|
||||
contract_template_ids = ids_for_type('contract_templates')
|
||||
appearance_release_ids = ids_for_type('appearance_releases')
|
||||
talent_release_ids = ids_for_type('talent_releases')
|
||||
location_release_ids = ids_for_type('location_releases')
|
||||
material_release_ids = ids_for_type('material_releases')
|
||||
note_ids = ids_for_type('notes')
|
||||
|
||||
expect(account_ids).to include(current_user.primary_account.id.to_s)
|
||||
expect(account_ids).not_to include(another_user.primary_account.id.to_s)
|
||||
expect(project_ids).to include(my_project.id.to_s)
|
||||
expect(project_ids).not_to include(their_project.id.to_s)
|
||||
expect(contract_template_ids).to include(my_contract_template.id.to_s)
|
||||
expect(contract_template_ids).not_to include(their_contract_template.id.to_s)
|
||||
expect(appearance_release_ids).to include(my_appearance_release.id.to_s)
|
||||
expect(appearance_release_ids).not_to include(their_appearance_release.id.to_s)
|
||||
expect(location_release_ids).to include(my_location_release.id.to_s)
|
||||
expect(location_release_ids).not_to include(their_location_release.id.to_s)
|
||||
expect(talent_release_ids).to include(my_talent_release.id.to_s)
|
||||
expect(talent_release_ids).not_to include(their_talent_release.id.to_s)
|
||||
expect(material_release_ids).to include(my_material_release.id.to_s)
|
||||
expect(material_release_ids).not_to include(their_material_release.id.to_s)
|
||||
expect(note_ids).to include(my_note.id.to_s)
|
||||
expect(note_ids).not_to include(their_note.id.to_s)
|
||||
end
|
||||
|
||||
it 'returns only the notes that belong to the accessible projects (the note bug)' do
|
||||
create_default_data
|
||||
|
||||
my_appearance_release = AppearanceRelease.last
|
||||
my_note = create(:note, notable: my_appearance_release, user: current_user)
|
||||
another_user = create(:user)
|
||||
their_project = create(:project, account: another_user.primary_account)
|
||||
their_appearance_release = create(:appearance_release, project: their_project)
|
||||
their_note = create(:note, notable: their_appearance_release, user: another_user)
|
||||
another_user.account_auths.create(account: current_user.primary_account, role: :account_manager)
|
||||
|
||||
get :index
|
||||
|
||||
note_ids = ids_for_type('notes')
|
||||
expect(note_ids).to eq [my_note.id.to_s]
|
||||
end
|
||||
|
||||
it 'shows guardian_photo property for appearance and talent releases and not for other release types' do
|
||||
create_default_data
|
||||
|
||||
get :index
|
||||
|
||||
appearance_releases = attributes_for_type('appearance_releases')
|
||||
talent_releases = attributes_for_type('talent_releases')
|
||||
location_releases = attributes_for_type('location_releases')
|
||||
material_releases = attributes_for_type('material_releases')
|
||||
|
||||
expect(appearance_releases.first).to include('guardian_photo')
|
||||
expect(talent_releases.first).to include('guardian_photo')
|
||||
expect(location_releases.first).not_to include('guardian_photo')
|
||||
expect(material_releases.first).not_to include('guardian_photo')
|
||||
end
|
||||
|
||||
it 'guardian photo has same format as other photos' do
|
||||
create_default_data_with_guardian_info
|
||||
|
||||
get :index
|
||||
|
||||
appearance_releases = attributes_for_type('appearance_releases')
|
||||
guardian_photo = appearance_releases.first['guardian_photo']
|
||||
photo_attributes = guardian_photo['attributes']
|
||||
|
||||
expect(appearance_releases.first).to include('guardian_photo')
|
||||
expect(guardian_photo).to include('id', 'type', 'attributes')
|
||||
expect(photo_attributes).to include('filename', 'content_type', 'url', 'thumbnail_url')
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def create_default_data
|
||||
project = create(:project, name: 'first', account: current_user.primary_account)
|
||||
create(:appearance_release, project: project)
|
||||
create(:talent_release, project: project)
|
||||
create(:location_release, project: project)
|
||||
create(:material_release, project: project)
|
||||
end
|
||||
|
||||
def create_default_data_with_guardian_info
|
||||
project = create(:project, name: 'first', account: current_user.primary_account)
|
||||
create(:appearance_release, :minor_with_guardian_photo, project: project)
|
||||
end
|
||||
|
||||
def response_body_json
|
||||
JSON.parse(response.body)
|
||||
end
|
||||
|
||||
def ids_for_type(name)
|
||||
response_body_json.dig('data', name).flat_map { |json| json.dig('attributes', 'id') }
|
||||
end
|
||||
|
||||
def attributes_for_type(name)
|
||||
response_body_json.dig('data', name).flat_map { |json| json['attributes'] }
|
||||
end
|
||||
end
|
||||
214
spec/controllers/api/talent_releases_controller_spec.rb
Normal file
214
spec/controllers/api/talent_releases_controller_spec.rb
Normal file
@@ -0,0 +1,214 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Api::TalentReleasesController, type: :controller do
|
||||
let(:current_user) { create(:user) }
|
||||
let(:project) { create(:project, name: 'first', account: current_user.primary_account) }
|
||||
|
||||
describe '#index' do
|
||||
it 'returns a succesful response' do
|
||||
create(:talent_release, person_first_name: 'ct1', person_last_name: 'ct12', project_id: project.id)
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
get :index, params: { project_id: project.id }
|
||||
|
||||
expect(response).to be_successful
|
||||
old_body = response.body
|
||||
|
||||
get :index, params: { project_id: project.id, updated_since: (DateTime.current + 10.days).to_date.to_s }
|
||||
expect(response.body).not_to eq(old_body)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#show' do
|
||||
it 'returns a succesful response' do
|
||||
tested_release = create(:talent_release, person_first_name: 'ct1', person_last_name: 'ct12', project_id: project.id)
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
get :show, params: { id: tested_release.id }
|
||||
|
||||
expect(response).to be_successful
|
||||
end
|
||||
|
||||
it 'includes photos' do
|
||||
tested_release = create("talent_release_with_photo", person_name: 'Release 1', project: project)
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
get :show, params: { id: tested_release.id }
|
||||
|
||||
tested_release.photos.each do |photo|
|
||||
data = {
|
||||
'id' => photo.id.to_s,
|
||||
'type' => 'active_storage_attachment',
|
||||
'attributes' => {
|
||||
'content_type' => photo.content_type,
|
||||
'filename' => photo.filename.to_s,
|
||||
'url' => photo_path_for(photo),
|
||||
'thumbnail_url' => photo_variant_path_for(photo, '150x150')
|
||||
}
|
||||
}
|
||||
|
||||
expect(response_body_included_attributes).to include(data)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#create' do
|
||||
before do
|
||||
template = create(:contract_template, name: 'ct1', project_id: project.id)
|
||||
small_photo = photo_base64
|
||||
guardian_photo = guardian_photo_base64
|
||||
signature = signature_base64
|
||||
@parameters = {
|
||||
contract_template_id: template.id,
|
||||
talent_release: {
|
||||
signature: signature,
|
||||
person_phone: '123',
|
||||
person_first_name: 'aaa',
|
||||
person_last_name: 'bbb',
|
||||
minor: true,
|
||||
guardian_first_name: 'John',
|
||||
guardian_last_name: 'Doe',
|
||||
guardian_phone: '9191919191',
|
||||
signed_at: '2020-01-01 16:50:26 UTC',
|
||||
photos: [{
|
||||
io: small_photo,
|
||||
filename: 'filenameee.jpg'
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
@guardian_info = {
|
||||
minor: true,
|
||||
guardian_name: 'Guardian',
|
||||
guardian_phone: '101010'
|
||||
}
|
||||
|
||||
@guardian_photo = {
|
||||
guardian_photo: {
|
||||
io: guardian_photo,
|
||||
filename: 'guardianPhoto.jpeg'
|
||||
}
|
||||
}
|
||||
sign_in_to_api(current_user)
|
||||
end
|
||||
|
||||
it 'returns 201 - created response' do
|
||||
post :create, params: @parameters
|
||||
expect(response.status).to eq 201
|
||||
|
||||
expect(TalentRelease.last.signature).to be_attached
|
||||
expect(TalentRelease.last.guardian_first_name).to eq('John')
|
||||
expect(TalentRelease.last.guardian_last_name).to eq('Doe')
|
||||
end
|
||||
|
||||
it 'saves signed on date correctly' do
|
||||
post :create, params: @parameters
|
||||
expect(response.status).to eq 201
|
||||
|
||||
expect(TalentRelease.last.signed_on).to eq '01/01/20'
|
||||
end
|
||||
|
||||
it 'runs attach contract to background job' do
|
||||
expect do
|
||||
post :create, params: @parameters
|
||||
end.to have_enqueued_job(AttachContractToReleasableJob).with(TalentRelease.last)
|
||||
end
|
||||
|
||||
it 'runs add headshot collection uid background job' do
|
||||
expect do
|
||||
post :create, params: @parameters
|
||||
end.to have_enqueued_job(AddHeadshotCollectionUidToProjectJob).with(project)
|
||||
end
|
||||
|
||||
it 'runs set tags for background releasable job' do
|
||||
expect do
|
||||
post :create, params: @parameters
|
||||
end.to have_enqueued_job(SetTagsForReleasableJob).with(TalentRelease.last)
|
||||
end
|
||||
|
||||
it 'fails if there is no signature' do
|
||||
template = create(:contract_template, name: 'ct1', project_id: project.id)
|
||||
small_photo = photo_base64
|
||||
parameters = {
|
||||
contract_template_id: template.id,
|
||||
talent_release: {
|
||||
person_name: 'aaa bbb'
|
||||
}
|
||||
}
|
||||
|
||||
parameters[:talent_release][:photos] = [{
|
||||
io: small_photo,
|
||||
filename: 'filenameee.jpg'
|
||||
}]
|
||||
|
||||
sign_in_to_api(current_user)
|
||||
expect do
|
||||
post :create, params: parameters
|
||||
end.to raise_error(ActiveRecord::RecordInvalid)
|
||||
end
|
||||
|
||||
it 'returns 201 for minor without guardian photo' do
|
||||
params_without_guardian_photo = @parameters
|
||||
params_without_guardian_photo[:talent_release].merge!(@guardian_info)
|
||||
|
||||
post :create, params: params_without_guardian_photo
|
||||
expect(response.status).to eq 201
|
||||
expect(TalentRelease.last.signature).to be_attached
|
||||
expect(TalentRelease.last.guardian_photo).not_to be_attached
|
||||
end
|
||||
|
||||
it 'returns 201 for minor with guardian photo' do
|
||||
params_with_guardian_photo = @parameters
|
||||
params_with_guardian_photo[:talent_release].merge!(@guardian_info, @guardian_photo)
|
||||
|
||||
post :create, params: params_with_guardian_photo
|
||||
expect(response.status).to eq 201
|
||||
talent_release = TalentRelease.last
|
||||
photo = talent_release.guardian_photo
|
||||
data = {
|
||||
'id' => photo.id.to_s,
|
||||
'type' => 'active_storage_attachment',
|
||||
'attributes' => {
|
||||
'content_type' => photo.content_type,
|
||||
'filename' => photo.filename.to_s,
|
||||
'url' => photo_path_for(photo),
|
||||
'thumbnail_url' => photo_variant_path_for(photo, '150x150')
|
||||
}
|
||||
}
|
||||
expect(talent_release.signature).to be_attached
|
||||
expect(talent_release.guardian_photo).to be_attached
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def photo_base64
|
||||
@photo_base64 ||= Base64Image.from_image(file_fixture('person_photo.png')).data_uri
|
||||
end
|
||||
|
||||
def guardian_photo_base64
|
||||
@guardian_photo_base64 ||= Base64Image.from_image(file_fixture('pratt.jpg')).data_uri
|
||||
end
|
||||
|
||||
def signature_base64
|
||||
@signature_base64 ||= Base64Image.from_image(file_fixture('signature.png')).data_uri
|
||||
end
|
||||
|
||||
def photo_path_for(attachment)
|
||||
Rails.application.routes.url_helpers.rails_blob_url(attachment, host: AppHost.new.domain_with_port)
|
||||
end
|
||||
|
||||
def photo_variant_path_for(attachment, _size)
|
||||
Rails.application.routes.url_helpers.rails_representation_url(attachment.variant(resize: '150x150'), host: AppHost.new.domain_with_port)
|
||||
end
|
||||
|
||||
def response_body_data_attributes
|
||||
JSON.parse(response.body).dig('data', 'attributes')
|
||||
end
|
||||
|
||||
def response_body_included_attributes
|
||||
JSON.parse(response.body).dig('included')
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user