-
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.
added name checker and availability control for domain names
- Loading branch information
1 parent
a971fdb
commit d286fbd
Showing
6 changed files
with
102 additions
and
9 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
8 changes: 8 additions & 0 deletions
8
app/services/business_registry/domain_availability_checker.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,8 @@ | ||
module BusinessRegistry | ||
class DomainAvailabilityChecker | ||
def self.filter_available(domains) | ||
reserved_domains = ReservedDomain.where(name: domains).pluck(:name) | ||
domains - reserved_domains | ||
end | ||
end | ||
end |
35 changes: 35 additions & 0 deletions
35
app/services/business_registry/domain_name_generator_service.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,35 @@ | ||
module BusinessRegistry | ||
class DomainNameGeneratorService | ||
LEGAL_FORMS = %w[AS OU FIE OÜ].freeze | ||
|
||
def self.generate(name) | ||
base_name = remove_legal_forms(sanitize_input(name)) | ||
variants = generate_variants(base_name) | ||
variants + generate_additional_variants(variants) | ||
end | ||
|
||
private | ||
|
||
def self.sanitize_input(name) | ||
name.gsub(/[^[:alnum:]\s\-]/, '').strip | ||
end | ||
|
||
def self.remove_legal_forms(name) | ||
words = name.split | ||
words.reject { |word| LEGAL_FORMS.include?(word.upcase) }.join(' ').strip | ||
end | ||
|
||
def self.generate_variants(name) | ||
[ | ||
name.downcase.gsub(/\s+/, ''), | ||
name.downcase.gsub(/\s+/, '-'), | ||
name.downcase.gsub(/\s+/, '_') | ||
] | ||
end | ||
|
||
def self.generate_additional_variants(variants) | ||
current_year = Time.current.year | ||
variants.map { |v| "#{v}#{current_year}" } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
one: | ||
name: reserved.test | ||
password: reserved-001 | ||
|
||
two: | ||
name: company-name.test | ||
password: company-002 |
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,17 +1,50 @@ | ||
require 'test_helper' | ||
|
||
class CheckTest < ApplicationIntegrationTest | ||
fixtures :reserved_domains | ||
|
||
def setup | ||
super | ||
ENV['ALLOWED_ORIGINS'] = 'http://example.com,http://test.com' | ||
end | ||
|
||
def test_return_list_of_available_organization_domain_names | ||
get '/api/v1/business_registry/check?organization_name=Company Name AS', headers: { 'Origin' => 'http://example.com' } | ||
json = JSON.parse(response.body, symbolize_names: true) | ||
|
||
assert_response :success | ||
assert_equal 'http://example.com', response.headers['Access-Control-Allow-Origin'] | ||
refute_includes json[:variants], 'company-name.test' | ||
assert_includes json[:variants], 'companyname' | ||
assert_includes json[:variants], 'company-name' | ||
assert_includes json[:variants], 'company_name' | ||
assert_includes json[:variants], "companyname#{Time.current.year}" | ||
refute_includes json[:variants].join, 'as' | ||
end | ||
|
||
def test_single_word_company_name | ||
get '/api/v1/business_registry/check?organization_name=Reserved', headers: { 'Origin' => 'http://test.com' } | ||
json = JSON.parse(response.body, symbolize_names: true) | ||
|
||
# @contact = contacts(:john) | ||
assert_response :success | ||
assert_equal 'http://test.com', response.headers['Access-Control-Allow-Origin'] | ||
assert_includes json[:variants], 'reserved' | ||
refute_includes json[:variants], 'reserved.test' | ||
assert_includes json[:variants], "reserved#{Time.current.year}" | ||
end | ||
|
||
def test_return_code_that_all_ok | ||
get '/api/v1/business_registry/check/common.ee' | ||
def test_invalid_organization_name | ||
get '/api/v1/business_registry/check?organization_name=Invalid!@#Name', headers: { 'Origin' => 'http://example.com' } | ||
json = JSON.parse(response.body, symbolize_names: true) | ||
|
||
puts(json) | ||
# assert_equal json[:errors], 'Contact not found' | ||
assert_response :bad_request | ||
assert_equal 'Invalid organization name', json[:error] | ||
end | ||
|
||
def test_cors_with_disallowed_origin | ||
get '/api/v1/business_registry/check?organization_name=Test', headers: { 'Origin' => 'http://malicious.com' } | ||
|
||
assert_response :success | ||
assert_nil response.headers['Access-Control-Allow-Origin'] | ||
end | ||
end |