From d3cd5719c9b9fb5a04a147aa39ecf46bbaf40469 Mon Sep 17 00:00:00 2001 From: Johannes Brandenburger <79154528+johannesbrandenburger@users.noreply.github.com> Date: Thu, 14 Mar 2024 14:50:30 +0100 Subject: [PATCH] add timestamps --- .../mobilelearning/models/Form.java | 19 +++++++++++++++++-- .../feedback/FeedbackFormService.java | 5 +++++ .../feedback/socket/LiveFeedbackSocket.java | 6 +++++- .../services/quiz/QuizFormService.java | 5 ++++- .../services/quiz/socket/LiveQuizSocket.java | 5 +++++ .../services/CourseServiceTest.java | 8 -------- .../services/LiveServiceTest.java | 6 ------ .../services/quiz/QuizFormServiceTest.java | 12 +++++++++--- .../quiz/socket/LiveQuizSocketTest.java | 14 +++++++++----- 9 files changed, 54 insertions(+), 26 deletions(-) diff --git a/backend/src/main/java/de/htwg_konstanz/mobilelearning/models/Form.java b/backend/src/main/java/de/htwg_konstanz/mobilelearning/models/Form.java index 6acbb40e..33861bd4 100644 --- a/backend/src/main/java/de/htwg_konstanz/mobilelearning/models/Form.java +++ b/backend/src/main/java/de/htwg_konstanz/mobilelearning/models/Form.java @@ -1,13 +1,14 @@ package de.htwg_konstanz.mobilelearning.models; import java.util.ArrayList; +import java.util.Date; import java.util.List; import org.bson.types.ObjectId; import de.htwg_konstanz.mobilelearning.enums.FormStatus; -public abstract class Form { +public class Form { public ObjectId id; public ObjectId courseId; public String name; @@ -16,6 +17,7 @@ public abstract class Form { public FormStatus status; public Integer connectCode; public String key; + public Date startTimestamp; public Form() { } @@ -27,7 +29,7 @@ public Form(ObjectId courseId, String name, String description, List(); this.status = status; - + this.startTimestamp = null; this.key = ""; // generate 6-digit connect code (100000 - 999999) @@ -113,4 +115,17 @@ public void setDescription(String description) { this.description = description; } + public void setStartTimestamp(Date startTimestamp) { + this.startTimestamp = startTimestamp; + } + + public void setStartTimestamp() { + this.startTimestamp = new Date(); + } + + public Date getStartTimestamp() { + return this.startTimestamp; + } + + } diff --git a/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/feedback/FeedbackFormService.java b/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/feedback/FeedbackFormService.java index 4face1fd..0d0667ac 100644 --- a/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/feedback/FeedbackFormService.java +++ b/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/feedback/FeedbackFormService.java @@ -213,6 +213,11 @@ public RestResponse participate(@RestPath String courseId, @RestPath Str if (course == null) { throw new NotFoundException("Course not found"); } FeedbackForm feedbackForm = course.getFeedbackFormById(formObjectId); if (feedbackForm == null) { throw new NotFoundException("FeedbackForm not found"); } + + // check if user is student of the course + if (!course.isStudent(userId)) { + return RestResponse.status(Response.Status.FORBIDDEN, "User is not student of the course"); + } // add the participant Boolean successfullyAdded = feedbackForm.addParticipant(new ObjectId(userId)); diff --git a/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/feedback/socket/LiveFeedbackSocket.java b/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/feedback/socket/LiveFeedbackSocket.java index 8f9afe61..3fd53a79 100644 --- a/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/feedback/socket/LiveFeedbackSocket.java +++ b/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/feedback/socket/LiveFeedbackSocket.java @@ -17,7 +17,6 @@ import de.htwg_konstanz.mobilelearning.models.QuestionWrapper; import de.htwg_konstanz.mobilelearning.models.Result; import de.htwg_konstanz.mobilelearning.models.auth.User; -import de.htwg_konstanz.mobilelearning.models.auth.UserRole; import de.htwg_konstanz.mobilelearning.models.feedback.FeedbackForm; import de.htwg_konstanz.mobilelearning.repositories.CourseRepository; import de.htwg_konstanz.mobilelearning.repositories.UserRepository; @@ -289,6 +288,11 @@ private Boolean changeFormStatus(LiveFeedbackSocketMessage feedbackSocketMessage this.broadcast(outgoingMessage, course.getId().toHexString(), formId); } + // if it is set to STARTED set the timestamp + if (formStatusEnum == FormStatus.STARTED) { + form.setStartTimestamp(); + } + // send the updated form to all receivers (stringify the form) LiveFeedbackSocketMessage outgoingMessage = new LiveFeedbackSocketMessage("FORM_STATUS_CHANGED", form.status.toString(), null, null, form); diff --git a/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/quiz/QuizFormService.java b/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/quiz/QuizFormService.java index 1b04ca1e..ebe87045 100644 --- a/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/quiz/QuizFormService.java +++ b/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/quiz/QuizFormService.java @@ -104,7 +104,10 @@ public RestResponse participate(String alias, @RestPath String courseId, QuizForm quizForm = course.getQuizFormById(formObjectId); if (quizForm == null) { throw new NotFoundException("QuizForm not found"); } - // TODO: check if user is student of the course + // check if user is student of the course + if (!course.isStudent(userId)) { + return RestResponse.status(Response.Status.FORBIDDEN, "User is not student of the course"); + } // add the participant and check if the alias is already taken Boolean successfullyAdded = quizForm.addParticipant(new ObjectId(userId), alias); diff --git a/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/quiz/socket/LiveQuizSocket.java b/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/quiz/socket/LiveQuizSocket.java index 128bf552..c12f3377 100644 --- a/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/quiz/socket/LiveQuizSocket.java +++ b/backend/src/main/java/de/htwg_konstanz/mobilelearning/services/quiz/socket/LiveQuizSocket.java @@ -293,6 +293,11 @@ private Boolean changeFormStatus(LiveQuizSocketMessage quizSocketMessage, Course this.broadcast(outgoingMessage, course.getId().toHexString(), formId); } + // if it is set to STARTED set the timestamp + if (formStatusEnum == FormStatus.STARTED) { + form.setStartTimestamp(); + } + // send the updated form to all receivers (stringify the form) LiveQuizSocketMessage outgoingMessage = new LiveQuizSocketMessage("FORM_STATUS_CHANGED", form.status.toString(), null, null, form); this.broadcast(outgoingMessage, course.getId().toHexString(), formId); diff --git a/backend/src/test/java/de/htwg_konstanz/mobilelearning/services/CourseServiceTest.java b/backend/src/test/java/de/htwg_konstanz/mobilelearning/services/CourseServiceTest.java index 9b55b705..6644507c 100644 --- a/backend/src/test/java/de/htwg_konstanz/mobilelearning/services/CourseServiceTest.java +++ b/backend/src/test/java/de/htwg_konstanz/mobilelearning/services/CourseServiceTest.java @@ -1,9 +1,6 @@ package de.htwg_konstanz.mobilelearning.services; import static io.restassured.RestAssured.given; -import static org.hamcrest.CoreMatchers.is; - -import java.net.URI; import java.util.List; import org.bson.types.ObjectId; @@ -13,22 +10,17 @@ import org.junit.jupiter.api.TestInfo; import de.htwg_konstanz.mobilelearning.Helper; -import de.htwg_konstanz.mobilelearning.SocketClient; import de.htwg_konstanz.mobilelearning.MockMongoTestProfile; import de.htwg_konstanz.mobilelearning.MockUser; import de.htwg_konstanz.mobilelearning.models.Course; import de.htwg_konstanz.mobilelearning.models.auth.User; -import de.htwg_konstanz.mobilelearning.models.quiz.QuizForm; import de.htwg_konstanz.mobilelearning.repositories.CourseRepository; import de.htwg_konstanz.mobilelearning.repositories.UserRepository; -import de.htwg_konstanz.mobilelearning.services.CourseService; import de.htwg_konstanz.mobilelearning.services.auth.UserService; import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.TestProfile; import io.restassured.response.Response; import jakarta.inject.Inject; -import jakarta.websocket.ContainerProvider; -import jakarta.websocket.Session; @QuarkusTest @TestProfile(MockMongoTestProfile.class) diff --git a/backend/src/test/java/de/htwg_konstanz/mobilelearning/services/LiveServiceTest.java b/backend/src/test/java/de/htwg_konstanz/mobilelearning/services/LiveServiceTest.java index 29845985..17a6821e 100644 --- a/backend/src/test/java/de/htwg_konstanz/mobilelearning/services/LiveServiceTest.java +++ b/backend/src/test/java/de/htwg_konstanz/mobilelearning/services/LiveServiceTest.java @@ -10,19 +10,13 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInfo; -import com.google.gson.Gson; - import de.htwg_konstanz.mobilelearning.Helper; import de.htwg_konstanz.mobilelearning.SocketClient; import de.htwg_konstanz.mobilelearning.MockMongoTestProfile; import de.htwg_konstanz.mobilelearning.MockUser; import de.htwg_konstanz.mobilelearning.models.Course; import de.htwg_konstanz.mobilelearning.models.FormShell; -import de.htwg_konstanz.mobilelearning.models.feedback.FeedbackForm; -import de.htwg_konstanz.mobilelearning.repositories.UserRepository; -import de.htwg_konstanz.mobilelearning.services.CourseService; import de.htwg_konstanz.mobilelearning.services.auth.UserService; -import de.htwg_konstanz.mobilelearning.services.quiz.socket.LiveQuizSocket; import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.TestProfile; import io.restassured.response.Response; diff --git a/backend/src/test/java/de/htwg_konstanz/mobilelearning/services/quiz/QuizFormServiceTest.java b/backend/src/test/java/de/htwg_konstanz/mobilelearning/services/quiz/QuizFormServiceTest.java index 2964077d..d81c3c47 100644 --- a/backend/src/test/java/de/htwg_konstanz/mobilelearning/services/quiz/QuizFormServiceTest.java +++ b/backend/src/test/java/de/htwg_konstanz/mobilelearning/services/quiz/QuizFormServiceTest.java @@ -156,8 +156,14 @@ public void participateUniqueAlias() { String courseId = courses.getFirst().getId().toString(); String formId = courses.getFirst().getQuizForms().get(0).getId().toString(); + // create 2 students and sync the courses to + MockUser student1 = Helper.createMockUser("Student-1"); + given().header("Authorization", "Bearer " + student1.getJwt()).when().get("/course").then().statusCode(200); + MockUser student2 = Helper.createMockUser("Student-2"); + given().header("Authorization", "Bearer " + student2.getJwt()).when().get("/course").then().statusCode(200); + // Check successful RestResponse - given().header("Authorization", "Bearer " + Helper.createMockUser("Student1").getJwt()) + given().header("Authorization", "Bearer " + student1.getJwt()) .pathParam("courseId", courseId) .pathParam("formId", formId) .body("alias") @@ -168,7 +174,7 @@ public void participateUniqueAlias() { .body(is("Successfully added")); // 200 if alias already taken by the same user - given().header("Authorization", "Bearer " + Helper.createMockUser("Student1").getJwt()) + given().header("Authorization", "Bearer " + student1.getJwt()) .pathParam("courseId", courseId) .pathParam("formId", formId) .body("alias") @@ -179,7 +185,7 @@ public void participateUniqueAlias() { .body(is("Successfully added")); // 409 if alias already taken by another user - given().header("Authorization", "Bearer " + Helper.createMockUser("Student2").getJwt()) + given().header("Authorization", "Bearer " + student2.getJwt()) .pathParam("courseId", courseId) .pathParam("formId", formId) .body("alias") diff --git a/backend/src/test/java/de/htwg_konstanz/mobilelearning/services/quiz/socket/LiveQuizSocketTest.java b/backend/src/test/java/de/htwg_konstanz/mobilelearning/services/quiz/socket/LiveQuizSocketTest.java index cce2b450..9e17dd7b 100644 --- a/backend/src/test/java/de/htwg_konstanz/mobilelearning/services/quiz/socket/LiveQuizSocketTest.java +++ b/backend/src/test/java/de/htwg_konstanz/mobilelearning/services/quiz/socket/LiveQuizSocketTest.java @@ -3,17 +3,13 @@ import static io.restassured.RestAssured.given; import java.net.URI; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; - +import java.util.Date; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInfo; -import com.fasterxml.jackson.databind.ObjectMapper; - import de.htwg_konstanz.mobilelearning.Helper; import de.htwg_konstanz.mobilelearning.SocketClient; import de.htwg_konstanz.mobilelearning.MockMongoTestProfile; @@ -57,6 +53,9 @@ public void startQuizForm() { MockUser prof = Helper.createMockUser("Prof"); given().header("Authorization", "Bearer " + prof.getJwt()).get("/course").then().statusCode(200); + // store a timestamp to check if the form's startTimestamp has been set + Date startTimestamp = new Date(); + // create a websocket client try { SocketClient client = new SocketClient(); @@ -81,6 +80,11 @@ public void startQuizForm() { System.out.println(e); Assertions.fail(e.getMessage()); } + + // check if the startTimestamp has been set and is less than 1 minute ago + Date formStartTimestamp = courseService.getCourse(courseId).getQuizForms().get(0).getStartTimestamp(); + Assertions.assertNotNull(formStartTimestamp); + Assertions.assertTrue(formStartTimestamp.after(startTimestamp)); } @Test