def setUpClass(cls): super(SMSCommon, cls).setUpClass() cls.user_employee.write({'login': '******'}) # update country to belgium in order to test sanitization of numbers cls.user_employee.company_id.write({'country_id': cls.env.ref('base.be').id}) # some numbers for testing cls.random_numbers_str = '+32456998877, 0456665544' cls.random_numbers = cls.random_numbers_str.split(', ') cls.random_numbers_san = [phone_validation.phone_format(number, 'BE', '32', force_format='E164') for number in cls.random_numbers] cls.test_numbers = ['+32456010203', '0456 04 05 06', '0032456070809'] cls.test_numbers_san = [phone_validation.phone_format(number, 'BE', '32', force_format='E164') for number in cls.test_numbers] # some numbers for mass testing cls.mass_numbers = ['04561%s2%s3%s' % (x, x, x) for x in range(0, 10)] cls.mass_numbers_san = [phone_validation.phone_format(number, 'BE', '32', force_format='E164') for number in cls.mass_numbers]
def setUpClass(cls): super(TestRecipients, cls).setUpClass() cls.partner_numbers = [ phone_validation.phone_format(partner.mobile, partner.country_id.code, partner.country_id.phone_code, force_format='E164') for partner in (cls.partner_1 | cls.partner_2) ]
def phone_format(self, number, country=None, company=None): country = country or self._phone_get_country() always_international = company.phone_international_format == 'prefix' if company else self._phone_get_always_international( ) return phone_validation.phone_format( number, country.code if country else None, country.phone_code if country else None, always_international=always_international, raise_exception=True)
def phone_format(self, number, country=None, company=None): country = country or self._phone_get_country() if not country: return number return phone_validation.phone_format( number, country.code if country else None, country.phone_code if country else None, force_format='INTERNATIONAL', raise_exception=False )
def test_sms_schedule(self): with self.mockSMSGateway(): self._create_registrations(self.event_0, 3) # check subscription scheduler schedulers = self.env['event.mail'].search([('event_id', '=', self.event_0.id), ('interval_type', '=', 'after_sub')]) self.assertEqual(len(schedulers), 1) self.assertEqual(schedulers.scheduled_date, self.event_0.create_date, 'event: incorrect scheduled date for checking controller') # verify that subscription scheduler was auto-executed after each registration self.assertEqual(len(schedulers.mail_registration_ids), 3) self.assertTrue(all(m.mail_sent is True for m in schedulers.mail_registration_ids)) self.assertEqual(schedulers.mapped('mail_registration_ids.registration_id'), self.event_0.registration_ids) sanitized_numbers = [] for registration in self.event_0.registration_ids: reg_sanitized_number = phone_validation.phone_format(registration.phone, 'BE', '32', force_format='E164') sanitized_numbers.append(reg_sanitized_number) self.assertSMSOutgoing( self.env['res.partner'], reg_sanitized_number, content='%s registration confirmation.' % self.event_0.organizer_id.name) # clear notification queue to avoid conflicts when checking next notifications self.env['mail.notification'].search([('sms_number', 'in', sanitized_numbers)]).unlink() self.env['sms.sms'].search([('number', 'in', sanitized_numbers)]).unlink() # check before event scheduler schedulers = self.env['event.mail'].search([('event_id', '=', self.event_0.id), ('interval_type', '=', 'before_event')]) self.assertEqual(len(schedulers), 1, 'event: wrong scheduler creation') self.assertEqual(schedulers[0].scheduled_date, self.event_0.date_begin + relativedelta(days=-3)) # execute event reminder scheduler explicitly with self.mockSMSGateway(): schedulers.execute() # verify that subscription scheduler was auto-executed after each registration for registration in self.event_0.registration_ids: reg_sanitized_number = phone_validation.phone_format(registration.phone, 'BE', '32', force_format='E164') self.assertSMSOutgoing( self.env['res.partner'], reg_sanitized_number, content='%s reminder' % self.event_0.organizer_id.name)
def test_mass_sms_internals_errors(self): # same customer, specific different number on record -> should be valid new_record_1 = self.env['mail.test.sms'].create({ 'name': 'MassSMSTest_nr1', 'customer_id': self.partners[0].id, 'phone_nbr': '0456999999', }) void_record = self.env['mail.test.sms'].create({ 'name': 'MassSMSTest_void', 'customer_id': False, 'phone_nbr': '', }) falsy_record_1 = self.env['mail.test.sms'].create({ 'name': 'MassSMSTest_falsy_1', 'customer_id': False, 'phone_nbr': 'abcd', }) falsy_record_2 = self.env['mail.test.sms'].create({ 'name': 'MassSMSTest_falsy_2', 'customer_id': False, 'phone_nbr': '04561122', }) bl_record_1 = self.env['mail.test.sms'].create({ 'name': 'MassSMSTest_bl_1', 'customer_id': False, 'phone_nbr': '0456110011', }) self.env['phone.blacklist'].create({'number': '0456110011'}) # new customer, number already on record -> should be ignored country_be_id = self.env.ref('base.be').id, nr2_partner = self.env['res.partner'].create({ 'name': 'Partner_nr2', 'country_id': country_be_id, 'mobile': '0456449999', }) new_record_2 = self.env['mail.test.sms'].create({ 'name': 'MassSMSTest_nr2', 'customer_id': nr2_partner.id, 'phone_nbr': self.records[0].phone_nbr, }) records_numbers = self.records_numbers + ['+32456999999'] with self.with_user('user_marketing'): with self.mockSMSGateway(): self.mailing.action_send_sms() self.assertSMSTraces( [{ 'partner': record.customer_id, 'number': records_numbers[i], 'content': 'Dear %s this is a mass SMS.' % record.display_name } for i, record in enumerate(self.records | new_record_1)], self.mailing, self.records | new_record_1, ) # duplicates self.assertSMSTraces( [{ 'partner': new_record_2.customer_id, 'number': self.records_numbers[0], 'content': 'Dear %s this is a mass SMS.' % new_record_2.display_name, 'state': 'ignored', 'failure_type': 'sms_duplicate' }], self.mailing, new_record_2, ) # blacklist self.assertSMSTraces( [{ 'partner': self.env['res.partner'], 'number': phone_validation.phone_format( bl_record_1.phone_nbr, 'BE', '32', force_format='E164'), 'content': 'Dear %s this is a mass SMS.' % bl_record_1.display_name, 'state': 'ignored', 'failure_type': 'sms_blacklist' }], self.mailing, bl_record_1, ) # missing number self.assertSMSTraces( [{ 'partner': self.env['res.partner'], 'number': False, 'content': 'Dear %s this is a mass SMS.' % void_record.display_name, 'state': 'exception', 'failure_type': 'sms_number_missing' }], self.mailing, void_record, ) # wrong values self.assertSMSTraces( [{ 'partner': self.env['res.partner'], 'number': record.phone_nbr, 'content': 'Dear %s this is a mass SMS.' % record.display_name, 'state': 'bounced', 'failure_type': 'sms_number_format' } for record in falsy_record_1 + falsy_record_2], self.mailing, falsy_record_1 + falsy_record_2, )
def setUpClass(cls): super(TestMassSMSCommon, cls).setUpClass() cls._test_body = 'Mass SMS in your face' records = cls.env['mail.test.sms'] partners = cls.env['res.partner'] country_be_id = cls.env.ref('base.be').id, country_us_id = cls.env.ref('base.us').id, for x in range(10): partners += cls.env['res.partner'].with_context( **cls._test_context).create({ 'name': 'Partner_%s' % (x), 'email': '*****@*****.**' % (x), 'country_id': country_be_id, 'mobile': '045600%s%s99' % (x, x) }) records += cls.env['mail.test.sms'].with_context( **cls._test_context).create({ 'name': 'MassSMSTest_%s' % (x), 'customer_id': partners[x].id, 'phone_nbr': '045600%s%s44' % (x, x) }) cls.records = cls._reset_mail_context(records) cls.records_numbers = [ phone_validation.phone_format(r.phone_nbr, 'BE', '32', force_format='E164') for r in cls.records ] cls.partners = partners cls.sms_template = cls.env['sms.template'].create({ 'name': 'Test Template', 'model_id': cls.env['ir.model']._get('mail.test.sms').id, 'body': 'Dear ${object.display_name} this is a mass SMS.', }) cls.partner_numbers = [ phone_validation.phone_format(partner.mobile, partner.country_id.code, partner.country_id.phone_code, force_format='E164') for partner in partners ] cls.mailing = cls.env['mailing.mailing'].with_user( cls.user_marketing).create({ 'name': 'Xmas Spam', 'subject': 'Xmas Spam', 'mailing_model_id': cls.env['ir.model']._get('mail.test.sms').id, 'mailing_type': 'sms', 'mailing_domain': '%s' % repr([('name', 'ilike', 'MassSMSTest')]), 'sms_template_id': cls.sms_template.id, 'sms_allow_unsubscribe': False, })
def test_crm_lead_partner_sync_email_phone(self): """ Specifically test synchronize between a lead and its partner about phone and email fields. Phone especially has some corner cases due to automatic formatting (notably with onchange in form view). """ lead, partner = self.lead_1.with_user(self.env.user), self.contact_2 lead_form = Form(lead) # reset partner phone to a local number and prepare formatted / sanitized values partner_phone, partner_mobile = self.test_phone_data[ 2], self.test_phone_data[1] partner_phone_formatted = phone_format(partner_phone, 'US', '1') partner_phone_sanitized = phone_format(partner_phone, 'US', '1', force_format='E164') partner_mobile_formatted = phone_format(partner_mobile, 'US', '1') partner_mobile_sanitized = phone_format(partner_mobile, 'US', '1', force_format='E164') partner_email, partner_email_normalized = self.test_email_data[ 2], self.test_email_data_normalized[2] self.assertEqual(partner_phone_formatted, '+1 202-555-0888') self.assertEqual(partner_phone_sanitized, self.test_phone_data_sanitized[2]) self.assertEqual(partner_mobile_formatted, '+1 202-555-0999') self.assertEqual(partner_mobile_sanitized, self.test_phone_data_sanitized[1]) # ensure initial data self.assertEqual(partner.phone, partner_phone) self.assertEqual(partner.mobile, partner_mobile) self.assertEqual(partner.email, partner_email) # LEAD/PARTNER SYNC: email and phone are propagated to lead # as well as mobile (who does not trigger the reverse sync) lead_form.partner_id = partner self.assertEqual(lead_form.email_from, partner_email) self.assertEqual(lead_form.phone, partner_phone_formatted, 'Lead: form automatically formats numbers') self.assertEqual(lead_form.mobile, partner_mobile_formatted, 'Lead: form automatically formats numbers') self.assertFalse(lead_form.ribbon_message) lead_form.save() self.assertEqual(partner.phone, partner_phone, 'Lead / Partner: partner values sent to lead') self.assertEqual(lead.email_from, partner_email, 'Lead / Partner: partner values sent to lead') self.assertEqual( lead.email_normalized, partner_email_normalized, 'Lead / Partner: equal emails should lead to equal normalized emails' ) self.assertEqual( lead.phone, partner_phone_formatted, 'Lead / Partner: partner values (formatted) sent to lead') self.assertEqual( lead.mobile, partner_mobile_formatted, 'Lead / Partner: partner values (formatted) sent to lead') self.assertEqual(lead.phone_sanitized, partner_mobile_sanitized, 'Lead: phone_sanitized computed field on mobile') # for email_from, if only formatting differs, warning ribbon should # not appear and email on partner should not be updated lead_form.email_from = '"Hermes Conrad" <%s>' % partner_email_normalized self.assertFalse(lead_form.ribbon_message) lead_form.save() self.assertEqual(lead_form.partner_id.email, partner_email) # LEAD/PARTNER SYNC: lead updates partner new_email = '"John Zoidberg" <*****@*****.**>' new_email_normalized = '*****@*****.**' lead_form.email_from = new_email self.assertIn('the customer email will', lead_form.ribbon_message) new_phone = '+1 202 555 7799' new_phone_formatted = phone_format(new_phone, 'US', '1') lead_form.phone = new_phone self.assertEqual(lead_form.phone, new_phone_formatted) self.assertIn('the customer email and phone number will', lead_form.ribbon_message) lead_form.save() self.assertEqual(partner.email, new_email) self.assertEqual(partner.email_normalized, new_email_normalized) self.assertEqual(partner.phone, new_phone_formatted) # LEAD/PARTNER SYNC: mobile does not update partner new_mobile = '+1 202 555 6543' new_mobile_formatted = phone_format(new_mobile, 'US', '1') lead_form.mobile = new_mobile lead_form.save() self.assertEqual(lead.mobile, new_mobile_formatted) self.assertEqual(partner.mobile, partner_mobile) # LEAD/PARTNER SYNC: reseting lead values also resets partner for email # and phone, but not for mobile lead_form.email_from, lead_form.phone, lead.mobile = False, False, False self.assertIn('the customer email and phone number will', lead_form.ribbon_message) lead_form.save() self.assertFalse(partner.email) self.assertFalse(partner.email_normalized) self.assertFalse(partner.phone) self.assertFalse(lead.phone) self.assertFalse(lead.mobile) self.assertFalse(lead.phone_sanitized) self.assertEqual(partner.mobile, partner_mobile) self.assertEqual(partner.phone_sanitized, partner_mobile_sanitized, 'Partner sanitized should be computed on mobile')