193 lines
5.8 KiB
Ruby
193 lines
5.8 KiB
Ruby
require "rails_helper"
|
|
|
|
RSpec.describe MaterialReleasesController, type: :controller do
|
|
render_views
|
|
|
|
let(:user) { create(:user) }
|
|
let(:account) { user.primary_account }
|
|
let(:project) { create(:project, account: user.primary_account) }
|
|
|
|
before :each do
|
|
sign_in user
|
|
end
|
|
|
|
describe "#index" do
|
|
it "responds successfully" do
|
|
get :index, params: { project_id: project }
|
|
|
|
expect(response).to be_successful
|
|
end
|
|
|
|
it "renders content" do
|
|
release = create(:material_release, project: project, name: "My Release")
|
|
create(:note, notable: release, content: "Some notes here")
|
|
|
|
get :index, params: { project_id: project }
|
|
|
|
expect(response.body).to have_content "My Release"
|
|
expect(response.body).to have_content "Some notes here"
|
|
expect(response.body).to have_link "Import Release"
|
|
expect(response.body).to have_content "Manage"
|
|
end
|
|
|
|
context "when there are no material releases" do
|
|
it "renders an empty message" do
|
|
get :index, params: { project_id: project }
|
|
|
|
expect(response.body).to have_content("Material Releases will appear here")
|
|
end
|
|
end
|
|
|
|
context "when there are many records" do
|
|
it "paginates the table" do
|
|
create_list(:material_release, 20, project: project)
|
|
get :index, params: { project_id: project }
|
|
|
|
expect(response.body).to have_link("2", href: project_material_releases_path(project, page: 2))
|
|
end
|
|
end
|
|
|
|
context "for xhr request" do
|
|
it "filters the releases by a query param" do
|
|
material_releases = [
|
|
create(:material_release, name: "Coke", project: project),
|
|
create(:material_release, name: "Pepsi", project: project),
|
|
]
|
|
|
|
get :index, params: { project_id: project, query: "Pepsi" }, xhr: true
|
|
|
|
expect(response.body).not_to have_content("Coke")
|
|
expect(response.body).to have_content("Pepsi")
|
|
end
|
|
end
|
|
end
|
|
|
|
describe "#new" do
|
|
it "responds successfully" do
|
|
get :new, params: { project_id: project }
|
|
|
|
expect(response).to be_successful
|
|
end
|
|
end
|
|
|
|
describe "#create" do
|
|
it "responds successfully" do
|
|
expect {
|
|
post :create, params: { project_id: project, material_release: material_release_params }
|
|
}.to change(MaterialRelease, :count).by(1)
|
|
|
|
expect(response).to be_redirect
|
|
expect(response).to redirect_to [project, :material_releases]
|
|
expect(flash.notice).to be_present
|
|
end
|
|
|
|
it "enqueues tagging job" do
|
|
expect {
|
|
post :create, params: { project_id: project, material_release: material_release_params }
|
|
}.to have_enqueued_job(SetTagsForReleasableJob).with(MaterialRelease.last)
|
|
end
|
|
|
|
context "when the record would be invalid" do
|
|
before do
|
|
allow_any_instance_of(MaterialRelease).to receive(:save).and_return(false)
|
|
end
|
|
|
|
it "re-renders the form" do
|
|
expect {
|
|
post :create, params: { project_id: project, material_release: material_release_params }
|
|
}.not_to change(MaterialRelease, :count)
|
|
|
|
expect(response).to be_successful
|
|
expect(flash.notice).to be_nil
|
|
end
|
|
|
|
it "does not enqueue any jobs" do
|
|
expect {
|
|
post :create, params: { project_id: project, material_release: material_release_params }
|
|
}.not_to have_enqueued_job
|
|
end
|
|
end
|
|
end
|
|
|
|
describe "#edit" do
|
|
let(:non_native_material_release) { create(:material_release, project: project) }
|
|
let(:native_material_release) { create(:material_release, :native, project: project) }
|
|
|
|
it "responds successfully for non-native release" do
|
|
get :edit, params: { project_id: project, id: non_native_material_release }
|
|
|
|
expect(response).to be_successful
|
|
end
|
|
|
|
it "responds with error for native release" do
|
|
expect{get :edit, params: { id: native_material_release }}.to raise_exception Pundit::NotAuthorizedError
|
|
end
|
|
end
|
|
|
|
describe "#update" do
|
|
let(:material_release) { create(:material_release, project: project) }
|
|
|
|
it "responds successfully" do
|
|
patch :update, params: { project_id: project, id: material_release, material_release: material_release_params }
|
|
|
|
expect(response).to redirect_to [project, :material_releases]
|
|
expect(flash.notice).to be_present
|
|
end
|
|
|
|
context "when the record would be invalid" do
|
|
before do
|
|
allow_any_instance_of(MaterialRelease).to receive(:save).and_return(false)
|
|
end
|
|
|
|
it "re-renders the form" do
|
|
patch :update, params: { project_id: project, id: material_release, material_release: material_release_params }
|
|
|
|
expect(response).to be_successful
|
|
expect(flash.notice).to be_nil
|
|
end
|
|
end
|
|
end
|
|
|
|
describe "#destroy" do
|
|
let!(:material_release) { create(:material_release, project: project) }
|
|
|
|
it "responds with redirect" do
|
|
delete :destroy, params: { project_id: project, id: material_release }
|
|
|
|
expect(response).to be_redirect
|
|
expect(response).to redirect_to [project, :material_releases]
|
|
end
|
|
|
|
it "sets the flash" do
|
|
delete :destroy, params: { project_id: project, id: material_release }
|
|
|
|
expect(flash.alert).not_to be_nil
|
|
end
|
|
|
|
it "destroys the record" do
|
|
expect {
|
|
delete :destroy, params: { project_id: project, id: material_release }
|
|
}.to change(MaterialRelease, :count).by(-1)
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def material_release_params
|
|
attributes_for(:material_release).merge(exploitable_rights_params)
|
|
end
|
|
|
|
def exploitable_rights_params
|
|
{
|
|
applicable_medium_id: ApplicableMedium.last.id,
|
|
applicable_medium_text: "applicable_media",
|
|
territory_id: Territory.last.id,
|
|
territory_text: "territory",
|
|
term_id: Term.last.id,
|
|
term_text: "term",
|
|
restriction_id: Restriction.last.id,
|
|
restriction_text: "restrictions",
|
|
}
|
|
end
|
|
end
|