Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
olegphenomenon committed Sep 22, 2021
1 parent 81b476f commit 4d64ee2
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/config/application.yml
/config/environments/development.rb
/config/deploy.rb
/.idea

# Do not commit one. Instead, download the latest from https://github.com/internetee/style-guide.
.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class RegistryLocksController < ::Api::V1::Registrant::BaseController
before_action :authorized_to_manage_locks?

def create
extensionsProhibited = params[:extensionsProhibited]
extensions_prohibited = params[:extensionsProhibited]

if @domain.apply_registry_lock(extensionsProhibited: extensionsProhibited.to_s.downcase == "true")
if @domain.apply_registry_lock(extensions_prohibited: extensions_prohibited.to_s.downcase == 'true')
render json: serialized_domain(@domain)
else
render json: { errors: [{ base: ['Domain cannot be locked'] }] },
Expand Down
42 changes: 24 additions & 18 deletions app/models/concerns/domain/registry_lockable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,25 @@ module Domain::RegistryLockable
end

EXTENSIONS_STATUS = [DomainStatus::SERVER_EXTENSION_UPDATE_PROHIBITED].freeze


def apply_registry_lock(extensionsProhibited:)
def apply_registry_lock(extensions_prohibited:)
return unless registry_lockable?
return if locked_by_registrant?

transaction do
self.statuses |= LOCK_STATUSES
self.statuses |= EXTENSIONS_STATUS if Feature.obj_and_extensions_statuses_enabled? and extensionsProhibited
self.locked_by_registrant_at = Time.zone.now
alert_registrar_lock_changes!(lock: true)

save!
apply_statuses_locked_statuses(extensions_prohibited: extensions_prohibited)
end
end

def apply_statuses_locked_statuses(extensions_prohibited:)
self.statuses |= LOCK_STATUSES
self.statuses |= EXTENSIONS_STATUS if Feature.obj_and_extensions_statuses_enabled? && extensions_prohibited
self.locked_by_registrant_at = Time.zone.now
alert_registrar_lock_changes!(lock: true)

save!
end

