Skip to content

Commit

Permalink
SEBSERV-467 fix plus added authentication to LMSSetup test
Browse files Browse the repository at this point in the history
  • Loading branch information
anhefti committed Oct 2, 2023
1 parent d678207 commit 1d2c88c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,23 @@ private LmsSetupTestResult testLmsSetupSettings() {
"lmsSetup:lmsClientsecret:notNull"));
}

final Result<OlatLmsRestTemplate> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -45,7 +49,6 @@ public synchronized ClientHttpResponse intercept(
final ClientHttpRequestExecution execution) throws IOException {

try {

// if there's no token, authenticate first
if (OlatLmsRestTemplate.this.token == null) {
authenticate();
Expand All @@ -59,6 +62,7 @@ else if (OlatLmsRestTemplate.this.token.equals("authenticating")) {

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);
Expand All @@ -85,6 +89,7 @@ else if (OlatLmsRestTemplate.this.token.equals("authenticating")) {
log.debug("OLAT [retry API call]: URL {}", request.getURI());
}

response.close();
response = execution.execute(request, body);

if (log.isDebugEnabled()) {
Expand All @@ -97,7 +102,7 @@ else if (OlatLmsRestTemplate.this.token.equals("authenticating")) {

} 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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config/application-dev-ws.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 1d2c88c

Please sign in to comment.