Search in sources :

Example 26 with Exam

use of de.tum.in.www1.artemis.domain.exam.Exam in project ArTEMiS by ls1intum.

the class ExamIntegrationTest method testRegisterUsersInExam.

@Test
@WithMockUser(username = "instructor1", roles = "INSTRUCTOR")
public void testRegisterUsersInExam() throws Exception {
    jiraRequestMockProvider.enableMockingOfRequests();
    var exam = ModelFactory.generateExam(course1);
    var savedExam = examRepository.save(exam);
    var student1 = database.getUserByLogin("student1");
    var student2 = database.getUserByLogin("student2");
    var student3 = database.getUserByLogin("student3");
    var student5 = database.getUserByLogin("student5");
    var student6 = database.getUserByLogin("student6");
    var student7 = database.getUserByLogin("student7");
    var student8 = database.getUserByLogin("student8");
    var student9 = database.getUserByLogin("student9");
    var student10 = database.getUserByLogin("student10");
    var registrationNumber1 = "1111111";
    var registrationNumber2 = "1111112";
    var registrationNumber3 = "1111113";
    var registrationNumber3WithTypo = registrationNumber3 + "0";
    var registrationNumber5 = "1111115";
    var registrationNumber5WithTypo = registrationNumber5 + "1";
    var registrationNumber6 = "1111116";
    var registrationNumber99 = "1111199";
    var registrationNumber100 = "1111100";
    var emptyRegistrationNumber = "";
    student1.setRegistrationNumber(registrationNumber1);
    student2.setRegistrationNumber(registrationNumber2);
    student3.setRegistrationNumber(registrationNumber3);
    student5.setRegistrationNumber(registrationNumber5);
    student6.setRegistrationNumber(registrationNumber6);
    student7.setRegistrationNumber(null);
    student8.setRegistrationNumber("");
    student9.setRegistrationNumber(" ");
    student10.setRegistrationNumber(null);
    student1 = userRepo.save(student1);
    student2 = userRepo.save(student2);
    userRepo.save(student3);
    userRepo.save(student5);
    userRepo.save(student6);
    userRepo.save(student7);
    userRepo.save(student8);
    userRepo.save(student9);
    userRepo.save(student10);
    // mock the ldap service
    doReturn(Optional.empty()).when(ldapUserService).findByRegistrationNumber(registrationNumber3WithTypo);
    doReturn(Optional.empty()).when(ldapUserService).findByRegistrationNumber(emptyRegistrationNumber);
    doReturn(Optional.empty()).when(ldapUserService).findByRegistrationNumber(registrationNumber5WithTypo);
    var ldapUser100Dto = new LdapUserDto().registrationNumber(registrationNumber100).firstName("Student100").lastName("Student100").username("student100").email("student100@tum.de");
    doReturn(Optional.of(ldapUser100Dto)).when(ldapUserService).findByRegistrationNumber(registrationNumber100);
    // first and second mocked calls are expected to add student 5 and 99 to the course students
    jiraRequestMockProvider.mockAddUserToGroup(course1.getStudentGroupName(), false);
    jiraRequestMockProvider.mockAddUserToGroup(course1.getStudentGroupName(), false);
    // third mocked call expected to create student 100
    jiraRequestMockProvider.mockCreateUserInExternalUserManagement(ldapUser100Dto.getUsername(), ldapUser100Dto.getFirstName() + " " + ldapUser100Dto.getLastName(), ldapUser100Dto.getEmail());
    // the last two mocked calls are expected to add students 100, 6, 7, 8, and 9 to the course student group
    for (int i = 0; i < 5; i++) {
        jiraRequestMockProvider.mockAddUserToGroup(course1.getStudentGroupName(), false);
    }
    bitbucketRequestMockProvider.mockUpdateUserDetails(student5.getLogin(), student5.getEmail(), student5.getName());
    bitbucketRequestMockProvider.mockAddUserToGroups();
    // not registered for the course
    var student99 = ModelFactory.generateActivatedUser("student99");
    student99.setRegistrationNumber(registrationNumber99);
    userRepo.save(student99);
    bitbucketRequestMockProvider.mockUpdateUserDetails(student99.getLogin(), student99.getEmail(), student99.getName());
    bitbucketRequestMockProvider.mockAddUserToGroups();
    student99 = userRepo.findOneWithGroupsAndAuthoritiesByLogin("student99").get();
    assertThat(student99.getGroups()).doesNotContain(course1.getStudentGroupName());
    // Note: student100 is not yet a user of Artemis and should be retrieved from the LDAP
    request.postWithoutLocation("/api/courses/" + course1.getId() + "/exams/" + savedExam.getId() + "/students/student1", null, HttpStatus.OK, null);
    request.postWithoutLocation("/api/courses/" + course1.getId() + "/exams/" + savedExam.getId() + "/students/nonExistingStudent", null, HttpStatus.NOT_FOUND, null);
    Exam storedExam = examRepository.findWithRegisteredUsersById(savedExam.getId()).get();
    assertThat(storedExam.getRegisteredUsers()).containsExactly(student1);
    request.delete("/api/courses/" + course1.getId() + "/exams/" + savedExam.getId() + "/students/student1", HttpStatus.OK);
    request.delete("/api/courses/" + course1.getId() + "/exams/" + savedExam.getId() + "/students/nonExistingStudent", HttpStatus.NOT_FOUND);
    storedExam = examRepository.findWithRegisteredUsersById(savedExam.getId()).get();
    assertThat(storedExam.getRegisteredUsers()).isEmpty();
    var studentDto1 = new StudentDTO().registrationNumber(registrationNumber1);
    var studentDto2 = new StudentDTO().registrationNumber(registrationNumber2);
    // explicit typo, should be a registration failure later
    var studentDto3 = new StudentDTO().registrationNumber(registrationNumber3WithTypo);
    // explicit typo, should fall back to login name later
    var studentDto5 = new StudentDTO().registrationNumber(registrationNumber5WithTypo);
    studentDto5.setLogin(student5.getLogin());
    var studentDto7 = new StudentDTO();
    studentDto7.setLogin(student7.getLogin());
    var studentDto8 = new StudentDTO();
    studentDto8.setLogin(student8.getLogin());
    var studentDto9 = new StudentDTO();
    studentDto9.setLogin(student9.getLogin());
    // completely empty
    var studentDto10 = new StudentDTO();
    var studentDto99 = new StudentDTO().registrationNumber(registrationNumber99);
    var studentDto100 = new StudentDTO().registrationNumber(registrationNumber100);
    // Add a student with login but empty registration number
    var studentDto6 = new StudentDTO().registrationNumber(emptyRegistrationNumber);
    studentDto6.setLogin(student6.getLogin());
    var studentsToRegister = List.of(studentDto1, studentDto2, studentDto3, studentDto5, studentDto99, studentDto100, studentDto6, studentDto7, studentDto8, studentDto9, studentDto10);
    bitbucketRequestMockProvider.mockUpdateUserDetails("student100", "student100@tum.de", "Student100 Student100");
    bitbucketRequestMockProvider.mockAddUserToGroups();
    bitbucketRequestMockProvider.mockUpdateUserDetails(student6.getLogin(), student6.getEmail(), student6.getName());
    bitbucketRequestMockProvider.mockAddUserToGroups();
    bitbucketRequestMockProvider.mockUpdateUserDetails(student7.getLogin(), student7.getEmail(), student7.getName());
    bitbucketRequestMockProvider.mockAddUserToGroups();
    bitbucketRequestMockProvider.mockUpdateUserDetails(student8.getLogin(), student8.getEmail(), student8.getName());
    bitbucketRequestMockProvider.mockAddUserToGroups();
    bitbucketRequestMockProvider.mockUpdateUserDetails(student9.getLogin(), student9.getEmail(), student9.getName());
    bitbucketRequestMockProvider.mockAddUserToGroups();
    bitbucketRequestMockProvider.mockUpdateUserDetails(student10.getLogin(), student10.getEmail(), student10.getName());
    bitbucketRequestMockProvider.mockAddUserToGroups();
    bitbucketRequestMockProvider.mockUpdateUserDetails("student100", "student100@tum.de", "Student100 Student100");
    bitbucketRequestMockProvider.mockAddUserToGroups();
    // now we register all these students for the exam.
    List<StudentDTO> registrationFailures = request.postListWithResponseBody("/api/courses/" + course1.getId() + "/exams/" + savedExam.getId() + "/students", studentsToRegister, StudentDTO.class, HttpStatus.OK);
    assertThat(registrationFailures).containsExactlyInAnyOrder(studentDto3, studentDto10);
    storedExam = examRepository.findWithRegisteredUsersById(savedExam.getId()).get();
    // now a new user student100 should exist
    var student100 = database.getUserByLogin("student100");
    assertThat(storedExam.getRegisteredUsers()).containsExactlyInAnyOrder(student1, student2, student5, student99, student100, student6, student7, student8, student9);
    for (var user : storedExam.getRegisteredUsers()) {
        // all registered users must have access to the course
        user = userRepo.findOneWithGroupsAndAuthoritiesByLogin(user.getLogin()).get();
        assertThat(user.getGroups()).contains(course1.getStudentGroupName());
    }
    // Make sure delete also works if so many objects have been created before
    request.delete("/api/courses/" + course1.getId() + "/exams/" + savedExam.getId(), HttpStatus.OK);
}
Also used : StudentDTO(de.tum.in.www1.artemis.service.dto.StudentDTO) LdapUserDto(de.tum.in.www1.artemis.service.ldap.LdapUserDto) StudentExam(de.tum.in.www1.artemis.domain.exam.StudentExam) Exam(de.tum.in.www1.artemis.domain.exam.Exam) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.jupiter.api.Test)

