def test_wrong_key(self): tmp = tempfile.NamedTemporaryFile(mode='w', delete=False) tmp.write('some secret') tmp.close() self.addCleanup(lambda: os.unlink(tmp.name)) with self.settings(AES_KEYS={'bango:signature': tmp.name}): sig = sign('123') assert not verify_sig(sig, '123')
def obj_create(self, bundle, request, **kwargs): form = CreateBillingConfigurationForm(bundle.data) if not form.is_valid(): raise self.form_errors(form) client = get_client() billing = client.client('billing') data = form.bango_data types = billing.factory.create('ArrayOfString') for f in PAYMENT_TYPES: types.string.append(f) data['typeFilter'] = types price_list = billing.factory.create('ArrayOfPrice') for item in form.cleaned_data['prices']: price = billing.factory.create('Price') price.amount = item.cleaned_data['amount'] price.currency = item.cleaned_data['currency'] price_list.Price.append(price) data['priceList'] = price_list config = billing.factory.create('ArrayOfBillingConfigurationOption') configs = { 'APPLICATION_CATEGORY_ID': '18', 'APPLICATION_SIZE_KB': 2, 'BILLING_CONFIGURATION_TIME_OUT': 120, 'REDIRECT_URL_ONSUCCESS': data.pop('redirect_url_onsuccess'), 'REDIRECT_URL_ONERROR': data.pop('redirect_url_onerror'), 'REQUEST_SIGNATURE': sign(data['externalTransactionId']), } for k, v in configs.items(): opt = billing.factory.create('BillingConfigurationOption') opt.configurationOptionName = k opt.configurationOptionValue = v config.BillingConfigurationOption.append(opt) data['configurationOptions'] = config resp = self.client('CreateBillingConfiguration', data) bundle.data = {'responseCode': resp.responseCode, 'responseMessage': resp.responseMessage, 'billingConfigurationId': resp.billingConfigurationId} create_data = data.copy() create_data['transaction_uuid'] = data.pop('externalTransactionId') create.send(sender=self, bundle=bundle, data=create_data, form=form) return bundle
def test_cannot_sign_non_ascii(self): sign(u'Ivan Krsti\u0107')
def test_sign_unicode(self): sig = sign('123') assert verify_sig(sig, u'123')
def test_sign(self): sig = sign('123') assert verify_sig(sig, '123')
def prepare(form, bango): data = form.bango_data # Add in the Bango number from the serializer. data['bango'] = bango # Used to create the approprate data structure. client = get_client() billing = client.client('billing') price_list = billing.factory.create('ArrayOfPrice') price_types = set() for item in form.cleaned_data['prices']: price = billing.factory.create('Price') price.amount = item.cleaned_data['price'] price.currency = item.cleaned_data['currency'] price_types.add(item.cleaned_data['method']) # TODO: remove this. # Very temporary and very fragile hack to fix bug 882183. # Bango cannot accept regions with price info so if there # are two USD values for different regions it triggers a 500 error. append = True for existing in price_list.Price: if existing.currency == price.currency: log.info('Skipping %s:%s because we already have %s:%s' % (price.currency, price.amount, existing.currency, existing.amount)) append = False break if append: price_list.Price.append(price) data['priceList'] = price_list # More workarounds for bug 882321, ideally we'd send one type per # region, price, combination. If all the prices say operator, then # we'll set it to that. Otherwise its all. type_filters = PAYMENT_TYPES if price_types == set([str(PAYMENT_METHOD_OPERATOR)]): type_filters = MICRO_PAYMENT_TYPES types = billing.factory.create('ArrayOfString') for f in type_filters: types.string.append(f) data['typeFilter'] = types config = billing.factory.create('ArrayOfBillingConfigurationOption') configs = { 'APPLICATION_CATEGORY_ID': '18', 'APPLICATION_SIZE_KB': data.pop('application_size'), # Tell Bango to use our same transaction expiry logic. # However, we pad it by 60 seconds to show a prettier Mozilla user # error in the case of a real timeout. 'BILLING_CONFIGURATION_TIME_OUT': settings.TRANSACTION_EXPIRY + 60, 'REDIRECT_URL_ONSUCCESS': data.pop('redirect_url_onsuccess'), 'REDIRECT_URL_ONERROR': data.pop('redirect_url_onerror'), 'REQUEST_SIGNATURE': sign(data['externalTransactionId']), } user_uuid = data.pop('user_uuid') if settings.SEND_USER_ID_TO_BANGO: configs['MOZ_USER_ID'] = user_uuid log.info('Sending MOZ_USER_ID {uuid} for transaction {tr}' .format(uuid=user_uuid, tr=data['externalTransactionId'])) if settings.BANGO_ICON_URLS: icon_url = data.pop('icon_url', None) if icon_url: configs['APPLICATION_LOGO_URL'] = icon_url for k, v in configs.items(): opt = billing.factory.create('BillingConfigurationOption') opt.configurationOptionName = k opt.configurationOptionValue = v config.BillingConfigurationOption.append(opt) data['configurationOptions'] = config return data
def obj_create(self, bundle, request, **kwargs): form = CreateBillingConfigurationForm(bundle.data) if not form.is_valid(): raise self.form_errors(form) client = get_client() billing = client.client('billing') data = form.bango_data usd_price = None price_list = billing.factory.create('ArrayOfPrice') for item in form.cleaned_data['prices']: price = billing.factory.create('Price') price.amount = item.cleaned_data['amount'] price.currency = item.cleaned_data['currency'] if price.currency == 'USD': usd_price = Decimal(price.amount) price_list.Price.append(price) data['priceList'] = price_list if not usd_price: # This should never happen because USD is always part of the list. raise ValueError('Purchase for %r did not contain a USD price' % data.get('externalTransactionId')) if usd_price < settings.BANGO_MAX_MICRO_AMOUNT: type_filters = MICRO_PAYMENT_TYPES else: type_filters = PAYMENT_TYPES types = billing.factory.create('ArrayOfString') for f in type_filters: types.string.append(f) data['typeFilter'] = types config = billing.factory.create('ArrayOfBillingConfigurationOption') configs = { 'APPLICATION_CATEGORY_ID': '18', 'APPLICATION_SIZE_KB': 2, 'BILLING_CONFIGURATION_TIME_OUT': 120, 'REDIRECT_URL_ONSUCCESS': data.pop('redirect_url_onsuccess'), 'REDIRECT_URL_ONERROR': data.pop('redirect_url_onerror'), 'REQUEST_SIGNATURE': sign(data['externalTransactionId']), } if settings.BANGO_ICON_URLS: icon_url = data.pop('icon_url', None) if icon_url: configs['APPLICATION_LOGO_URL'] = icon_url for k, v in configs.items(): opt = billing.factory.create('BillingConfigurationOption') opt.configurationOptionName = k opt.configurationOptionValue = v config.BillingConfigurationOption.append(opt) data['configurationOptions'] = config resp = self.client('CreateBillingConfiguration', data) bundle.data = {'responseCode': resp.responseCode, 'responseMessage': resp.responseMessage, 'billingConfigurationId': resp.billingConfigurationId} create_data = data.copy() create_data['transaction_uuid'] = data.pop('externalTransactionId') create.send(sender=self, bundle=bundle, data=create_data, form=form) return bundle
def call(self, form): data = form.bango_data client = get_client() billing = client.client('billing') usd_price = None price_list = billing.factory.create('ArrayOfPrice') for item in form.cleaned_data['prices']: price = billing.factory.create('Price') price.amount = item.cleaned_data['price'] price.currency = item.cleaned_data['currency'] if price.currency == 'USD': usd_price = Decimal(price.amount) # TODO: remove this. # Very temporary and very fragile hack to fix bug 882183. # Bango cannot accept regions with price info so if there # are two USD values for different regions it triggers a 500 error. append = True for existing in price_list.Price: if existing.currency == price.currency: log.info('Skipping %s:%s because we already have %s:%s' % (price.currency, price.amount, existing.currency, existing.amount)) append = False break if append: price_list.Price.append(price) data['priceList'] = price_list if not usd_price: # This should never happen because USD is always part of the list. raise ValueError('Purchase for %r did not contain a USD price' % data.get('externalTransactionId')) if usd_price < settings.BANGO_MAX_MICRO_AMOUNT: type_filters = MICRO_PAYMENT_TYPES else: type_filters = PAYMENT_TYPES types = billing.factory.create('ArrayOfString') for f in type_filters: types.string.append(f) data['typeFilter'] = types config = billing.factory.create('ArrayOfBillingConfigurationOption') configs = { 'APPLICATION_CATEGORY_ID': '18', 'APPLICATION_SIZE_KB': data.pop('application_size'), # Tell Bango to use our same transaction expiry logic. # However, we pad it by 60 seconds to show a prettier Mozilla user # error in the case of a real timeout. 'BILLING_CONFIGURATION_TIME_OUT': settings.TRANSACTION_EXPIRY + 60, 'REDIRECT_URL_ONSUCCESS': data.pop('redirect_url_onsuccess'), 'REDIRECT_URL_ONERROR': data.pop('redirect_url_onerror'), 'REQUEST_SIGNATURE': sign(data['externalTransactionId']), } user_uuid = data.pop('user_uuid') if settings.SEND_USER_ID_TO_BANGO: configs['MOZ_USER_ID'] = user_uuid if settings.BANGO_ICON_URLS: icon_url = data.pop('icon_url', None) if icon_url: configs['APPLICATION_LOGO_URL'] = icon_url for k, v in configs.items(): opt = billing.factory.create('BillingConfigurationOption') opt.configurationOptionName = k opt.configurationOptionValue = v config.BillingConfigurationOption.append(opt) data['configurationOptions'] = config return self.client('CreateBillingConfiguration', data)
def prepare(form, bango): data = form.bango_data # Add in the Bango number from the serializer. data['bango'] = bango # Used to create the approprate data structure. client = get_client() billing = client.client('billing') price_list = billing.factory.create('ArrayOfPrice') price_types = set() for item in form.cleaned_data['prices']: price = billing.factory.create('Price') price.amount = item.cleaned_data['price'] price.currency = item.cleaned_data['currency'] price_types.add(item.cleaned_data['method']) # TODO: remove this. # Very temporary and very fragile hack to fix bug 882183. # Bango cannot accept regions with price info so if there # are two USD values for different regions it triggers a 500 error. append = True for existing in price_list.Price: if existing.currency == price.currency: log.info('Skipping %s:%s because we already have %s:%s' % (price.currency, price.amount, existing.currency, existing.amount)) append = False break if append: price_list.Price.append(price) data['priceList'] = price_list # More workarounds for bug 882321, ideally we'd send one type per # region, price, combination. If all the prices say operator, then # we'll set it to that. Otherwise its all. type_filters = PAYMENT_TYPES if price_types == set([str(PAYMENT_METHOD_OPERATOR)]): type_filters = MICRO_PAYMENT_TYPES types = billing.factory.create('ArrayOfString') for f in type_filters: types.string.append(f) data['typeFilter'] = types config = billing.factory.create('ArrayOfBillingConfigurationOption') configs = { 'APPLICATION_CATEGORY_ID': '18', 'APPLICATION_SIZE_KB': data.pop('application_size'), # Tell Bango to use our same transaction expiry logic. # However, we pad it by 60 seconds to show a prettier Mozilla user # error in the case of a real timeout. 'BILLING_CONFIGURATION_TIME_OUT': settings.TRANSACTION_EXPIRY + 60, 'REDIRECT_URL_ONSUCCESS': data.pop('redirect_url_onsuccess'), 'REDIRECT_URL_ONERROR': data.pop('redirect_url_onerror'), 'REQUEST_SIGNATURE': sign(data['externalTransactionId']), } user_uuid = data.pop('user_uuid') if settings.SEND_USER_ID_TO_BANGO: configs['MOZ_USER_ID'] = user_uuid log.info('Sending MOZ_USER_ID {uuid} for transaction {tr}'.format( uuid=user_uuid, tr=data['externalTransactionId'])) if settings.BANGO_ICON_URLS: icon_url = data.pop('icon_url', None) if icon_url: configs['APPLICATION_LOGO_URL'] = icon_url for k, v in configs.items(): opt = billing.factory.create('BillingConfigurationOption') opt.configurationOptionName = k opt.configurationOptionValue = v config.BillingConfigurationOption.append(opt) data['configurationOptions'] = config return data