diff --git a/app/models/user.rb b/app/models/user.rb index e4c4f58391b..1c17fce179d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -122,6 +122,10 @@ def suspend! OutOfBandSessionAccessor.new(unique_session_id).destroy if unique_session_id update!(suspended_at: Time.zone.now, unique_session_id: nil) analytics.user_suspended(success: true) + + event = PushNotification::AccountDisabledEvent.new(user: self) + PushNotification::HttpPush.deliver(event) + email_addresses.map do |email_address| SuspendedEmail.create_from_email_address!(email_address) end @@ -134,6 +138,10 @@ def reinstate! end update!(reinstated_at: Time.zone.now) analytics.user_reinstated(success: true) + + event = PushNotification::AccountEnabledEvent.new(user: self) + PushNotification::HttpPush.deliver(event) + email_addresses.map do |email_address| SuspendedEmail.find_with_email(email_address.email)&.destroy end diff --git a/app/services/push_notification/account_disabled_event.rb b/app/services/push_notification/account_disabled_event.rb new file mode 100644 index 00000000000..d35ec8acd8a --- /dev/null +++ b/app/services/push_notification/account_disabled_event.rb @@ -0,0 +1,31 @@ +module PushNotification + # This is used for account suspension + class AccountDisabledEvent + EVENT_TYPE = 'https://schemas.openid.net/secevent/risc/event-type/account-disabled'.freeze + + attr_reader :user + + def initialize(user:) + @user = user + end + + def event_type + EVENT_TYPE + end + + def payload(iss_sub:) + { + subject: { + subject_type: 'iss-sub', + iss: Rails.application.routes.url_helpers.root_url, + sub: iss_sub, + }, + reason: 'account-suspension', + } + end + + def ==(other) + self.class == other.class && user == other.user + end + end +end diff --git a/app/services/push_notification/account_enabled_event.rb b/app/services/push_notification/account_enabled_event.rb new file mode 100644 index 00000000000..759d2436230 --- /dev/null +++ b/app/services/push_notification/account_enabled_event.rb @@ -0,0 +1,18 @@ +module PushNotification + # This is used for account reinstatement + class AccountEnabledEvent + include IssSubEvent + + EVENT_TYPE = 'https://schemas.openid.net/secevent/risc/event-type/account-enabled'.freeze + + attr_reader :user + + def initialize(user:) + @user = user + end + + def event_type + EVENT_TYPE + end + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 4a23b2ac108..f3a8518c6cd 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -923,6 +923,14 @@ user.suspend! end + it 'send account disabled push event' do + expect(PushNotification::HttpPush).to receive(:deliver).once. + with(PushNotification::AccountDisabledEvent.new( + user: user, + )) + user.suspend! + end + it 'logs out the suspended user from the active session' do # Add information to session store to allow `exists?` check to work as desired OutOfBandSessionAccessor.new(mock_session_id).put_pii( @@ -984,6 +992,14 @@ user.reinstate! end + it 'send account enabled push event' do + expect(PushNotification::HttpPush).to receive(:deliver).once. + with(PushNotification::AccountEnabledEvent.new( + user: user, + )) + user.reinstate! + end + it 'raises an error if the user is not currently suspended' do user.suspended_at = nil expect(user.analytics).to receive(:user_reinstated).with( diff --git a/spec/services/push_notification/account_disabled_event_spec.rb b/spec/services/push_notification/account_disabled_event_spec.rb new file mode 100644 index 00000000000..99ceec32c3d --- /dev/null +++ b/spec/services/push_notification/account_disabled_event_spec.rb @@ -0,0 +1,34 @@ +require 'rails_helper' + +RSpec.describe PushNotification::AccountDisabledEvent do + include Rails.application.routes.url_helpers + + subject(:event) do + PushNotification::AccountDisabledEvent.new(user: user) + end + + let(:user) { build(:user) } + + describe '#event_type' do + it 'is the RISC event type' do + expect(event.event_type).to eq(PushNotification::AccountDisabledEvent::EVENT_TYPE) + end + end + + describe '#payload' do + let(:iss_sub) { SecureRandom.uuid } + + subject(:payload) { event.payload(iss_sub: iss_sub) } + + it 'is a subject with the provided iss_sub and reason' do + expect(payload).to eq( + subject: { + subject_type: 'iss-sub', + sub: iss_sub, + iss: root_url, + }, + reason: 'account-suspension', + ) + end + end +end diff --git a/spec/services/push_notification/account_enabled_event_spec.rb b/spec/services/push_notification/account_enabled_event_spec.rb new file mode 100644 index 00000000000..17be59943c1 --- /dev/null +++ b/spec/services/push_notification/account_enabled_event_spec.rb @@ -0,0 +1,33 @@ +require 'rails_helper' + +RSpec.describe PushNotification::AccountEnabledEvent do + include Rails.application.routes.url_helpers + + subject(:event) do + PushNotification::AccountEnabledEvent.new(user: user) + end + + let(:user) { build(:user) } + + describe '#event_type' do + it 'is the RISC event type' do + expect(event.event_type).to eq(PushNotification::AccountEnabledEvent::EVENT_TYPE) + end + end + + describe '#payload' do + let(:iss_sub) { SecureRandom.uuid } + + subject(:payload) { event.payload(iss_sub: iss_sub) } + + it 'is a subject with the provided iss_sub ' do + expect(payload).to eq( + subject: { + subject_type: 'iss-sub', + sub: iss_sub, + iss: root_url, + }, + ) + end + end +end