def test_update_address(self):
        adapter = IShoppingSite(self.portal)
        data = {}
        self.assertEqual(adapter.update_address('address', data), u'First name is missing.')

        data = {'address_first_name': 'F|RST'}
        self.assertEqual(adapter.update_address('address', data), u'Last name is missing.')

        data = {'address_first_name': 'F|RST', 'address_last_name': 'LÄST'}
        self.assertEqual(adapter.update_address('address', data), u'Invalid e-mail address.')

        data = {'address_first_name': 'F|RST', 'address_last_name': 'LÄST', 'address_email': 'EMAIL'}
        self.assertEqual(adapter.update_address('address', data), u'Invalid e-mail address.')

        data = {'address_first_name': 'F|RST', 'address_last_name': 'LÄST', 'address_email': '*****@*****.**'}
        self.assertEqual(adapter.update_address('address', data), u'Street address is missing.')

        data = {'address_first_name': 'F|RST', 'address_last_name': 'LÄST', 'address_email': '*****@*****.**', 'address_street': 'STR€€T'}
        self.assertEqual(adapter.update_address('address', data), u'Post code is missing.')

        data = {'address_first_name': 'F|RST', 'address_last_name': 'LÄST', 'address_email': '*****@*****.**', 'address_street': 'STR€€T',
            'address_post': 'PÖST'}
        self.assertEqual(adapter.update_address('address', data), u'City is missing.')

        data = {'address_first_name': 'F|RST', 'address_last_name': 'LÄST', 'address_email': '*****@*****.**', 'address_street': 'STR€€T',
            'address_post': 'PÖST', 'address_city': 'C|TY'}
        self.assertEqual(adapter.update_address('address', data), u'Phone number is missing.')

        data = {'address_first_name': 'F|RST', 'address_last_name': 'LÄST', 'address_email': '*****@*****.**', 'address_street': 'STR€€T',
            'address_city': 'C|TY', 'address_post': 'PÖST', 'address_phone': 'PHÖNE'}
        self.assertIsNone(adapter.update_address('address', data))

        adapter.get_address = mock.Mock(return_value=None)
        adapter.update_cart = mock.Mock()
        self.assertIsNone(adapter.update_address('address', data))
        adapter.update_cart.assert_called_with('address', {
            'city': 'C|TY',
            'email': '*****@*****.**',
            'first_name': 'F|RST',
            'last_name': 'LÄST',
            'phone': 'PHÖNE',
            'post': 'PÖST',
            'street': 'STR€€T',
        })

        names = ['city', 'last_name', 'first_name', 'email', 'phone', 'post', 'street']
        address = {}
        for name in names:
            address[name] = name.upper()
        adapter.get_address = mock.Mock(return_value=address)
        self.assertIsNone(adapter.update_address('address', data))
        adapter.update_cart.assert_called_with('address', {
            'city': 'C|TY',
            'email': '*****@*****.**',
            'first_name': 'F|RST',
            'last_name': 'LÄST',
            'phone': 'PHÖNE',
            'post': 'PÖST',
            'street': 'STR€€T',
        })
    def test_is_address_filled(self):
        adapter = IShoppingSite(self.portal)
        self.assertFalse(adapter.is_address_filled('billing'))

        adapter.get_address = mock.Mock(return_value={})
        self.assertFalse(adapter.is_address_filled('billing'))

        names = ['city', 'last_name', 'first_name', 'email', 'phone', 'post', 'street']
        address = {}
        for name in names:
            address[name] = name.upper()

        adapter.get_address = mock.Mock(return_value=address)
        self.assertTrue(adapter.is_address_filled('billing'))

        del address['email']
        self.assertFalse(adapter.is_address_filled('billing'))
    def test_create_cart(self, create_order):
        adapter = IShoppingSite(self.portal)

        create_order.return_value = None
        self.assertIsNone(adapter.create_order())

        order1 = self.create_content('collective.cart.core.Order', id='1')
        create_order.return_value = order1
        self.assertEqual(adapter.create_order(), order1)
        with self.assertRaises(KeyError):
            order1['shipping_method']
        with self.assertRaises(KeyError):
            order1['billing']
        with self.assertRaises(KeyError):
            order1['shipping']

        adapter.shipping_method = mock.Mock(return_value={'gross': self.money('10.00')})
        order2 = self.create_content('collective.cart.core.Order', id='2')
        create_order.return_value = order2
        self.assertEqual(adapter.create_order(), order2)
        self.assertIsNotNone(order2['shipping_method'])
        with self.assertRaises(KeyError):
            order2['billing']
        with self.assertRaises(KeyError):
            order2['shipping']

        names = ['city', 'last_name', 'first_name', 'email', 'phone', 'post', 'street']
        address = {}
        for name in names:
            address[name] = name.upper()
        adapter.get_address = mock.Mock(return_value=address)
        adapter.billing_same_as_shipping = mock.Mock(return_value=True)
        order3 = self.create_content('collective.cart.core.Order', id='3')
        create_order.return_value = order3
        self.assertEqual(adapter.create_order(), order3)
        self.assertIsNotNone(order3['shipping_method'])
        self.assertIsNotNone(order3['billing'])
        with self.assertRaises(KeyError):
            order3['shipping']

        adapter.billing_same_as_shipping = mock.Mock(return_value=False)
        order4 = self.create_content('collective.cart.core.Order', id='4')
        create_order.return_value = order4
        self.assertEqual(adapter.create_order(), order4)
        self.assertIsNotNone(order4['shipping_method'])
        self.assertIsNotNone(order4['billing'])
        self.assertIsNotNone(order4['shipping'])
    def test_get_info(self):
        adapter = IShoppingSite(self.portal)
        self.assertEqual(adapter.get_info('NAME'), {
            'first_name': '',
            'last_name': '',
            'organization': '',
            'vat': '',
            'email': '',
            'street': '',
            'post': '',
            'city': '',
            'phone': '',
        })

        membership = getToolByName(self.portal, 'portal_membership')
        member = membership.getAuthenticatedMember()
        member.setProperties(fullname='First', email="*****@*****.**")
        self.assertEqual(adapter.get_info('billing'), {
            'first_name': 'First',
            'last_name': '',
            'organization': '',
            'vat': '',
            'email': '*****@*****.**',
            'street': '',
            'post': '',
            'city': '',
            'phone': '',
        })

        member.setProperties(fullname='First Last', email="*****@*****.**")
        self.assertEqual(adapter.get_info('billing'), {
            'first_name': 'First',
            'last_name': 'Last',
            'organization': '',
            'vat': '',
            'email': '*****@*****.**',
            'street': '',
            'post': '',
            'city': '',
            'phone': '',
        })

        member.setProperties(fullname='First Middle Last', email="*****@*****.**")
        self.assertEqual(adapter.get_info('billing'), {
            'first_name': 'First',
            'last_name': 'Middle Last',
            'organization': '',
            'vat': '',
            'email': '*****@*****.**',
            'street': '',
            'post': '',
            'city': '',
            'phone': '',
        })

        names = ['city', 'last_name', 'first_name', 'email', 'phone', 'post', 'street']
        address = {}
        for name in names:
            address[name] = name.upper()
        adapter.get_address = mock.Mock(return_value=address)

        self.assertEqual(adapter.get_info('billing'), {
            'city': 'CITY',
            'email': 'EMAIL',
            'first_name': 'FIRST_NAME',
            'last_name': 'LAST_NAME',
            'organization': '',
            'phone': 'PHONE',
            'post': 'POST',
            'street': 'STREET',
            'vat': '',
        })
    def test_get_address(self):
        adapter = IShoppingSite(self.portal)
        self.assertIsNone(adapter.get_address('billing'))

        adapter.cart = mock.Mock(return_value={'billing': 'BILLING'})
        self.assertEqual(adapter.get_address('billing'), 'BILLING')
