Skip to content

Commit

Permalink
Extract RevokeUser command
Browse files Browse the repository at this point in the history
And I added more tests covering missing cases while at it.
  • Loading branch information
sauloperez committed Apr 12, 2021
1 parent 6064e4f commit 5cc20be
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 28 deletions.
46 changes: 46 additions & 0 deletions lib/decidim/direct_verifications/revoke_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

module Decidim
module DirectVerifications
class RevokeUser
def initialize(email, organization, instrumenter)
@email = email
@organization = organization
@instrumenter = instrumenter
end

def call
if (u = find_user)
auth = authorization(u)
return unless auth.granted?

Verification::DestroyUserAuthorization.call(auth) do
on(:ok) do
instrumenter.add_processed :revoked, email
end
on(:invalid) do
add_error :revoked, email
end
end
else
instrumenter.add_error :revoked, email
end
end

private

attr_reader :email, :organization, :instrumenter

def find_user
User.find_by(email: email, decidim_organization_id: organization.id)
end

def authorization(user)
Authorization.find_or_initialize_by(
user: user,
name: :direct_verifications
)
end
end
end
end
28 changes: 2 additions & 26 deletions lib/decidim/direct_verifications/user_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "decidim/direct_verifications/register_user"
require "decidim/direct_verifications/authorize_user"
require "decidim/direct_verifications/revoke_user"

module Decidim
module DirectVerifications
Expand Down Expand Up @@ -38,21 +39,7 @@ def authorize_users

def revoke_users
emails.each do |email, _name|
if (u = find_user(email))
auth = authorization(u)
next unless auth.granted?

Verification::DestroyUserAuthorization.call(auth) do
on(:ok) do
add_processed :revoked, email
end
on(:invalid) do
add_error :revoked, email
end
end
else
add_error :revoked, email
end
RevokeUser.new(email, organization, self).call
end
end

Expand Down Expand Up @@ -86,17 +73,6 @@ def log_action(user)
}
)
end

def find_user(email)
User.find_by(email: email, decidim_organization_id: @organization.id)
end

def authorization(user)
Authorization.find_or_initialize_by(
user: user,
name: authorization_handler
)
end
end
end
end
36 changes: 34 additions & 2 deletions spec/lib/decidim/direct_verifications/user_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,11 @@ module DirectVerifications
before do
subject.emails = { user.email => user.name }
subject.authorize_users
subject.revoke_users
end

it "has no errors" do
subject.revoke_users

expect(subject.processed[:revoked].count).to eq(1)
expect(subject.errors[:revoked].count).to eq(0)
end
Expand All @@ -153,12 +154,43 @@ module DirectVerifications
before do
subject.emails = ["[email protected]"]
subject.authorize_users
end

it "has errors" do
subject.revoke_users

expect(subject.processed[:revoked].count).to eq(0)
expect(subject.errors[:revoked].count).to eq(1)
end
end

context "when the authorization does not exist" do
before do
create(:user, email: "[email protected]", organization: organization)
subject.emails = ["[email protected]"]
end

it "has no errors" do
subject.revoke_users

expect(subject.processed[:revoked].count).to eq(0)
expect(subject.errors[:revoked].count).to eq(0)
end
end

context "when the authorization is not granted" do
let(:user) { create(:user, email: "[email protected]", organization: organization) }

before do
subject.emails = ["[email protected]"]
create(:authorization, :pending, user: user, name: :direct_verifications)
end

it "has no errors" do
subject.revoke_users

expect(subject.processed[:revoked].count).to eq(0)
expect(subject.errors[:revoked].count).to eq(1)
expect(subject.errors[:revoked].count).to eq(0)
end
end
end
Expand Down

0 comments on commit 5cc20be

Please sign in to comment.