Skip to content

Commit

Permalink
Removed extra elasticsearch/prefix setup. Move to build_stubbed model…
Browse files Browse the repository at this point in the history
…s where possibl
  • Loading branch information
jrhoads committed Sep 16, 2024
1 parent 77f0693 commit 9f6235b
Showing 1 changed file with 162 additions and 105 deletions.
267 changes: 162 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,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) }

Expand All @@ -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) }
Expand All @@ -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) }
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9f6235b

Please sign in to comment.