def test_unknown_payment_method( self, ctx, mock_membership_repository: MembershipRepository, mock_member_repository: MemberRepository, mock_charter_repository: CharterRepository, mock_account_repository: AccountRepository, mock_payment_method_repository: PaymentMethodRepository, sample_member: Member, sample_subscription_duration_account_payment_method: SubscriptionBody, sample_membership_pending_rules: Membership, sample_account1: Account, member_manager: MemberManager): mock_member_repository.get_by_id = MagicMock( return_value=(sample_member)) mock_membership_repository.search = MagicMock( return_value=([sample_membership_pending_rules], 1)) mock_account_repository.get_by_id = MagicMock( return_value=(sample_account1)) mock_charter_repository.get = MagicMock( return_value=str(datetime.datetime.today())) mock_payment_method_repository.get_by_id = MagicMock( return_value=(None), side_effect=PaymentMethodNotFoundError("")) with raises(PaymentMethodNotFoundError): member_manager.update_subscription( ctx, sample_member.id, sample_subscription_duration_account_payment_method) mock_membership_repository.search.assert_called_once() mock_member_repository.get_by_id.assert_called_once() mock_account_repository.get_by_id.assert_called_once() mock_charter_repository.get.assert_called_once() mock_payment_method_repository.get_by_id.assert_called_once() mock_membership_repository.update.assert_not_called()
def get_by_id(self, ctx, object_id: int) -> PaymentMethod: session: Session = ctx.get(CTX_SQL_SESSION) obj = session.query(SQLPaymentMethod).filter( SQLPaymentMethod.id == object_id).one_or_none() if obj is None: raise PaymentMethodNotFoundError(object_id) return _map_payment_method_sql_to_entity(obj)
def test_payment_method_not_found( self, ctx, mock_payment_method_repository: PaymentMethodRepository, product_manager: ProductManager): mock_payment_method_repository.get_by_id = MagicMock( return_value=(None), side_effect=PaymentMethodNotFoundError("0")) with pytest.raises(PaymentMethodNotFoundError): product_manager.buy(ctx, 0, 0, [1]) mock_payment_method_repository.get_by_id.assert_called_once_with( ctx, 0)
def create(self, ctx, abstract_transaction: AbstractTransaction) -> object: session: Session = ctx.get(CTX_SQL_SESSION) now = datetime.now() admin_id = ctx.get(CTX_ADMIN) account_src = None if abstract_transaction.src is not None: account_src = session.query(Account).filter( Account.id == abstract_transaction.src).one_or_none() if not account_src: raise AccountNotFoundError(abstract_transaction.src) account_dst = None if abstract_transaction.dst is not None: account_dst = session.query(Account).filter( Account.id == abstract_transaction.dst).one_or_none() if not account_dst: raise AccountNotFoundError(abstract_transaction.dst) method = None if abstract_transaction.payment_method is not None: method = session.query(PaymentMethod).filter( PaymentMethod.id == abstract_transaction.payment_method).one_or_none() if not method: raise PaymentMethodNotFoundError( abstract_transaction.payment_method) transaction = SQLTransaction( src=account_src.id if account_src else None, dst=account_dst.id if account_dst else None, value=abstract_transaction.value, name=abstract_transaction.name, timestamp=now, attachments="", type=method.id if method else None, author_id=admin_id, pending_validation=abstract_transaction.pending_validation if abstract_transaction.pending_validation else False) with track_modifications(ctx, session, transaction): session.add(transaction) session.flush() return _map_transaction_sql_to_entity(transaction)
def update_subscription(self, ctx, member_id: int, body: SubscriptionBody) -> None: member = self.member_repository.get_by_id(ctx, member_id) if not member: raise MemberNotFoundError(member_id) subscription = self.latest_subscription(ctx=ctx, member_id=member_id) if not subscription: raise MembershipNotFoundError() if subscription.status in [ MembershipStatus.COMPLETE, MembershipStatus.CANCELLED, MembershipStatus.ABORTED ]: raise MembershipStatusNotAllowed( subscription.status, "membership already completed, cancelled or aborted") state = MembershipStatus(subscription.status) if state == MembershipStatus.PENDING_RULES: date_signed_minet = self.charter_repository.get( ctx, member_id=member_id, charter_id=1) if date_signed_minet is not None and date_signed_minet != "": LOG.debug( "create_membership_record_switch_status_to_pending_payment_initial" ) state = MembershipStatus.PENDING_PAYMENT_INITIAL else: raise CharterNotSigned(str(member_id)) if body.duration is not None and body.duration != 0: if body.duration not in self.duration_price: LOG.warning("create_membership_record_no_price_defined", extra=log_extra(ctx, duration=body.duration)) raise NoPriceAssignedToThatDuration(body.duration) if state == MembershipStatus.PENDING_PAYMENT_INITIAL: if body.duration is not None: LOG.debug( "create_membership_record_switch_status_to_pending_payment" ) state = MembershipStatus.PENDING_PAYMENT if body.account is not None: account = self.account_repository.get_by_id(ctx, body.account) if not account: raise AccountNotFoundError(body.account) if body.payment_method is not None: payment_method = self.payment_method_repository.get_by_id( ctx, body.payment_method) if not payment_method: raise PaymentMethodNotFoundError(body.payment_method) if state == MembershipStatus.PENDING_PAYMENT: if body.account is not None and body.payment_method is not None: LOG.debug( "create_membership_record_switch_status_to_pending_payment_validation" ) state = MembershipStatus.PENDING_PAYMENT_VALIDATION try: self.membership_repository.update(ctx, subscription.uuid, body, state) except Exception: raise
def create_subscription(self, ctx, member_id: int, body: SubscriptionBody) -> Membership: """ Core use case of ADH. Registers a membership. User story: As an admin, I can create a new membership record, so that a member can have internet access. :param ctx: context :param member_id: member_id :param membership: entity AbstractMembership :raise IntMustBePositiveException :raise NoPriceAssignedToThatDurationException :raise MemberNotFound :raise UnknownPaymentMethod """ member = self.member_repository.get_by_id(ctx, member_id) if not member: raise MemberNotFoundError(member_id) latest_subscription = self.latest_subscription(ctx=ctx, member_id=member_id) if latest_subscription and latest_subscription.status not in [ MembershipStatus.COMPLETE.value, MembershipStatus.CANCELLED.value, MembershipStatus.ABORTED.value ]: raise MembershipAlreadyExist(latest_subscription.status) state = MembershipStatus.PENDING_RULES if state == MembershipStatus.PENDING_RULES: date_signed_minet = self.charter_repository.get( ctx, member_id=member_id, charter_id=1) if date_signed_minet is not None and date_signed_minet != "": LOG.debug( "create_membership_record_switch_status_to_pending_payment_initial" ) state = MembershipStatus.PENDING_PAYMENT_INITIAL if state == MembershipStatus.PENDING_PAYMENT_INITIAL: if body.duration is not None and body.duration != 0: if body.duration not in self.duration_price: LOG.warning("create_membership_record_no_price_defined", extra=log_extra(ctx, duration=body.duration)) raise NoPriceAssignedToThatDuration(body.duration) LOG.debug( "create_membership_record_switch_status_to_pending_payment" ) state = MembershipStatus.PENDING_PAYMENT if state == MembershipStatus.PENDING_PAYMENT: if body.account is not None and body.payment_method is not None: account = self.account_repository.get_by_id(ctx, body.account) if not account: raise AccountNotFoundError(body.account) payment_method = self.payment_method_repository.get_by_id( ctx, body.payment_method) if not payment_method: raise PaymentMethodNotFoundError(body.payment_method) LOG.debug( "create_membership_record_switch_status_to_pending_payment_validation" ) state = MembershipStatus.PENDING_PAYMENT_VALIDATION try: membership_created = self.membership_repository.create( ctx, body, state) except UnknownPaymentMethod: LOG.warning("create_membership_record_unknown_payment_method", extra=log_extra(ctx, payment_method=body.payment_method)) raise LOG.info("create_membership_record", extra=log_extra(ctx, membership_uuis=membership_created.uuid, membership_status=membership_created.status)) return membership_created