diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/LmsSetupController.java b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/LmsSetupController.java index 2083bd51..50479869 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/LmsSetupController.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/LmsSetupController.java @@ -75,6 +75,7 @@ public class LmsSetupController extends ActivatableEntityController() { @@ -103,7 +105,7 @@ public class LmsSetupAPITest extends AdministrationAPIIntegrationTester { // get lmsSetup = new RestAPITestHelper() .withAccessToken(getAdminInstitution1Access()) - .withPath(API.LMS_SETUP_ENDPOINT).withPath("/") + .withPath(API.LMS_SETUP_ENDPOINT) .withPath(String.valueOf(lmsSetup.id)) .withMethod(HttpMethod.GET) .withExpectedStatus(HttpStatus.OK) @@ -117,7 +119,7 @@ public class LmsSetupAPITest extends AdministrationAPIIntegrationTester { report = new RestAPITestHelper() .withAccessToken(getAdminInstitution1Access()) .withPath(API.LMS_SETUP_ENDPOINT) - .withPath("/").withPath(String.valueOf(lmsSetup.id)).withPath("/inactive") + .withPath(String.valueOf(lmsSetup.id)).withPath("/inactive") .withMethod(HttpMethod.POST) .withExpectedStatus(HttpStatus.OK) .getAsObject(new TypeReference() { @@ -132,7 +134,7 @@ public class LmsSetupAPITest extends AdministrationAPIIntegrationTester { lmsSetup = new RestAPITestHelper() .withAccessToken(getAdminInstitution1Access()) - .withPath(API.LMS_SETUP_ENDPOINT).withPath("/") + .withPath(API.LMS_SETUP_ENDPOINT) .withPath(String.valueOf(lmsSetup.id)) .withMethod(HttpMethod.GET) .withExpectedStatus(HttpStatus.OK) @@ -146,7 +148,7 @@ public class LmsSetupAPITest extends AdministrationAPIIntegrationTester { report = new RestAPITestHelper() .withAccessToken(getAdminInstitution1Access()) .withPath(API.LMS_SETUP_ENDPOINT) - .withPath("/").withPath(String.valueOf(lmsSetup.id)) + .withPath(String.valueOf(lmsSetup.id)) .withMethod(HttpMethod.DELETE) .withExpectedStatus(HttpStatus.OK) .getAsObject(new TypeReference() { @@ -162,7 +164,7 @@ public class LmsSetupAPITest extends AdministrationAPIIntegrationTester { // get final List error = new RestAPITestHelper() .withAccessToken(getAdminInstitution1Access()) - .withPath(API.LMS_SETUP_ENDPOINT).withPath("/") + .withPath(API.LMS_SETUP_ENDPOINT) .withPath(String.valueOf(lmsSetup.id)) .withMethod(HttpMethod.GET) .withExpectedStatus(HttpStatus.NOT_FOUND) @@ -316,4 +318,153 @@ public class LmsSetupAPITest extends AdministrationAPIIntegrationTester { .getAsString(); } + @Test + public void testInstituionalView() throws Exception { + // create new LmsSetup Mock with seb-admin + final LmsSetup lmsSetup1 = new RestAPITestHelper() + .withAccessToken(getSebAdminAccess()) + .withPath(API.LMS_SETUP_ENDPOINT) + .withMethod(HttpMethod.POST) + .withAttribute(Domain.LMS_SETUP.ATTR_NAME, "new LmsSetup 1") + .withAttribute(Domain.LMS_SETUP.ATTR_LMS_TYPE, LmsType.MOCKUP.name()) + .withExpectedStatus(HttpStatus.OK) + .getAsObject(new TypeReference() { + }); + + // create new LmsSetup Mock with institutional 2 admin + final LmsSetup lmsSetup2 = new RestAPITestHelper() + .withAccessToken(getAdminInstitution2Access()) + .withPath(API.LMS_SETUP_ENDPOINT) + .withMethod(HttpMethod.POST) + .withAttribute(Domain.LMS_SETUP.ATTR_NAME, "new LmsSetup 1") + .withAttribute(Domain.LMS_SETUP.ATTR_LMS_TYPE, LmsType.MOCKUP.name()) + .withExpectedStatus(HttpStatus.OK) + .getAsObject(new TypeReference() { + }); + + // get with institutional 1 admin, expected to see only the one created by seb-admin + Page lmsSetups = new RestAPITestHelper() + .withAccessToken(getAdminInstitution1Access()) + .withPath(API.LMS_SETUP_ENDPOINT) + .withExpectedStatus(HttpStatus.OK) + .getAsObject(new TypeReference>() { + }); + + assertNotNull(lmsSetups); + assertNotNull(lmsSetups.content); + assertTrue(lmsSetups.content.size() == 1); + LmsSetup lmsSetup = lmsSetups.content.get(0); + assertEquals(lmsSetup1.id, lmsSetup.id); + + // get with institutional 2 admin, expected to see only the one self created + lmsSetups = new RestAPITestHelper() + .withAccessToken(getAdminInstitution2Access()) + .withPath(API.LMS_SETUP_ENDPOINT) + .withExpectedStatus(HttpStatus.OK) + .getAsObject(new TypeReference>() { + }); + + assertNotNull(lmsSetups); + assertNotNull(lmsSetups.content); + assertTrue(lmsSetups.content.size() == 1); + lmsSetup = lmsSetups.content.get(0); + assertEquals(lmsSetup2.id, lmsSetup.id); + } + + @Test + public void testLmsSetupConnectionTest() throws Exception { + // create new LmsSetup Mock with seb-admin + LmsSetup lmsSetup = new RestAPITestHelper() + .withAccessToken(getSebAdminAccess()) + .withPath(API.LMS_SETUP_ENDPOINT) + .withMethod(HttpMethod.POST) + .withAttribute(Domain.LMS_SETUP.ATTR_NAME, "new LmsSetup 1") + .withAttribute(Domain.LMS_SETUP.ATTR_LMS_TYPE, LmsType.MOCKUP.name()) + .withExpectedStatus(HttpStatus.OK) + .getAsObject(new TypeReference() { + }); + + // test LMS connection should fail because there is no server set yet + List errors = new RestAPITestHelper() + .withAccessToken(getSebAdminAccess()) + .withPath(API.LMS_SETUP_ENDPOINT) + .withPath(API.LMS_SETUP_TEST_PATH_SEGMENT) + .withPath(lmsSetup.getModelId()) + .withMethod(HttpMethod.GET) + .withExpectedStatus(HttpStatus.BAD_REQUEST) + .getAsObject(new TypeReference>() { + }); + + assertNotNull(errors); + assertTrue(errors.size() == 3); + + // save (wrong) LMS server and credentials + lmsSetup = new LmsSetup( + lmsSetup.id, + lmsSetup.institutionId, + lmsSetup.name, + lmsSetup.lmsType, + "lms1Name", + null, // no secret + "https://www.lms1.com", + null, + null); + lmsSetup = new RestAPITestHelper() + .withAccessToken(getAdminInstitution1Access()) + .withPath(API.LMS_SETUP_ENDPOINT) + .withMethod(HttpMethod.PUT) + .withBodyJson(lmsSetup) + .withExpectedStatus(HttpStatus.OK) + .getAsObject(new TypeReference() { + }); + + // test LMS connection again should fail because there is no secret set + errors = new RestAPITestHelper() + .withAccessToken(getSebAdminAccess()) + .withPath(API.LMS_SETUP_ENDPOINT) + .withPath(API.LMS_SETUP_TEST_PATH_SEGMENT) + .withPath(lmsSetup.getModelId()) + .withMethod(HttpMethod.GET) + .withExpectedStatus(HttpStatus.BAD_REQUEST) + .getAsObject(new TypeReference>() { + }); + + assertNotNull(errors); + assertTrue(errors.size() == 1); + assertEquals("[lmsSetup, lmsClientsecret, notNull]", String.valueOf(errors.get(0).attributes)); + + // save correct LMS server and credentials + lmsSetup = new LmsSetup( + lmsSetup.id, + lmsSetup.institutionId, + lmsSetup.name, + lmsSetup.lmsType, + "lms1Name", + "someSecret", + "https://www.lms1.com", + null, + null); + lmsSetup = new RestAPITestHelper() + .withAccessToken(getAdminInstitution1Access()) + .withPath(API.LMS_SETUP_ENDPOINT) + .withMethod(HttpMethod.PUT) + .withBodyJson(lmsSetup) + .withExpectedStatus(HttpStatus.OK) + .getAsObject(new TypeReference() { + }); + + final LmsSetupTestResult testResult = new RestAPITestHelper() + .withAccessToken(getSebAdminAccess()) + .withPath(API.LMS_SETUP_ENDPOINT) + .withPath(API.LMS_SETUP_TEST_PATH_SEGMENT) + .withPath(lmsSetup.getModelId()) + .withMethod(HttpMethod.GET) + .withExpectedStatus(HttpStatus.OK) + .getAsObject(new TypeReference() { + }); + + assertNotNull(testResult); + assertTrue(testResult.getOkStatus()); + } + } diff --git a/src/test/java/ch/ethz/seb/sebserver/webservice/integration/api/admin/QuizDataTest.java b/src/test/java/ch/ethz/seb/sebserver/webservice/integration/api/admin/QuizDataTest.java new file mode 100644 index 00000000..498200c8 --- /dev/null +++ b/src/test/java/ch/ethz/seb/sebserver/webservice/integration/api/admin/QuizDataTest.java @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2019 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +package ch.ethz.seb.sebserver.webservice.integration.api.admin; + +import static org.junit.Assert.*; + +import org.junit.Test; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.test.context.jdbc.Sql; + +import com.fasterxml.jackson.core.type.TypeReference; + +import ch.ethz.seb.sebserver.gbl.api.API; +import ch.ethz.seb.sebserver.gbl.model.Domain; +import ch.ethz.seb.sebserver.gbl.model.EntityProcessingReport; +import ch.ethz.seb.sebserver.gbl.model.Page; +import ch.ethz.seb.sebserver.gbl.model.exam.QuizData; +import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetup; +import ch.ethz.seb.sebserver.gbl.model.institution.LmsSetup.LmsType; + +@Sql(scripts = { "classpath:schema-test.sql", "classpath:data-test.sql" }) +public class QuizDataTest extends AdministrationAPIIntegrationTester { + + @Test + public void testGetInstitutionalQuizPage() throws Exception { + // create new active LmsSetup Mock with seb-admin + final LmsSetup lmsSetup1 = createLmsSetupMockWith( + this, + getSebAdminAccess(), + "new LmsSetup 1", + true); + + assertNotNull(lmsSetup1); + assertTrue(lmsSetup1.isActive()); + + // create new inactive LmsSetup Mock with institution 2 admin + final LmsSetup lmsSetup2 = createLmsSetupMockWith( + this, + getAdminInstitution2Access(), + "new LmsSetup 2", + false); + + assertNotNull(lmsSetup2); + assertFalse(lmsSetup2.isActive()); + + // for the active LmsSetup we should get the quizzes page but only the quizzes from LmsSetup of seb-admin + Page quizzes = new RestAPITestHelper() + .withAccessToken(getSebAdminAccess()) + .withPath(API.QUIZ_DISCOVERY_ENDPOINT) + .withExpectedStatus(HttpStatus.OK) + .getAsObject(new TypeReference>() { + }); + + assertNotNull(quizzes); + assertTrue(quizzes.content.size() == 5); + + // for the inactive LmsSetup we should'nt get any quizzes + quizzes = new RestAPITestHelper() + .withAccessToken(getAdminInstitution2Access()) + .withPath(API.QUIZ_DISCOVERY_ENDPOINT) + .withExpectedStatus(HttpStatus.OK) + .getAsObject(new TypeReference>() { + }); + + assertNotNull(quizzes); + assertTrue(quizzes.content.size() == 0); + + // activate / deactivate + new RestAPITestHelper() + .withAccessToken(getSebAdminAccess()) + .withPath(API.LMS_SETUP_ENDPOINT) + .withPath(String.valueOf(lmsSetup1.id)).withPath("/inactive") + .withMethod(HttpMethod.POST) + .withExpectedStatus(HttpStatus.OK) + .getAsObject(new TypeReference() { + }); + new RestAPITestHelper() + .withAccessToken(getAdminInstitution2Access()) + .withPath(API.LMS_SETUP_ENDPOINT) + .withPath(String.valueOf(lmsSetup2.id)).withPath("/active") + .withMethod(HttpMethod.POST) + .withExpectedStatus(HttpStatus.OK) + .getAsObject(new TypeReference() { + }); + + // now we should not get any quizzes for the seb-admin + quizzes = new RestAPITestHelper() + .withAccessToken(getSebAdminAccess()) + .withPath(API.QUIZ_DISCOVERY_ENDPOINT) + .withExpectedStatus(HttpStatus.OK) + .getAsObject(new TypeReference>() { + }); + + assertNotNull(quizzes); + assertTrue(quizzes.content.size() == 0); + + // but for the now active lmsSetup2 we should get the quizzes + quizzes = new RestAPITestHelper() + .withAccessToken(getAdminInstitution2Access()) + .withPath(API.QUIZ_DISCOVERY_ENDPOINT) + .withExpectedStatus(HttpStatus.OK) + .getAsObject(new TypeReference>() { + }); + + assertNotNull(quizzes); + assertTrue(quizzes.content.size() == 5); + } + + public static final LmsSetup createLmsSetupMockWith( + final AdministrationAPIIntegrationTester tester, + final String token, + final String name, + final boolean active) throws Exception { + + LmsSetup lmsSetup = tester.restAPITestHelper() + .withAccessToken(token) + .withPath(API.LMS_SETUP_ENDPOINT) + .withMethod(HttpMethod.POST) + .withAttribute(Domain.LMS_SETUP.ATTR_NAME, name) + .withAttribute(Domain.LMS_SETUP.ATTR_LMS_TYPE, LmsType.MOCKUP.name()) + .withExpectedStatus(HttpStatus.OK) + .getAsObject(new TypeReference() { + }); + + lmsSetup = new LmsSetup( + lmsSetup.id, + lmsSetup.institutionId, + lmsSetup.name, + lmsSetup.lmsType, + "lms1Name", + "somePW", + "https://www.lms1.com", + null, + null); + + lmsSetup = tester.restAPITestHelper() + .withAccessToken(token) + .withPath(API.LMS_SETUP_ENDPOINT) + .withMethod(HttpMethod.PUT) + .withBodyJson(lmsSetup) + .withExpectedStatus(HttpStatus.OK) + .getAsObject(new TypeReference() { + }); + + if (active) { + final EntityProcessingReport report = tester.restAPITestHelper() + .withAccessToken(token) + .withPath(API.LMS_SETUP_ENDPOINT) + .withPath(String.valueOf(lmsSetup.id)).withPath("/active") + .withMethod(HttpMethod.POST) + .withExpectedStatus(HttpStatus.OK) + .getAsObject(new TypeReference() { + }); + assertTrue(report.errors.isEmpty()); + return tester.restAPITestHelper() + .withAccessToken(token) + .withPath(API.LMS_SETUP_ENDPOINT) + .withPath(String.valueOf(lmsSetup.id)) + .withMethod(HttpMethod.GET) + .withExpectedStatus(HttpStatus.OK) + .getAsObject(new TypeReference() { + }); + } + + return lmsSetup; + } +}