Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
31 changes: 31 additions & 0 deletions app/services/push_notification/account_disabled_event.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions app/services/push_notification/account_enabled_event.rb
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
34 changes: 34 additions & 0 deletions spec/services/push_notification/account_disabled_event_spec.rb
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions spec/services/push_notification/account_enabled_event_spec.rb
Original file line number Diff line number Diff line change
@@ -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