From 1d2c88c971ae524a152bc13de5e5b60e58efd166 Mon Sep 17 00:00:00 2001 From: anhefti Date: Mon, 2 Oct 2023 12:03:39 +0200 Subject: [PATCH] SEBSERV-467 fix plus added authentication to LMSSetup test --- .../lms/impl/olat/OlatLmsAPITemplate.java | 17 +++++++++++++++++ .../lms/impl/olat/OlatLmsRestTemplate.java | 9 +++++++-- .../config/application-dev-ws.properties | 2 +- src/main/resources/messages.properties | 1 + .../api/admin/OlatLmsAPITemplateTest.java | 2 ++ 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsAPITemplate.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsAPITemplate.java index 917ba0bc..040942e8 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsAPITemplate.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsAPITemplate.java @@ -163,6 +163,23 @@ public class OlatLmsAPITemplate extends AbstractCachedCourseAccess implements Lm "lmsSetup:lmsClientsecret:notNull")); } + final Result restTemplateResult = getRestTemplate(); + if (restTemplateResult.hasError()) { + missingAttrs.add(APIMessage.fieldValidationError( + LMS_SETUP.ATTR_LMS_URL, + "lmsSetup:lmsUrl:url.noservice")); + } else { + final OlatLmsRestTemplate olatLmsRestTemplate = restTemplateResult.get(); + try { + olatLmsRestTemplate.testAuthentication(); + } catch (final Exception e) { + log.error("Failed to test Authentication: {}", e.getMessage()); + missingAttrs.add(APIMessage.fieldValidationError( + LMS_SETUP.ATTR_LMS_URL, + "lmsSetup:lmsUrl:url.noaccess")); + } + } + if (!missingAttrs.isEmpty()) { return LmsSetupTestResult.ofMissingAttributes(LmsType.OPEN_OLAT, missingAttrs); } diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsRestTemplate.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsRestTemplate.java index 533a3acc..5f0d3636 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsRestTemplate.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/olat/OlatLmsRestTemplate.java @@ -32,6 +32,10 @@ public class OlatLmsRestTemplate extends RestTemplate { private String token; private ClientCredentialsResourceDetails details; + public void testAuthentication() { + authenticate(); + } + public OlatLmsRestTemplate(final ClientCredentialsResourceDetails details) { super(); this.details = details; @@ -45,7 +49,6 @@ public class OlatLmsRestTemplate extends RestTemplate { final ClientHttpRequestExecution execution) throws IOException { try { - // if there's no token, authenticate first if (OlatLmsRestTemplate.this.token == null) { authenticate(); @@ -59,6 +62,7 @@ public class OlatLmsRestTemplate extends RestTemplate { return execution.execute(request, body); } + // otherwise, add the X-OLAT-TOKEN request.getHeaders().set("accept", "application/json"); request.getHeaders().set("X-OLAT-TOKEN", OlatLmsRestTemplate.this.token); @@ -85,6 +89,7 @@ public class OlatLmsRestTemplate extends RestTemplate { log.debug("OLAT [retry API call]: URL {}", request.getURI()); } + response.close(); response = execution.execute(request, body); if (log.isDebugEnabled()) { @@ -97,7 +102,7 @@ public class OlatLmsRestTemplate extends RestTemplate { } catch (final Exception e) { // TODO find a way to better deal with Olat temporary unavailability - log.error("Unexpected error: ", e); + log.error("Unexpected error: {}", e.getMessage()); throw e; } } diff --git a/src/main/resources/config/application-dev-ws.properties b/src/main/resources/config/application-dev-ws.properties index b49bad3b..d2a040b4 100644 --- a/src/main/resources/config/application-dev-ws.properties +++ b/src/main/resources/config/application-dev-ws.properties @@ -25,7 +25,7 @@ sebserver.webservice.clean-db-on-startup=false # webservice configuration sebserver.init.adminaccount.gen-on-init=false -sebserver.webservice.distributed=true +sebserver.webservice.distributed=false #sebserver.webservice.master.delay.threshold=10000 sebserver.webservice.http.external.scheme=http sebserver.webservice.http.external.servername=localhost diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index 6af4ca76..3873d6c2 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -111,6 +111,7 @@ sebserver.form.validation.fieldError.serverNotAvailable=No service seems to be a sebserver.form.validation.fieldError.url.invalid=Invalid URL. The given URL cannot be reached. sebserver.form.validation.fieldError.typeInvalid=This type is not implemented yet and cannot be used. sebserver.form.validation.fieldError.url.noservice=The expected service is not available within the given URL and API access. +sebserver.form.validation.fieldError.url.noaccess=There has no access been granted by the service. Please check the given access credentials. sebserver.form.validation.fieldError.thresholdDuplicate=There are duplicate threshold values. sebserver.form.validation.fieldError.thresholdEmpty=There are missing values or colors for the threshold declaration sebserver.form.validation.fieldError.invalidIP=Invalid IP v4. Please enter a valid IP-address (v4) diff --git a/src/test/java/ch/ethz/seb/sebserver/webservice/integration/api/admin/OlatLmsAPITemplateTest.java b/src/test/java/ch/ethz/seb/sebserver/webservice/integration/api/admin/OlatLmsAPITemplateTest.java index 2431220e..5e2b5850 100644 --- a/src/test/java/ch/ethz/seb/sebserver/webservice/integration/api/admin/OlatLmsAPITemplateTest.java +++ b/src/test/java/ch/ethz/seb/sebserver/webservice/integration/api/admin/OlatLmsAPITemplateTest.java @@ -41,6 +41,8 @@ public class OlatLmsAPITemplateTest extends AdministrationAPIIntegrationTester { private ExamConfigurationValueService examConfigurationValueService; @Autowired private CacheManager cacheManager; + @Autowired + private OlatLmsRestTemplate olatLmsRestTemplate; @Test public void testSetRestriction() throws Exception {