def test_invalid_id(self): self._add_identity_for_users() with raises(ProgrammingError): CreateAccountFlow('-1', self.bankcard.id_) with raises(ProgrammingError): CreateAccountFlow(self.user.id_, '-1')
def verify_bankcard(bankcard_id): """零钱包绑卡 :request: :class:`.BankcardRequestSchema` :reqheader Authorization: OAuth 2.0 Bearer Token :status 200: 绑定或更新成功, 返回 :class:`.BankCardSchema` :status 403: 绑定被拒 """ bankcard = obtain_bankcard(bankcard_id) bankcard_schema = BankCardSchema(strict=True) flow = CreateAccountFlow(request.oauth.user.id_, bankcard.id_) bankcard_request_schema = BankcardRequestSchema(strict=True) result = bankcard_request_schema.load(request.get_json()) sms_code = result.data.get('sms_code') if sms_code: try: if flow.need_to_create(): flow.create_account(sms_code) else: flow.bind_bankcard(sms_code) except BusinessError as e: if e.kind is BusinessError.kinds.sms_code_incorrect: abort(403, SMS_CODE_INCORECT_FOR_BINDING) raise else: flow.send_sms(SMS_BINDING_BANKCARD, bankcard=bankcard) return jsonify(success=True, data=bankcard_schema.dump(bankcard).data)
def verify_bankcard(bankcard_id): """零钱包绑卡.""" form = VerifyForm() form.raise_for_validation() bankcard = obtain_bankcard(bankcard_id) flow = CreateAccountFlow(g.user.id_, bankcard.id_) if form.sms_code.data: try: if flow.need_to_create(): flow.create_account(form.sms_code.data) else: flow.bind_bankcard(form.sms_code.data) except BusinessError as e: if form.is_new.data: g.bankcards.remove(bankcard.card_number, silent=True) if e.kind is BusinessError.kinds.sms_code_incorrect: return jsonify(r=False, error=SMS_CODE_INCORECT_FOR_BINDING) raise else: flow.send_sms(SMS_BINDING_BANKCARD, bankcard=bankcard) bind_cards = [ card for card in g.bankcards.get_all() if is_bound_bankcard(card, zhongshan) ] return make_bankcard_response(g.bankcards, g.wallet_provider.bank_partner, g.user.id_, bind_cards)
def test_send_sms(self, zslib, uuid): self._add_identity_for_users() flow = CreateAccountFlow(self.user.id_, self.bankcard.id_) assert flow.send_sms('dummy %s') is zslib.send_sms.return_value zslib.send_sms.assert_called_once_with( transaction_id=uuid.uuid4.return_value.hex, user_id=flow.wallet_account.secret_id, mobile_phone=self.bankcard.mobile_phone, template='dummy %s')
def test_bankcard_checking(self): self._add_identity_for_users() # 深发银行还没有任何产品线支持, 正好用来测试 invalid_bankcard = self.add_bankcard(self.user.id_, '10010', card_number='6222980000010001') with raises(UnsupportedBankError): CreateAccountFlow(self.user.id_, invalid_bankcard.id_) assert CreateAccountFlow(self.user.id_, self.bankcard.id_)
def test_real_identity_checking(self): with raises(RealIdentityRequiredError): CreateAccountFlow(self.user.id_, self.bankcard.id_) with raises(RealIdentityRequiredError): CreateAccountFlow(self.user_with_email.id_, self.bankcard.id_) self._add_identity_for_users() assert CreateAccountFlow(self.user.id_, self.bankcard.id_) with raises(RealIdentityRequiredError): CreateAccountFlow(self.user_with_email.id_, self.bankcard.id_)
def test_create_failure(self, zslib, uuid): self._add_identity_for_users() class TestingException(Exception): pass # calls with exception zslib.create_account.side_effect = TestingException() flow = CreateAccountFlow(self.user.id_, self.bankcard.id_) with raises(TestingException): flow.create_account('10010') assert zslib.create_account.called # check failure wallet_account = WalletAccount.get_by_local_account( self.user, flow.provider) assert wallet_account.status is WalletAccount.Status.failure assert flow.need_to_create() # retry zslib.create_account.side_effect = None wallet_account = flow.create_account('10010') assert wallet_account.status is WalletAccount.Status.success assert not flow.need_to_create()
def test_create_account(self, zslib, uuid): self._add_identity_for_users() flow = CreateAccountFlow(self.user.id_, self.bankcard.id_) assert flow.need_to_create() wallet_account = flow.create_account('10010') zslib.create_account.assert_called_once_with( transaction_id=uuid.uuid4.return_value.hex, user_id=wallet_account.secret_id, bank_id=self.bankcard.bank.zslib_id, card_number=self.bankcard.card_number, person_name=self.identity.person_name, person_ricn=self.identity.person_ricn, mobile_phone=self.bankcard.mobile_phone, password=wallet_account.secret_token, sms_code='10010') assert wallet_account.status is WalletAccount.Status.success assert not flow.need_to_create() zslib.create_account.reset_mock() wallet_account = flow.create_account('10010') assert not zslib.create_account.called