Skip to content

Commit

Permalink
Merge pull request #1244 from datacite/faster-tests-1
Browse files Browse the repository at this point in the history
Faster tests 1 - ability_spec.rb
  • Loading branch information
jrhoads authored Sep 16, 2024
2 parents 42d9c01 + a398fb6 commit 25a8b6b
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 106 deletions.
2 changes: 1 addition & 1 deletion spec/factories/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}" }
Expand Down
266 changes: 161 additions & 105 deletions spec/models/ability_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -75,63 +97,85 @@
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) }

Expand All @@ -146,39 +190,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) }
Expand All @@ -194,23 +240,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) }
Expand Down Expand Up @@ -368,33 +414,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
Expand Down
12 changes: 12 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = []
Expand Down

0 comments on commit 25a8b6b

Please sign in to comment.