From 37cdcf07fbd08167f1cd8eeb0f30000cbf401396 Mon Sep 17 00:00:00 2001 From: jrhoads Date: Mon, 9 Sep 2024 10:07:51 +0200 Subject: [PATCH 1/4] User should use sequential pattern in its factory --- spec/factories/user.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/factories/user.rb b/spec/factories/user.rb index 9e1d15879..3fc2e3bbd 100644 --- a/spec/factories/user.rb +++ b/spec/factories/user.rb @@ -2,7 +2,7 @@ FactoryBot.define do factory :user do - sequence(:name) { |_n| "Josiah Carberry{n}" } + sequence(:name) { |_n| "Josiah Carberry#{_n}" } provider { "globus" } role_id { "user" } sequence(:uid) { |n| "0000-0002-1825-000#{n}" } From 77f0693e37794943f8ac74e7e22549b8d0bb93b5 Mon Sep 17 00:00:00 2001 From: jrhoads Date: Mon, 9 Sep 2024 10:15:06 +0200 Subject: [PATCH 2/4] Add spec options/helpers to monitor test fixtures and to skip generating the prefix_pool --- spec/rails_helper.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 68955c532..10f66e819 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -67,6 +67,12 @@ end end + config.before(:each, :monitor_factories) do |example| + ActiveSupport::Notifications.subscribe("factory_bot.run_factory") do |name, start, finish, id, payload| + $stderr.puts "FactoryBot: #{payload[:strategy]}(:#{payload[:name]}) at #{start}" + end + end + config.before(:suite) do puts("Clearing_cache") Rails.cache.clear @@ -77,6 +83,12 @@ end config.before(:each) do |example| + # Checking if :skip_prefix_pool parameter is set in metadata + if example.metadata[:skip_prefix_pool] + # Skip the prefix pool setup + next + end + prefix_pool_size = example.metadata[:prefix_pool_size].present? ? example.metadata[:prefix_pool_size].to_i : ENV["PREFIX_POOL_SIZE"].to_i if prefix_pool_size <= 0 @prefix_pool = [] From 9f6235b664e6b8dffbed87cc0b4c3f80fd329e0c Mon Sep 17 00:00:00 2001 From: jrhoads Date: Mon, 9 Sep 2024 10:56:52 +0200 Subject: [PATCH 3/4] Removed extra elasticsearch/prefix setup. Move to build_stubbed models where possibl --- spec/models/ability_spec.rb | 267 ++++++++++++++++++++++-------------- 1 file changed, 162 insertions(+), 105 deletions(-) diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index 53f3e49e7..0156bd6c8 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -3,38 +3,60 @@ require "rails_helper" require "cancan/matchers" -describe User, type: :model, elasticsearch: true do +describe User, type: :model, elasticsearch: false, skip_prefix_pool: true do let(:token) { User.generate_token } let(:user) { User.new(token) } - let!(:consortium) { create(:provider, role_name: "ROLE_CONSORTIUM") } - let!(:provider) do - create( + let(:consortium) { build_stubbed(:provider, role_name: "ROLE_CONSORTIUM") } + let(:provider) do + build_stubbed( :provider, consortium: consortium, role_name: "ROLE_CONSORTIUM_ORGANIZATION", ) end - let(:contact) { create(:contact, provider: provider) } - let(:consortium_contact) { create(:contact, provider: consortium) } - let!(:prefix) { create(:prefix, uid: "10.14454") } - let!(:client) { create(:client, provider: provider) } - let!(:provider_prefix) do - create(:provider_prefix, provider: provider, prefix: prefix) + let(:contact) { build_stubbed(:contact, provider: provider) } + let(:consortium_contact) { build_stubbed(:contact, provider: consortium) } + let(:prefix) { build_stubbed(:prefix, uid: "10.14454") } + let(:client) { build(:client, provider: provider) } + let(:provider_prefix) do + build_stubbed(:provider_prefix, provider: provider, prefix: prefix) end - let!(:client_prefix) do - create(:client_prefix, client: client, prefix: prefix) + let(:client_prefix) do + build_stubbed(:client_prefix, client: client, prefix: prefix) end - let(:doi) { create(:doi, client: client) } - let(:media) { create(:media, doi: doi) } + let(:doi) { build_stubbed(:doi, client: client) } + let(:media) { build_stubbed(:media, doi: doi) } let(:xml) { file_fixture("datacite.xml").read } - let(:metadata) { create(:metadata, xml: xml, doi: doi) } + let(:metadata) { build_stubbed(:metadata, xml: xml, doi: doi) } + + before(:all) do + @consortium = create( :provider, + role_name: "ROLE_CONSORTIUM") + @provider = create(:provider, + consortium: @consortium, + role_name: "ROLE_CONSORTIUM_ORGANIZATION" + ) + @prefix = create(:prefix, uid: "10.14454") + @client = create(:client, provider: @provider) + @provider_prefix = create( + :provider_prefix, + provider: @provider, + prefix: @prefix + ) + @client_prefix = create( + :client_prefix, + client: @client, + prefix: @prefix + ) + @doi = create(:doi, client: @client) + end - describe "User attributes", order: :defined do + describe "User attributes", order: :defined, skip_prefix_pool: true do it "is valid with valid attributes" do expect(user.name).to eq("Josiah Carberry") end end - describe "abilities", vcr: true do + describe "abilities", vcr: true, skip_prefix_pool: true do subject { Ability.new(user) } context "when is a user" do @@ -75,63 +97,86 @@ end context "when is a client admin" do - let(:token) do - User.generate_token( + + before(:all) do + @token = User.generate_token( role_id: "client_admin", - provider_id: provider.symbol.downcase, - client_id: client.symbol.downcase, + provider_id: @provider.symbol.downcase, + client_id: @client.symbol.downcase, + ) end + let(:token) { @token } + it { is_expected.to be_able_to(:read, user) } - it { is_expected.to be_able_to(:read, provider) } + it { is_expected.to be_able_to(:read, @provider) } - it { is_expected.not_to be_able_to(:create, provider) } - it { is_expected.not_to be_able_to(:update, provider) } - it { is_expected.not_to be_able_to(:destroy, provider) } - it { is_expected.not_to be_able_to(:read_billing_information, provider) } - it { is_expected.not_to be_able_to(:read_contact_information, provider) } + it { is_expected.not_to be_able_to(:create, @provider) } + it { is_expected.not_to be_able_to(:update, @provider) } + it { is_expected.not_to be_able_to(:destroy, @provider) } + it { is_expected.not_to be_able_to(:read_billing_information, @provider) } + it { is_expected.not_to be_able_to(:read_contact_information, @provider) } it { is_expected.not_to be_able_to(:read, contact) } it { is_expected.not_to be_able_to(:create, contact) } it { is_expected.not_to be_able_to(:update, contact) } it { is_expected.not_to be_able_to(:destroy, contact) } - it { is_expected.to be_able_to(:read, client) } - it { is_expected.not_to be_able_to(:create, client) } - it { is_expected.to be_able_to(:update, client) } - it { is_expected.not_to be_able_to(:destroy, client) } - it { is_expected.not_to be_able_to(:transfer, client) } - it { is_expected.to be_able_to(:read_contact_information, client) } - it { is_expected.to be_able_to(:read_analytics, client) } - - it { is_expected.not_to be_able_to(:read, prefix) } - it { is_expected.not_to be_able_to(:create, prefix) } - it { is_expected.not_to be_able_to(:update, prefix) } - it { is_expected.not_to be_able_to(:destroy, prefix) } - - it { is_expected.to be_able_to(:read, client_prefix) } - it { is_expected.not_to be_able_to(:create, client_prefix) } - it { is_expected.not_to be_able_to(:update, client_prefix) } - it { is_expected.not_to be_able_to(:destroy, client_prefix) } - - it { is_expected.to be_able_to(:read, doi) } - it { is_expected.not_to be_able_to(:transfer, doi) } - it { is_expected.to be_able_to(:create, doi) } - it { is_expected.to be_able_to(:update, doi) } - it { is_expected.to be_able_to(:destroy, doi) } + it { is_expected.to be_able_to(:read, @client) } + it { is_expected.not_to be_able_to(:create, @client) } + it { is_expected.to be_able_to(:update, @client) } + it { is_expected.not_to be_able_to(:destroy, @client) } + it { is_expected.not_to be_able_to(:transfer, @client) } + it { is_expected.to be_able_to(:read_contact_information, @client) } + it { is_expected.to be_able_to(:read_analytics, @client) } + + it { is_expected.not_to be_able_to(:read, @prefix) } + it { is_expected.not_to be_able_to(:create, @prefix) } + it { is_expected.not_to be_able_to(:update, @prefix) } + it { is_expected.not_to be_able_to(:destroy, @prefix) } + + it { is_expected.to be_able_to(:read, @client_prefix) } + it { is_expected.not_to be_able_to(:create, @client_prefix) } + it { is_expected.not_to be_able_to(:update, @client_prefix) } + it { is_expected.not_to be_able_to(:destroy, @client_prefix) } + + it { is_expected.to be_able_to(:read, @doi) } + it { is_expected.not_to be_able_to(:transfer, @doi) } + it { is_expected.to be_able_to(:create, @doi) } + it { is_expected.to be_able_to(:update, @doi) } + it { is_expected.to be_able_to(:destroy, @doi) } end context "when is a client admin inactive" do - let(:client) { create(:client, provider: provider, is_active: false) } - let(:token) do - User.generate_token( + before(:all) do + @prefix = create(:prefix, uid: "10.14455") + @client = create( + :client, + provider: @provider, + is_active: false + ) + @provider_prefix = create( + :provider_prefix, + provider: @provider, + prefix: @prefix + ) + @client_prefix = create( + :client_prefix, + client: @client, + prefix: @prefix + ) + @doi = create(:doi, client: @client) + @token = User.generate_token( role_id: "client_admin", - provider_id: provider.symbol.downcase, - client_id: client.symbol.downcase, + provider_id: @provider.symbol.downcase, + client_id: @client.symbol.downcase, + ) end + let(:token) { @token } + it { is_expected.to be_able_to(:read, user) } it { is_expected.to be_able_to(:read, provider) } @@ -146,39 +191,41 @@ it { is_expected.not_to be_able_to(:update, contact) } it { is_expected.not_to be_able_to(:destroy, contact) } - it { is_expected.to be_able_to(:read, client) } - it { is_expected.not_to be_able_to(:create, client) } - it { is_expected.not_to be_able_to(:update, client) } - it { is_expected.not_to be_able_to(:destroy, client) } - it { is_expected.not_to be_able_to(:transfer, client) } - it { is_expected.to be_able_to(:read_contact_information, client) } - it { is_expected.to be_able_to(:read_analytics, client) } + it { is_expected.to be_able_to(:read, @client) } + it { is_expected.not_to be_able_to(:create, @client) } + it { is_expected.not_to be_able_to(:update, @client) } + it { is_expected.not_to be_able_to(:destroy, @client) } + it { is_expected.not_to be_able_to(:transfer, @client) } + it { is_expected.to be_able_to(:read_contact_information, @client) } + it { is_expected.to be_able_to(:read_analytics, @client) } it { is_expected.not_to be_able_to(:read, prefix) } it { is_expected.not_to be_able_to(:create, prefix) } it { is_expected.not_to be_able_to(:update, prefix) } it { is_expected.not_to be_able_to(:destroy, prefix) } - it { is_expected.to be_able_to(:read, client_prefix) } - it { is_expected.not_to be_able_to(:create, client_prefix) } - it { is_expected.not_to be_able_to(:update, client_prefix) } - it { is_expected.not_to be_able_to(:destroy, client_prefix) } + it { is_expected.to be_able_to(:read, @client_prefix) } + it { is_expected.not_to be_able_to(:create, @client_prefix) } + it { is_expected.not_to be_able_to(:update, @client_prefix) } + it { is_expected.not_to be_able_to(:destroy, @client_prefix) } - it { is_expected.to be_able_to(:read, doi) } - it { is_expected.not_to be_able_to(:transfer, doi) } - it { is_expected.not_to be_able_to(:create, doi) } - it { is_expected.not_to be_able_to(:update, doi) } - it { is_expected.not_to be_able_to(:destroy, doi) } + it { is_expected.to be_able_to(:read, @doi) } + it { is_expected.not_to be_able_to(:transfer, @doi) } + it { is_expected.not_to be_able_to(:create, @doi) } + it { is_expected.not_to be_able_to(:update, @doi) } + it { is_expected.not_to be_able_to(:destroy, @doi) } end context "when is a client user" do - let(:token) do - User.generate_token( + before(:all) do + @token = User.generate_token( role_id: "client_user", - provider_id: provider.symbol.downcase, - client_id: client.symbol.downcase, + provider_id: @provider.symbol.downcase, + client_id: @client.symbol.downcase, + ) end + let(:token) { @token } it { is_expected.to be_able_to(:read, user) } it { is_expected.to be_able_to(:read, provider) } @@ -194,23 +241,23 @@ it { is_expected.not_to be_able_to(:update, contact) } it { is_expected.not_to be_able_to(:destroy, contact) } - it { is_expected.to be_able_to(:read, client) } - it { is_expected.not_to be_able_to(:create, client) } - it { is_expected.not_to be_able_to(:update, client) } - it { is_expected.not_to be_able_to(:destroy, client) } - it { is_expected.not_to be_able_to(:transfer, client) } - it { is_expected.to be_able_to(:read_contact_information, client) } - it { is_expected.to be_able_to(:read_analytics, client) } + it { is_expected.to be_able_to(:read, @client) } + it { is_expected.not_to be_able_to(:create, @client) } + it { is_expected.not_to be_able_to(:update, @client) } + it { is_expected.not_to be_able_to(:destroy, @client) } + it { is_expected.not_to be_able_to(:transfer, @client) } + it { is_expected.to be_able_to(:read_contact_information, @client) } + it { is_expected.to be_able_to(:read_analytics, @client) } it { is_expected.not_to be_able_to(:read, prefix) } it { is_expected.not_to be_able_to(:create, prefix) } it { is_expected.not_to be_able_to(:update, prefix) } it { is_expected.not_to be_able_to(:destroy, prefix) } - it { is_expected.to be_able_to(:read, client_prefix) } - it { is_expected.not_to be_able_to(:create, client_prefix) } - it { is_expected.not_to be_able_to(:update, client_prefix) } - it { is_expected.not_to be_able_to(:destroy, client_prefix) } + it { is_expected.to be_able_to(:read, @client_prefix) } + it { is_expected.not_to be_able_to(:create, @client_prefix) } + it { is_expected.not_to be_able_to(:update, @client_prefix) } + it { is_expected.not_to be_able_to(:destroy, @client_prefix) } it { is_expected.to be_able_to(:read, doi) } it { is_expected.not_to be_able_to(:transfer, doi) } @@ -368,33 +415,43 @@ end context "when is a staff admin" do + before(:all) do + @token = User.generate_token( + role_id: "staff_admin", + provider_id: @provider.symbol.downcase, + client_id: @client.symbol.downcase, + + ) + end + let(:token) { @token } + it { is_expected.to be_able_to(:read, user) } - it { is_expected.to be_able_to(:read, provider) } - it { is_expected.to be_able_to(:create, provider) } - it { is_expected.to be_able_to(:update, provider) } - it { is_expected.to be_able_to(:destroy, provider) } - it { is_expected.to be_able_to(:transfer, client) } - it { is_expected.to be_able_to(:read_billing_information, provider) } - it { is_expected.to be_able_to(:read_contact_information, provider) } + it { is_expected.to be_able_to(:read, @provider) } + it { is_expected.to be_able_to(:create, @provider) } + it { is_expected.to be_able_to(:update, @provider) } + it { is_expected.to be_able_to(:destroy, @provider) } + it { is_expected.to be_able_to(:transfer, @client) } + it { is_expected.to be_able_to(:read_billing_information, @provider) } + it { is_expected.to be_able_to(:read_contact_information, @provider) } it { is_expected.to be_able_to(:read, contact) } it { is_expected.to be_able_to(:create, contact) } it { is_expected.to be_able_to(:update, contact) } it { is_expected.to be_able_to(:destroy, contact) } - it { is_expected.to be_able_to(:read, client) } - it { is_expected.to be_able_to(:create, client) } - it { is_expected.to be_able_to(:update, client) } - it { is_expected.to be_able_to(:destroy, client) } - it { is_expected.to be_able_to(:read_contact_information, client) } - it { is_expected.to be_able_to(:read_analytics, client) } - - it { is_expected.to be_able_to(:read, doi) } - it { is_expected.to be_able_to(:transfer, doi) } - it { is_expected.to be_able_to(:create, doi) } - it { is_expected.to be_able_to(:update, doi) } - it { is_expected.to be_able_to(:destroy, doi) } + it { is_expected.to be_able_to(:read, @client) } + it { is_expected.to be_able_to(:create, @client) } + it { is_expected.to be_able_to(:update, @client) } + it { is_expected.to be_able_to(:destroy, @client) } + it { is_expected.to be_able_to(:read_contact_information, @client) } + it { is_expected.to be_able_to(:read_analytics, @client) } + + it { is_expected.to be_able_to(:read, @doi) } + it { is_expected.to be_able_to(:transfer, @doi) } + it { is_expected.to be_able_to(:create, @doi) } + it { is_expected.to be_able_to(:update, @doi) } + it { is_expected.to be_able_to(:destroy, @doi) } end context "when is a staff user" do From a398fb6338af67fb4f42ae1b1214544444b8df07 Mon Sep 17 00:00:00 2001 From: jrhoads Date: Mon, 16 Sep 2024 14:59:11 +0200 Subject: [PATCH 4/4] Appease rubocop --- spec/models/ability_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index 0156bd6c8..50997f635 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -29,7 +29,7 @@ let(:metadata) { build_stubbed(:metadata, xml: xml, doi: doi) } before(:all) do - @consortium = create( :provider, + @consortium = create(:provider, role_name: "ROLE_CONSORTIUM") @provider = create(:provider, consortium: @consortium, @@ -97,7 +97,6 @@ end context "when is a client admin" do - before(:all) do @token = User.generate_token( role_id: "client_admin",