Add Registrant API contact update action

Closes #849
This commit is contained in:
Artur Beljajev 2018-08-20 17:19:54 +03:00
parent 90ed23f64d
commit b6ecae6a35
41 changed files with 1239 additions and 61 deletions

5
test/fixtures/actions.yml vendored Normal file
View file

@ -0,0 +1,5 @@
contact_update:
operation: update
contact: john
created_at: <%= Time.zone.parse('2010-07-05').to_s(:db) %>
user: registrant

View file

@ -9,8 +9,8 @@ john:
code: john-001
auth_info: cacb5b
uuid: eb2f2766-b44c-4e14-9f16-32ab1a7cb957
created_at: <%= Time.zone.parse('2010-07-05').to_s(:db) %>
updated_at: <%= Time.zone.parse('2010-07-06').to_s(:db) %>
created_at: <%= Time.zone.parse('2010-07-05') %>
updated_at: <%= Time.zone.parse('2010-07-06') %>
william: &william
name: William

View file

@ -4,8 +4,8 @@ greeting:
registrar: bestnames
created_at: <%= Time.zone.parse('2010-07-04') %>
domain_deleted:
text: Your domain has been deleted
complete:
text: Your domain has been updated
read: false
registrar: bestnames
created_at: <%= Time.zone.parse('2010-07-05') %>

View file

@ -0,0 +1,179 @@
require 'test_helper'
require 'auth_token/auth_token_creator'
class RegistrantApiV1ContactUpdateTest < ActionDispatch::IntegrationTest
setup do
@contact = contacts(:john)
@original_address_processing_setting = Setting.address_processing
@original_business_registry_cache_setting = Setting.days_to_keep_business_registry_cache
@original_fax_enabled_setting = ENV['fax_enabled']
Setting.days_to_keep_business_registry_cache = 1
travel_to Time.zone.parse('2010-07-05')
end
teardown do
Setting.address_processing = @original_address_processing_setting
Setting.days_to_keep_business_registry_cache = @original_business_registry_cache_setting
ENV['fax_enabled'] = @original_fax_enabled_setting
end
def test_update_contact
patch api_v1_registrant_contact_path(@contact.uuid), { name: 'new name',
email: 'new-email@coldmail.test',
phone: '+666.6' },
'HTTP_AUTHORIZATION' => auth_token
assert_response :ok
@contact.reload
assert_equal 'new name', @contact.name
assert_equal 'new-email@coldmail.test', @contact.email
assert_equal '+666.6', @contact.phone
end
def test_notify_registrar
assert_difference -> { @contact.registrar.notifications.count } do
patch api_v1_registrant_contact_path(@contact.uuid), { name: 'new name' },
'HTTP_AUTHORIZATION' => auth_token
end
notification = @contact.registrar.notifications.last
assert_equal 'Contact john-001 has been updated by registrant', notification.text
end
def test_update_fax_when_enabled
ENV['fax_enabled'] = 'true'
@contact = contacts(:william)
patch api_v1_registrant_contact_path(@contact.uuid), { 'fax' => '+777.7' },
'HTTP_AUTHORIZATION' => auth_token
assert_response :ok
@contact.reload
assert_equal '+777.7', @contact.fax
end
def test_fax_cannot_be_updated_when_disabled
ENV['fax_enabled'] = 'false'
patch api_v1_registrant_contact_path(@contact.uuid), { 'fax' => '+823.7' },
'HTTP_AUTHORIZATION' => auth_token
assert_response :bad_request
@contact.reload
assert_not_equal '+823.7', @contact.fax
error_msg = 'Fax processing is disabled and therefore cannot be updated'
assert_equal ({ errors: [{ address: [error_msg] }] }), JSON.parse(response.body,
symbolize_names: true)
end
def test_update_address_when_enabled
Setting.address_processing = true
patch api_v1_registrant_contact_path(@contact.uuid), { 'address[city]' => 'new city',
'address[street]' => 'new street',
'address[zip]' => '92837',
'address[country_code]' => 'RU',
'address[state]' => 'new state' },
'HTTP_AUTHORIZATION' => auth_token
assert_response :ok
@contact.reload
assert_equal Contact::Address.new('new street', '92837', 'new city', 'new state', 'RU'),
@contact.address
end
def test_address_is_optional_when_enabled
@contact = contacts(:william)
Setting.address_processing = true
patch api_v1_registrant_contact_path(@contact.uuid), { 'name' => 'any' },
'HTTP_AUTHORIZATION' => auth_token
assert_response :ok
end
def test_address_cannot_be_updated_when_disabled
@contact = contacts(:william)
@original_address = @contact.address
Setting.address_processing = false
patch api_v1_registrant_contact_path(@contact.uuid), { 'address[city]' => 'new city' },
'HTTP_AUTHORIZATION' => auth_token
@contact.reload
assert_response :bad_request
assert_equal @original_address, @contact.address
error_msg = 'Address processing is disabled and therefore cannot be updated'
assert_equal ({ errors: [{ address: [error_msg] }] }), JSON.parse(response.body,
symbolize_names: true)
end
def test_return_contact_details
patch api_v1_registrant_contact_path(@contact.uuid), { name: 'new name' },
'HTTP_AUTHORIZATION' => auth_token
assert_equal ({ id: @contact.uuid,
name: 'new name',
code: @contact.code,
fax: @contact.fax,
ident: {
code: @contact.ident,
type: @contact.ident_type,
country_code: @contact.ident_country_code,
},
email: @contact.email,
phone: @contact.phone,
address: {
street: @contact.street,
zip: @contact.zip,
city: @contact.city,
state: @contact.state,
country_code: @contact.country_code,
},
auth_info: @contact.auth_info,
statuses: @contact.statuses }), JSON.parse(response.body, symbolize_names: true)
end
def test_errors
patch api_v1_registrant_contact_path(@contact.uuid), { phone: 'invalid' },
'HTTP_AUTHORIZATION' => auth_token
assert_response :bad_request
assert_equal ({ errors: { phone: ['Phone nr is invalid'] } }), JSON.parse(response.body,
symbolize_names: true)
end
def test_contact_of_another_user_cannot_be_updated
@contact = contacts(:jack)
patch api_v1_registrant_contact_path(@contact.uuid), { name: 'any' },
'HTTP_AUTHORIZATION' => auth_token
assert_response :not_found
@contact.reload
assert_not_equal 'any', @contact.name
end
def test_non_existent_contact
patch api_v1_registrant_contact_path('non-existent'), nil, 'HTTP_AUTHORIZATION' => auth_token
assert_response :not_found
assert_equal ({ errors: [{ base: ['Not found'] }] }),
JSON.parse(response.body, symbolize_names: true)
end
def test_anonymous_user
patch api_v1_registrant_contact_path(@contact.uuid)
assert_response :unauthorized
assert_equal ({ errors: [{ base: ['Not authorized'] }] }),
JSON.parse(response.body, symbolize_names: true)
end
private
def auth_token
token_creator = AuthTokenCreator.create_with_defaults(users(:registrant))
hash = token_creator.token_in_hash
"Bearer #{hash[:access_token]}"
end
end

