Skip to content

Commit

Permalink
added tests for base service objects
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegPhenomenon committed Oct 12, 2023
1 parent 30c0f60 commit 06465c2
Show file tree
Hide file tree
Showing 13 changed files with 423 additions and 24 deletions.
13 changes: 7 additions & 6 deletions app/services/bsa/application_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module ApplicationService
include Core::Settings

OK = '200'
ACCEPTED = '202'

def connect(url:)
uri = URI(url)
Expand All @@ -17,19 +18,19 @@ def connect(url:)

def headers(content_type: 'x-www-form-urlencoded')
{
'Content-Type' => "application/#{content_type}",
'Content-Type' => "application/#{content_type}"
}
end

def token_format(token)
{
'Authorization' => "Bearer #{token}",
'Authorization' => "Bearer #{token}"
}
end

def struct_response(response)
parsed_data = parse_response(response)
result = response.code == OK
result = [OK, ACCEPTED].include? response.code

return Struct.new(:result?, :body).new(result, OpenStruct.new(parsed_data)) if result

Expand All @@ -41,16 +42,16 @@ def parse_response(response)
end

def expire_token_at(token)
header_enc, payload_enc, signature = token.split('.')
_header_enc, payload_enc, _signature = token.split('.')
payload_dec = Base64.urlsafe_decode64(payload_enc)
payload_json = Zlib::GzipReader.new(StringIO.new(payload_dec)).read
payload = JSON.parse(payload_json)

Time.at(payload['exp']).utc.in_time_zone
end

def redis
@redis ||= Rails.env.test? ? $mock_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
4 changes: 2 additions & 2 deletions app/services/bsa/auth_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ def request_token
res = struct_response(response)

return res unless res.result?

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

res
end

def check_for_expired_token
retrieved_token = redis.get("bsa_token")
retrieved_token = redis.get('bsa_token')
return if retrieved_token.nil?

time = expire_token_at(retrieved_token)
Expand Down
2 changes: 1 addition & 1 deletion app/services/bsa/block_order_list_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def query_string
'offset' => offset,
'limit' => limit
}.merge(q).compact

URI.encode_www_form(params)
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/bsa/block_order_view_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def query_string
'offset' => offset,
'limit' => limit
}.compact

URI.encode_www_form(params)
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/services/bsa/core/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ module Settings
def bsa
'BSA'
end

def api_key
ENV['bsa_api_key']
end

def base_url
ENV['bsa_base_url']
end
Expand Down
10 changes: 4 additions & 6 deletions app/services/bsa/core/token_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ module Core
module TokenHelper
def token
response = Bsa::AuthService.call

if response.result?
response.body.id_token
else
raise Bsa::AuthError, "#{response.error.message}: #{response.error.description}"
end

raise Bsa::AuthError, "#{response.error.message}: #{response.error.description}" unless response.result?

response.body.id_token
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions app/services/bsa/download_non_blocked_name_list_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def call
file.write(response.body)
end

puts '----'
puts response.inspect
puts '-------'

# TODO: finish with response
# struct_response(response)
end
Expand Down
4 changes: 2 additions & 2 deletions app/services/bsa/non_blocked_name_action_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def endpoint
def query_string
params = {
'action' => action,
'suborderid' => suborder_id,
'suborderid' => suborder_id
}.compact

URI.encode_www_form(params)
end
end
Expand Down
8 changes: 4 additions & 4 deletions app/services/bsa/upload_unavailable_domain_report_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def call
request['Content-Type'] = 'multipart/form-data'

form_data = [
['Zone', { "checkSum" => checksum }.to_json ],
['Zone', { 'checkSum' => checksum }.to_json],
['File', File.open(@file_path)]
]

Expand All @@ -38,17 +38,17 @@ def call
private

def endpoint
"/bsa/api/zonefile"
'/bsa/api/zonefile'
end

def form_data_headers
{
'Content-Type' => 'multipart/form-data',
'Content-Type' => 'multipart/form-data'
}
end

def calculate_checksum(file_path)
digest = OpenSSL::Digest::SHA256.new
digest = OpenSSL::Digest.new('SHA256')
File.open(file_path, 'rb') do |f|
while chunk = f.read(4096)
digest.update(chunk)
Expand Down
119 changes: 119 additions & 0 deletions test/services/bsa/block_order_list_service_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
require 'test_helper'

