1717 */
1818package de .fraunhofer .iosb .ilt .statests .f01auth ;
1919
20+ import static de .fraunhofer .iosb .ilt .statests .TestSuite .KEY_DB_NAME ;
21+ import static de .fraunhofer .iosb .ilt .statests .util .EntityUtils .filterForException ;
22+ import static de .fraunhofer .iosb .ilt .statests .util .EntityUtils .testFilterResults ;
23+
2024import dasniko .testcontainers .keycloak .KeycloakContainer ;
2125import de .fraunhofer .iosb .ilt .frostclient .SensorThingsService ;
26+ import de .fraunhofer .iosb .ilt .frostclient .model .Entity ;
27+ import de .fraunhofer .iosb .ilt .frostclient .models .SensorThingsSensingV11 ;
2228import de .fraunhofer .iosb .ilt .frostclient .utils .TokenManagerOpenIDConnect ;
2329import de .fraunhofer .iosb .ilt .statests .ServerVersion ;
2430import de .fraunhofer .iosb .ilt .statests .TestSuite ;
31+ import java .io .IOException ;
32+ import java .net .MalformedURLException ;
33+ import java .net .URL ;
34+ import java .util .ArrayList ;
2535import java .util .LinkedHashMap ;
36+ import java .util .List ;
2637import java .util .Map ;
38+ import org .apache .commons .io .IOUtils ;
39+ import org .junit .jupiter .api .Test ;
2740import org .slf4j .Logger ;
2841import org .slf4j .LoggerFactory ;
2942
@@ -45,11 +58,47 @@ public abstract class KeyCloakTests extends AbstractAuthTests {
4558 public static final String KEYCLOAK_FROST_CONFIG_SECRET = "5aa9087d-817f-47b6-92a1-2b5f7caac967" ;
4659 public static final String KEYCLOAK_TOKEN_PATH = "/realms/FROST-Test/protocol/openid-connect/token" ;
4760
61+ private static final SensorThingsSensingV11 mdlSensing = new SensorThingsSensingV11 ();
62+ private static final SensorThingsUserModel mdlUsers = new SensorThingsUserModel ();
63+ private static final SensorThingsService baseService = new SensorThingsService (mdlSensing , mdlUsers );
64+ private static final List <Entity > USERS = new ArrayList <>();
65+
66+ private static String modelUrl (String name ) {
67+ return resourceUrl ("finegrainedsecurity/model/" , name );
68+ }
69+
70+ private static String resourceUrl (String path , String name ) {
71+ try {
72+ return IOUtils .resourceToURL (path + "/" + name , KeyCloakTests .class .getClassLoader ()).getFile ();
73+ } catch (IOException ex ) {
74+ LOGGER .error ("Failed" , ex );
75+ return "" ;
76+ }
77+ }
78+
4879 static {
80+ final String dbName = "keycloakauth" ;
81+ SERVER_PROPERTIES .put ("auth.db.url" , TestSuite .createDbUrl (dbName ));
82+ SERVER_PROPERTIES .put ("auth.db.driver" , "org.postgresql.Driver" );
83+ SERVER_PROPERTIES .put ("auth.db.username" , TestSuite .VAL_PG_USER );
84+ SERVER_PROPERTIES .put ("auth.db.password" , TestSuite .VAL_PG_PASS );
85+ SERVER_PROPERTIES .put (KEY_DB_NAME , dbName );
86+
4987 SERVER_PROPERTIES .put ("auth_provider" , "de.fraunhofer.iosb.ilt.frostserver.auth.keycloak.KeycloakAuthProvider" );
5088 SERVER_PROPERTIES .put ("auth_keycloakConfigUrl" , TestSuite .getInstance ().getKeycloak ().getAuthServerUrl () + "/realms/FROST-Test/clients-registrations/install/" + KEYCLOAK_FROST_CLIENT_ID );
5189 SERVER_PROPERTIES .put ("auth_keycloakConfigSecret" , KEYCLOAK_FROST_CONFIG_SECRET );
5290 SERVER_PROPERTIES .put ("auth_allowAnonymousRead" , "false" );
91+ SERVER_PROPERTIES .put ("auth_registerUserLocally" , "true" );
92+ SERVER_PROPERTIES .put ("plugins.coreModel.idType" , "LONG" );
93+ SERVER_PROPERTIES .put ("plugins.modelLoader.enable" , "true" );
94+ SERVER_PROPERTIES .put ("plugins.modelLoader.modelPath" , "" );
95+ SERVER_PROPERTIES .put ("plugins.modelLoader.modelFiles" , modelUrl ("Role.json" ) + ", " + modelUrl ("UserNoPass.json" ));
96+ SERVER_PROPERTIES .put ("plugins.modelLoader.liquibasePath" , "target/test-classes/finegrainedsecurity/liquibase" );
97+ SERVER_PROPERTIES .put ("plugins.modelLoader.liquibaseFiles" , "tablesSecurityUPR.xml" );
98+ SERVER_PROPERTIES .put ("plugins.modelLoader.idType.Role" , "STRING" );
99+ SERVER_PROPERTIES .put ("plugins.modelLoader.idType.User" , "STRING" );
100+ SERVER_PROPERTIES .put ("persistence.idGenerationMode.Role" , "ClientGeneratedOnly" );
101+ SERVER_PROPERTIES .put ("persistence.idGenerationMode.User" , "ClientGeneratedOnly" );
53102 }
54103
55104 public KeyCloakTests (ServerVersion version ) {
@@ -59,7 +108,29 @@ public KeyCloakTests(ServerVersion version) {
59108 @ Override
60109 protected void setUpVersion () {
61110 LOGGER .info ("Setting up for version {}." , version .urlPart );
111+ sMdl = mdlSensing ;
62112 super .setUpVersion ();
113+ USERS .clear ();
114+ USERS .add (mdlUsers .newUser ("c8e84639-9914-4b1e-b756-349afed255f6" , null ));
115+ USERS .add (mdlUsers .newUser ("1d6b3bb2-a869-4686-b781-c1ea481e6085" , null ));
116+ USERS .add (mdlUsers .newUser ("74fe01f1-2ecc-4696-87f0-340ee3fe1a86" , null ));
117+ }
118+
119+ @ Test
120+ void test_100_ReadUser () {
121+ LOGGER .info (" test_100_ReadUser" );
122+ testFilterResults (serviceAdmin , mdlUsers .etUser , "" , USERS );
123+ filterForException (serviceAnon , mdlUsers .etUser , "" , AuthTestHelper .HTTP_CODE_403_FORBIDDEN );
124+ }
125+
126+ @ Override
127+ protected SensorThingsService createService () {
128+ try {
129+ return new SensorThingsService (baseService .getModelRegistry ())
130+ .setEndpoint (new URL (serverSettings .getServiceUrl (version )));
131+ } catch (MalformedURLException ex ) {
132+ throw new IllegalArgumentException ("Serversettings contains malformed URL." , ex );
133+ }
63134 }
64135
65136 @ Override
0 commit comments