From 1f1d0e9db82f616de1fb55a066888e3e51b93b85 Mon Sep 17 00:00:00 2001 From: Joaquin Correa Date: Wed, 15 Jan 2025 15:24:13 -0300 Subject: [PATCH] Add tests --- .../sanger_products_controller.rb | 2 +- .../app/models/sanger_sequencing/ability.rb | 2 +- .../sanger_sequencing/sanger_product.rb | 2 + .../sanger_sequencing/sanger_products_spec.rb | 73 ++++++++++++++ .../spec/system/sanger_product_spec.rb | 52 ---------- .../spec/system/sanger_products_spec.rb | 98 +++++++++++++++++++ 6 files changed, 175 insertions(+), 54 deletions(-) create mode 100644 vendor/engines/sanger_sequencing/spec/controllers/sanger_sequencing/sanger_products_spec.rb delete mode 100644 vendor/engines/sanger_sequencing/spec/system/sanger_product_spec.rb create mode 100644 vendor/engines/sanger_sequencing/spec/system/sanger_products_spec.rb diff --git a/vendor/engines/sanger_sequencing/app/controllers/sanger_sequencing/sanger_products_controller.rb b/vendor/engines/sanger_sequencing/app/controllers/sanger_sequencing/sanger_products_controller.rb index ecdc3f8195..9b38d9347e 100644 --- a/vendor/engines/sanger_sequencing/app/controllers/sanger_sequencing/sanger_products_controller.rb +++ b/vendor/engines/sanger_sequencing/app/controllers/sanger_sequencing/sanger_products_controller.rb @@ -49,7 +49,7 @@ def set_resources @product = current_facility.products.find_by!( url_name: params[:service_id] ) - @sanger_product = @product.sanger_product || @product.create_sanger_product(group: "default") + @sanger_product = @product.sanger_product || @product.create_sanger_product end end diff --git a/vendor/engines/sanger_sequencing/app/models/sanger_sequencing/ability.rb b/vendor/engines/sanger_sequencing/app/models/sanger_sequencing/ability.rb index 62c3110461..1fca393535 100644 --- a/vendor/engines/sanger_sequencing/app/models/sanger_sequencing/ability.rb +++ b/vendor/engines/sanger_sequencing/app/models/sanger_sequencing/ability.rb @@ -9,7 +9,7 @@ class Ability def initialize(user, facility = nil) return unless user - can [:show, :create, :update, :create_sample], Submission, user: + can [:show, :create, :update, :create_sample], Submission, user: user if facility && user.operator_of?(facility) can [:index, :show], Submission diff --git a/vendor/engines/sanger_sequencing/app/models/sanger_sequencing/sanger_product.rb b/vendor/engines/sanger_sequencing/app/models/sanger_sequencing/sanger_product.rb index dd7e7d08a7..3267f9721a 100644 --- a/vendor/engines/sanger_sequencing/app/models/sanger_sequencing/sanger_product.rb +++ b/vendor/engines/sanger_sequencing/app/models/sanger_sequencing/sanger_product.rb @@ -13,6 +13,8 @@ class SangerProduct < ApplicationRecord belongs_to :product + attribute :group, default: 'default' + validates :group, presence: true, inclusion: { in: GROUPS } validates :product, presence: true, uniqueness: { case_sensitive: false } diff --git a/vendor/engines/sanger_sequencing/spec/controllers/sanger_sequencing/sanger_products_spec.rb b/vendor/engines/sanger_sequencing/spec/controllers/sanger_sequencing/sanger_products_spec.rb new file mode 100644 index 0000000000..506d5bd16a --- /dev/null +++ b/vendor/engines/sanger_sequencing/spec/controllers/sanger_sequencing/sanger_products_spec.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +require "rails_helper" +require "controller_spec_helper" + +RSpec.describe SangerSequencing::SangerProductsController do + let(:facility) { create(:setup_facility, sanger_sequencing_enabled: true) } + let(:service) { create(:setup_service, facility:, sanger_sequencing_enabled: true) } + let(:user) { create(:user, :facility_administrator, facility:) } + + before do + sign_in user + + @params = { facility_id: facility.url_name, service_id: service.url_name } + end + + shared_examples "creates a sanger product if needed" do + it "creates a sanger product if needed" do + expect { do_request }.to( + change do + service.reload.sanger_product + end.from(nil).to(SangerSequencing::SangerProduct) + ) + end + end + + describe "show" do + before do + @method = :get + @action = :show + end + + include_examples "creates a sanger product if needed" + end + + describe "edit" do + before do + @method = :get + @action = :edit + end + + include_examples "creates a sanger product if needed" + end + + describe "update" do + before do + @method = :put + @action = :update + @params[:sanger_sequencing_sanger_product] = { group: "default" } + end + + include_examples "creates a sanger product if needed" + + it "updates the sanger product" do + sanger_product_json = { + "needs_primer" => true, "group" => "fragment" + } + @params[:sanger_sequencing_sanger_product] = sanger_product_json + + service.create_sanger_product + + expect { do_request }.to( + change do + service.reload.sanger_product.as_json(only: %i[needs_primer group]) + end.from( + { "needs_primer" => false, "group" => "default" } + ).to( + sanger_product_json + ) + ) + end + end +end diff --git a/vendor/engines/sanger_sequencing/spec/system/sanger_product_spec.rb b/vendor/engines/sanger_sequencing/spec/system/sanger_product_spec.rb deleted file mode 100644 index 2385d48116..0000000000 --- a/vendor/engines/sanger_sequencing/spec/system/sanger_product_spec.rb +++ /dev/null @@ -1,52 +0,0 @@ -# frozen_string_literal: true - -require "rails_helper" - -RSpec.describe "Sanger Product" do - let(:facility) { create(:setup_facility, sanger_sequencing_enabled: true) } - let(:service) { create(:setup_service, sanger_sequencing_enabled: true) } - let(:admin) { create(:user, :facility_administrator, facility:) } - let(:user) { create(:user) } - - describe "admin tab" do - context "when not logged in" do - it "redirects to login" do - visit facility_service_sanger_sequencing_sanger_product_path(facility, service) - - expect(page).to have_content("Login") - expect(page).to_not have_content("Sanger") - expect(page).to_not have_content(service.name) - end - end - - context "when logged in as normal user" do - before { login_as user } - - it "renders permission denied" do - visit facility_service_sanger_sequencing_sanger_product_path(facility, service) - - expect(page).to have_content("Permission Denied") - end - end - - context "when logged in as admin" do - before { login_as admin } - - it "requires the facility to be sanger enabled" do - facility.update(sanger_sequencing_enabled: false) - - visit facility_service_sanger_sequencing_sanger_product_path(facility, service) - - expect(page).to have_content("Not Found") - end - - it "shows the tab if service is sanger enabled" do - service.update(sanger_sequencing_enabled: true) - - visit facility_service_sanger_sequencing_sanger_product_path(facility, service) - - expect(page).to have_content("Sanger") - end - end - end -end diff --git a/vendor/engines/sanger_sequencing/spec/system/sanger_products_spec.rb b/vendor/engines/sanger_sequencing/spec/system/sanger_products_spec.rb new file mode 100644 index 0000000000..196e1317a0 --- /dev/null +++ b/vendor/engines/sanger_sequencing/spec/system/sanger_products_spec.rb @@ -0,0 +1,98 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe "Sanger Products" do + let(:facility) { create(:setup_facility, sanger_sequencing_enabled: true) } + let(:service) { create(:setup_service, facility:, sanger_sequencing_enabled: true) } + let(:admin) { create(:user, :facility_administrator, facility:) } + let(:user) { create(:user) } + + describe "admin tab" do + context "when not logged in" do + it "redirects to login" do + visit facility_service_sanger_sequencing_sanger_product_path(facility, service) + + expect(page).to have_content("Login") + expect(page).to_not have_content("Sanger") + expect(page).to_not have_content(service.name) + end + end + + context "when logged in as normal user" do + before { login_as user } + + it "renders permission denied" do + visit facility_service_sanger_sequencing_sanger_product_path(facility, service) + + expect(page).to have_content("Permission Denied") + end + end + + context "when logged in as facility admin" do + before { login_as admin } + + it "requires the facility to be sanger enabled" do + facility.update(sanger_sequencing_enabled: false) + + visit facility_service_sanger_sequencing_sanger_product_path(facility, service) + + expect(page).to have_content("Not Found") + end + + it "does not show the tab if service is sanger disabled" do + service.update(sanger_sequencing_enabled: false) + + visit facility_service_sanger_sequencing_sanger_product_path(facility, service) + + within(".nav-tabs") do + expect(page).to_not have_content("Sanger") + end + end + + it "shows the tab if service is sanger enabled" do + visit facility_service_sanger_sequencing_sanger_product_path(facility, service) + + within(".nav-tabs") do + expect(page).to have_content("Sanger") + end + end + end + end + + describe "sanger product show" do + before { login_as admin } + + it "can navigate by clicking the tab" do + visit manage_facility_service_path(facility, service) + + within(".nav-tabs") do + click_link("Sanger") + end + + expect(page).to have_content("Sanger Configuration") + end + + it "shows sanger product information" do + visit facility_service_sanger_sequencing_sanger_product_path(facility, service) + + expect(page).to have_content("Sanger Configuration") + expect(page).to have_content("Needs a Primer") + expect(page).to have_content("Plated Service Type") + expect(page).to have_link("Edit") + end + + it "allows to edit sanger product" do + visit facility_service_sanger_sequencing_sanger_product_path(facility, service) + + click_link("Edit") + + check("sanger_sequencing_sanger_product[needs_primer]") + select("Fragment Analysis", from: "sanger_sequencing_sanger_product[group]") + + click_button("Save") + + expect(page).to have_content("Sanger Configuration updated successfully") + end + end +end