View file

@ -1,9 +1,33 @@
require 'test_helper'
class EppPollTest < ApplicationIntegrationTest
setup do
@notification = notifications(:complete)
end
# Deliberately does not conform to RFC5730, which requires the first notification to be returned
def test_return_latest_notification_when_queue_is_not_empty
notification = notifications(:domain_deleted)
request_xml = <<-XML
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="https://epp.tld.ee/schema/epp-ee-1.0.xsd">
<command>
<poll op="req"/>
</command>
</epp>
XML
post '/epp/command/poll', { frame: request_xml }, 'HTTP_COOKIE' => 'session=api_bestnames'
xml_doc = Nokogiri::XML(response.body)
assert_equal 1301.to_s, xml_doc.at_css('result')[:code]
assert_equal 1, xml_doc.css('result').size
assert_equal 2.to_s, xml_doc.at_css('msgQ')[:count]
assert_equal @notification.id.to_s, xml_doc.at_css('msgQ')[:id]
assert_equal Time.zone.parse('2010-07-05').utc.xmlschema, xml_doc.at_css('msgQ qDate').text
assert_equal 'Your domain has been updated', xml_doc.at_css('msgQ msg').text
end
def test_return_action_data_when_present
@notification.update!(action: actions(:contact_update))
request_xml = <<-XML
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
@ -14,14 +38,16 @@ class EppPollTest < ApplicationIntegrationTest
</epp>
XML
post '/epp/command/poll', { frame: request_xml }, 'HTTP_COOKIE' => 'session=api_bestnames'
response_xml = Nokogiri::XML(response.body)
assert_equal 1301.to_s, response_xml.at_css('result')[:code]
assert_equal 1, response_xml.css('result').size
assert_equal 2.to_s, response_xml.at_css('msgQ')[:count]
assert_equal notification.id.to_s, response_xml.at_css('msgQ')[:id]
assert_equal Time.zone.parse('2010-07-05').utc.xmlschema, response_xml.at_css('msgQ qDate').text
assert_equal 'Your domain has been deleted', response_xml.at_css('msgQ msg').text
xml_doc = Nokogiri::XML(response.body)
namespace = 'https://epp.tld.ee/schema/changePoll-1.0.xsd'
assert_equal 'update', xml_doc.xpath('//changePoll:operation', 'changePoll' => namespace).text
assert_equal Time.zone.parse('2010-07-05').utc.xmlschema,
xml_doc.xpath('//changePoll:date', 'changePoll' => namespace).text
assert_equal @notification.action.id.to_s, xml_doc.xpath('//changePoll:svTRID',
'changePoll' => namespace).text
assert_equal 'Registrant User', xml_doc.xpath('//changePoll:who',
'changePoll' => namespace).text
end
def test_no_notifications

