def test_get_oldest_registrant(): tester = testsupport.prepare_tester_account() assert zcontacts.get_oldest_registrant(tester) is None first_registrant = testsupport.prepare_tester_registrant(tester, epp_id='id1', create_new=True) assert zcontacts.registrant_exists(epp_id='id1') is True assert zcontacts.registrant_exists(epp_id='id2') is False assert zcontacts.registrant_find(epp_id='id1') == first_registrant assert zcontacts.get_oldest_registrant(tester) == first_registrant second_registrant = testsupport.prepare_tester_registrant(tester, epp_id='id2', create_new=True) assert zcontacts.registrant_exists(epp_id='id2') is True assert zcontacts.registrant_find(epp_id='id2') != first_registrant assert zcontacts.registrant_find(epp_id='id2') == second_registrant assert zcontacts.get_oldest_registrant(tester) == first_registrant zcontacts.registrant_delete(epp_id='id1') assert zcontacts.get_oldest_registrant(tester) == second_registrant
def doDBCheckChangeOwner(self, *args, **kwargs): """ Action method. """ if not self.target_domain: return # update registrant of the domain if it is known if self.target_domain.registrant and self.known_registrant and self.target_domain.registrant != self.known_registrant: if not self.change_owner_allowed: logger.error( 'domain registrant changed, but domain already have another owner in local DB' ) raise zerrors.RegistrantAuthFailed( 'domain registrant changed, but domain already have another owner in local DB' ) logger.info('domain %r going to switch registrant %r to %r', self.target_domain, self.target_domain.registrant, self.known_registrant) self.target_domain = zdomains.domain_change_registrant( self.target_domain, self.known_registrant) self.target_domain.refresh_from_db() first_contact = self.known_registrant.owner.contacts.first() self.target_domain = zdomains.domain_replace_contacts( self.target_domain, new_admin_contact=first_contact) self.target_domain.refresh_from_db() return # make sure owner of the domain is correct if self.known_registrant and self.target_domain.owner != self.known_registrant.owner: # just in case given user have multiple registrant contacts... if not self.change_owner_allowed: logger.error('domain already have another owner in local DB') raise zerrors.RegistrantAuthFailed( 'domain already have another owner in local DB') logger.info( 'domain %r going to switch owner %r to %r because of different registrant found', self.target_domain, self.target_domain.owner, self.known_registrant.owner) self.target_domain = zdomains.domain_change_owner( self.target_domain, self.known_registrant.owner) self.target_domain.refresh_from_db() first_contact = self.known_registrant.owner.contacts.first() self.target_domain = zdomains.domain_replace_contacts( self.target_domain, new_admin_contact=first_contact) self.target_domain.refresh_from_db() return # also check if registrant's email changed on back-end # in that situation new account needs to be created and all domains and contacts re-attached to the new account # this must be done separately, because that flow is only focused on single domain object try: received_registrant_email = args[0]['registrant']['response'][ 'epp']['response']['resData']['infData']['email'].lower() except: received_registrant_email = self.target_domain.registrant.contact_email.lower( ) if received_registrant_email != self.target_domain.registrant.contact_email.lower( ): existing_registrant = zcontacts.registrant_find( contact_email=received_registrant_email) if existing_registrant: logger.error( 'registrant %r contact email changed to %r, but another registrant with same email already exist in local DB: %r', self.target_domain.registrant, received_registrant_email, existing_registrant) raise zerrors.RegistrantAuthFailed( 'registrant contact email changed, but another registrant with same email already exist in local DB' ) existing_account = zusers.find_account( email=received_registrant_email) if existing_account: logger.error( 'registrant %r contact email changed to %r, but another account already exist in local DB with same email: %r', self.target_domain.registrant, received_registrant_email, existing_registrant) raise zerrors.RegistrantAuthFailed( 'registrant contact email changed, but another account already exist in local DB with same email' ) logger.error( 'registrant %r contact email changed to %s and needs to be synchronized', self.target_domain.registrant, received_registrant_email) raise zerrors.RegistrantAuthFailed( 'registrant contact email changed and needs to be synchronized' )
def doDBCheckCreateUserAccount(self, *args, **kwargs): """ Action method. """ if self.known_registrant: logger.info( 'registrant already known so skip creating user account') return known_owner = zusers.find_account( self.current_registrant_info['email']) if not known_owner: known_owner = zusers.create_account( email=self.current_registrant_info['email'], account_password=zusers.generate_password(length=10), also_profile=True, is_active=True, person_name=self.current_registrant_address_info.get( 'name', 'unknown'), organization_name=self.current_registrant_address_info.get( 'org', 'unknown'), address_street=self.current_registrant_address_info.get( 'street', 'unknown'), address_city=self.current_registrant_address_info.get( 'city', 'unknown'), address_province=self.current_registrant_address_info.get( 'sp', 'unknown'), address_postal_code=self.current_registrant_address_info.get( 'pc', 'unknown'), address_country=self.current_registrant_address_info.get( 'cc', 'AF'), contact_voice=zcontacts.extract_phone_number( self.current_registrant_info.get('voice', '')), contact_fax=zcontacts.extract_phone_number( self.current_registrant_info.get('fax', '')), contact_email=self.current_registrant_info['email'], ) if not hasattr(known_owner, 'profile'): zusers.create_profile( known_owner, person_name=self.current_registrant_address_info.get( 'name', 'unknown'), organization_name=self.current_registrant_address_info.get( 'org', 'unknown'), address_street=self.current_registrant_address_info.get( 'street', 'unknown'), address_city=self.current_registrant_address_info.get( 'city', 'unknown'), address_province=self.current_registrant_address_info.get( 'sp', 'unknown'), address_postal_code=self.current_registrant_address_info.get( 'pc', 'unknown'), address_country=self.current_registrant_address_info.get( 'cc', 'AF'), contact_voice=zcontacts.extract_phone_number( self.current_registrant_info.get('voice', '')), contact_fax=zcontacts.extract_phone_number( self.current_registrant_info.get('fax', '')), contact_email=self.current_registrant_info['email'], ) self.received_registrant_epp_id = self.new_registrant_epp_id self.known_registrant = zcontacts.registrant_find( epp_id=self.received_registrant_epp_id) if not self.known_registrant: logger.info('new registrant will be created for %r', known_owner) self.known_registrant = zcontacts.registrant_create_from_profile( owner=known_owner, profile_object=known_owner.profile, epp_id=self.received_registrant_epp_id, ) if self.target_domain: zdomains.domain_detach_contact(self.target_domain, 'admin') zdomains.domain_detach_contact(self.target_domain, 'billing') zdomains.domain_detach_contact(self.target_domain, 'tech') self.new_domain_contacts.pop('admin', None) self.new_domain_contacts.pop('billing', None) self.new_domain_contacts.pop('tech', None) if self.target_domain: self.target_domain.refresh_from_db()
def doEppDomainInfo(self, *args, **kwargs): """ Action method. """ try: response = rpc_client.cmd_domain_info( domain=self.domain_name, raise_for_result=False, ) except rpc_error.EPPError as exc: self.log(self.debug_level, 'Exception in doEppDomainInfo: %s' % exc) self.event('error', exc) return # catch "2201 Authorization error" result, that means domain exists, but have another owner try: code = int(response['epp']['response']['result']['@code']) except ( ValueError, IndexError, TypeError, ) as exc: self.log(self.debug_level, 'Exception in doEppDomainInfo: %s' % exc) self.event('error', exc) return if code == 2201: self.domain_info_response = response self.event('response', response) return # check create/expire date try: datetime.datetime.strptime( response['epp']['response']['resData']['infData']['exDate'], '%Y-%m-%dT%H:%M:%S.%fZ', ) datetime.datetime.strptime( response['epp']['response']['resData']['infData']['crDate'], '%Y-%m-%dT%H:%M:%S.%fZ', ) except Exception as exc: self.log(self.debug_level, 'Exception in doEppDomainInfo: %s' % exc) raise rpc_error.EPPBadResponse('response field not recognized') # detect current nameservers try: current_nameservers = response['epp']['response']['resData'][ 'infData']['ns']['hostObj'] except: current_nameservers = [] if not isinstance(current_nameservers, list): current_nameservers = [ current_nameservers, ] self.received_nameservers = current_nameservers # detect current contacts try: current_contacts = response['epp']['response']['resData'][ 'infData']['contact'] except: current_contacts = [] if not isinstance(current_contacts, list): current_contacts = [ current_contacts, ] self.received_contacts = [{ 'type': i['@type'], 'id': i['#text'], } for i in current_contacts] self.new_domain_contacts = { i['@type']: i['#text'] for i in current_contacts } # compare known contacts we have in DB with contacts received from back-end if not self.target_domain: self.contacts_changed = True else: if not self.refresh_contacts: add_contacts, remove_contacts, change_registrant = zdomains.compare_contacts( domain_object=self.target_domain, domain_info_response=response, ) if add_contacts or remove_contacts or change_registrant: self.contacts_changed = True # detect current registrant try: self.received_registrant_epp_id = response['epp']['response'][ 'resData']['infData']['registrant'] except Exception as exc: self.log(self.debug_level, 'Exception in doEppDomainInfo: %s' % exc) self.event('error', zerrors.RegistrantUnknown(response=response)) return self.domain_info_response = response if self.expected_owner: # if pending order exists, select registrant from the owner of that order self.known_registrant = self.expected_owner.registrants.first() if self.known_registrant and self.known_registrant.epp_id: self.received_registrant_epp_id = self.known_registrant.epp_id logger.info('trying to use registrant from existing owner: %r', self.known_registrant) else: # find registrant in local DB self.known_registrant = zcontacts.registrant_find( epp_id=self.received_registrant_epp_id) logger.info('trying to find registrant by epp id %r: %r', self.received_registrant_epp_id, self.known_registrant) if self.rewrite_contacts and self.known_registrant: logger.info( 'going to rewrite domain %r contacts from existing owner %r', self.domain_name, self.known_registrant.owner) self.event('rewrite-contacts', response) return if self.known_registrant: logger.info('registrant for domain %r is known: %r', self.domain_name, self.known_registrant) self.event('response', response) return if not self.create_new_owner_allowed: if self.domain_transferred_away: logger.error('domain %r is marked as transferred away', self.domain_name) self.event('domain-transferred-away') else: # fail if registrant not exist in local DB # that means owner of the domain changed, or his contact is not in sync with back-end # this also happens when domain is transferred to Zenaida, but user account not exist yet logger.error('registrant %r not exist in local DB', self.received_registrant_epp_id) self.event( 'error', zerrors.RegistrantAuthFailed( 'registrant not exist in local DB')) return # currently registrant not exist in local DB, user account needs to be checked and created first logger.info( 'registrant not exist in local DB, going to create new registrant') self.event('registrant-unknown', response)
def test_registrant_find_by_epp_id(): tester = testsupport.prepare_tester_account(email='*****@*****.**') tester_registrant = testsupport.prepare_tester_registrant(tester=tester, epp_id='reg1234', profile_object=tester.profile, create_new=True) assert zcontacts.registrant_find(epp_id='reg1234') == tester_registrant