def test_exceptions(self): """Method to test all exceptions.""" # Unprocessable Entity Exception with self.assertRaises(UnprocessableEntityError): raise UnprocessableEntityError( {'pointer': '/data/attributes/min-quantity'}, "min-quantity should be less than max-quantity", ) # Conflict Exception with self.assertRaises(ConflictError): raise ConflictError({'pointer': '/data/attributes/email'}, "Email already exists") # Forbidden Exception with self.assertRaises(ForbiddenError): raise ForbiddenError({'source': ''}, "Access Forbidden") # Not Found Error with self.assertRaises(NotFoundError): raise NotFoundError({'source': ''}, "Not Found") # Server Error with self.assertRaises(ServerError): raise ServerError({'source': ''}, "Internal Server Error") # Bad Request Error with self.assertRaises(BadRequestError): raise BadRequestError({'source': ''}, "Bad Request") # Method Not Allowed Exception with self.assertRaises(MethodNotAllowed): raise MethodNotAllowed({'source': ''}, "Method Not Allowed")
def before_post(self, args, kwargs, data): """ before post method to check for required relationship and proper permission :param args: :param kwargs: :param data: :return: """ require_relationship(['event'], data) if not has_access('is_coorganizer', event_id=data['event']): raise ForbiddenError({'source': ''}, 'Co-organizer access is required.') if (get_count( db.session.query(Event).filter_by(id=int(data['event']), is_tax_enabled=False)) > 0): raise MethodNotAllowed({'parameter': 'event_id'}, "Tax is disabled for this Event")
def before_get(self, args, kwargs): if kwargs.get('ticket_id'): if has_access('is_coorganizer'): ticket = safe_query_kwargs(Ticket, kwargs, 'ticket_id') if ticket.discount_code_id: kwargs['id'] = ticket.discount_code_id else: kwargs['id'] = None else: raise UnprocessableEntityError( {'source': ''}, "Please verify your permission. You must have coorganizer " "privileges to view ticket discount code details", ) if kwargs.get('event_id'): if has_access('is_admin'): event = safe_query_kwargs(Event, kwargs, 'event_id') if event.discount_code_id: kwargs['id'] = event.discount_code_id else: kwargs['id'] = None else: raise UnprocessableEntityError( {'source': ''}, "Please verify your permission. You must be admin to view event discount code details", ) if kwargs.get('event_identifier'): event = safe_query_kwargs(Event, kwargs, 'event_identifier', 'identifier') kwargs['event_id'] = event.id if kwargs.get('discount_event_identifier'): event = safe_query( Event, 'identifier', kwargs['discount_event_identifier'], 'event_identifier', ) kwargs['discount_event_id'] = event.id if kwargs.get('event_id') and has_access('is_admin'): event = safe_query_kwargs(Event, kwargs, 'event_id') if event.discount_code_id: kwargs['id'] = event.discount_code_id else: kwargs['id'] = None # Any registered user can fetch discount code details using the code. if kwargs.get('code'): # filter on deleted_at is required to catch the id of a # discount code which has not been deleted. discount = ( db.session.query(DiscountCode) .filter_by( code=kwargs.get('code'), event_id=kwargs.get('discount_event_id'), deleted_at=None, ) .first() ) if discount: kwargs['id'] = discount.id discount_tz = discount.valid_from.tzinfo current_time = datetime.now().replace(tzinfo=discount_tz) if not discount.is_active: raise MethodNotAllowed( {'parameter': '{code}'}, "Discount Code is not active" ) if ( current_time < discount.valid_from or current_time > discount.valid_till ): raise MethodNotAllowed( {'parameter': '{code}'}, "Discount Code is not active in current time frame", ) else: raise ObjectNotFound({'parameter': '{code}'}, "DiscountCode: not found") self.schema = DiscountCodeSchemaTicket return if kwargs.get('id'): try: discount = ( db.session.query(DiscountCode).filter_by(id=kwargs.get('id')).one() ) except NoResultFound: raise ObjectNotFound({'parameter': '{id}'}, "DiscountCode: not found") # if discount.used_for == 'ticket' and has_access('is_coorganizer', event_id=discount.event_id): if discount.used_for == 'ticket': self.schema = DiscountCodeSchemaTicket # elif discount.used_for == 'event' and has_access('is_admin'): elif discount.used_for == 'event': self.schema = DiscountCodeSchemaEvent else: raise UnprocessableEntityError( {'source': ''}, "Please verify your permission" )