Skip to content

Commit b7a4051

Browse files
authored
Fix user verification (#22)
1 parent a5ab2cb commit b7a4051

File tree

6 files changed

+66
-51
lines changed

6 files changed

+66
-51
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ Following Semantic Versioning 2.
33

44
## next version:
55

6+
## Version 0.7.2 (PATCH)
7+
- Fix user verification.
8+
69
## Version 0.7.1 (PATCH)
710
- Fix invokation of `OnOmniauthRegistrationListener.on_omniauth_registration` after login/registration.
811

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
decidim-idcat_mobil (0.7.1)
4+
decidim-idcat_mobil (0.7.2)
55
decidim (>= 0.29.0)
66
decidim-core (>= 0.29.0)
77
omniauth-idcat_mobil (~> 0.6.0)

app/forms/decidim/verifications/valid_handler.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ValidHandler < AuthorizationHandler
1010
validate :idcatmobil_method?, :has_ok_status?
1111

1212
def unique_id
13-
oauth_data["identifier"]
13+
oauth_data[:uid]
1414
end
1515

1616
def metadata
@@ -31,14 +31,14 @@ def to_partial_path
3131
#-----------------------------------------------------------
3232

3333
def idcatmobil_method?
34-
return true if oauth_data["method"] == "idcatmobil"
34+
return true if oauth_data.dig(:raw_data, :extra, :method) == "idcatmobil"
3535

3636
errors.add(:base, I18n.t("decidim.verifications.valid.errors.invalid_method"))
3737
false
3838
end
3939

4040
def has_ok_status?
41-
return true if oauth_data["status"] == "ok"
41+
return true if oauth_data.dig(:raw_data, :extra, :status) == "ok"
4242

4343
errors.add(:base, I18n.t("decidim.verifications.valid.errors.invalid_status"))
4444
false

app/jobs/decidim/valid/verification_job.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ class VerificationJob < ApplicationJob
66
queue_as :default
77

88
def perform(oauth_data)
9-
authorization ||= Decidim::Authorization.find_by(unique_id: handler.unique_id)
9+
oauth_data = oauth_data.deep_symbolize_keys
10+
handler= retrieve_handler(oauth_data)
11+
authorization= Decidim::Authorization.find_by(name: "valid", unique_id: handler.unique_id)
1012
return if authorization&.granted?
1113

12-
handler = retrieve_handler(oauth_data)
13-
Decidim::Verifications::AuthorizeUser.call(handler) do
14+
user= Decidim::User.find(oauth_data[:user_id])
15+
Decidim::Verifications::AuthorizeUser.call(handler, user.organization) do
1416
on(:ok) do
17+
Rails.logger.info("User #{user.id} verified successfully with VÀLid.")
1518
notify_user(handler.user, :ok, handler)
1619
end
1720

1821
on(:invalid) do
19-
notify_user(handler.user, :invalid, handler)
22+
Rails.logger.error("Could not verify user #{user.id} with VÀLid: #{handler.errors.full_messages.join(", ")}")
23+
notify_user(user, :invalid, handler) if user
2024
end
2125
end
2226
end
@@ -28,16 +32,16 @@ def perform(oauth_data)
2832

2933
# Retrieves handler from Verification workflows registry.
3034
def retrieve_handler(oauth_data)
31-
Decidim::AuthorizationHandler.handler_for(:valid, oauth_data:)
35+
Decidim::AuthorizationHandler.handler_for("valid", oauth_data:)
3236
end
3337

3438
def notify_user(user, status, handler)
3539
notification_class = status == :ok ? Decidim::IdcatMobil::VerificationSuccessNotification : Decidim::IdcatMobil::VerificationInvalidNotification
3640
Decidim::EventsManager.publish(
3741
event: "decidim.verifications.idcat_mobil.#{status}",
3842
event_class: notification_class,
39-
# resource: result,
40-
recipient_ids: [user.id],
43+
resource: user,
44+
affected_users: [user],
4145
extra: {
4246
status:,
4347
errors: handler.errors.full_messages

lib/decidim/idcat_mobil/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Decidim
44
module IdcatMobil
55
def self.version
66
# See CHANGELOG.md to see what changed
7-
"0.7.1"
7+
"0.7.2"
88
end
99

1010
def self.decidim_version

spec/jobs/decidim/valid/verification_job_spec.rb

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,57 @@
66

77
module Decidim::Valid
88
describe VerificationJob do
9-
def pending_to_be_finished
10-
pending("Implementation pending, this Decidim module does not support user verification")
11-
end
9+
let!(:organization) { create(:organization, host: "#{SecureRandom.hex(10)}.example.com") }
10+
let!(:user) { create(:user, organization:) }
11+
let!(:identity) { create(:identity, provider: "idcat_mobil", user:) }
1212

13-
let!(:user) { create(:user) }
14-
let!(:identity) { create(:identity, provider: "idecat_mobil", user:) }
1513
let(:oauth_data) do
1614
{
1715
user_id: user.id,
1816
identity_id: identity.id,
1917
provider: "idcat_mobil",
20-
uid: "idcat_mobil/#{user.id}",
18+
uid: "00000000K",
2119
email: user.email,
22-
name: "idcat_mobil",
23-
nickname: nil,
20+
name: "Alice",
21+
nickname: "alice",
2422
avatar_url: nil,
25-
raw_data: {}
23+
raw_data: {
24+
provider: :idcat_mobil,
25+
uid: "00000000K",
26+
info: {
27+
email: user.email,
28+
name: "Alice",
29+
prefix: "0034",
30+
phone: "972972972",
31+
surname1: "COOPER",
32+
surname2: "IRON",
33+
surnames: "COOPER IRON",
34+
country_code: "ES"
35+
},
36+
credentials: {
37+
token: "1/abcdefABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgh",
38+
expires_at: 1_773_128_240,
39+
expires: true
40+
},
41+
extra: {
42+
identifier_type: "1",
43+
method: "idcatmobil",
44+
assurance_level: "low",
45+
status: "ok"
46+
}
47+
}
2648
}
2749
end
2850

29-
# rubocop: disable Lint/ConstantDefinitionInBlock
30-
class TestDecidimPublisher < Decidim::Command
31-
include Wisper::Publisher
32-
def initialize(*args)
33-
super
34-
end
35-
end
36-
37-
# rubocop: enable Lint/ConstantDefinitionInBlock
38-
def stub_decidim_publisher(clazz, called_method, event_to_publish, *published_event_args)
39-
stub_const(clazz, Class.new(TestDecidimPublisher) do
40-
define_method(called_method) do |*_args|
41-
publish(event_to_publish, *published_event_args)
42-
end
43-
end)
44-
end
45-
4651
context "when omniauth_registration event is notified" do
4752
context "when authorization is created with success" do
4853
it "notifies the user for the success" do
49-
pending_to_be_finished
50-
stub_decidim_publisher("Decidim::Verifications::AuthorizeUser", :call, :ok)
5154
expect(Decidim::EventsManager)
5255
.to receive(:publish)
5356
.with(
5457
event: "decidim.verifications.idcat_mobil.ok",
5558
event_class: Decidim::IdcatMobil::VerificationSuccessNotification,
59+
resource: user,
5660
recipient_ids: [user.id],
5761
extra: {
5862
status: :ok,
@@ -65,21 +69,25 @@ def stub_decidim_publisher(clazz, called_method, event_to_publish, *published_ev
6569
end
6670

6771
context "when authorization creation fails" do
72+
let(:oauth_data) do
73+
super().merge(status: "invalid", "status" => "invalid")
74+
end
75+
6876
it "notifies the user for the failure" do
69-
pending_to_be_finished
70-
stub_decidim_publisher("Decidim::Verifications::AuthorizeUser", :call, :invalid)
7177
expect(Decidim::EventsManager)
7278
.to receive(:publish)
73-
.with(
74-
event: "decidim.verifications.idcat_mobil.invalid",
75-
event_class: Decidim::IdcatMobil::VerificationInvalidNotification,
76-
recipient_ids: [user.id],
77-
extra: {
78-
status: :invalid,
79-
errors: []
80-
}
81-
)
79+
.with(hash_including(
80+
event: "decidim.verifications.idcat_mobil.invalid",
81+
event_class: Decidim::IdcatMobil::VerificationInvalidNotification,
82+
resource: user,
83+
recipient_ids: [user.id],
84+
extra: hash_including(
85+
status: :invalid,
86+
errors: ["Unique cannot be blank"]
87+
)
88+
))
8289

90+
oauth_data.delete(:uid)
8391
VerificationJob.new.perform(oauth_data)
8492
end
8593
end

0 commit comments

Comments
 (0)