-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
And I added more tests covering missing cases while at it.
- Loading branch information
1 parent
6064e4f
commit 5cc20be
Showing
3 changed files
with
82 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|