-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
287 additions
and
2 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,25 @@ | ||
# frozen_string_literal: true | ||
|
||
require "digest" | ||
|
||
module Decidim | ||
module Centers | ||
module Verifications | ||
class Center < Decidim::AuthorizationHandler | ||
validate :center_present | ||
|
||
def metadata | ||
super.merge( | ||
centers: user.centers.pluck(:id) | ||
) | ||
end | ||
|
||
protected | ||
|
||
def center_present | ||
errors.add(:user, I18n.t("decidim.centers.authorizations.new.error")) unless user.centers.any? | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module Centers | ||
class AutoVerificationJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(user_id) | ||
@user = Decidim::User.find(user_id) | ||
@user.centers.any? ? create_auth : remove_auth | ||
rescue ActiveRecord::RecordNotFound => _e | ||
Rails.logger.error "AutoVerificationJob: ERROR: user not found #{user_id}" | ||
end | ||
|
||
private | ||
|
||
def create_auth | ||
return unless (handler = Decidim::AuthorizationHandler.handler_for("center", user: @user)) | ||
|
||
Decidim::Verifications::AuthorizeUser.call(handler, @user.organization) do | ||
on(:ok) do | ||
Rails.logger.info "AutoVerificationJob: Success: created for user #{handler.user.id}" | ||
end | ||
|
||
on(:invalid) do | ||
Rails.logger.error "AutoVerificationJob: ERROR: not created for user #{handler.user&.id}" | ||
end | ||
end | ||
end | ||
|
||
def remove_auth | ||
Decidim::Authorization.where(user: @user, name: "center").each do |auth| | ||
Decidim::Verifications::DestroyUserAuthorization.call(auth) do | ||
on(:ok) do | ||
Rails.logger.info "AutoVerificationJob: Success: removed for user #{auth.user.id}" | ||
end | ||
|
||
on(:invalid) do | ||
Rails.logger.error "AutoVerificationJob: ERROR: not removed for user #{auth.user&.id}" | ||
end | ||
end | ||
end | ||
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
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,13 @@ | ||
# frozen_string_literal: true | ||
|
||
def check_center_authorization(authorization, user, center) | ||
expect(authorization.name).to eq("center") | ||
expect(authorization.user).to eq(user) | ||
expect(authorization.metadata["centers"]).to include(center.id) | ||
end | ||
|
||
shared_examples_for "no authorization is created" do | ||
it "does not create an authorization" do | ||
expect { subject.perform_now(params) }.not_to change(Decidim::Authorization, :count) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
module Decidim | ||
module Centers | ||
module Verifications | ||
describe Center do | ||
subject { described_class.from_params(attributes) } | ||
|
||
let(:attributes) do | ||
{ | ||
"user" => user | ||
} | ||
end | ||
let(:user) { center_user.user } | ||
let(:center_user) { create :center_user } | ||
let(:metadata) do | ||
{ | ||
centers: [center_user.center.id] | ||
} | ||
end | ||
|
||
context "when everything is ok" do | ||
it { is_expected.to be_valid } | ||
|
||
it "returns valid metadata" do | ||
expect(subject.metadata).to eq(metadata) | ||
end | ||
end | ||
|
||
context "when the user has no center" do | ||
let(:user) { create :user } | ||
|
||
it { is_expected.not_to be_valid } | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
require "decidim/centers/test/shared_contexts" | ||
|
||
module Decidim | ||
module Centers | ||
describe AutoVerificationJob do | ||
subject { described_class } | ||
|
||
describe "queue" do | ||
it "is queued to events" do | ||
expect(subject.queue_name).to eq "default" | ||
end | ||
end | ||
|
||
describe "perform" do | ||
let(:user) { create :user } | ||
let(:params) { user.id } | ||
|
||
before do | ||
allow(Rails.logger).to receive(:info).and_call_original | ||
allow(Rails.logger).to receive(:error).and_call_original | ||
end | ||
|
||
context "when the user has no center" do | ||
it_behaves_like "no authorization is created" | ||
|
||
context "when there is a previous authorization for the user" do | ||
let!(:authorization) { create :authorization, name: "center", user: user } | ||
|
||
it "removes an authorization" do | ||
expect { subject.perform_now(params) }.to change(Decidim::Authorization, :count).by(-1) | ||
end | ||
|
||
context "when the authorization cannot be removed" do | ||
before do | ||
# rubocop: disable RSpec/AnyInstance | ||
allow_any_instance_of(Decidim::Verifications::DestroyUserAuthorization).to receive(:authorization).and_return(nil) | ||
# rubocop: enable RSpec/AnyInstance | ||
end | ||
|
||
it "writes an error log" do | ||
subject.perform_now(params) | ||
expect(Rails.logger).to have_received(:error).with(/AutoVerificationJob: ERROR: not removed for user/) | ||
end | ||
end | ||
end | ||
end | ||
|
||
context "when the user has center" do | ||
let!(:center_user) { create :center_user, user: user } | ||
|
||
it "creates an authorization" do | ||
expect { subject.perform_now(params) }.to change(Decidim::Authorization, :count).by(1) | ||
end | ||
|
||
context "when the authorization cannot be created" do | ||
before do | ||
# rubocop: disable RSpec/AnyInstance | ||
allow_any_instance_of(Decidim::AuthorizationHandler).to receive(:invalid?).and_return(true) | ||
# rubocop: enable RSpec/AnyInstance | ||
end | ||
|
||
it "writes an error log" do | ||
subject.perform_now(params) | ||
expect(Rails.logger).to have_received(:error).with(/AutoVerificationJob: ERROR: not created for user/) | ||
end | ||
end | ||
|
||
context "when there is a previous authorization for the user" do | ||
let!(:authorization) { create :authorization, name: "center", user: user } | ||
|
||
it_behaves_like "no authorization is created" | ||
end | ||
end | ||
|
||
context "when the user does not exist" do | ||
let(:params) { -1 } | ||
|
||
it_behaves_like "no authorization is created" | ||
|
||
it "writes an error log" do | ||
subject.perform_now(params) | ||
expect(Rails.logger).to have_received(:error).with(/AutoVerificationJob: ERROR: user not found/) | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
module Decidim | ||
module Centers | ||
describe SyncCenterUserJob do | ||
subject { described_class } | ||
|
||
describe "queue" do | ||
it "is queued to events" do | ||
expect(subject.queue_name).to eq "default" | ||
end | ||
end | ||
|
||
describe "perform" do | ||
let(:user) { create :user } | ||
let(:center) { create :center } | ||
let(:params) { { user_id: user.id, center_id: center.id } } | ||
|
||
before do | ||
allow(Rails.logger).to receive(:info).and_call_original | ||
allow(Rails.logger).to receive(:error).and_call_original | ||
subject.perform_now(params) | ||
end | ||
|
||
context "when the sync runs successfully" do | ||
it "writes an info log" do | ||
expect(Rails.logger).to have_received(:info).with(/SyncCenterUserJob: Success/) | ||
end | ||
end | ||
|
||
context "when the sync fails" do | ||
before do | ||
# rubocop: disable RSpec/AnyInstance | ||
allow_any_instance_of(Decidim::Centers::CreateOrUpdateCenterUser).to receive(:delete_existing_center_user!).and_raise | ||
# rubocop: enable RSpec/AnyInstance | ||
end | ||
|
||
it "writes an error log" do | ||
expect(Rails.logger).to have_received(:error).with(/SyncCenterUserJob: ERROR/) | ||
end | ||
end | ||
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