diff --git a/spec/controllers/archive_controller_spec.rb b/spec/controllers/archive_controller_spec.rb new file mode 100644 index 00000000..75864cd1 --- /dev/null +++ b/spec/controllers/archive_controller_spec.rb @@ -0,0 +1,314 @@ +require 'rails_helper' + +class MockArchiveFile + + def get!(result) + @result = result + end + + def log_denied_attempt!(request_hash: metadata) + end + + def to_s + "MockArchiveFile" + end +end + + + +RSpec.describe ArchiveController, type: :controller do + + describe "#user_is_authorized?" do + before { + allow(subject).to receive(:set_variables) + } + + situations = [{auth_user: true, recaptcha: true, result: true}, + {auth_user: true, recaptcha: false, result: false}, + {auth_user: false, recaptcha: true, result: false}, + {auth_user: false, recaptcha: false, result: false}] + + situations.each do |situation| + context "when authenticated_user? is #{situation[:auth_user]} and recaptcha_success? is #{situation[:recaptcha]}" do + before { + allow(subject).to receive(:authenticated_user?).and_return situation[:auth_user] + allow(subject).to receive(:recaptcha_success?).and_return situation[:recaptcha] + } + + it "sets variables and returns #{situation[:result]}" do + + expect(subject).to receive(:authenticated_user?).and_return situation[:auth_user] + if (situation[:auth_user]) + expect(subject).to receive(:recaptcha_success?).and_return situation[:recaptcha] + end + + expect(subject.user_is_authorized?).to eq situation[:result] + end + end + end + + after { + expect(subject).to have_received(:set_variables) + } + end + + + describe "#status" do + context "when user is authorized" do + before { + allow(subject).to receive(:user_is_authorized?).and_return true + subject.instance_variable_set(:@archive_file, OpenStruct.new(display_status:"User is Authorized")) + allow(subject).to receive(:render).with({plain: "User is Authorized"}).and_return "User is Authorized" + } + it "returns string" do + expect(subject.status).to eq "User is Authorized" + end + end + + context "when user is not authorized" do + before { + allow(subject).to receive(:user_is_authorized?).and_return false + allow(subject).to receive(:render).with({plain: "action unavailable", status: 403}).and_return "action unavailable 403" + } + it "returns 403 status" do + expect(subject.status).to eq "action unavailable 403" + end + end + end + + + describe "#download_request" do + mock_archive_file = MockArchiveFile.new + before { + allow(subject).to receive(:root_url).and_return "root url" + subject.instance_variable_set(:@archive_file, mock_archive_file) + } + + context "when user is authorized" do + before { + allow(subject).to receive(:user_is_authorized?).and_return true + } + + context "when file_path is present in @archive_file" do + before { + allow(subject).to receive(:request_metadata).and_return :file_path => "file path", :filename => "file name" + allow(subject).to receive(:download_filename).with("file name").and_return "download filename" + } + it "calls send_file" do + expect(subject).to receive(:send_file).with("file path", filename: "download filename") + subject.download_request + end + end + + context "when file_path is not present in @archive_file" do + + context "when message is not present in @archive_file" do + before { + allow(subject).to receive(:request_metadata).and_return :file_path => "" + allow(Rails.logger).to receive(:error).with("Message missing from MockArchiveFile result: {:file_path=>\"\"}") + } + it "redirects with default error message" do + expect(Rails.logger).to receive(:error).with("Message missing from MockArchiveFile result: {:file_path=>\"\"}") + expect(subject).to receive(:redirect_back).with(fallback_location: "root url", notice: "Request failed. Please seek technical support.") + subject.download_request + end + end + + context "when message is present in @archive_file and alert is not" do + before { + allow(subject).to receive(:request_metadata).and_return :message => "message" + } + it "redirects with notice" do + expect(subject).to receive(:redirect_back).with(fallback_location: "root url", notice: "message") + subject.download_request + end + end + + context "when message is present in @archive_file and so is alert" do + before { + allow(subject).to receive(:request_metadata).and_return :message => "message", :alert => "alert" + } + it "redirects with alert" do + expect(subject).to receive(:redirect_back).with(fallback_location: "root url", alert: "message") + subject.download_request + end + end + end + end + + context "when user is not authorized" do + before { + allow(subject).to receive(:user_is_authorized?).and_return false + allow(subject).to receive(:request_metadata).and_return "request metadata" + subject.instance_variable_set(:@failure_description, "failure description") + } + it "logs denied attempt and calls redirect_back with @failure_description" do + expect(mock_archive_file).to receive(:log_denied_attempt!).with(request_hash: "request metadata") + expect(subject).to receive(:redirect_back).with(fallback_location: "root url", alert: "failure description") + subject.download_request + end + end + end + + + # private methods start + + describe "#variable_params" do + before { + allow(subject.params).to receive(:permit).with(:collection, :object, :format, :request, :user_email, :file_set_id, 'g-recaptcha-response'.to_sym, 'g-recaptcha-response-data'.to_sym => [:sda_request]) + + } + it "calls params.permit" do + expect(subject.params.permit(:collection, :object, :format, :request, :user_email, :file_set_id, 'g-recaptcha-response'.to_sym, 'g-recaptcha-response-data'.to_sym => [:sda_request])) + end + end + + + describe "#set_variables" do + before { + allow(subject).to receive(:params).and_return :collection => "The Collection", :user_email => "user@example.com", :file_set_id => "Q-3333" + allow(subject).to receive(:variable_params).and_return :object => "I Object", :format => "formatted" + allow(ArchiveFile).to receive(:new).with(collection: "The Collection", object: "I Object.formatted").and_return "archive file" + } + + it "sets instance variables" do + subject.send(:set_variables) + + expect(subject.instance_variable_get(:@collection)).to eq "The Collection" + expect(subject.instance_variable_get(:@object)).to eq "I Object.formatted" + expect(subject.instance_variable_get(:@archive_file)).to eq "archive file" + expect(subject.instance_variable_get(:@user_email)).to eq "user@example.com" + expect(subject.instance_variable_get(:@file_set_id)).to eq "Q-3333" + end + end + + + describe "#download_filename" do + context "when user-displayed filename is not available" do + before { + allow(FileSet).to receive(:search_with_conditions).and_return [] + } + it "returns the default filename (the parameter entered)" do + expect(subject.send(:download_filename, "V. Basic File")).to eq "V. Basic File" + end + end + + context "when user-displayed filename is available" do + before { + subject.instance_variable_set(:@file_set_id, "B-2000") + allow(FileSet).to receive(:search_with_conditions).with(id: "B-2000").and_return [Hash.new(label_ssi: "Descriptively labelled file with emojis")] + } + it "returns the user-displayed filename" do + expect(subject.send(:download_filename, "V. Basic File")).to eq label_ssi: "Descriptively labelled file with emojis" + end + end + end + + + describe "#authenticated_user?" do + context "when user authentication is not required" do + before { + allow(Settings.archive_api).to receive(:require_user_authentication).and_return false + } + it "returns true" do + expect(subject.send(:authenticated_user?)).to eq true + + expect(subject.instance_variable_get(:@failure_description)).to be_blank + end + end + + context "when user authentication is required" do + before { + allow(Settings.archive_api).to receive(:require_user_authentication).and_return true + allow(subject).to receive(:user_signed_in?).and_return true + } + it "returns result of user_signed_in?" do + expect(subject.send(:authenticated_user?)).to eq true + + expect(subject.instance_variable_get(:@failure_description)).to eq "Action available only to signed-in users." + end + end + end + + + describe "#recaptcha_success?" do + context "when recaptcha not in use" do + before { + allow(Settings.archive_api).to receive(:use_recaptcha).and_return false + } + it "returns true" do + expect(subject.send(:recaptcha_success?)).to eq true + expect(subject.instance_variable_get(:@failure_description)).to be_blank + end + end + + context "when recaptcha in use" do + before { + allow(Settings.archive_api).to receive(:use_recaptcha).and_return true + } + + context "when recaptcha v3 is successful" do + before { + allow(subject).to receive(:verify_recaptcha).with(action: 'sda_request', minimum_score: Settings.recaptcha.minimum_score.to_f, + secret_key: Settings.recaptcha.v3.secret_key) + .and_return true + } + it "returns true" do + expect(subject).to receive(:verify_recaptcha).with(action: 'sda_request', minimum_score: Settings.recaptcha.minimum_score.to_f, + secret_key: Settings.recaptcha.v3.secret_key).and_return true + expect(subject).not_to receive(:verify_recaptcha) + expect(subject.send(:recaptcha_success?)).to eq true + end + end + + context "when recaptcha v2 is successful" do + before { + allow(subject).to receive(:verify_recaptcha).with(action: 'sda_request', minimum_score: Settings.recaptcha.minimum_score.to_f, + secret_key: Settings.recaptcha.v3.secret_key) + .and_return false + allow(subject).to receive(:verify_recaptcha).and_return true + } + it "returns true" do + expect(subject).to receive(:verify_recaptcha).with(action: 'sda_request', minimum_score: Settings.recaptcha.minimum_score.to_f, + secret_key: Settings.recaptcha.v3.secret_key).and_return false + expect(subject).to receive(:verify_recaptcha).and_return true + expect(subject.send(:recaptcha_success?)).to eq true + end + end + + context "when recaptcha v3 and v2 are both unsuccessful" do + before { + allow(subject).to receive(:verify_recaptcha).with(action: 'sda_request', minimum_score: Settings.recaptcha.minimum_score.to_f, + secret_key: Settings.recaptcha.v3.secret_key) + .and_return false + allow(subject).to receive(:verify_recaptcha).and_return false + } + it "returns false" do + expect(subject).to receive(:verify_recaptcha).with(action: 'sda_request', minimum_score: Settings.recaptcha.minimum_score.to_f, + secret_key: Settings.recaptcha.v3.secret_key).and_return false + expect(subject).to receive(:verify_recaptcha).and_return false + expect(subject.send(:recaptcha_success?)).to eq false + end + end + + after { + expect(subject.instance_variable_get(:@failure_description)).to eq 'Action requires successful recaptcha completion.' + } + end + end + + + describe "#request_metadata" do + before { + allow(Time).to receive(:now).and_return "It's time..." + subject.instance_variable_set(:@user_email, "User's email") + subject.instance_variable_set(:@file_set_id, "A-1000") + } + + it "returns hashset" do + result_hash = {:time => "It's time...", :user_email => "User's email", :file_set_id => "A-1000"} + expect(subject.send(:request_metadata)).to eq result_hash + end + end + +end diff --git a/spec/controllers/guest_user_message_controller_spec.rb b/spec/controllers/guest_user_message_controller_spec.rb index e4d02034..d30b1bb3 100644 --- a/spec/controllers/guest_user_message_controller_spec.rb +++ b/spec/controllers/guest_user_message_controller_spec.rb @@ -1,11 +1,18 @@ require 'rails_helper' -RSpec.describe GuestUserMessageController do +RSpec.describe GuestUserMessageController, type: :controller do + + describe "presenter_class" do + it do + expect(GuestUserMessageController.presenter_class).to eq GuestUserMessagePresenter + end + end describe "#show" do it "renders a response" do get :show expect(response.status).to eq 200 + expect(subject.instance_variable_get(:@presenter)).to be_instance_of GuestUserMessagePresenter end end diff --git a/spec/controllers/hyrax/dissertations_controller_spec.rb b/spec/controllers/hyrax/dissertations_controller_spec.rb index 6d1e00ee..6913df51 100644 --- a/spec/controllers/hyrax/dissertations_controller_spec.rb +++ b/spec/controllers/hyrax/dissertations_controller_spec.rb @@ -1,9 +1,17 @@ -# Generated via -# `rails generate hyrax:work Dissertation` require 'rails_helper' -RSpec.describe Hyrax::DissertationsController do - it "has tests" do - skip "Add your tests here" +RSpec.describe Hyrax::DissertationsController, type: :controller do + + describe "#curation_concern_type" do + it do + expect(Hyrax::DissertationsController.curation_concern_type).to eq ::Dissertation + end + end + + describe "#show_presenter" do + it do + expect(Hyrax::DissertationsController.show_presenter).to eq Hyrax::DissertationPresenter + end end + end diff --git a/spec/controllers/hyrax/file_sets_controller_spec.rb b/spec/controllers/hyrax/file_sets_controller_spec.rb index 05815fbe..d828da87 100644 --- a/spec/controllers/hyrax/file_sets_controller_spec.rb +++ b/spec/controllers/hyrax/file_sets_controller_spec.rb @@ -1,6 +1,19 @@ require 'rails_helper' -RSpec.describe Hyrax::FileSetsController do +class MockActor + def revert_content(params) + end + + def update_content(params) + end + + def update_metadata(params) + end +end + + + +RSpec.describe Hyrax::FileSetsController, type: :controller do describe 'constants' do it do @@ -8,6 +21,13 @@ end end + describe "#self.show_presenter" do + it do + expect( Hyrax::FileSetsController.show_presenter ).instance_of? Hyrax::DsFileSetPresenter + end + end + + before { allow(subject).to receive(:current_user).and_return "user1" } @@ -15,22 +35,20 @@ describe 'provenance_log_create' do before { allow(subject.curation_concern).to receive(:provenance_create).with(current_user: "user1", event_note: 'FileSetsController') - .and_return "provenance log creation" } it "calls curation_concern.provenance_create" do expect(subject.curation_concern).to receive(:provenance_create).with(current_user: "user1", event_note: 'FileSetsController') - expect(subject.provenance_log_create).to eq "provenance log creation" + subject.provenance_log_create end end describe 'provenance_log_destroy' do before { allow(subject.curation_concern).to receive(:provenance_destroy).with(current_user: "user1", event_note: 'FileSetsController') - .and_return "provenance log destruction" } it "calls curation_concern.provenance_destroy" do expect(subject.curation_concern).to receive(:provenance_destroy).with(current_user: "user1", event_note: 'FileSetsController') - expect(subject.provenance_log_destroy).to eq "provenance log destruction" + subject.provenance_log_destroy end end @@ -39,18 +57,17 @@ subject.instance_variable_set(:@update_attr_key_values, "after key values") allow(subject.curation_concern).to receive(:provenance_log_update_after).with(current_user: "user1", update_attr_key_values: "after key values") - .and_return "provenance log update after" } it "calls curation_concern.provenance_log_update_after" do expect(subject.curation_concern).to receive(:provenance_log_update_after).with(current_user: "user1", update_attr_key_values: "after key values") - expect(subject.provenance_log_update_after).to eq "provenance log update after" + subject.provenance_log_update_after end end describe 'provenance_log_update_before' do - # NOTE: could not resolve params[PARAMS_KEY].dup before { - allow(subject.curation_concern).to receive(:provenance_log_update_before).with( anything ) + allow(subject).to receive(:params).and_return "file_set" => "form params file set" + allow(subject.curation_concern).to receive(:provenance_log_update_before).with( form_params: "form params file set" ) .and_return "provenance log update before" } it "sets instance variable to curation_concern.provenance_log_update_before" do @@ -104,4 +121,68 @@ end end end + + + # protected methods + + describe "#attempt_update" do + thespian = MockActor.new + before { + allow(Deepblue::LoggingHelper).to receive(:here).and_return "here" + allow(Deepblue::LoggingHelper).to receive(:called_from).and_return "called from" + allow(subject).to receive(:current_user).and_return "Current User" + allow(subject).to receive(:actor).and_return thespian + allow(Deepblue::LoggingHelper).to receive(:obj_class).with("actor", thespian).and_return "castmember" + } + + context "if wants_to_revert? is true" do + before { + allow(subject).to receive(:wants_to_revert?).and_return true + + allow(Deepblue::LoggingHelper).to receive(:bold_debug).with ["here", "called from", "current_user=Current User", "castmember", "wants to revert"] + allow(subject).to receive(:params).and_return :revision => "revised" + } + it "calls revert_content" do + expect(Deepblue::LoggingHelper).to receive(:bold_debug).with ["here", "called from", "current_user=Current User", "castmember", "wants to revert"] + expect(thespian).to receive(:revert_content).with "revised" + + subject.send(:attempt_update) + end + end + + context "if wants_to_revert? is false" do + before { + allow(subject).to receive(:wants_to_revert?).and_return false + } + context "if params has file_set" do + before{ + allow(subject).to receive(:params).and_return :file_set => {:files => ["files"]} + allow(Deepblue::LoggingHelper).to receive(:bold_debug).with ["here", "called from", "current_user=Current User", "castmember", "actor.update_content"] + } + + it "calls update_content" do + expect(subject).to receive(:params).and_return :file_set => {:files => ["files"]} + + expect(Deepblue::LoggingHelper).to receive(:bold_debug).with ["here", "called from", "current_user=Current User", "castmember", "actor.update_content"] + expect(thespian).to receive(:update_content).with "files" + subject.send(:attempt_update) + end + end + + context "if params does not have file_set" do + before { + allow(subject).to receive(:params).and_return :file_set => {:unknown => ["unknowable"]} + allow(Deepblue::LoggingHelper).to receive(:bold_debug).with ["here", "called from", "current_user=Current User", "update_metadata"] + allow(subject).to receive(:update_metadata) + } + + it "calls update_metadata" do + expect(Deepblue::LoggingHelper).to receive(:bold_debug).with ["here", "called from", "current_user=Current User", "update_metadata"] + expect(subject).to receive(:update_metadata) + subject.send(:attempt_update) + end + end + end + end + end diff --git a/spec/controllers/hyrax/permissions_controller_spec.rb b/spec/controllers/hyrax/permissions_controller_spec.rb new file mode 100644 index 00000000..5d469b27 --- /dev/null +++ b/spec/controllers/hyrax/permissions_controller_spec.rb @@ -0,0 +1,92 @@ +require 'rails_helper' + +class MockCurationConcern + def embargo_release_date() + "embargo release date" + end +end + + + +RSpec.describe Hyrax::PermissionsController, type: :controller do + before { + allow(Deepblue::LoggingHelper).to receive(:here).and_return "here" + allow(Deepblue::LoggingHelper).to receive(:called_from).and_return "called from" + allow(Deepblue::LoggingHelper).to receive(:bold_debug).with ["here", "called from", ""] + } + + describe '#confirm' do + before { + allow(subject).to receive(:curation_concern).and_return MockCurationConcern.new + allow(subject).to receive(:copy) + } + + context "embargo_allow_children_unembargo_choice is false" do + before { + allow(DeepBlueDocs::Application.config).to receive(:embargo_allow_children_unembargo_choice).and_return false + } + it "calls copy" do + expect(Deepblue::LoggingHelper).to receive(:bold_debug).with ["here", "called from", ""] + expect(Deepblue::LoggingHelper).to receive(:bold_debug).with ["here", "called from", "curation_concern.embargo_release_date=embargo release date", ""] + + expect(subject).to receive(:copy) + subject.confirm + end + end + + context "embargo_allow_children_unembargo_choice is true" do + before { + allow(DeepBlueDocs::Application.config).to receive(:embargo_allow_children_unembargo_choice).and_return true + } + it "does NOT call copy" do + expect(Deepblue::LoggingHelper).to receive(:bold_debug).with ["here", "called from", ""] + expect(Deepblue::LoggingHelper).to receive(:bold_debug).with ["here", "called from", "curation_concern.embargo_release_date=embargo release date", ""] + + expect(subject).not_to receive(:copy) + subject.confirm + end + end + end + + + describe "#copy" do + skip "Add a test" + end + + + describe "#confirm_access" do + it "calls LoggingHelper.bold_debug" do + expect(Deepblue::LoggingHelper).to receive(:bold_debug).with ["here", "called from", ""] + subject.confirm_access + end + end + + + describe "#copy_access" do + skip "Add a test" + end + + + describe "#curation_concern" do + context "instance variable has a value" do + before { + subject.instance_variable_set(:@curation_concern, "curation of concern") + } + it "gets instance variable" do + expect(subject.curation_concern).to eq "curation of concern" + end + end + + context "instance variable does not have a value" do + before { + allow(subject).to receive(:params).and_return :id => "found" + allow(ActiveFedora::Base).to receive(:find).with("found").and_return "concern curator" + } + it "calls Base.Find and sets instance variable" do + expect(subject.curation_concern).to eq "concern curator" + + subject.instance_variable_get(:@curation_concern) == "concern curator" + end + end + end +end diff --git a/spec/controllers/provenance_log_controller_spec.rb b/spec/controllers/provenance_log_controller_spec.rb new file mode 100644 index 00000000..ec28c2ac --- /dev/null +++ b/spec/controllers/provenance_log_controller_spec.rb @@ -0,0 +1,396 @@ +require 'rails_helper' + +class MockRunner + + def run + end + + def deleted_ids + ["456", "789"] + end + + def deleted_id_to_key_values_map + "00-XXX" + end +end + +class MockPathname + def initialize(path) + @path = path + end + + def join(string) + "#{@path}#{string}" + end + + def to_s + @path.to_s + end +end + +class MockZipfile + def add(name1, name2) + end +end + +RSpec.describe ProvenanceLogController, type: :controller do + + before { + allow(Deepblue::LoggingHelper).to receive(:here).and_return "here" + allow(Deepblue::LoggingHelper).to receive(:called_from).and_return "called from" + } + + + describe "presenter_class" do + it do + expect(ProvenanceLogController.presenter_class).to eq ProvenanceLogPresenter + end + end + + + describe "#show" do + context "when current_ability.admin is false" do + before { + allow(subject).to receive(:current_ability).and_return OpenStruct.new(admin?: false) + } + it "raises CanCan::AccessDenied exception" do + expect(subject.show).to_raise(CanCan::AccessDenied) + + rescue CanCan::AccessDenied + # raises Exception + end + end + + context "when current_ability.admin is true" do + before { + allow(subject).to receive(:current_ability).and_return OpenStruct.new(admin?: true) + allow(subject).to receive(:params).and_return :id => "123" + allow(subject).to receive(:id_check) + allow(Deepblue::LoggingHelper).to receive(:bold_debug).with [ "here", "called from", "id=123", "" ] + allow(subject).to receive(:provenance_log_entries_refresh).with(id: "123") + allow(ProvenanceLogPresenter).to receive(:new).with(controller: anything).and_return "Provenance Log Presenter" + allow(subject).to receive(:render).with "provenance_log/provenance_log" + } + + context "when id_valid? returns true" do + before { + allow(subject).to receive(:id_valid?).and_return true + } + it "calls provenance_log_entries_refresh" do + expect(subject).to receive(:provenance_log_entries_refresh).with(id: "123") + expect(ProvenanceLogPresenter).to receive(:new).with(controller: anything) + subject.show + end + end + + context "when id_deleted returns true" do + before { + allow(subject).to receive(:id_valid?).and_return false + allow(subject).to receive(:id_deleted).and_return true + } + it "calls provenance_log_entries_refresh" do + expect(subject).to receive(:provenance_log_entries_refresh).with(id: "123") + expect(ProvenanceLogPresenter).to receive(:new).with(controller: anything) + subject.show + end + end + + context "when id_valid? and id_deleted return false" do + before { + allow(subject).to receive(:id_valid?).and_return false + allow(subject).to receive(:id_deleted).and_return false + } + it "does NOT call provenance_log_entries_refresh" do + expect(subject).not_to receive(:provenance_log_entries_refresh) + expect(ProvenanceLogPresenter).to receive(:new).with(controller: anything) + subject.show + end + end + + after { + expect(subject).to have_received(:id_check) + expect(Deepblue::LoggingHelper).to have_received(:bold_debug).with [ "here", "called from", "id=123", "" ] + expect(subject).to have_received(:render).with "provenance_log/provenance_log" + + expect(subject.instance_variable_get(:@id)).to eq "123" + expect(subject.instance_variable_get(:@id_deleted)).to eq false + expect(subject.instance_variable_get(:@id_invalid)).to eq false + expect(subject.instance_variable_get(:@id_msg)).to eq "" + expect(subject.instance_variable_get(:@presenter)).to eq "Provenance Log Presenter" + } + end + end + + + describe "#id_check" do + context "when id is blank" do + before { + allow(subject).to receive(:id).and_return "" + } + it "returns nil" do + expect(subject.id_check).to be_nil + end + end + + context "when id is NOT blank" do + before { + allow(subject).to receive(:id).and_return "ID" + allow(ActiveFedora::Base).to receive(:find).with("ID").and_return "found" + } + it "calls Base.Find" do + expect(subject.id_check).to eq "found" + end + end + + context "when Ldp::Gone error occurs" do + before { + allow(subject).to receive(:id).and_return "ID" + allow(ActiveFedora::Base).to receive(:find).with("ID").and_raise(Ldp::Gone) + } + it "sets instance variables to deleted values" do + subject.id_check + + expect(subject.instance_variable_get(:@id_msg)).to eq "deleted" + expect(subject.instance_variable_get(:@id_deleted)).to eq true + expect(subject.instance_variable_get(:@id_invalid)).to be_nil + end + end + + context "when ActiveFedora::ObjectNotFoundError occurs" do + before { + allow(subject).to receive(:id).and_return "ID" + allow(ActiveFedora::Base).to receive(:find).with("ID").and_raise(ActiveFedora::ObjectNotFoundError) + } + it "sets instance variables to invalid values" do + subject.id_check + + expect(subject.instance_variable_get(:@id_msg)).to eq "invalid" + expect(subject.instance_variable_get(:@id_invalid)).to eq true + expect(subject.instance_variable_get(:@id_deleted)).to be_nil + end + end + end + + + describe "#id_valid?" do + + context "when id is blank" do + before { + allow(subject).to receive(:id).and_return "" + } + it "returns false" do + expect(subject.id_valid?).to eq false + end + end + + context "when id is NOT blank" do + before { + allow(subject).to receive(:id).and_return "id" + } + + context "when id is deleted" do + before { + allow(subject).to receive(:id_deleted).and_return true + } + it "returns false" do + expect(subject.id_valid?).to eq false + end + end + + context "when id is invalid" do + before { + allow(subject).to receive(:id_deleted).and_return false + allow(subject).to receive(:id_invalid).and_return true + } + it "returns false" do + expect(subject.id_valid?).to eq false + end + end + + context "when id is not deleted or invalid" do + before { + allow(subject).to receive(:id_deleted).and_return false + allow(subject).to receive(:id_invalid).and_return false + } + it "returns true" do + expect(subject.id_valid?).to eq true + end + end + end + + end + + + describe "#log_zip_download" do + context "when current_ability admin field is false" do + before { + allow(subject).to receive(:current_ability).and_return OpenStruct.new(admin?: false) + } + + it "raises CanCan::AccessDenied error" do + subject.log_zip_download + + rescue CanCan::AccessDenied + # raises Exception + end + end + + context "when current_ability admin field is true" do + pathname_obj = MockPathname.new("pathname tmp") + pathname_log = MockPathname.new("provenance log") + zipfile = MockZipfile.new + + before { + allow(subject).to receive(:current_ability).and_return OpenStruct.new(admin?: true) + # could not stub Settings.tmpdir - returns /tmp + allow(Pathname).to receive(:new).with("/tmp").and_return pathname_obj + allow(Dir).to receive(:exist?).with(pathname_obj).and_return false + allow(Dir).to receive(:mkdir).with(pathname_obj) + allow(Rails).to receive(:env).and_return "rails_dev" + + allow(Rails.root).to receive(:join).with("log", "provenance_rails_dev.log").and_return "provenance log" + allow(Pathname).to receive(:new).with("provenance log").and_return pathname_log + allow(File).to receive(:exist?).and_return true + allow(File).to receive(:delete).with("pathname tmpprovenance_rails_dev.log.zip") + + allow(Zip::File).to receive(:open).with( "pathname tmpprovenance_rails_dev.log.zip", Zip::File::CREATE).and_yield zipfile + allow(subject).to receive(:send_file).with( "pathname tmpprovenance_rails_dev.log.zip") + } + + it "logs the zip log download" do + expect(Deepblue::LoggingHelper).to receive(:bold_debug).with [ "here", "called from", "" ] + expect(Deepblue::LoggingHelper).to receive(:bold_debug).with [ "zip_log_download begin", "tmp_dir=pathname tmp"] + expect(Deepblue::LoggingHelper).to receive(:bold_debug).with [ "zip_log_download", "target_dir=pathname tmp"] + expect(Deepblue::LoggingHelper).to receive(:bold_debug).with [ "zip_log_download", "target_zipfile=pathname tmpprovenance_rails_dev.log.zip" ] + expect(File).to receive(:delete).with("pathname tmpprovenance_rails_dev.log.zip") + + expect(Deepblue::LoggingHelper).to receive(:debug).with "Download Zip begin copy to folder pathname tmp" + expect(Deepblue::LoggingHelper).to receive(:bold_debug).with [ "zip_log_download", "begin zip of src_file_name=provenance log" ] + expect(Zip::File).to receive(:open).with( "pathname tmpprovenance_rails_dev.log.zip", Zip::File::CREATE) + expect(zipfile).to receive(:add).with("provenance_rails_dev.log", pathname_log) + expect(Deepblue::LoggingHelper).to receive(:bold_debug).with [ "zip_log_download", "download complete target_dir=pathname tmp" ] + expect(subject).to receive(:send_file).with("pathname tmpprovenance_rails_dev.log.zip") + + subject.log_zip_download + end + end + + skip "Add a test where Settings.tmpdir is nil" + end + + + describe "#find" do + context "when current_ability admin field is false" do + before { + allow(subject).to receive(:current_ability).and_return OpenStruct.new(admin?: false) + } + + it "raises CanCan::AccessDenied error" do + subject.find + + rescue CanCan::AccessDenied + # raises Exception + end + end + + context "when current_ability.admin is true" do + before { + allow(subject).to receive(:current_ability).and_return OpenStruct.new(admin?: true) + allow(subject).to receive(:params).and_return :find_id => "123" + allow(subject).to receive(:id_check) + allow(Deepblue::LoggingHelper).to receive(:bold_debug).with [ "here", "called from", "find_id=123", "id=123", "" ] + allow(subject).to receive(:provenance_log_entries_refresh).with(id: "123") + allow(ProvenanceLogPresenter).to receive(:new).with(controller: anything).and_return "Provenance Log Presenter" + allow(subject).to receive(:render).with "provenance_log/provenance_log" + } + + context "when id_valid? returns true" do + before { + allow(subject).to receive(:id_valid?).and_return true + } + it "calls provenance_log_entries_refresh" do + expect(subject).to receive(:provenance_log_entries_refresh).with(id: "123") + expect(ProvenanceLogPresenter).to receive(:new).with(controller: anything) + subject.find + end + end + + context "when id_deleted returns true" do + before { + allow(subject).to receive(:id_valid?).and_return false + allow(subject).to receive(:id_deleted).and_return true + } + it "calls provenance_log_entries_refresh" do + expect(subject).to receive(:provenance_log_entries_refresh).with(id: "123") + expect(ProvenanceLogPresenter).to receive(:new).with(controller: anything) + subject.find + end + end + + context "when id_valid? and id_deleted return false" do + before { + allow(subject).to receive(:id_valid?).and_return false + allow(subject).to receive(:id_deleted).and_return false + } + it "does NOT call provenance_log_entries_refresh" do + expect(subject).not_to receive(:provenance_log_entries_refresh) + expect(ProvenanceLogPresenter).to receive(:new).with(controller: anything) + subject.find + end + end + + after { + expect(subject).to have_received(:id_check) + expect(Deepblue::LoggingHelper).to have_received(:bold_debug).with [ "here", "called from", "find_id=123","id=123", "" ] + expect(subject).to have_received(:render).with "provenance_log/provenance_log" + + expect(subject.instance_variable_get(:@id)).to eq "123" + expect(subject.instance_variable_get(:@id_deleted)).to eq false + expect(subject.instance_variable_get(:@id_invalid)).to eq false + expect(subject.instance_variable_get(:@id_msg)).to eq "" + expect(subject.instance_variable_get(:@presenter)).to eq "Provenance Log Presenter" + } + end + end + + + describe "#deleted_works" do + context "when current_ability admin field is false" do + before { + allow(subject).to receive(:current_ability).and_return OpenStruct.new(admin?: false) + } + + it "raises CanCan::AccessDenied error" do + subject.deleted_works + + rescue CanCan::AccessDenied + # raises Exception + end + end + + context "when current_ability admin field is true" do + runner = MockRunner.new + before { + allow(subject).to receive(:current_ability).and_return OpenStruct.new(admin?: true) + allow(Deepblue::ProvenanceLogService).to receive(:provenance_log_path).and_return "prov log path" + allow(Deepblue::DeletedWorksFromLog).to receive(:new).with( input: "prov log path").and_return runner + allow(ProvenanceLogPresenter).to receive(:new).with(controller: anything).and_return "Provenance Log Presenter" + allow(subject).to receive(:render).with "provenance_log/provenance_log" + } + + it "run Deepblue::DeletedWorksFromLog and set instance variables" do + expect(Deepblue::LoggingHelper).to receive(:bold_debug).with [ "here", "called from", "" ] + expect(runner).to receive(:run) + expect(ProvenanceLogPresenter).to receive(:new).with(controller: anything) + expect(subject).to receive(:render).with "provenance_log/provenance_log" + + subject.deleted_works + + expect(subject.instance_variable_get(:@deleted_ids)).to eq ["456", "789"] + expect(subject.instance_variable_get(:@deleted_id_to_key_values_map)).to eq "00-XXX" + expect(subject.instance_variable_get(:@presenter)).to eq "Provenance Log Presenter" + end + end + end + +end \ No newline at end of file diff --git a/spec/controllers/rack_attacks_controller_spec.rb b/spec/controllers/rack_attacks_controller_spec.rb new file mode 100644 index 00000000..1891eacf --- /dev/null +++ b/spec/controllers/rack_attacks_controller_spec.rb @@ -0,0 +1,114 @@ +require 'rails_helper' + +class MockConfig + + def initialize(text) + @value = text + end + def value + @value + end + + def value=(text) + @value = text + end +end + +class MockContentBlock + + def permit(param) + end +end + + + + +RSpec.describe RackAttacksController, type: :controller do + + describe "#show" do + before { + subject.instance_variable_set(:@rack_attack_config, MockConfig.new("rack attack config")) + allow(subject).to receive(:render).with(body: "rack attack config") + } + + it "renders @rack_attack_config as body" do + expect(subject).to receive(:render).with(body: "rack attack config") + + subject.show + end + end + + + describe "#edit" do + before { + subject.instance_variable_set(:@rack_attack_config, "rack attack config") + allow(subject).to receive(:authorize!).with(:edit, "rack attack config") + } + + it "calls authorize!" do + expect(subject).to receive(:authorize!).with(:edit, "rack attack config") + + subject.edit + end + end + + + pending "#update" + + + # private methods + + describe "#load_rack_attack_config" do + before { + allow(Datacore::RackAttackConfig).to receive(:config_source).and_return "config source" + } + it "sets the @rack_attack_config variable" do + subject.send(:load_rack_attack_config) + + expect(subject.instance_variable_get(:@rack_attack_config)).to eq "config source" + end + end + + + describe "#throw_breadcrumbs" do + before { + allow(subject).to receive(:root_path).and_return "root path" + allow(subject.hyrax).to receive(:dashboard_path).and_return "dashboard path" + allow(subject).to receive(:edit_rack_attack_path).and_return "edit rack" + + # Unable to stub hyrax YAML values + allow(subject).to receive(:add_breadcrumb).with("Home", "root path") + allow(subject).to receive(:add_breadcrumb).with("Dashboard", "dashboard path") + allow(subject).to receive(:add_breadcrumb).with("Configuration", "#") + allow(subject).to receive(:add_breadcrumb).with("Rack Attack", "edit rack") + } + + it "adds breadcrumbs" do + expect(subject).to receive(:root_path) + expect(subject.hyrax).to receive(:dashboard_path) + expect(subject).to receive(:edit_rack_attack_path) + + expect(subject).to receive(:add_breadcrumb).with("Home", "root path") + expect(subject).to receive(:add_breadcrumb).with("Dashboard", "dashboard path") + expect(subject).to receive(:add_breadcrumb).with("Configuration", "#") + expect(subject).to receive(:add_breadcrumb).with("Rack Attack", "edit rack") + + subject.send(:throw_breadcrumbs) + end + end + + + describe "#permitted_params" do + mock_block = MockContentBlock.new + before { + allow(subject.params).to receive(:require).with(:content_block).and_return mock_block + } + it "sets params to require content block" do + expect(subject.params).to receive(:require).with(:content_block) + expect(mock_block).to receive(:permit).with(:value) + + subject.send(:permitted_params) + end + end + +end diff --git a/spec/controllers/robots_controller_spec.rb b/spec/controllers/robots_controller_spec.rb index 3ac26c35..aea76fa8 100644 --- a/spec/controllers/robots_controller_spec.rb +++ b/spec/controllers/robots_controller_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -describe RobotsController do +RSpec.describe RobotsController, type: :controller do let(:user) { create(:user) } let(:admin) { create(:admin) } let(:robots_txt) { ContentBlock.create(name: 'robots_txt', value: content) } @@ -76,4 +76,47 @@ expect(response.body).to eq new_content end end -end \ No newline at end of file + + # private methods + + describe "#find_robots_txt" do + before { + allow(ContentBlock).to receive(:find_or_create_by).with(name: 'robots_txt').and_return "robots txt file" + } + it "calls ContentBlock.find_or_create_by for the robots txt file" do + expect(ContentBlock).to receive(:find_or_create_by).with(name: 'robots_txt') + subject.send(:find_robots_txt) + + expect(subject.instance_variable_get(:@robots_txt)).to eq "robots txt file" + end + end + + + describe "#throw_breadcrumbs" do + before { + allow(I18n).to receive(:t).with('hyrax.controls.home').and_return "Home" + allow(I18n).to receive(:t).with('hyrax.dashboard.breadcrumbs.admin').and_return "Dashboard" + allow(I18n).to receive(:t).with('hyrax.admin.sidebar.configuration').and_return "Configuration" + allow(subject).to receive(:root_path).and_return "root path" + allow(subject).to receive(:edit_robots_path).and_return "robot path" + + allow(subject).to receive(:add_breadcrumb).with("Home", "root path") + allow(subject).to receive(:add_breadcrumb).with("Dashboard", "/dashboard?locale=en") + allow(subject).to receive(:add_breadcrumb).with("Configuration", "#") + allow(subject).to receive(:add_breadcrumb).with("robots.txt", "robot path") + } + + it "adds breadcrumbs" do + expect(subject).to receive(:add_breadcrumb).with("Home", "root path") + expect(subject).to receive(:add_breadcrumb).with("Dashboard", "/dashboard?locale=en") + expect(subject).to receive(:add_breadcrumb).with("Configuration", "#") + expect(subject).to receive(:add_breadcrumb).with("robots.txt", "robot path") + + subject.send(:throw_breadcrumbs) + end + end + + + pending "#permitted_params" + +end diff --git a/spec/presenters/guest_user_message_presenter_spec.rb b/spec/presenters/guest_user_message_presenter_spec.rb new file mode 100644 index 00000000..bb6190eb --- /dev/null +++ b/spec/presenters/guest_user_message_presenter_spec.rb @@ -0,0 +1,13 @@ +require 'rails_helper' + +RSpec.describe GuestUserMessagePresenter do + + describe '#initialize' do + it "sets @controller variable" do + guest_user = GuestUserMessagePresenter.new(controller: "MysteryController") + + expect(guest_user.instance_variable_get(:@controller)).to eq "MysteryController" + end + end + +end diff --git a/spec/presenters/hyrax/admin_set_options_presenter_spec.rb b/spec/presenters/hyrax/admin_set_options_presenter_spec.rb new file mode 100644 index 00000000..a9120b28 --- /dev/null +++ b/spec/presenters/hyrax/admin_set_options_presenter_spec.rb @@ -0,0 +1,225 @@ +require 'rails_helper' + +class MockService + def initialize(mockadminset) + @mockadminset = mockadminset + end + + def search_results(access) + @mockadminset + end +end + +class MockAdminSet + def initialize(id, title) + @id = id + @title = title + end + + def id + @id + end + + def to_s + @title + end +end + +class MockWorkflow + def allows_access_grant? + end +end + +class MockPermissionTemplate + def initialize(release_no_delay, release_date, release_before_date, visibility) + @release_no_delay = release_no_delay + @release_date = release_date + @release_before_date = release_before_date + @visibility = visibility + end + + def release_no_delay? + @release_no_delay + end + + def release_before_date? + @release_before_date + end + + def release_date + @release_date + end + + def visibility + @visibility + end +end + + +RSpec.describe Hyrax::AdminSetOptionsPresenter do + + subject { described_class.new( "service" ) } + + + describe '#initialize' do + it "sets instance variable" do + admin_set_options = Hyrax::AdminSetOptionsPresenter.new("service") + + admin_set_options.instance_variable_get(:@service) == "service" + end + end + + + describe "#select_options" do + admin_set_mock = MockAdminSet.new(101, "monogram") + + before { + subject.instance_variable_set(:@service, MockService.new([admin_set_mock])) + allow(subject).to receive(:data_attributes).with( admin_set_mock ).and_return "data attributes" + } + it "returns admin set(s)" do + expect(subject).to receive(:data_attributes).with( admin_set_mock ) + expect(subject.select_options).to eq [["monogram", 101, "data attributes"]] + end + end + + + describe "#select_options_default_admin" do + mock_admin_set1 = MockAdminSet.new("admin_set/default", "Default Admin Set") + mock_admin_set2 = MockAdminSet.new("brand_new_set", "Personal Admin Set") + + before { + subject.instance_variable_set(:@service, MockService.new([mock_admin_set1, mock_admin_set2])) + allow(subject).to receive(:data_attributes).with( mock_admin_set1 ).and_return "data attributes" + } + it "returns default admin set" do + expect(subject).to receive(:data_attributes).with( mock_admin_set1 ) + expect(subject).not_to receive(:data_attributes).with( mock_admin_set2 ) + + expect(subject.select_options_default_admin).to eq [["Default Admin Set", "admin_set/default", "data attributes"]] + end + end + + + describe "#select_options_non_default_admin" do + mock_admin_set1 = MockAdminSet.new("admin_set/default", "Default Admin Set") + mock_admin_set2 = MockAdminSet.new("brand_new_set", "Personal Admin Set") + + before { + subject.instance_variable_set(:@service, MockService.new([mock_admin_set1, mock_admin_set2])) + allow(subject).to receive(:data_attributes).with( mock_admin_set2 ).and_return "data attributes" + } + it "returns set(s) that are not the default admin set" do + expect(subject).not_to receive(:data_attributes).with( mock_admin_set1 ) + expect(subject).to receive(:data_attributes).with( mock_admin_set2 ) + + expect(subject.select_options_non_default_admin).to eq [["Personal Admin Set", "brand_new_set", "data attributes"]] + end + end + + + # private methods + + describe "#data_attributes" do + + context "when find_by evaluates to true" do + before { + allow(Hyrax::PermissionTemplate).to receive(:find_by).with(source_id: "S-111").and_return true + allow(subject).to receive(:attributes_for).with(permission_template: true) + } + it "calls attributes_for" do + expect(subject).to receive(:attributes_for).with(permission_template: true) + + subject.send(:data_attributes, OpenStruct.new(id: "S-111")) + end + end + + context "when find_by evaluates to false" do + before { + allow(Hyrax::PermissionTemplate).to receive(:find_by).with(source_id: "S-111").and_return false + } + it "returns empty" do + expect(subject).not_to receive(:attributes_for) + + expect(subject.send(:data_attributes, OpenStruct.new(id: "S-111"))).to be_empty + end + end + end + + + describe "#attributes_for" do + context "when release_no_delay? is true and release_date and visibility are present" do + permission_template = MockPermissionTemplate.new(true, nil, true, "visible") + before { + allow(subject).to receive(:sharing?).with(permission_template: permission_template).and_return "data sharing" + } + + it "returns data-sharing, data-release-no-delay, data-release-before-date, and data-visibility" do + expect(subject).to receive(:sharing?).with(permission_template: permission_template) + + expect(subject.send(:attributes_for, permission_template: permission_template)).to eq "data-sharing" => "data sharing", + "data-release-no-delay" => true, "data-release-before-date" => true, "data-visibility" => "visible" + end + end + + context "when release_date present, no release_before_date? and no visibility" do + permission_template = MockPermissionTemplate.new(false, "release date",false, nil) + before { + allow(subject).to receive(:sharing?).with(permission_template: permission_template).and_return "data sharing" + } + + it "returns data-sharing and data-release-date" do + expect(subject).to receive(:sharing?).with(permission_template: permission_template) + + expect(subject.send(:attributes_for, permission_template: permission_template)).to eq "data-sharing" => "data sharing", + "data-release-date" => "release date" + end + end + end + + + describe "#sharing?" do + context "when no workflow with permission template" do + before { + allow(subject).to receive(:workflow).with(permission_template: "permission template").and_return false + } + it "returns false" do + expect(subject).to receive(:workflow).with(permission_template: "permission template") + expect(subject.send(:sharing?, permission_template: "permission template")).to eq false + end + end + + context "when existing workflow with permission template" do + workflow = MockWorkflow.new + before { + allow(subject).to receive(:workflow).with(permission_template: "permission template").and_return workflow + allow(workflow).to receive(:allows_access_grant?).and_return true + } + it "calls allows_access_grant?" do + expect(subject).to receive(:workflow).with(permission_template: "permission template") + expect(workflow).to receive(:allows_access_grant?) + expect(subject.send(:sharing?, permission_template: "permission template")).to eq true + end + end + end + + + describe "#workflow" do + context "when not active_workflow" do + it "returns blank" do + expect(subject.send(:workflow, permission_template: OpenStruct.new(active_workflow: nil))).to be_blank + end + end + + context "when active_workflow" do + before { + allow(Sipity::Workflow).to receive(:find_by!).with(id: "C-333") + } + it "calls Sipity::Workflow.find_by! with active workflow id" do + expect(Sipity::Workflow).to receive(:find_by!).with(id: "C-333") + subject.send(:workflow, permission_template: OpenStruct.new(active_workflow: OpenStruct.new(id: "C-333"))) + end + end + end + +end diff --git a/spec/presenters/hyrax/homepage_presenter_spec.rb b/spec/presenters/hyrax/homepage_presenter_spec.rb new file mode 100644 index 00000000..eb31df58 --- /dev/null +++ b/spec/presenters/hyrax/homepage_presenter_spec.rb @@ -0,0 +1,204 @@ +require 'rails_helper' + +RSpec.describe Hyrax::HomepagePresenter do + let(:user) { FactoryBot.create :user } + let(:current_ability) { instance_double(Ability, current_user: user ) } + let(:blacklight_config) { Blacklight::Configuration.new } + let(:collections) { Hyrax::CollectionSearchBuilder.new(self).rows(1) } + + subject{ described_class.new(current_ability, collections) } + + + describe '#create_work_presenter_class' do + it do + expect(Hyrax::HomepagePresenter.create_work_presenter_class).instance_of? Hyrax::SelectTypeListPresenter + end + end + + + describe '#initialize' do + it "sets instance variables using parameters" do + present = Hyrax::HomepagePresenter.new("current ability", "collections") + + present.instance_variable_get(:@current_ability) == "current ability" + present.instance_variable_get(:@collections) == "collections" + end + end + + + describe "#display_share_button?" do + expected_results = [OpenStruct.new( user_unregistered: true, not_logged_in: true, can_create: true, expected_result: true ), + OpenStruct.new( user_unregistered: true, not_logged_in: true, can_create: false, expected_result: true ), + OpenStruct.new( user_unregistered: false, not_logged_in: false, can_create: true, expected_result: true ), + OpenStruct.new( user_unregistered: true, not_logged_in: false, can_create: false, expected_result: false ), + OpenStruct.new( user_unregistered: false, not_logged_in: true, can_create: false, expected_result: false ), + OpenStruct.new( user_unregistered: false, not_logged_in: false, can_create: false, expected_result: false )] + + expected_results.each { |n| + context "when user_unregistered? is #{n.user_unregistered}, display_share_button_when_not_logged_in? is #{n.not_logged_in}, and can_create_any_work? is #{n.can_create}" do + before { + allow(subject).to receive(:user_unregistered?).and_return n.user_unregistered + allow(Hyrax.config).to receive(:display_share_button_when_not_logged_in?).and_return n.not_logged_in + allow(subject.current_ability).to receive(:can_create_any_work?).and_return n.can_create + } + it "returns #{n.expected_result}" do + expect(subject).to receive(:user_unregistered?) + + if n.user_unregistered + expect(Hyrax.config).to receive(:display_share_button_when_not_logged_in?) + else + expect(Hyrax.config).not_to receive(:display_share_button_when_not_logged_in?) + end + + if !n.user_unregistered or !n.not_logged_in + expect(subject.current_ability).to receive(:can_create_any_work?) + else + expect(subject.current_ability).not_to receive(:can_create_any_work?) + end + + expect(subject.display_share_button?).to eq(n.expected_result) + end + end + } + end + + + describe "#create_work_presenter" do + context "when @create_work_presenter has a value" do + before { + subject.instance_variable_set(:@create_work_presenter, "create work") + } + it 'returns @create_work_presenter value' do + expect(subject.create_work_presenter_class).not_to receive(:new).with(user) + expect(subject.create_work_presenter).to eq "create work" + end + end + + context "when @create_work_presenter has no value" do + before { + allow(subject.create_work_presenter_class).to receive(:new).with(user).and_return "new class" + } + it 'calls create_work_presenter_class.new and sets instance variable' do + expect(subject.create_work_presenter_class).to receive(:new).with(user) + expect(subject.create_work_presenter).to eq "new class" + + subject.instance_variable_get(:@create_work_presenter) == "new class" + end + end + end + + + describe "#create_many_work_types?" do + before { + allow(subject.create_work_presenter).to receive(:many?).and_return true + } + + context "when Flipflop.only_use_data_set_work_type? is true" do + before { + allow(Flipflop).to receive(:only_use_data_set_work_type?).and_return true + } + it "returns false" do + expect(subject.create_work_presenter).not_to receive(:many?) + + expect(subject.create_many_work_types?).to eq false + end + end + + context "when Flipflop.only_use_data_set_work_type? is false" do + before { + allow(Flipflop).to receive(:only_use_data_set_work_type?).and_return false + } + it "returns create_work_presenter.many?" do + expect(subject.create_work_presenter).to receive(:many?) + + expect(subject.create_many_work_types?).to eq true + end + end + + after { + expect(Flipflop).to have_received(:only_use_data_set_work_type?) + } + end + + + describe "#draw_select_work_modal?" do + factors = [{"display" => true, "create" => true, "expected_result" => true}, + {"display" => false, "create" => false, "expected_result" => false}, + {"display" => true, "create" => false, "expected_result" => false}, + {"display" => false, "create" => true, "expected_result" => false}] + + factors.each { |factor| + context "display_share_button? is #{factor[:display]} and create_many_work_types? is #{factor[:create]}" do |it| + before { + allow(subject).to receive(:display_share_button?).and_return factor[:display] + allow(subject).to receive(:create_many_work_types?).and_return factor[:create] + } + it "returns #{factor[:expected_result]}" do + expect(subject).to receive(:display_share_button?) + + if factor[:display] + expect(subject).to receive(:create_many_work_types?) + else + expect(subject).not_to receive(:create_many_work_types?) + end + + expect(subject.draw_select_work_modal?).to eq factor[:expected_result] + end + end + } + end + + + describe "#first_work_type" do + context "create_work_presenter has at least one model" do + before { + allow(subject.create_work_presenter).to receive(:authorized_models).and_return ["Uno", "Dos"] + } + it "returns first model" do + expect(subject.first_work_type).to eq "Uno" + end + end + end + + + # private method + + describe "#user_unregistered?" do + context "when current user is a new record" do + before { + allow(subject).to receive(:current_ability).and_return OpenStruct.new(current_user: OpenStruct.new(new_record?: true)) + } + it "returns true" do + expect(subject).to receive(:current_ability) + + expect(subject.send(:user_unregistered?)).to eq true + end + end + + context "when current user is not a new record and is a guest" do + before { + allow(subject).to receive(:current_ability).and_return OpenStruct.new(current_user: OpenStruct.new(new_record?: false, guest?: true)) + } + it "returns true" do + expect(subject).to receive(:current_ability).twice + + expect(subject.send(:user_unregistered?)).to eq true + end + end + + context "when current user is not a new record or a guest" do + before { + allow(subject).to receive(:current_ability).and_return OpenStruct.new(current_user: OpenStruct.new(new_record?: false, guest?: false)) + } + it "returns false" do + expect(subject).to receive(:current_ability).twice + + expect(subject.send(:user_unregistered?)).to eq false + end + end + + # after { + # expect(subject).to have_received(:current_ability) + # } + end +end