def test_charter_sign_minet(client, sample_member): r = client.post( f"{base_url}{1}/member/{sample_member.id}", headers=TEST_HEADERS, ) assert r.status_code == 201 assert db.session().execute( select(Adherent.datesignedminet).where( Adherent.id == sample_member.id)).scalar_one()
def wrapper(*args,**kwargs): """ Wrap http_api function. """ s = session_handler.session() if session_handler else db.session() if "token_info" not in connexion.context: LOG.warning('could_not_extract_token_info_kwargs') raise UnauthenticatedError("Not token informations") token_info = connexion.context["token_info"] testing = current_app.config["TESTING"] ctx = build_context( session=s, testing=testing, request_id=str(uuid.uuid4()), # Generates an unique ID on this request so we can track it. url=request.url, admin=token_info.get("uid", ""), roles=token_info.get("scope", []) ) kwargs["ctx"] = ctx try: result = f(*args, **kwargs) # It makes things clearer and less error-prone. if not isinstance(result, tuple) or len(result) <= 1: raise ValueError("Please always pass the result AND the HTTP code.") status_code = result[1] msg = result[0] if result[0] == NoContent: msg = None if status_code and (200 <= status_code <= 299 or status_code == 302): s.commit() else: LOG.info("rollback_sql_transaction_non_200_http_code", extra=log_extra(ctx, code=status_code, message=msg)) s.rollback() return result except Exception as e: LOG.error("rollback_sql_transaction_exception_caught", extra=log_extra(ctx, exception=str(e), traceback=traceback.format_exc())) s.rollback() raise finally: # When running unit tests, we don't close the session so tests can actually perform some work on that # session. if not testing: s.close()
def find( self, method: Union[AuthenticationMethod, None] = None, identifiers: Union[List[str], None] = None, roles: Union[List[Roles], None] = None) -> Tuple[List[RoleMapping], int]: smt: Select = select(AuthenticationRoleMapping) if method is not None: smt = smt.where(AuthenticationRoleMapping.authentication == method) if identifiers is not None: smt = smt.where( AuthenticationRoleMapping.identifier.in_(identifiers)) if roles is not None: smt = smt.where(AuthenticationRoleMapping.role.in_(roles)) all_roles = db.session().execute(smt).all() return [self._map_to_role_mapping(i[0]) for i in set(all_roles)], len(all_roles)
def available_ip(self, ctx, ip_range: str = "", member_id: Union[int, None] = None) -> str: if ip_range == "": return 'En attente' try: network = ip_network(ip_range) except AddressValueError: raise BadSubnetError("Unknown ip subnet") if isinstance(network, IPv4Network): smt = select(Device.ip).where((Device.ip != None) & (Device.ip != "En attente")) # @TODO retrocompatibilité ADH5, à retirer à terme) else: smt = select(Device.ipv6).where((Device.ipv6 != None) & (Device.ipv6 != "En attente")) # @TODO retrocompatibilité ADH5, à retirer à terme) if member_id: smt = smt.where(Device.adherent_id == member_id) ips = db.session().execute(smt).scalars().all() for i, h in enumerate(network.hosts()): if i == 0: continue if str(h) not in ips: return str(h) raise NoMoreIPAvailableException(ip_range)
def get(self, id: int) -> Union[RoleMapping, None]: smt: Select = select(AuthenticationRoleMapping).where( AuthenticationRoleMapping.id == id) return db.session().execute(smt).one_or_none()