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" )
22+ NT_URL = os .environ .get ("NT_URL" , "http://localhost:8406/graphql" )
2023
2124USERS = {"alice" : "alice" , "bob" : "bob" }
2225
@@ -138,6 +141,9 @@ def cleanup_components() -> None:
138141
139142def create_users () -> None :
140143 for username , password in USERS .items ():
144+ if admin .users .get (username = username ):
145+ print (f">> Skipping { username } (exists)" )
146+ continue
141147 response , _ = admin .users .as_raw ().post ({"username" : username , "enabled" : True })
142148 user_id = response .headers ["Location" ].rsplit ("/" , 1 )[- 1 ]
143149 admin .users (user_id ).reset_password .put (
@@ -229,6 +235,7 @@ def create_user_service_account_client(
229235def create_clients () -> None :
230236 create_cli_client (client_id = "ixx-cli-blueapi" , aud = "ixx-blueapi" )
231237 create_cli_client (client_id = "tiled-cli" , aud = "tiled" )
238+ create_cli_client (client_id = "numtracker" , aud = "numtracker" )
232239 create_web_client (
233240 client_id = "ixx-blueapi" ,
234241 aud = "ixx-blueapi" ,
@@ -254,7 +261,42 @@ def create_clients() -> None:
254261 )
255262
256263
264+ def configure_numtracker ():
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+ access_token = response .json ().get ("access_token" )
276+
277+ response = requests .post (
278+ NT_URL ,
279+ json = {
280+ "query" : """mutation {
281+ configure(instrument: "adsim",
282+ config: {directory: "/tmp/",
283+ scan: "{instrument}-{scan_number}",
284+ detector: "{instrument}-{scan_number}-{detector}",
285+ scanNumber: 43}) {
286+ scanTemplate
287+ }
288+ }"""
289+ },
290+ headers = {"authorization" : "Bearer " + access_token },
291+ )
292+ response .raise_for_status ()
293+ if response .json ().get ("errors" ) is not None :
294+ raise Exception (response .json ())
295+ print (">> Configurated numtracker" )
296+
297+
257298if __name__ == "__main__" :
258299 cleanup_components ()
259300 create_users ()
260301 create_clients ()
302+ configure_numtracker ()
0 commit comments