Example 27 with Exam

use of de.tum.in.www1.artemis.domain.exam.Exam in project ArTEMiS by ls1intum.

the class ExamIntegrationTest method testCreateTestExam_asInstructor.

@Test
@WithMockUser(username = "instructor1", roles = "INSTRUCTOR")
public void testCreateTestExam_asInstructor() throws Exception {
    // Test the creation of a TestExam
    Exam examA = ModelFactory.generateTestExam(course1);
    request.post("/api/courses/" + course1.getId() + "/exams", examA, HttpStatus.CREATED);
    verify(examAccessService, times(1)).checkCourseAccessForInstructorElseThrow(course1.getId());
}
Also used : StudentExam(de.tum.in.www1.artemis.domain.exam.StudentExam) Exam(de.tum.in.www1.artemis.domain.exam.Exam) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.jupiter.api.Test)

Example 28 with Exam

use of de.tum.in.www1.artemis.domain.exam.Exam in project ArTEMiS by ls1intum.

the class ExamIntegrationTest method testDeleteStudentThatDoesNotExist.

@Test
@WithMockUser(username = "instructor1", roles = "INSTRUCTOR")
public void testDeleteStudentThatDoesNotExist() throws Exception {
    Exam exam = database.setupExamWithExerciseGroupsExercisesRegisteredStudents(course1);
    request.delete("/api/courses/" + course1.getId() + "/exams/" + exam.getId() + "/students/nonExistingStudent", HttpStatus.NOT_FOUND);
}
Also used : StudentExam(de.tum.in.www1.artemis.domain.exam.StudentExam) Exam(de.tum.in.www1.artemis.domain.exam.Exam) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.jupiter.api.Test)