def notify_ordered(context, event):
    if event.action == 'ordered':
        shopping_site = IShoppingSite(context)
        adapter = IOrderAdapter(context)
        portal = shopping_site.portal()
        email_from_address = getUtility(IRegistry)['collective.cart.shopping.notification_cc_email'] or portal.getProperty('email_from_address')

        billing = shopping_site.get_address('billing')
        default_charset = getattr(getattr(getToolByName(context, 'portal_properties'), 'site_properties'), 'default_charset', 'utf-8')
        email_charset = getUtility(ISiteRoot).getProperty('email_charset', 'utf-8')
        subject = context.translate(_(u'order-number', u'Order Number: ${number}', mapping={'number': context.id}))
        utility = getUtility(IUnicodeUtility)
        mfrom = u'"{}" <{}>'.format(utility.safe_unicode(shopping_site.shop().title), email_from_address)
        host = getToolByName(context, 'MailHost')

        underline = '=' * 28
        billing_address = utility.address(billing)
        if shopping_site.billing_same_as_shipping():
            shipping_address = billing_address
        else:
            shipping = shopping_site.get_address('shipping')
            shipping_address = utility.address(shipping)
        articles = shopping_site.cart_article_listing()
        for article in articles:
            subtotal = article['gross'] * article['quantity']
            article.update({'subtotal': shopping_site.format_money(subtotal)})
        shipping_method_title = hasattr(
            adapter.shipping_method(), 'Title') and adapter.shipping_method().Title.decode(default_charset) or u''

        items = shopping_site.cart().copy()
        for key in ['articles', 'billing_same_as_shipping', 'shipping_method', 'billing', 'shipping']:
            if key in items:
                del items[key]

        items.update({
            'number': context.id,
            'underline': underline,
            'billing_address': billing_address,
            'shipping_address': shipping_address,
            'articles': articles,
            'shipping_method_title': shipping_method_title,
            'is_shipping_free': shopping_site.shipping_gross_money().amount == 0.0,
            'shipping_gross': shopping_site.locale_shipping_gross(),
            'total': shopping_site.locale_total(),
        })

        message_to_customer = context.unrestrictedTraverse('@@to-customer-order-mail-template')(**items)
        mto_customer = u'"{}" <{}>'.format(utility.fullname(billing), billing['email'])
        subject_to_customer = subject

        message_to_shop = context.unrestrictedTraverse('@@to-shop-order-mail-template')(**items)
        mto_shop = mfrom
        subject_to_shop = subject

        try:
            host.send(message_to_customer, mto_customer, mfrom, subject=subject_to_customer, charset=email_charset)
            host.send(message_to_shop, mto_shop, mfrom, subject=subject_to_shop, charset=email_charset)

        except:
            message = _(u'order-processed-but',
                default=u'The order was processed but we could not send e-mail to you successfully. Please consult the shop owner.')
            IStatusMessage(context.REQUEST).addStatusMessage(message, type='warn')