Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LG-15273 Create a Redis Set to track Socure users #11773

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
35 changes: 35 additions & 0 deletions app/services/idv/socure_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module Idv
class SocureUser
attr_reader :redis_pool

def initialize(redis_pool: REDIS_POOL)
@redis_pool = redis_pool
end

def add_user!(user_uuid:)
return if maxed_users?

redis_pool.with do |client|
client.sadd(key, user_uuid)
end
end

def count
redis_pool.with do |client|
client.scard(key)
end
end

private

def maxed_users?
count >= IdentityConfig.store.doc_auth_socure_max_allowed_users
end

def key
'idv:socure:users'
end
end
end
58 changes: 58 additions & 0 deletions spec/services/idv/socure_user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require 'rails_helper'

RSpec.describe Idv::SocureUser do
let(:socure_user_set) { Idv::SocureUser.new }
let(:dummy_uuid_1) { 'ABC0001' }
let(:dummy_uuid_2) { 'ABC0002' }
let(:dummy_uuid_3) { 'ABC0003' }

around do |ex|
REDIS_POOL.with { |client| client.flushdb }
ex.run
REDIS_POOL.with { |client| client.flushdb }
end

describe '#add_user!' do
before do
allow(IdentityConfig.store).to receive(:doc_auth_socure_max_allowed_users).and_return(2)
end

it 'correctly adds user and tracks count' do
socure_user_set.add_user!(user_uuid: dummy_uuid_1)
expect(socure_user_set.count).to eq(1)
end

it 'does not add duplicates' do
socure_user_set.add_user!(user_uuid: dummy_uuid_1)
expect(socure_user_set.count).to eq(1)
socure_user_set.add_user!(user_uuid: dummy_uuid_1)
expect(socure_user_set.count).to eq(1)
end

it 'does not allow more than doc_auth_socure_max_allowed_users to be added to set' do
socure_user_set.add_user!(user_uuid: dummy_uuid_1)
expect(socure_user_set.count).to eq(1)
socure_user_set.add_user!(user_uuid: dummy_uuid_2)
expect(socure_user_set.count).to eq(2)
socure_user_set.add_user!(user_uuid: dummy_uuid_3)
expect(socure_user_set.count).to eq(2)
end
end

describe '#count' do
before do
allow(IdentityConfig.store).to receive(:doc_auth_socure_max_allowed_users).and_return(10)
end
it 'count is zero when there are no users in the redis store' do
expect(socure_user_set.count).to eq(0)
end

it 'gives the user count' do
10.times.each do |index|
socure_user_set.add_user!(user_uuid: "ABC000#{index}")
end

expect(socure_user_set.count).to eq(10)
end
end
end