Skip to content

Commit

Permalink
add more specs
Browse files Browse the repository at this point in the history
  • Loading branch information
microstudi committed May 24, 2024
1 parent 4c26b66 commit b18f6cb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def new
def create
enforce_permission_to :create, :delegation

@form = DelegationForm.from_params(params)
@form = form(DelegationForm).from_params(params)

CreateDelegation.call(@form, current_user, current_setting) do
on(:ok) do
Expand Down
8 changes: 6 additions & 2 deletions app/forms/decidim/action_delegator/admin/delegation_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ class DelegationForm < Form
validate :grantee_exists

def granter
User.find_by(id: granter_id) || User.find_by(email: granter_email)
User.find_by(id: granter_id, organization: current_organization) || User.find_by(email: granter_email, organization: current_organization)
end

def grantee
User.find_by(id: grantee_id) || User.find_by(email: grantee_email)
User.find_by(id: grantee_id, organization: current_organization) || User.find_by(email: grantee_email, organization: current_organization)
end

private

def current_organization
context&.current_organization
end

def granter_exists
return if granter.present?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,12 @@
expect { subject.call }.to broadcast(:error)
end
end

context "when granter is not in the same organization" do
let(:granter) { create(:user) }

it "broadcasts :error" do
expect { subject.call }.to broadcast(:error)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require "spec_helper"

describe Decidim::ActionDelegator::Admin::DelegationForm do
subject { described_class.from_params(attributes) }
subject { described_class.from_params(attributes).with_context(current_organization: organization) }

let(:organization) { create(:organization) }
let(:granter) { create(:user, organization: organization) }
Expand All @@ -23,6 +23,18 @@

context "when there's granter and grantee" do
it { is_expected.to be_valid }

context "when granter belongs to another organization" do
let(:granter) { create(:user) }

it { is_expected.not_to be_valid }
end

context "when grantee belongs to another organization" do
let(:grantee) { create(:user) }

it { is_expected.not_to be_valid }
end
end

context "when granter is missing" do
Expand Down
9 changes: 9 additions & 0 deletions spec/models/decidim/action_delegator/delegation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ module ActionDelegator
it { is_expected.to be_valid }
it { is_expected.not_to be_grantee_voted }

context "when grantee is the same as the granter" do
let(:setting) { create(:setting) }
let(:grantee) { create(:user, organization: setting.organization) }

subject { build(:delegation, setting: setting, grantee: grantee, granter: grantee) }

it { is_expected.not_to be_valid }
end

context "when users from different organizations" do
let(:grantee) { create(:user) }

Expand Down

0 comments on commit b18f6cb

Please sign in to comment.