Example 29 with Exam

use of de.tum.in.www1.artemis.domain.exam.Exam in project ArTEMiS by ls1intum.

the class ExamIntegrationTest method testRemovingAllStudents.

@Test
@WithMockUser(username = "admin", roles = "ADMIN")
public void testRemovingAllStudents() throws Exception {
    Exam exam = database.setupExamWithExerciseGroupsExercisesRegisteredStudents(course1);
    // Generate student exams
    List<StudentExam> studentExams = request.postListWithResponseBody("/api/courses/" + course1.getId() + "/exams/" + exam.getId() + "/generate-student-exams", Optional.empty(), StudentExam.class, HttpStatus.OK);
    assertThat(studentExams).hasSize(4);
    assertThat(exam.getRegisteredUsers()).hasSize(4);
    // /courses/{courseId}/exams/{examId}/student-exams/start-exercises
    Integer numberOfGeneratedParticipations = request.postWithResponseBody("/api/courses/" + course1.getId() + "/exams/" + exam.getId() + "/student-exams/start-exercises", Optional.empty(), Integer.class, HttpStatus.OK);
    assertThat(numberOfGeneratedParticipations).isEqualTo(16);
    // Fetch student exams
    List<StudentExam> studentExamsDB = request.getList("/api/courses/" + course1.getId() + "/exams/" + exam.getId() + "/student-exams", HttpStatus.OK, StudentExam.class);
    assertThat(studentExamsDB).hasSize(4);
    List<StudentParticipation> participationList = new ArrayList<>();
    Exercise[] exercises = examRepository.findAllExercisesByExamId(exam.getId()).toArray(new Exercise[0]);
    for (Exercise value : exercises) {
        participationList.addAll(studentParticipationRepository.findByExerciseId(value.getId()));
    }
    assertThat(participationList).hasSize(16);
    // TODO there should be some participation but no submissions unfortunately
    // remove all students
    request.delete("/api/courses/" + course1.getId() + "/exams/" + exam.getId() + "/students", HttpStatus.OK);
    // Get the exam with all registered users
    var params = new LinkedMultiValueMap<String, String>();
    params.add("withStudents", "true");
    Exam storedExam = request.get("/api/courses/" + course1.getId() + "/exams/" + exam.getId(), HttpStatus.OK, Exam.class, params);
    assertThat(storedExam.getRegisteredUsers()).isEmpty();
    // Fetch student exams
    studentExamsDB = request.getList("/api/courses/" + course1.getId() + "/exams/" + exam.getId() + "/student-exams", HttpStatus.OK, StudentExam.class);
    assertThat(studentExamsDB).isEmpty();
    // Fetch participations
    exercises = examRepository.findAllExercisesByExamId(exam.getId()).toArray(new Exercise[0]);
    participationList = new ArrayList<>();
    for (Exercise exercise : exercises) {
        participationList.addAll(studentParticipationRepository.findByExerciseId(exercise.getId()));
    }
    assertThat(participationList).hasSize(16);
}
Also used : QuizExercise(de.tum.in.www1.artemis.domain.quiz.QuizExercise) ModelingExercise(de.tum.in.www1.artemis.domain.modeling.ModelingExercise) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) StudentExam(de.tum.in.www1.artemis.domain.exam.StudentExam) StudentExam(de.tum.in.www1.artemis.domain.exam.StudentExam) Exam(de.tum.in.www1.artemis.domain.exam.Exam) StudentParticipation(de.tum.in.www1.artemis.domain.participation.StudentParticipation) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.jupiter.api.Test)

