Skip to content

Commit

Permalink
added tests for jobs, refactored jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegPhenomenon committed Oct 19, 2023
1 parent ca9b37c commit f57dc1d
Show file tree
Hide file tree
Showing 7 changed files with 224 additions and 8 deletions.
4 changes: 2 additions & 2 deletions app/jobs/fetch_godaddy_bsa_block_order_list_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ class FetchGodaddyBsaBlockOrderListJob < ApplicationJob
LIMIT_MESSAGE = 'Limit reached. No more block orders to fetch'
QUEUED_FOR_ACTIVATION = 'QueuedForActivation'

def perform(status_name=QUEUED_FOR_ACTIVATION)
def perform(status_name = QUEUED_FOR_ACTIVATION)
fetch_block_order_list(offset: 0, status_name: status_name)
end

def fetch_block_order_list(offset:, status_name:)
res = Bsa::BlockOrderListService.call(offset: offset, limit: LIMIT,
q: { 'blockOrderStatus.name' => status_name })
return res.error.inspect unless res.result?
return { message: res.error.message, description: res.error.description } unless res.result?
return LIMIT_MESSAGE if res.body.total.zero? || res.body.list.blank?

bsa_attributes = collect_bsa_values(res)
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/update_godaddy_domains_status_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def process(suborders_block:, state:)
payload = serialize_suborders_block(suborders_block: suborders_block, state: state)

result = Bsa::BlockOrderStatusSettingService.call(payload: payload)
return result.error.inspect unless result.result?
return { message: result.error.message, description: result.error.description } unless result.result?

refresh_statuses(suborders_block: suborders_block, state: state)

Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/bsa_protected_domains.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
one:
order_id: 1
suborder_id: 1
domain_name: example1.ee
state: 2
registration_code: 1234567890
create_date: <%= Time.zone.parse('2010-07-05').to_s(:db) %>
created_at: <%= Time.zone.parse('2010-07-05').to_s(:db) %>
updated_at: <%= Time.zone.parse('2010-07-05').to_s(:db) %>
two:
order_id: 2
suborder_id: 2
domain_name: example2.ee
state: 2
registration_code: 0987654321
create_date: <%= Time.zone.parse('2010-07-05').to_s(:db) %>
created_at: <%= Time.zone.parse('2010-07-05').to_s(:db) %>
updated_at: <%= Time.zone.parse('2010-07-05').to_s(:db) %>
134 changes: 134 additions & 0 deletions test/jobs/fetch_godaddy_bsa_block_order_list_job_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
require 'test_helper'

RESPONSE_BLOCK_VIEW_SERVICE_FOR_JOB = {
"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'
}.freeze

EMPTY_RESPONSE_BLOCK_VIEW_SERVICE_FOR_JOB = {
"list": [],
"offset": 0,
"limit": 100,
"count": 0,
"total": 0,
"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'
}.freeze

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

def test_succesfull_fetch_block_order_list_and_added_to_db
stub_request(:get, 'https://api-ote.bsagateway.co/bsa/api/blockrsporder?limit=20&offset=0&q=blockOrderStatus.name=QueuedForActivation')
.to_return(
status: 200,
body: RESPONSE_BLOCK_VIEW_SERVICE_FOR_JOB.to_json,
headers: { 'Content-Type' => 'application/json' }
)

assert_difference 'BsaProtectedDomain.count', 2 do
FetchGodaddyBsaBlockOrderListJob.perform_now
end
end

def test_should_show_message_if_list_is_empty
stub_request(:get, 'https://api-ote.bsagateway.co/bsa/api/blockrsporder?limit=20&offset=0&q=blockOrderStatus.name=QueuedForActivation')
.to_return(
status: 200,
body: EMPTY_RESPONSE_BLOCK_VIEW_SERVICE_FOR_JOB.to_json,
headers: { 'Content-Type' => 'application/json' }
)

result = FetchGodaddyBsaBlockOrderListJob.perform_now

assert_equal result, 'Limit reached. No more block orders to fetch'
end

def test_should_show_message_if_error_occur
stub_request(:get, 'https://api-ote.bsagateway.co/bsa/api/blockrsporder?limit=20&offset=0&q=blockOrderStatus.name=QueuedForActivation')
.to_return(
status: 415,
body: INVALID_RESPONSE.to_json,
headers: { 'Content-Type' => 'application/json' }
)

result = FetchGodaddyBsaBlockOrderListJob.perform_now

assert_equal result[:message], 'Unsupported Media Type'
assert_equal result[: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_auth_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
63 changes: 63 additions & 0 deletions test/jobs/update_godaddy_domains_status_job_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'test_helper'

class UpdateGodaddyDomainsStatusJobTest < ActiveSupport::TestCase
BODY_ORDER_STATUS_SETTING_JOB = [
{ blockSubOrderId: 1, status: 'ActivationInProgress' },
{ blockSubOrderId: 2, status: 'ActivationInProgress' },
]

RESPONSE_ORDER_STATUS_SETTING_JOB = {
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'
}

setup do
token = generate_test_bsa_token(Time.zone.now + 20.minute)
stub_succesfull_auth_request(token)
end

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

assert BsaProtectedDomain.all.all? { |domain| domain.state == 'QueuedForActivation' }

UpdateGodaddyDomainsStatusJob.perform_now('QueuedForActivation', 'ActivationInProgress')

assert BsaProtectedDomain.all.all? { |domain| domain.state == 'ActivationInProgress' }
end

def test_should_return_reason_of_error_and_not_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' }
)

assert BsaProtectedDomain.all.all? { |domain| domain.state == 'QueuedForActivation' }
result = UpdateGodaddyDomainsStatusJob.perform_now('QueuedForActivation', 'ActivationInProgress')

assert BsaProtectedDomain.all.all? { |domain| domain.state == 'QueuedForActivation' }
end

private

def stub_succesfull_auth_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
1 change: 1 addition & 0 deletions test/models/whois_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def test_generates_json_with_registrant
disclosed_attributes: %w[name])

domain = domains(:shop)
domain.reload
domain.update!(registrant: contact.becomes(Registrant))

whois_record = whois_records(:shop)
Expand Down
10 changes: 5 additions & 5 deletions test/services/bsa/block_order_status_setting_service_test.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
require 'test_helper'

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

RESPONSE = {
RESPONSE_ORDER_STATUS_SETTING = {
message: 'ok'
}

Expand All @@ -28,11 +28,11 @@ 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,
body: RESPONSE_ORDER_STATUS_SETTING.to_json,
headers: { 'Content-Type' => 'application/json' }
)

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

assert r.result?
end
Expand All @@ -45,7 +45,7 @@ def test_for_failed_update_statuses
headers: { 'Content-Type' => 'application/json' }
)

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

refute r.result?
assert_equal r.error.message, 'Unsupported Media Type'
Expand Down

0 comments on commit f57dc1d

Please sign in to comment.