-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MHV Login/Logout facility Integration (#452)
* adding the logout logging facility for mhv as an integration, specs to follow * remove unused redis_store change * only do it mhv_correlation_id exists * allow specs to pass but still need to add specs to test service * get test coverage to 100% * addressing comments from @ayal * oops * fixing linter issues
- Loading branch information
Showing
6 changed files
with
93 additions
and
1 deletion.
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
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,29 @@ | ||
# frozen_string_literal: true | ||
require 'mhv_logging/client' | ||
class MHVLoggingService | ||
def self.login(current_user) | ||
# If login has already been submitted, do nothing | ||
return false if current_user.mhv_correlation_id.nil? || current_user.mhv_last_signed_in | ||
# Otherwise send the login audit trail | ||
MHVLogging::Client.new(session: { user_id: current_user.mhv_correlation_id }) | ||
.authenticate | ||
.auditlogin | ||
# Update the user object with the time of login | ||
current_user.mhv_last_signed_in = Time.current | ||
current_user.save | ||
true | ||
end | ||
|
||
def self.logout(current_user) | ||
# If login has never been sent, no need to send logout | ||
return false unless current_user.mhv_correlation_id.nil? || current_user.mhv_last_signed_in | ||
# Otherwise send the logout audit trail | ||
MHVLogging::Client.new(session: { user_id: current_user.mhv_correlation_id }) | ||
.authenticate | ||
.auditlogout | ||
# Update the user object with nil to indicate not logged in | ||
current_user.mhv_last_signed_in = nil | ||
current_user.save | ||
true | ||
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
require 'rails_helper' | ||
require 'support/rx_client_helpers' | ||
|
||
RSpec.describe MHVLoggingService do | ||
subject(:login_service) { described_class.login(mhv_user) } | ||
subject(:logout_service) { described_class.logout(mhv_user) } | ||
|
||
let(:authenticated_client) do | ||
MHVLogging::Client.new(session: { user_id: mhv_user.mhv_correlation_id, | ||
expires_at: Time.current + 60 * 60, | ||
token: '<SESSION_TOKEN>' }) | ||
end | ||
|
||
before(:each) { allow(MHVLogging::Client).to receive(:new).and_return(authenticated_client) } | ||
|
||
context 'with current_user not having logged in to MHV' do | ||
let(:mhv_user) { build(:mhv_user, :mhv_not_logged_in) } | ||
|
||
it 'posts audit log when not logged in' do | ||
VCR.use_cassette('mhv_logging_client/audits/submits_an_audit_log_for_signing_in') do | ||
expect(mhv_user.mhv_last_signed_in).to be_nil | ||
expect(login_service).to eq(true) | ||
expect(mhv_user.mhv_last_signed_in).to be_a(Time) | ||
end | ||
end | ||
|
||
it 'does not logout when not logged in' do | ||
expect(mhv_user.mhv_last_signed_in).to be_nil | ||
expect(logout_service).to eq(false) | ||
expect(mhv_user.mhv_last_signed_in).to be_nil | ||
end | ||
end | ||
|
||
context 'with current_user having already logged in to MHV' do | ||
let(:mhv_user) { build(:mhv_user) } | ||
|
||
it 'posts audit log when not logged in' do | ||
expect(mhv_user.mhv_last_signed_in).to be_a(Time) | ||
expect(login_service).to eq(false) | ||
expect(mhv_user.mhv_last_signed_in).to be_a(Time) | ||
end | ||
|
||
it 'does not logout when not logged in' do | ||
VCR.use_cassette('mhv_logging_client/audits/submits_an_audit_log_for_signing_out') do | ||
expect(mhv_user.mhv_last_signed_in).to be_a(Time) | ||
expect(logout_service).to eq(true) | ||
expect(mhv_user.mhv_last_signed_in).to be_nil | ||
end | ||
end | ||
end | ||
end |