22# requires-python = ">=3.11"
33# dependencies = [
44# "mantelo==2.2.1",
5+ # "requests"
56# ]
67# ///
78"""Configure the local Keycloak instance used by the system tests."""
1112from functools import wraps
1213from typing import Any
1314
15+ import requests
1416from mantelo import KeycloakAdmin
1517
16- SERVER = os .environ .get ("KEYCLOAK_SERVER" )
17- REALM = os .environ .get ("KEYCLOAK_REALM" )
18- ADMIN_USERNAME = os .environ .get ("KC_BOOTSTRAP_ADMIN_USERNAME" )
19- ADMIN_PASSWORD = os .environ .get ("KC_BOOTSTRAP_ADMIN_PASSWORD" )
18+ SERVER = os .environ .get ("KEYCLOAK_SERVER" , "http://localhost:8081" )
19+ REALM = os .environ .get ("KEYCLOAK_REALM" , "master" )
20+ ADMIN_USERNAME = os .environ .get ("KC_BOOTSTRAP_ADMIN_USERNAME" , "admin" )
21+ ADMIN_PASSWORD = os .environ .get ("KC_BOOTSTRAP_ADMIN_PASSWORD" , "admin" )
2022
2123USERS = {"alice" : "alice" , "bob" : "bob" }
2224
@@ -138,6 +140,9 @@ def cleanup_components() -> None:
138140
139141def create_users () -> None :
140142 for username , password in USERS .items ():
143+ if admin .users .get (username = username ):
144+ print (f">> Skipping { username } (exists)" )
145+ continue
141146 response , _ = admin .users .as_raw ().post ({"username" : username , "enabled" : True })
142147 user_id = response .headers ["Location" ].rsplit ("/" , 1 )[- 1 ]
143148 admin .users (user_id ).reset_password .put (
@@ -229,6 +234,7 @@ def create_user_service_account_client(
229234def create_clients () -> None :
230235 create_cli_client (client_id = "ixx-cli-blueapi" , aud = "ixx-blueapi" )
231236 create_cli_client (client_id = "tiled-cli" , aud = "tiled" )
237+ create_cli_client (client_id = "numtracker" , aud = "numtracker" )
232238 create_web_client (
233239 client_id = "ixx-blueapi" ,
234240 aud = "ixx-blueapi" ,
@@ -254,7 +260,44 @@ def create_clients() -> None:
254260 )
255261
256262
263+ def configure_numtracker ():
264+ def _ ():
265+ token_url = SERVER + "/realms/master/protocol/openid-connect/token"
266+ response = requests .post (
267+ token_url ,
268+ data = {
269+ "client_id" : "system-test-blueapi-admin" ,
270+ "client_secret" : "secret" ,
271+ "grant_type" : "client_credentials" ,
272+ },
273+ )
274+ response .raise_for_status ()
275+ return response .json ().get ("access_token" )
276+
277+ nt_url = os .environ .get ("NT_URL" , "http://localhost:8406/graphql" )
278+ response = requests .post (
279+ str (nt_url ),
280+ json = {
281+ "query" : """mutation {
282+ configure(instrument: "adsim",
283+ config: {directory: "/tmp/",
284+ scan: "{instrument}-{scan_number}",
285+ detector: "{instrument}-{scan_number}-{detector}",
286+ scanNumber: 43}) {
287+ scanTemplate
288+ }
289+ }"""
290+ },
291+ headers = {"authorization" : "Bearer " + _ ()},
292+ )
293+ response .raise_for_status ()
294+ if response .json ().get ("errors" ) is not None :
295+ raise Exception (response .json ())
296+ print (">> Configurated numtracker" )
297+
298+
257299if __name__ == "__main__" :
258300 cleanup_components ()
259301 create_users ()
260302 create_clients ()
303+ configure_numtracker ()
0 commit comments