def registry_lockable?
(statuses & [DomainStatus::PENDING_DELETE_CONFIRMATION,
DomainStatus::PENDING_CREATE, DomainStatus::PENDING_UPDATE,
Expand All @@ -45,18 +48,21 @@ def remove_registry_lock
return unless locked_by_registrant?

transaction do
LOCK_STATUSES.each do |domain_status|
statuses.delete([domain_status])
end

statuses.delete([EXTENSIONS_STATUS]) if statuses.include? EXTENSIONS_STATUS

self.locked_by_registrant_at = nil
self.statuses = admin_store_statuses_history || []
alert_registrar_lock_changes!(lock: false)
remove_statuses_from_locked_domain
end
end

save!
def remove_statuses_from_locked_domain
LOCK_STATUSES.each do |domain_status|
statuses.delete([domain_status])
end

statuses.delete([EXTENSIONS_STATUS]) if statuses.include? EXTENSIONS_STATUS
self.locked_by_registrant_at = nil
self.statuses = admin_store_statuses_history || []
alert_registrar_lock_changes!(lock: false)

save!
end

def alert_registrar_lock_changes!(lock: true)
Expand Down
2 changes: 1 addition & 1 deletion app/models/registrant_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def companies(company_register = CompanyRegister::Client.new)
company_register.representation_rights(citizen_personal_code: ident,
citizen_country_code: country.alpha3)
end

def contacts(representable: true)
Contact.registrant_user_contacts(self, representable: representable)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_cannot_lock_a_domain_in_pending_state
end

def test_cannot_lock_an_already_locked_domain
@domain.apply_registry_lock(extensionsProhibited: false)
@domain.apply_registry_lock(extensions_prohibited: false)
assert(@domain.locked_by_registrant?)

post '/api/v1/registrant/domains/2df2c1a1-8f6a-490a-81be-8bdf29866880/registry_lock',
Expand All @@ -59,7 +59,7 @@ def test_cannot_lock_an_already_locked_domain
end

def test_can_unlock_a_locked_domain
@domain.apply_registry_lock(extensionsProhibited: false)
@domain.apply_registry_lock(extensions_prohibited: false)

delete '/api/v1/registrant/domains/2df2c1a1-8f6a-490a-81be-8bdf29866880/registry_lock',
headers: @auth_headers
Expand Down
4 changes: 2 additions & 2 deletions test/jobs/domain_update_confirm_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ def teardown

def test_registrant_locked_domain
refute @domain.locked_by_registrant?
@domain.apply_registry_lock(extensionsProhibited: false)
@domain.apply_registry_lock(extensions_prohibited: false)
assert @domain.locked_by_registrant?
assert_equal(@domain.registrar.notifications.last.text, "Domain #{@domain.name} has been locked by registrant")
end

def test_registrant_unlocked_domain
refute @domain.locked_by_registrant?
@domain.apply_registry_lock(extensionsProhibited: false)
@domain.apply_registry_lock(extensions_prohibited: false)
assert @domain.locked_by_registrant?
@domain.remove_registry_lock
refute @domain.locked_by_registrant?
Expand Down
2 changes: 1 addition & 1 deletion test/lib/serializers/registrant_api/domain_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_returns_domain_locked_by_registrant_time_or_nil
assert_not(@json[:locked_by_registrant_at])

travel_to Time.zone.parse('2010-07-05 10:30')
@domain.apply_registry_lock(extensionsProhibited: false)
@domain.apply_registry_lock(extensions_prohibited: false)
serializer_for_locked_domain = Serializers::RegistrantApi::Domain.new(@domain.reload)
new_json = serializer_for_locked_domain.to_json

Expand Down
2 changes: 1 addition & 1 deletion test/models/domain/domain_version_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_assigns_updator_to_paper_trail_whodunnit
PaperTrail.request.whodunnit = @user.id_role_username

assert_difference '@domain.versions.count', 1 do
@domain.apply_registry_lock(extensionsProhibited: false)
@domain.apply_registry_lock(extensions_prohibited: false)
end

assert_equal(@domain.updator, @user)
Expand Down
22 changes: 11 additions & 11 deletions test/models/domain/registry_lockable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ def test_user_can_set_lock_for_domain_if_it_has_any_prohibited_status
refute(@domain.locked_by_registrant?)
@domain.update(statuses: [DomainStatus::SERVER_TRANSFER_PROHIBITED])

@domain.apply_registry_lock(extensionsProhibited: false) # Raise validation error
@domain.apply_registry_lock(extensions_prohibited: false) # Raise validation error

check_statuses_lockable_domain
assert(@domain.locked_by_registrant?)
end

def test_remove_lockalable_statuses_after_admin_intervention
@domain.apply_registry_lock(extensionsProhibited: false)
@domain.apply_registry_lock(extensions_prohibited: false)
assert @domain.locked_by_registrant?
assert_equal @domain.statuses.sort, Domain::RegistryLockable::LOCK_STATUSES.sort

deleted_status = @domain.statuses - [DomainStatus::SERVER_DELETE_PROHIBITED]
@domain.update(statuses: deleted_status)
assert_not @domain.locked_by_registrant?

@domain.apply_registry_lock(extensionsProhibited: false)
@domain.apply_registry_lock(extensions_prohibited: false)
assert @domain.locked_by_registrant?
@domain.remove_registry_lock

Expand All @@ -41,7 +41,7 @@ def test_restore_domain_statuses_after_unlock
@domain.save
assert @domain.admin_store_statuses_history.include? DomainStatus::SERVER_UPDATE_PROHIBITED

@domain.apply_registry_lock(extensionsProhibited: false)
@domain.apply_registry_lock(extensions_prohibited: false)
assert @domain.locked_by_registrant?
assert_equal @domain.statuses.sort, Domain::RegistryLockable::LOCK_STATUSES.sort

Expand All @@ -52,7 +52,7 @@ def test_restore_domain_statuses_after_unlock
end

def test_add_additinal_status_for_locked_domain
@domain.apply_registry_lock(extensionsProhibited: false)
@domain.apply_registry_lock(extensions_prohibited: false)
assert @domain.locked_by_registrant?
assert_equal @domain.statuses.sort, Domain::RegistryLockable::LOCK_STATUSES.sort

Expand All @@ -69,7 +69,7 @@ def test_add_additinal_status_for_locked_domain

def test_lockable_domain_if_remove_some_prohibited_status
refute(@domain.locked_by_registrant?)
@domain.apply_registry_lock(extensionsProhibited: false)
@domain.apply_registry_lock(extensions_prohibited: false)
check_statuses_lockable_domain
assert(@domain.locked_by_registrant?)

Expand All @@ -85,7 +85,7 @@ def test_lockable_domain_if_remove_some_prohibited_status

def test_registry_lock_on_lockable_domain
refute(@domain.locked_by_registrant?)
@domain.apply_registry_lock(extensionsProhibited: false)
@domain.apply_registry_lock(extensions_prohibited: false)

assert_equal(
[DomainStatus::SERVER_UPDATE_PROHIBITED,
Expand All @@ -99,21 +99,21 @@ def test_registry_lock_on_lockable_domain
end

def test_registry_lock_cannot_be_applied_twice
@domain.apply_registry_lock(extensionsProhibited: false)
refute(@domain.apply_registry_lock(extensionsProhibited: false))
@domain.apply_registry_lock(extensions_prohibited: false)
refute(@domain.apply_registry_lock(extensions_prohibited: false))
assert(@domain.locked_by_registrant?)
assert(@domain.locked_by_registrant_at)
end

def test_registry_lock_cannot_be_applied_on_pending_statuses
@domain.statuses << DomainStatus::PENDING_RENEW
refute(@domain.apply_registry_lock(extensionsProhibited: false))
refute(@domain.apply_registry_lock(extensions_prohibited: false))
refute(@domain.locked_by_registrant?)
refute(@domain.locked_by_registrant_at)
end

def test_remove_registry_lock_on_locked_domain
@domain.apply_registry_lock(extensionsProhibited: false)
@domain.apply_registry_lock(extensions_prohibited: false)

assert_equal(
[DomainStatus::SERVER_UPDATE_PROHIBITED,
Expand Down
4 changes: 2 additions & 2 deletions test/system/admin_area/domains/registry_lock_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_does_not_have_link_when_domain_is_not_locked
end

def test_can_remove_registry_lock_from_a_domain
@domain.apply_registry_lock(extensionsProhibited: false)
@domain.apply_registry_lock(extensions_prohibited: false)

visit edit_admin_domain_path(@domain)
click_link_or_button('Actions')
Expand All @@ -34,7 +34,7 @@ def test_can_remove_registry_lock_from_a_domain
end

def test_cannot_remove_registry_lock_from_not_locked_domain
@domain.apply_registry_lock(extensionsProhibited: false)
@domain.apply_registry_lock(extensions_prohibited: false)
visit edit_admin_domain_path(@domain)
@domain.remove_registry_lock

Expand Down
2 changes: 1 addition & 1 deletion test/system/admin_area/domains_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_admin_registry_lock_date
refute_text 'Registry lock time 2010-07-05 00:30'

lockable_domain = domains(:airport)
lockable_domain.apply_registry_lock(extensionsProhibited: false)
lockable_domain.apply_registry_lock(extensions_prohibited: false)

visit admin_domain_path(lockable_domain)
assert_text 'Registry lock time 2010-07-05 00:30'
Expand Down

0 comments on commit 4d64ee2

Please sign in to comment.