View file

@ -0,0 +1,20 @@
require 'test_helper'
class ActionTest < ActiveSupport::TestCase
setup do
@action = actions(:contact_update)
end
def test_fixture_is_valid
assert @action.valid?
end
def test_invalid_with_unsupported_operation
@action.operation = 'invalid'
assert @action.invalid?
end
def test_notification_key_for_contact
assert_equal :contact_update, @action.notification_key
end
end

View file

@ -0,0 +1,16 @@
require 'test_helper'
class ContactAddressTest < ActiveSupport::TestCase
setup do
@address = Contact::Address.new('Main Street', '1234', 'NY City', 'NY State', 'US')
end
def test_equal_when_all_parts_are_the_same
assert_equal @address, Contact::Address.new('Main Street', '1234', 'NY City', 'NY State', 'US')
end
def test_not_equal_when_some_part_is_different
assert_not_equal @address, Contact::Address.new('Main Street', '1234', 'NY City', 'NY State',
'DE')
end
end

View file

@ -26,4 +26,16 @@ class ContactTest < ActiveSupport::TestCase
def test_not_in_use_if_acts_as_neither_registrant_nor_domain_contact
refute contacts(:not_in_use).in_use?
end
def test_managed_when_identity_codes_match
contact = Contact.new(ident: '1234')
user = RegistrantUser.new(registrant_ident: 'US-1234')
assert contact.managed_by?(user)
end
def test_unmanaged_when_identity_codes_do_not_match
contact = Contact.new(ident: '1234')
user = RegistrantUser.new(registrant_ident: 'US-12345')
assert_not contact.managed_by?(user)
end
end

View file

@ -21,4 +21,13 @@ class ContactPostalAddressTest < ActiveSupport::TestCase
@contact.country_code = 'invalid'
assert @contact.valid?
end
def test_state_is_optional_when_address_is_enabled
Setting.address_processing = true
contact = contacts(:william)
assert contact.valid?
contact.state = ''
assert contact.valid?
end
end

View file

@ -34,4 +34,18 @@ class ContactTest < ActiveSupport::TestCase
@contact.phone = '+123.4'
assert @contact.valid?
end
def test_address
address = Contact::Address.new('new street', '83746', 'new city', 'new state', 'EE')
@contact.address = address
@contact.save!
@contact.reload
assert_equal 'new street', @contact.street
assert_equal '83746', @contact.zip
assert_equal 'new city', @contact.city
assert_equal 'new state', @contact.state
assert_equal 'EE', @contact.country_code
assert_equal address, @contact.address
end
end

View file

@ -35,4 +35,14 @@ class RegistrantUserTest < ActiveSupport::TestCase
assert_equal('1234', @user.ident)
assert_equal('US', @user.country_code)
end
def test_first_name_from_username
user = RegistrantUser.new(username: 'John Doe')
assert_equal 'John', user.first_name
end
def test_last_name_from_username
user = RegistrantUser.new(username: 'John Doe')
assert_equal 'Doe', user.last_name
end
end

View file

