mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-18 04:39:14 +00:00
Enterprise validates owner enterprise_limit
This commit is contained in:
@@ -257,13 +257,13 @@ feature %q{
|
||||
let(:supplier2) { create(:supplier_enterprise, name: 'Another Supplier') }
|
||||
let(:distributor1) { create(:distributor_enterprise, name: 'First Distributor') }
|
||||
let(:distributor2) { create(:distributor_enterprise, name: 'Another Distributor') }
|
||||
let(:enterprise_user) { create_enterprise_user }
|
||||
|
||||
before(:each) do
|
||||
@new_user = create_enterprise_user
|
||||
@new_user.enterprise_roles.build(enterprise: supplier1).save
|
||||
@new_user.enterprise_roles.build(enterprise: distributor1).save
|
||||
enterprise_user.enterprise_roles.build(enterprise: supplier1).save
|
||||
enterprise_user.enterprise_roles.build(enterprise: distributor1).save
|
||||
|
||||
login_to_admin_as @new_user
|
||||
login_to_admin_as enterprise_user
|
||||
end
|
||||
|
||||
scenario "can view enterprises I have permission to" do
|
||||
@@ -290,23 +290,42 @@ feature %q{
|
||||
expect(page).to_not have_content "distributor2.name"
|
||||
end
|
||||
|
||||
scenario "creating an enterprise" do
|
||||
# When I create an enterprise
|
||||
click_link 'Enterprises'
|
||||
click_link 'New Enterprise'
|
||||
fill_in 'enterprise_name', with: 'zzz'
|
||||
fill_in 'enterprise_address_attributes_address1', with: 'z'
|
||||
fill_in 'enterprise_address_attributes_city', with: 'z'
|
||||
fill_in 'enterprise_address_attributes_zipcode', with: 'z'
|
||||
click_button 'Create'
|
||||
context "creating an enterprise" do
|
||||
before do
|
||||
# When I create an enterprise
|
||||
click_link 'Enterprises'
|
||||
click_link 'New Enterprise'
|
||||
fill_in 'enterprise_name', with: 'zzz'
|
||||
fill_in 'enterprise_address_attributes_address1', with: 'z'
|
||||
fill_in 'enterprise_address_attributes_city', with: 'z'
|
||||
fill_in 'enterprise_address_attributes_zipcode', with: 'z'
|
||||
end
|
||||
|
||||
# Then it should be created
|
||||
page.should have_content 'Enterprise "zzz" has been successfully created!'
|
||||
enterprise = Enterprise.last
|
||||
enterprise.name.should == 'zzz'
|
||||
scenario "without violating rules" do
|
||||
click_button 'Create'
|
||||
|
||||
# And I should be managing it
|
||||
Enterprise.managed_by(@new_user).should include enterprise
|
||||
# Then it should be created
|
||||
page.should have_content 'Enterprise "zzz" has been successfully created!'
|
||||
enterprise = Enterprise.last
|
||||
enterprise.name.should == 'zzz'
|
||||
|
||||
# And I should be managing it
|
||||
Enterprise.managed_by(enterprise_user).should include enterprise
|
||||
end
|
||||
|
||||
context "overstepping my owned enterprises limit" do
|
||||
before do
|
||||
create(:enterprise, owner: enterprise_user)
|
||||
end
|
||||
|
||||
it "shows me an error message" do
|
||||
click_button 'Create'
|
||||
|
||||
# Then it should show me an error
|
||||
expect(page).to_not have_content 'Enterprise "zzz" has been successfully created!'
|
||||
expect(page).to have_content "You are not permitted to own own any more enterprises (limit is 1)."
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
scenario "editing enterprises I have permission to" do
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Enterprise do
|
||||
include AuthenticationWorkflow
|
||||
|
||||
describe "associations" do
|
||||
it { should belong_to(:owner) }
|
||||
@@ -54,9 +55,9 @@ describe Enterprise do
|
||||
end
|
||||
|
||||
describe "ownership" do
|
||||
let(:u1) { create(:user) }
|
||||
let(:u2) { create(:user) }
|
||||
let(:e) { create(:enterprise, owner: u1 ) }
|
||||
let(:u1) { create_enterprise_user }
|
||||
let(:u2) { create_enterprise_user }
|
||||
let!(:e) { create(:enterprise, owner: u1 ) }
|
||||
|
||||
it "adds new owner to list of managers" do
|
||||
expect(e.owner).to eq u1
|
||||
@@ -68,6 +69,16 @@ describe Enterprise do
|
||||
expect(e.owner).to eq u2
|
||||
expect(e.users).to include u1, u2
|
||||
end
|
||||
|
||||
it "validates ownership limit" do
|
||||
expect(u1.enterprise_limit).to be 1
|
||||
expect(u1.owned_enterprises(:reload)).to eq [e]
|
||||
e2 = create(:enterprise, owner: u2 )
|
||||
expect{
|
||||
e2.owner = u1
|
||||
e2.save!
|
||||
}.to raise_error ActiveRecord::RecordInvalid, "Validation failed: You are not permitted to own own any more enterprises (limit is 1)."
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -3,23 +3,23 @@ describe Spree.user_class do
|
||||
it { should have_many(:owned_enterprises) }
|
||||
|
||||
describe "enterprise ownership" do
|
||||
let(:u1) { create(:user) }
|
||||
let(:u2) { create(:user) }
|
||||
let(:e1) { create(:enterprise, owner: u1) }
|
||||
let(:e2) { create(:enterprise, owner: u1) }
|
||||
let(:u1) { create(:user, enterprise_limit: 2) }
|
||||
let(:u2) { create(:user, enterprise_limit: 1) }
|
||||
let!(:e1) { create(:enterprise, owner: u1) }
|
||||
let!(:e2) { create(:enterprise, owner: u1) }
|
||||
|
||||
it "provides access to owned enteprises" do
|
||||
expect(u1.owned_enterprises).to include e1, e2
|
||||
it "provides access to owned enterprises" do
|
||||
expect(u1.owned_enterprises(:reload)).to include e1, e2
|
||||
end
|
||||
|
||||
it "enforces the limit on the number of enterprise owned" do
|
||||
expect(u2.owned_enterprises).to eq []
|
||||
expect(u2.owned_enterprises(:reload)).to eq []
|
||||
u2.owned_enterprises << e1
|
||||
u2.owned_enterprises << e2
|
||||
expect(u2.save!).to_not raise_error
|
||||
expect {
|
||||
u2.owned_enterprises << e2
|
||||
u2.save!
|
||||
}.to raise_error ActiveRecord::RecordInvalid, "Validation failed: The nominated user is not permitted to own own any more enterprises."
|
||||
|
||||
}.to raise_error ActiveRecord::RecordInvalid, "Validation failed: The nominated user is not permitted to own own any more enterprises (limit is 1)."
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -38,7 +38,7 @@ module AuthenticationWorkflow
|
||||
end
|
||||
|
||||
def create_enterprise_user(enterprises = [])
|
||||
new_user = create(:user, email: 'enterprise@hub.com', password: 'blahblah', :password_confirmation => 'blahblah', )
|
||||
new_user = create(:user, password: 'blahblah', :password_confirmation => 'blahblah')
|
||||
new_user.spree_roles = [] # for some reason unbeknown to me, this new user gets admin permissions by default.
|
||||
for enterprise in enterprises do
|
||||
new_user.enterprise_roles.build(enterprise: enterprise).save
|
||||
|
||||
Reference in New Issue
Block a user