Example 30 with Exam

use of de.tum.in.www1.artemis.domain.exam.Exam in project ArTEMiS by ls1intum.

the class ExamIntegrationTest method testGenerateStudentExamsNotEnoughExerciseGroups_badRequest.

@Test
@WithMockUser(username = "instructor1", roles = "INSTRUCTOR")
public void testGenerateStudentExamsNotEnoughExerciseGroups_badRequest() throws Exception {
    Exam exam = database.setupExamWithExerciseGroupsExercisesRegisteredStudents(course1);
    exam.setNumberOfExercisesInExam(exam.getNumberOfExercisesInExam() + 2);
    examRepository.save(exam);
    // invoke generate student exams
    request.postListWithResponseBody("/api/courses/" + course1.getId() + "/exams/" + exam.getId() + "/generate-student-exams", Optional.empty(), StudentExam.class, HttpStatus.BAD_REQUEST);
}
Also used : StudentExam(de.tum.in.www1.artemis.domain.exam.StudentExam) Exam(de.tum.in.www1.artemis.domain.exam.Exam) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.jupiter.api.Test)

Aggregations

Exam (de.tum.in.www1.artemis.domain.exam.Exam)228 StudentExam (de.tum.in.www1.artemis.domain.exam.StudentExam)180 WithMockUser (org.springframework.security.test.context.support.WithMockUser)164 Test (org.junit.jupiter.api.Test)158 ExerciseGroup (de.tum.in.www1.artemis.domain.exam.ExerciseGroup)92 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)75 ModelingExercise (de.tum.in.www1.artemis.domain.modeling.ModelingExercise)52 EntityNotFoundException (de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException)48 AbstractSpringIntegrationBambooBitbucketJiraTest (de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest)46 StudentParticipation (de.tum.in.www1.artemis.domain.participation.StudentParticipation)46 QuizExercise (de.tum.in.www1.artemis.domain.quiz.QuizExercise)42 ZonedDateTime (java.time.ZonedDateTime)40 BadRequestAlertException (de.tum.in.www1.artemis.web.rest.errors.BadRequestAlertException)38 Course (de.tum.in.www1.artemis.domain.Course)36 GradingScale (de.tum.in.www1.artemis.domain.GradingScale)34 BeforeEach (org.junit.jupiter.api.BeforeEach)30 User (de.tum.in.www1.artemis.domain.User)27 Collectors (java.util.stream.Collectors)26 ModelingSubmission (de.tum.in.www1.artemis.domain.modeling.ModelingSubmission)24 de.tum.in.www1.artemis.repository (de.tum.in.www1.artemis.repository)24