@ -0,0 +1,177 @@
require 'test_helper'
class RegistrantAreaContactUpdateTest < ApplicationIntegrationTest
setup do
@domain = domains(:shop)
@contact = contacts(:john)
sign_in users(:registrant)
@original_address_processing_setting = Setting.address_processing
@original_business_registry_cache_setting = Setting.days_to_keep_business_registry_cache
@original_fax_enabled_setting = ENV['fax_enabled']
@original_registrant_api_base_url_setting = ENV['registrant_api_base_url']
ENV['registrant_api_base_url'] = 'https://api.test'
Setting.days_to_keep_business_registry_cache = 1
travel_to Time.zone.parse('2010-07-05')
end
teardown do
Setting.address_processing = @original_address_processing_setting
Setting.days_to_keep_business_registry_cache = @original_business_registry_cache_setting
ENV['fax_enabled'] = @original_fax_enabled_setting
ENV['registrant_api_base_url'] = @original_registrant_api_base_url_setting
end
def test_form_is_pre_populated_with_contact_data
visit edit_registrant_domain_contact_url(@domain, @contact)
assert_field 'Name', with: 'John'
assert_field 'Email', with: 'john@inbox.test'
assert_field 'Phone', with: '+555.555'
end
def test_update_contact
stub_auth_request
request_body = { name: 'new name', email: 'new@inbox.test', phone: '+666.6' }
headers = { 'Authorization' => 'Bearer test-access-token' }
url = "https://api.test/api/v1/registrant/contacts/#{@contact.uuid}"
update_request_stub = stub_request(:patch, url).with(body: request_body, headers: headers)
.to_return(body: '{}', status: 200)
visit registrant_domain_contact_url(@domain, @contact)
click_link_or_button 'Edit'
fill_in 'Name', with: 'new name'
fill_in 'Email', with: 'new@inbox.test'
fill_in 'Phone', with: '+666.6'
click_link_or_button 'Update contact'
assert_requested update_request_stub
assert_current_path registrant_domain_contact_path(@domain, @contact)
assert_text 'Contact has been successfully updated'
end
def test_form_is_pre_populated_with_fax_when_enabled
ENV['fax_enabled'] = 'true'
@contact.update!(fax: '+111.1')
visit edit_registrant_domain_contact_url(@domain, @contact)
assert_field 'Fax', with: '+111.1'
end
def test_update_fax_when_enabled
ENV['fax_enabled'] = 'true'
stub_auth_request
request_body = { email: 'john@inbox.test', name: 'John', phone: '+555.555', fax: '+222.2' }
headers = { 'Authorization' => 'Bearer test-access-token' }
url = "https://api.test/api/v1/registrant/contacts/#{@contact.uuid}"
update_request_stub = stub_request(:patch, url).with(body: request_body, headers: headers)
.to_return(body: '{}', status: 200)
visit edit_registrant_domain_contact_url(@domain, @contact)
fill_in 'Fax', with: '+222.2'
click_link_or_button 'Update contact'
assert_requested update_request_stub
assert_current_path registrant_domain_contact_path(@domain, @contact)
assert_text 'Contact has been successfully updated'
end
def test_hide_fax_field_when_disabled
visit edit_registrant_domain_contact_url(@domain, @contact)
assert_no_field 'Fax'
end
def test_form_is_pre_populated_with_address_when_enabled
Setting.address_processing = true
@contact = contacts(:william)
visit edit_registrant_domain_contact_url(@domain, @contact)
assert_field 'Street', with: 'Main Street'
assert_field 'Zip', with: '12345'
assert_field 'City', with: 'New York'
assert_field 'State', with: 'New York State'
assert_select 'Country', selected: 'United States'
end
def test_update_address_when_enabled
Setting.address_processing = true
stub_auth_request
request_body = { email: 'john@inbox.test',
name: 'John',
phone: '+555.555',
address: {
street: 'new street',
zip: '93742',
city: 'new city',
state: 'new state',
country_code: 'AT'
} }
headers = { 'Authorization' => 'Bearer test-access-token' }
url = "https://api.test/api/v1/registrant/contacts/#{@contact.uuid}"
update_request_stub = stub_request(:patch, url).with(body: request_body, headers: headers)
.to_return(body: '{}', status: 200)
visit edit_registrant_domain_contact_url(@domain, @contact)
fill_in 'Street', with: 'new street'
fill_in 'City', with: 'new city'
fill_in 'State', with: 'new state'
fill_in 'Zip', with: '93742'
select 'Austria', from: 'Country'
click_link_or_button 'Update contact'
assert_requested update_request_stub
assert_current_path registrant_domain_contact_path(@domain, @contact)
assert_text 'Contact has been successfully updated'
end
def test_hide_address_field_when_disabled
visit edit_registrant_domain_contact_url(@domain, @contact)
assert_no_field 'Address'
assert_no_field 'Street'
end
def test_unmanaged_contact_cannot_be_updated
@contact.update!(ident: '12345')
visit registrant_domain_contact_url(@domain, @contact)
assert_no_button 'Edit'
assert_no_link 'Edit'
end
def test_fail_gracefully
stub_auth_request
response_body = { errors: { name: ['Name is invalid'] } }.to_json
headers = { 'Authorization' => 'Bearer test-access-token' }
stub_request(:patch, "https://api.test/api/v1/registrant/contacts/#{@contact.uuid}")
.with(headers: headers)
.to_return(body: response_body, status: 400)
visit edit_registrant_domain_contact_url(@domain, @contact)
fill_in 'Name', with: 'invalid name'
click_link_or_button 'Update contact'
assert_current_path registrant_domain_contact_path(@domain, @contact)
assert_text 'Name is invalid'
assert_field 'Name', with: 'invalid name'
assert_no_text 'Contact has been successfully updated'
end
private
def stub_auth_request
body = { ident: '1234', first_name: 'Registrant', last_name: 'User' }
stub_request(:post, 'https://api.test/api/v1/registrant/auth/eid').with(body: body)
.to_return(body: { access_token: 'test-access-token' }.to_json,
headers: { 'Content-type' => 'application/json' },
status: 200)
end
end