RESPONSE = {
"list": [
{
"blockSubOrderId": 690_680_666_563_633,
"rspAccountId": 1005,
"blockOrder": {
"blockOrderId": 3_363_656_660_861
},
"blockOrderStatus": {
"blockOrderStatusId": 2,
"name": 'QueuedForActivation',
"displayName": 'Queued for Activation',
"description": 'Queued for Activation'
},
"productType": 'Standard',
"tld": {
"tldId": 22,
"name": 'TEST-ONE',
"displayName": '.test-one'
},
"label": 'testandvalidate',
"createdDt": '2023-04-05T03:49:19.000+0000'
},
{
"blockSubOrderId": 790_681_879_713_707,
"rspAccountId": 1005,
"blockOrder": {
"blockOrderId": 7_073_179_781_861
},
"blockOrderStatus": {
"blockOrderStatusId": 2,
"name": 'QueuedForActivation',
"displayName": 'Queued for Activation',
"description": 'Queued for Activation'
},
"productType": 'Plus',
"tld": {
"tldId": 21,
"name": 'TEST-TWO',
"displayName": '.test-two'
},
"label": ' testandvalidate',
"createdDt": '2023-04-19T04:48:29.000+0000'
}
],
"offset": 0,
"limit": 100,
"count": 2,
"total": 2,
"sortBy": [
'createdDt'
],
"order": 'asc'
}

INVALID_RESPONSE = {
"message": 'Unsupported Media Type',
"description": 'The server is refusing to service the request because the entity of the request is in a format' \
' not supported by the requested resource for the requested method'
}

class Bsa::BlockOrderListServiceTest < ActiveSupport::TestCase
setup do
token = generate_test_bsa_token(Time.zone.now + 20.minute)
stub_succesfull_request(token)
end

def test_for_succesfull_block_order_list
stub_request(:get, 'https://api-ote.bsagateway.co/bsa/api/blockrsporder')
.to_return(
status: 200,
body: RESPONSE.to_json,
headers: { 'Content-Type' => 'application/json' }
)

r = Bsa::BlockOrderListService.call

assert r.result?
assert_equal r.body.list.count, 2
end

def test_for_failed_block_order_list
stub_request(:get, 'https://api-ote.bsagateway.co/bsa/api/blockrsporder')
.to_return(
status: 415,
body: INVALID_RESPONSE.to_json,
headers: { 'Content-Type' => 'application/json' }
)

r = Bsa::BlockOrderListService.call

refute r.result?
assert_equal r.error.message, 'Unsupported Media Type'
assert_equal r.error.description, 'The server is refusing to service the request because the entity of the' \
' request is in a format not supported by the requested resource for the' \
' requested method'
end

def test_parse_query_parameters
instance_serive = Bsa::BlockOrderListService.new(sort_by: 'createdAt', order: 'desc', offset: 0, limit: 100, q: { 'tld' => 'test' })

result = instance_serive.send(:query_string)

assert_equal result, 'sortBy=createdAt&order=desc&offset=0&limit=100&tld=test'
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
end
67 changes: 67 additions & 0 deletions test/services/bsa/block_order_status_setting_service_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require 'test_helper'

BODY = [
{ blockSubOrderId: 1, status: 'ActivationInProgress' },
{ blockSubOrderId: 2, status: 'ActivationInProgress' },
{ blockSubOrderId: 4, status: 'Active' },
{ blockSubOrderId: 5, status: 'ReleaseInProgress' },
{ blockSubOrderId: 6, status: 'Closed' }
]

RESPONSE = {
message: 'ok'
}

INVALID_RESPONSE = {
"message": 'Unsupported Media Type',
"description": 'The server is refusing to service the request because the entity of the request is in a format' \
' not supported by the requested resource for the requested method'
}

class Bsa::BlockOrderListServiceTest < ActiveSupport::TestCase
setup do
token = generate_test_bsa_token(Time.zone.now + 20.minute)
stub_succesfull_request(token)
end

def test_for_succesfull_update_statuses
stub_request(:post, 'https://api-ote.bsagateway.co/bsa/api/blockrsporder/status')
.to_return(
status: 200,
body: RESPONSE.to_json,
headers: { 'Content-Type' => 'application/json' }
)

r = Bsa::BlockOrderStatusSettingService.call(payload: BODY)

assert r.result?
end

def test_for_failed_update_statuses
stub_request(:post, 'https://api-ote.bsagateway.co/bsa/api/blockrsporder/status')
.to_return(
status: 415,
body: INVALID_RESPONSE.to_json,
headers: { 'Content-Type' => 'application/json' }
)

r = Bsa::BlockOrderStatusSettingService.call(payload: BODY)

refute r.result?
assert_equal r.error.message, 'Unsupported Media Type'
assert_equal r.error.description, 'The server is refusing to service the request because the entity of the' \
' request is in a format not supported by the requested resource for the' \
' requested method'
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
end
Loading

0 comments on commit 06465c2

Please sign in to comment.