-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06465c2
commit 4545833
Showing
8 changed files
with
135 additions
and
29 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
# rubocop:disable Metrics | ||
|
||
class FetchGodaddyBsaBlockOrderListJob < ApplicationJob | ||
queue_as :default | ||
|
||
LIMIT = 20 | ||
LIMIT_MESSAGE = 'Limit reached. No more block orders to fetch' | ||
|
||
def perform | ||
fetch_block_order_list(offset: 0) | ||
end | ||
|
||
def fetch_block_order_list(offset:) | ||
res = Bsa::BlockOrderListService.call(offset: offset, limit: LIMIT) | ||
return res.error.inspect unless res.result? | ||
return LIMIT_MESSAGE if res.body.total.zero? || res.body.list.blank? | ||
|
||
bsa_attributes = collect_bsa_values(res) | ||
BsaProtectedDomain.upsert_all(bsa_attributes) | ||
offset_limit = res.body.total / LIMIT | ||
return LIMIT_MESSAGE if offset >= offset_limit | ||
|
||
offset += 1 | ||
fetch_block_order_list(offset: offset) | ||
end | ||
|
||
def collect_bsa_values(res) | ||
res.body.list.map do |block_order| | ||
{ | ||
order_id: block_order['blockOrder']['blockOrderId'], | ||
suborder_id: block_order['blockSubOrderId'], | ||
domain_name: "#{block_order['label']}#{block_order['tld']['displayName']}", | ||
state: block_order['blockOrderStatus']['blockOrderStatusId'], | ||
registration_code: SecureRandom.hex, | ||
create_date: DateTime.parse(block_order['createdDt']), | ||
created_at: Time.zone.now, | ||
updated_at: Time.zone.now | ||
} | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
class BsaProtectedDomain < ApplicationRecord | ||
# validate :suborder_id, uniqueness: true, presence: true | ||
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 |
---|---|---|
@@ -1,15 +1,15 @@ | ||
class CreateBsaProtectedDomains < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table :bsa_protected_domains do |t| | ||
t.integer :order_id, null: false | ||
t.integer :suborder_id, null: false | ||
t.string :order_id, null: false | ||
# t.string :suborder_id, null: false, index: { unique: true, name: 'unique_suborder_id' } | ||
t.string :suborder_id, null: false | ||
t.string :domain_name, null: false | ||
t.integer :state, null: false, default: 0 | ||
t.string :registration_code, null: false | ||
t.datetime :create_date | ||
t.datetime :update_date | ||
|
||
# t.timestamps | ||
t.timestamps | ||
end | ||
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
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
58 changes: 58 additions & 0 deletions
58
test/services/bsa/download_non_blocked_name_list_service_test.rb
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,58 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
RESPONSE = 'some,csv,data' | ||
|
||
class Bsa::BlockOrderViewServiceTest < ActiveSupport::TestCase | ||
setup do | ||
@filename = 'mock-csv' | ||
token = generate_test_bsa_token(Time.zone.now + 20.minute) | ||
stub_succesfull_request(token) | ||
end | ||
|
||
def teardown | ||
File.delete("#{@filename}.csv") if File.exist?("#{@filename}.csv") | ||
end | ||
|
||
def test_for_succesfull_downloaded_non_blocked_name | ||
stub_request(:get, 'https://api-ote.bsagateway.co/bsa/api/blockrsporder/1/nonblockednames') | ||
.to_return( | ||
status: 200, | ||
body: RESPONSE.to_json, | ||
headers: { 'Content-Type' => 'text/csv', | ||
'Content-Disposition' => 'attachment; filename="mock-csv.csv"' } | ||
) | ||
|
||
result = Bsa::DownloadNonBlockedNameListService.call(suborder_id: 1, filename: @filename) | ||
|
||
assert File.exist?("#{@filename}.csv") | ||
assert_equal RESPONSE, File.read("#{@filename}.csv").gsub('"', '') | ||
assert result.result? | ||
assert_equal "Data was added to #{@filename}.csv file", result.body.message | ||
end | ||
|
||
def test_for_failed_downloaded_non_blocked_name | ||
stub_request(:get, 'https://api-ote.bsagateway.co/bsa/api/blockrsporder/1/nonblockednames') | ||
.to_return( | ||
status: 404, | ||
headers: { 'Content-Type' => 'application/json' } | ||
) | ||
|
||
result = Bsa::DownloadNonBlockedNameListService.call(suborder_id: 1, filename: @filename) | ||
|
||
refute File.exist?("#{@filename}.csv") | ||
refute result.result? | ||
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 |