Search in sources :

Example 1 with LdapUserDto

use of de.tum.in.www1.artemis.service.ldap.LdapUserDto 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 2 with LdapUserDto

use of de.tum.in.www1.artemis.service.ldap.LdapUserDto in project ArTEMiS by ls1intum.

the class UserService method createUserFromLdap.

/**
 * Searches the (optional) LDAP service for a user with the give registration number (= Matrikelnummer) and returns a new Artemis user.
 * Also creates the user in the external user management (e.g. JIRA), in case this is activated
 * Note: this method should only be used if the user does not yet exist in the database
 *
 * @param registrationNumber the matriculation number of the student
 * @return a new user or null if the LDAP user was not found
 */
public Optional<User> createUserFromLdap(String registrationNumber) {
    if (!StringUtils.hasText(registrationNumber)) {
        return Optional.empty();
    }
    if (ldapUserService.isPresent()) {
        Optional<LdapUserDto> ldapUserOptional = ldapUserService.get().findByRegistrationNumber(registrationNumber);
        if (ldapUserOptional.isPresent()) {
            LdapUserDto ldapUser = ldapUserOptional.get();
            log.info("Ldap User {} has registration number: {}", ldapUser.getUsername(), ldapUser.getRegistrationNumber());
            // handle edge case, the user already exists in Artemis, but for some reason does not have a registration number or it is wrong
            if (StringUtils.hasText(ldapUser.getUsername())) {
                var existingUser = userRepository.findOneByLogin(ldapUser.getUsername());
                if (existingUser.isPresent()) {
                    existingUser.get().setRegistrationNumber(ldapUser.getRegistrationNumber());
                    saveUser(existingUser.get());
                    return existingUser;
                }
            }
            // Use empty password, so that we don't store the credentials of Jira users in the Artemis DB
            User user = userCreationService.createUser(ldapUser.getUsername(), "", null, ldapUser.getFirstName(), ldapUser.getLastName(), ldapUser.getEmail(), registrationNumber, null, "en", false);
            if (useExternalUserManagement) {
                artemisAuthenticationProvider.createUserInExternalUserManagement(user);
            }
            return Optional.of(user);
        } else {
            log.warn("Ldap User with registration number {} not found", registrationNumber);
        }
    }
    return Optional.empty();
}
Also used : User(de.tum.in.www1.artemis.domain.User) LdapUserDto(de.tum.in.www1.artemis.service.ldap.LdapUserDto)

Example 3 with LdapUserDto

use of de.tum.in.www1.artemis.service.ldap.LdapUserDto in project Artemis by ls1intum.

the class UserService method createUserFromLdap.

/**
 * Searches the (optional) LDAP service for a user with the give registration number (= Matrikelnummer) and returns a new Artemis user.
 * Also creates the user in the external user management (e.g. JIRA), in case this is activated
 * Note: this method should only be used if the user does not yet exist in the database
 *
 * @param registrationNumber the matriculation number of the student
 * @return a new user or null if the LDAP user was not found
 */
public Optional<User> createUserFromLdap(String registrationNumber) {
    if (!StringUtils.hasText(registrationNumber)) {
        return Optional.empty();
    }
    if (ldapUserService.isPresent()) {
        Optional<LdapUserDto> ldapUserOptional = ldapUserService.get().findByRegistrationNumber(registrationNumber);
        if (ldapUserOptional.isPresent()) {
            LdapUserDto ldapUser = ldapUserOptional.get();
            log.info("Ldap User {} has registration number: {}", ldapUser.getUsername(), ldapUser.getRegistrationNumber());
            // handle edge case, the user already exists in Artemis, but for some reason does not have a registration number or it is wrong
            if (StringUtils.hasText(ldapUser.getUsername())) {
                var existingUser = userRepository.findOneByLogin(ldapUser.getUsername());
                if (existingUser.isPresent()) {
                    existingUser.get().setRegistrationNumber(ldapUser.getRegistrationNumber());
                    saveUser(existingUser.get());
                    return existingUser;
                }
            }
            // Use empty password, so that we don't store the credentials of Jira users in the Artemis DB
            User user = userCreationService.createUser(ldapUser.getUsername(), "", null, ldapUser.getFirstName(), ldapUser.getLastName(), ldapUser.getEmail(), registrationNumber, null, "en", false);
            if (useExternalUserManagement) {
                artemisAuthenticationProvider.createUserInExternalUserManagement(user);
            }
            return Optional.of(user);
        } else {
            log.warn("Ldap User with registration number {} not found", registrationNumber);
        }
    }
    return Optional.empty();
}
Also used : User(de.tum.in.www1.artemis.domain.User) LdapUserDto(de.tum.in.www1.artemis.service.ldap.LdapUserDto)

Example 4 with LdapUserDto

use of de.tum.in.www1.artemis.service.ldap.LdapUserDto 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)

Aggregations

LdapUserDto (de.tum.in.www1.artemis.service.ldap.LdapUserDto)4 User (de.tum.in.www1.artemis.domain.User)2 Exam (de.tum.in.www1.artemis.domain.exam.Exam)2 StudentExam (de.tum.in.www1.artemis.domain.exam.StudentExam)2 StudentDTO (de.tum.in.www1.artemis.service.dto.StudentDTO)2 Test (org.junit.jupiter.api.Test)2 WithMockUser (org.springframework.security.test.context.support.WithMockUser)2