Skip to content

Commit

Permalink
added test for auth service
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegPhenomenon committed Oct 11, 2023
1 parent f3a0340 commit 30c0f60
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 6 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ group :test do
gem 'selenium-webdriver'
gem 'simplecov', '0.17.1', require: false # CC last supported v0.17
gem 'spy'
gem 'mock_redis'
gem 'webmock'
end

Expand Down
4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ GEM
mini_portile2 (2.8.2)
minitest (5.18.1)
minitest-stub_any_instance (1.0.3)
mock_redis (0.37.0)
monetize (1.9.4)
money (~> 6.12)
money (6.13.8)
Expand Down Expand Up @@ -569,6 +570,7 @@ DEPENDENCIES
mimemagic (= 0.4.3)
minitest (~> 5.17)
minitest-stub_any_instance
mock_redis
money-rails
newrelic-infinite_tracing
newrelic_rpm
Expand Down Expand Up @@ -604,4 +606,4 @@ DEPENDENCIES
wkhtmltopdf-binary (~> 0.12.6.1)

BUNDLED WITH
2.4.19
2.4.20
2 changes: 1 addition & 1 deletion app/services/bsa/application_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def expire_token_at(token)
end

def redis
@redis ||= Redis.new(host: redist_host, port: redis_port, db: redis_db)
@redis ||= Rails.env.test? ? $mock_redis : Redis.new(host: redist_host, port: redis_port, db: redis_db)
end
end
end
19 changes: 15 additions & 4 deletions app/services/bsa/auth_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@ module Bsa
class AuthService
include Bsa::ApplicationService

def self.call
new.call
attr_reader :redis_required

def self.call(redis_required: true)
new(redis_required: redis_required).call
end

def initialize(redis_required:)
@redis_required = redis_required
end

def call
check_for_expired_token ? build_struct(check_for_expired_token) : request_token
if redis_required
check_for_expired_token ? build_struct(check_for_expired_token) : request_token
else
request_token
end
end

private
Expand All @@ -24,7 +34,8 @@ def request_token

return res unless res.result?

redis.set('bsa_token', res.body.id_token)
redis.set('bsa_token', res.body.id_token) if @redis_required

res
end

Expand Down
96 changes: 96 additions & 0 deletions test/services/bsa/auth_service_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
require 'test_helper'

class Bsa::AuthServiceTest < ActiveSupport::TestCase

def test_for_succesfull_authentication
service_instance = Bsa::AuthService.new(redis_required: false)
service_instance.redis.del('bsa_token')

token = generate_test_bsa_token(Time.zone.now + 10.minute)
stub_succesfull_request(token)

r = service_instance.call

assert r.result?
assert_equal r.body.id_token, token

stored_token = service_instance.redis.get('bsa_token')
assert_nil stored_token
end

def test_for_failed_authentication
stub_failed_request

r = Bsa::AuthService.call(redis_required: false)

refute r.result?
assert_equal r.error.message, 'NOT_FOUND'
assert_equal r.error.description, 'User not found'
end

def test_token_should_be_put_in_redis_if_redis_available
service_instance = Bsa::AuthService.new(redis_required: true)
service_instance.redis.del('bsa_token')

token = generate_test_bsa_token(Time.zone.now + 10.minute)
stub_succesfull_request(token)

r = service_instance.call

assert r.result?
assert_equal r.body.id_token, token


stored_token = service_instance.redis.get('bsa_token')

assert_equal token, stored_token
end

def test_token_shoule_be_get_from_redis_if_redis_available
# To verify that the data is coming from redis, I will not mock a web query
token = generate_test_bsa_token(Time.zone.now + 10.minute)

service_instance = Bsa::AuthService.new(redis_required: true)
service_instance.redis.set('bsa_token', token)

r = service_instance.call

assert r.result?
assert_equal r.body.id_token, token
end

def test_token_should_be_required_if_token_is_expired
token = generate_test_bsa_token(Time.zone.now - 20.minute)

service_instance = Bsa::AuthService.new(redis_required: true)
service_instance.redis.set('bsa_token', token)

token = generate_test_bsa_token(Time.zone.now + 20.minute)
stub_succesfull_request(token)

r = service_instance.call

assert r.result?
assert_equal r.body.id_token, token
end

private

def stub_succesfull_request(token)
stub_request(:post, "https://api-ote.bsagateway.co/iam/api/authenticate/apiKey")
.to_return(
status: 200,
body: { id_token: token }.to_json,
headers: { 'Content-Type' => 'application/json' }
)
end

def stub_failed_request
stub_request(:post, "https://api-ote.bsagateway.co/iam/api/authenticate/apiKey")
.to_return(
status: 401,
body: { message: 'NOT_FOUND', description: "User not found" }.to_json,
headers: { 'Content-Type' => 'application/json' }
)
end
end
20 changes: 20 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

ActiveJob::Base.queue_adapter = :test

$mock_redis = MockRedis.new

class CompanyRegisterClientStub
Company = Struct.new(:registration_number, :company_name)

Expand All @@ -40,6 +42,24 @@ def representation_rights(citizen_personal_code:, citizen_country_code:)
end
end

def generate_test_bsa_token(expiry_time)
header = { typ: 'JWT', alg: 'none' }
payload = { exp: expiry_time.to_i }

header_enc = Base64.urlsafe_encode64(header.to_json)

sio = StringIO.new
gz = Zlib::GzipWriter.new(sio)
gz.write payload.to_json
gz.close
payload_gzip = sio.string
sio.close

payload_enc = Base64.urlsafe_encode64(payload_gzip)

"#{header_enc}.#{payload_enc}."
end

CompanyRegister::Client = CompanyRegisterClientStub

EInvoice.provider = EInvoice::Providers::TestProvider.new
Expand Down

0 comments on commit 30c0f60

Please sign in to comment.