Search in sources :

Example 56 with AccountAttributes

use of teammates.common.datatransfer.attributes.AccountAttributes in project teammates by TEAMMATES.

the class AccountsDbTest method testCreateAccount.

@Test
public void testCreateAccount() throws Exception {
    ______TS("typical success case (legacy data)");
    AccountAttributes a = AccountAttributes.builder().withGoogleId("test.account").withName("Test account Name").withIsInstructor(false).withEmail("fresh-account@email.com").withInstitute("TEAMMATES Test Institute 1").build();
    a.studentProfile = null;
    accountsDb.createAccount(a);
    ______TS("success case: duplicate account");
    StudentProfileAttributes spa = StudentProfileAttributes.builder(a.googleId).build();
    spa.shortName = "test acc na";
    spa.email = "test@personal.com";
    spa.gender = Const.GenderTypes.MALE;
    spa.nationality = "American";
    spa.institute = "institute";
    spa.moreInfo = "this is more info";
    spa.googleId = a.googleId;
    a.studentProfile = spa;
    accountsDb.createAccount(a);
    ______TS("test persistence of latest entry");
    AccountAttributes accountDataTest = accountsDb.getAccount(a.googleId, true);
    assertEquals(spa.shortName, accountDataTest.studentProfile.shortName);
    assertEquals(spa.gender, accountDataTest.studentProfile.gender);
    assertEquals(spa.institute, accountDataTest.studentProfile.institute);
    assertEquals(a.institute, accountDataTest.institute);
    assertEquals(spa.email, accountDataTest.studentProfile.email);
    assertFalse(accountDataTest.isInstructor);
    // Change a field
    accountDataTest.isInstructor = true;
    accountDataTest.studentProfile.gender = Const.GenderTypes.FEMALE;
    accountsDb.createAccount(accountDataTest);
    // Re-retrieve
    accountDataTest = accountsDb.getAccount(a.googleId, true);
    assertTrue(accountDataTest.isInstructor);
    assertEquals(Const.GenderTypes.FEMALE, accountDataTest.studentProfile.gender);
    accountsDb.deleteAccount(a.googleId);
    // Should we not allow empty fields?
    ______TS("failure case: invalid parameter");
    a.email = "invalid email";
    try {
        accountsDb.createAccount(a);
        signalFailureToDetectException(" - InvalidParametersException");
    } catch (InvalidParametersException e) {
        AssertHelper.assertContains(getPopulatedErrorMessage(FieldValidator.EMAIL_ERROR_MESSAGE, "invalid email", FieldValidator.EMAIL_FIELD_NAME, FieldValidator.REASON_INCORRECT_FORMAT, FieldValidator.EMAIL_MAX_LENGTH), e.getMessage());
    }
    ______TS("failure: null parameter");
    try {
        accountsDb.createAccount(null);
        signalFailureToDetectException(" - AssertionError");
    } catch (AssertionError ae) {
        assertEquals(Const.StatusCodes.DBLEVEL_NULL_INPUT, ae.getMessage());
    }
}
Also used : AccountAttributes(teammates.common.datatransfer.attributes.AccountAttributes) InvalidParametersException(teammates.common.exception.InvalidParametersException) StudentProfileAttributes(teammates.common.datatransfer.attributes.StudentProfileAttributes) Test(org.testng.annotations.Test)

Example 57 with AccountAttributes

use of teammates.common.datatransfer.attributes.AccountAttributes in project teammates by TEAMMATES.

the class AccountsDbTest method createInstructorAccounts.

private List<AccountAttributes> createInstructorAccounts(int numOfInstructors) throws Exception {
    AccountAttributes a;
    List<AccountAttributes> result = new ArrayList<>();
    for (int i = 0; i < numOfInstructors; i++) {
        a = getNewAccountAttributes();
        a.googleId = "id." + i;
        a.isInstructor = true;
        accountsDb.createAccount(a);
        result.add(a);
    }
    return result;
}
Also used : AccountAttributes(teammates.common.datatransfer.attributes.AccountAttributes) ArrayList(java.util.ArrayList)

Example 58 with AccountAttributes

use of teammates.common.datatransfer.attributes.AccountAttributes in project teammates by TEAMMATES.

the class AccountsLogicTest method testJoinCourseForStudent.

@Test
public void testJoinCourseForStudent() throws Exception {
    String correctStudentId = "correctStudentId";
    String courseId = "idOfTypicalCourse1";
    String originalEmail = "original@email.com";
    // Create correct student with original@email.com
    StudentAttributes studentData = StudentAttributes.builder(courseId, "name", originalEmail).withSection("sectionName").withTeam("teamName").withComments("").build();
    studentsLogic.createStudentCascadeWithoutDocument(studentData);
    studentData = StudentsLogic.inst().getStudentForEmail(courseId, originalEmail);
    verifyPresentInDatastore(studentData);
    ______TS("failure: wrong key");
    try {
        accountsLogic.joinCourseForStudent(StringHelper.encrypt("wrongkey"), correctStudentId);
        signalFailureToDetectException();
    } catch (JoinCourseException e) {
        assertEquals("You have used an invalid join link: %s", e.getMessage());
    }
    ______TS("failure: invalid parameters");
    try {
        accountsLogic.joinCourseForStudent(StringHelper.encrypt(studentData.key), "wrong student");
        signalFailureToDetectException();
    } catch (InvalidParametersException e) {
        AssertHelper.assertContains(FieldValidator.REASON_INCORRECT_FORMAT, e.getMessage());
    }
    ______TS("failure: googleID belongs to an existing student in the course");
    String existingId = "AccLogicT.existing.studentId";
    StudentAttributes existingStudent = StudentAttributes.builder(courseId, "name", "differentEmail@email.com").withSection("sectionName").withTeam("teamName").withComments("").withGoogleId(existingId).build();
    studentsLogic.createStudentCascadeWithoutDocument(existingStudent);
    try {
        accountsLogic.joinCourseForStudent(StringHelper.encrypt(studentData.key), existingId);
        signalFailureToDetectException();
    } catch (JoinCourseException e) {
        assertEquals(String.format(Const.StatusMessages.JOIN_COURSE_GOOGLE_ID_BELONGS_TO_DIFFERENT_USER, existingId), e.getMessage());
    }
    ______TS("success: without encryption and account already exists");
    StudentProfileAttributes spa = StudentProfileAttributes.builder(correctStudentId).withInstitute("TEAMMATES Test Institute 1").build();
    AccountAttributes accountData = AccountAttributes.builder().withGoogleId(correctStudentId).withName("nameABC").withEmail("real@gmail.com").withInstitute("TEAMMATES Test Institute 1").withIsInstructor(true).withStudentProfileAttributes(spa).build();
    accountsLogic.createAccount(accountData);
    accountsLogic.joinCourseForStudent(StringHelper.encrypt(studentData.key), correctStudentId);
    studentData.googleId = accountData.googleId;
    verifyPresentInDatastore(studentData);
    assertEquals(correctStudentId, logic.getStudentForEmail(studentData.course, studentData.email).googleId);
    ______TS("failure: already joined");
    try {
        accountsLogic.joinCourseForStudent(StringHelper.encrypt(studentData.key), correctStudentId);
        signalFailureToDetectException();
    } catch (JoinCourseException e) {
        assertEquals("You (" + correctStudentId + ") have already joined this course", e.getMessage());
    }
    ______TS("failure: valid key belongs to a different user");
    try {
        accountsLogic.joinCourseForStudent(StringHelper.encrypt(studentData.key), "wrongstudent");
        signalFailureToDetectException();
    } catch (JoinCourseException e) {
        assertEquals("The join link used belongs to a different user whose " + "Google ID is corre..dentId (only part of the Google ID is " + "shown to protect privacy). If that Google ID is owned by you, " + "please logout and re-login using that Google account. " + "If it doesn’t belong to you, please " + "<a href=\"mailto:" + Config.SUPPORT_EMAIL + "?" + "body=Your name:%0AYour course:%0AYour university:\">" + "contact us</a> so that we can investigate.", e.getMessage());
    }
    ______TS("success: with encryption and new account to be created");
    logic.deleteAccount(correctStudentId);
    originalEmail = "email2@gmail.com";
    studentData = StudentAttributes.builder(courseId, "name", originalEmail).withSection("sectionName").withTeam("teamName").withComments("").build();
    studentsLogic.createStudentCascadeWithoutDocument(studentData);
    studentData = StudentsLogic.inst().getStudentForEmail(courseId, originalEmail);
    String encryptedKey = StringHelper.encrypt(studentData.key);
    accountsLogic.joinCourseForStudent(encryptedKey, correctStudentId);
    studentData.googleId = correctStudentId;
    verifyPresentInDatastore(studentData);
    assertEquals(correctStudentId, logic.getStudentForEmail(studentData.course, studentData.email).googleId);
    // check that we have the corresponding new account created.
    accountData.googleId = correctStudentId;
    accountData.email = originalEmail;
    accountData.name = "name";
    accountData.isInstructor = false;
    verifyPresentInDatastore(accountData);
    ______TS("success: join course as student does not revoke instructor status");
    // promote account to instructor
    accountsLogic.makeAccountInstructor(correctStudentId);
    // make the student 'unregistered' again
    studentData.googleId = "";
    studentsLogic.updateStudentCascadeWithoutDocument(studentData.email, studentData);
    assertEquals("", logic.getStudentForEmail(studentData.course, studentData.email).googleId);
    // rejoin
    logic.joinCourseForStudent(encryptedKey, correctStudentId);
    assertEquals(correctStudentId, logic.getStudentForEmail(studentData.course, studentData.email).googleId);
    // check if still instructor
    assertTrue(logic.isInstructor(correctStudentId));
    accountsLogic.deleteAccountCascade(correctStudentId);
    accountsLogic.deleteAccountCascade(existingId);
}
Also used : AccountAttributes(teammates.common.datatransfer.attributes.AccountAttributes) InvalidParametersException(teammates.common.exception.InvalidParametersException) JoinCourseException(teammates.common.exception.JoinCourseException) StudentProfileAttributes(teammates.common.datatransfer.attributes.StudentProfileAttributes) StudentAttributes(teammates.common.datatransfer.attributes.StudentAttributes) Test(org.testng.annotations.Test)

Example 59 with AccountAttributes

use of teammates.common.datatransfer.attributes.AccountAttributes in project teammates by TEAMMATES.

the class AccountsLogicTest method testJoinCourseForInstructor.

@SuppressWarnings("deprecation")
@Test
public void testJoinCourseForInstructor() throws Exception {
    InstructorAttributes instructor = dataBundle.instructors.get("instructorNotYetJoinCourse");
    String loggedInGoogleId = "AccLogicT.instr.id";
    String encryptedKey = instructorsLogic.getEncryptedKeyForInstructor(instructor.courseId, instructor.email);
    ______TS("failure: googleID belongs to an existing instructor in the course");
    try {
        accountsLogic.joinCourseForInstructor(encryptedKey, "idOfInstructorWithOnlyOneSampleCourse");
        signalFailureToDetectException();
    } catch (JoinCourseException e) {
        assertEquals(String.format(Const.StatusMessages.JOIN_COURSE_GOOGLE_ID_BELONGS_TO_DIFFERENT_USER, "idOfInstructorWithOnlyOneSampleCourse"), e.getMessage());
    }
    ______TS("success: instructor joined and new account be created");
    accountsLogic.joinCourseForInstructor(encryptedKey, loggedInGoogleId);
    InstructorAttributes joinedInstructor = instructorsLogic.getInstructorForEmail(instructor.courseId, instructor.email);
    assertEquals(loggedInGoogleId, joinedInstructor.googleId);
    AccountAttributes accountCreated = accountsLogic.getAccount(loggedInGoogleId);
    assertNotNull(accountCreated);
    ______TS("success: instructor joined but Account object creation goes wrong");
    // Delete account to simulate Account object creation goes wrong
    AccountsDb accountsDb = new AccountsDb();
    accountsDb.deleteAccount(loggedInGoogleId);
    // Try to join course again, Account object should be recreated
    accountsLogic.joinCourseForInstructor(encryptedKey, loggedInGoogleId);
    joinedInstructor = instructorsLogic.getInstructorForEmail(instructor.courseId, instructor.email);
    assertEquals(loggedInGoogleId, joinedInstructor.googleId);
    accountCreated = accountsLogic.getAccount(loggedInGoogleId);
    assertNotNull(accountCreated);
    accountsLogic.deleteAccountCascade(loggedInGoogleId);
    ______TS("success: instructor joined but account already exists");
    AccountAttributes nonInstrAccount = dataBundle.accounts.get("student1InCourse1");
    InstructorAttributes newIns = InstructorAttributes.builder(null, instructor.courseId, nonInstrAccount.name, nonInstrAccount.email).build();
    instructorsLogic.createInstructor(newIns);
    encryptedKey = instructorsLogic.getEncryptedKeyForInstructor(instructor.courseId, nonInstrAccount.email);
    assertFalse(accountsLogic.getAccount(nonInstrAccount.googleId).isInstructor);
    accountsLogic.joinCourseForInstructor(encryptedKey, nonInstrAccount.googleId);
    joinedInstructor = instructorsLogic.getInstructorForEmail(instructor.courseId, nonInstrAccount.email);
    assertEquals(nonInstrAccount.googleId, joinedInstructor.googleId);
    assertTrue(accountsLogic.getAccount(nonInstrAccount.googleId).isInstructor);
    instructorsLogic.verifyInstructorExists(nonInstrAccount.googleId);
    ______TS("success: instructor join and assigned institute when some instructors have not joined course");
    instructor = dataBundle.instructors.get("instructor4");
    newIns = InstructorAttributes.builder(null, instructor.courseId, "anInstructorWithoutGoogleId", "anInstructorWithoutGoogleId@gmail.com").build();
    instructorsLogic.createInstructor(newIns);
    nonInstrAccount = dataBundle.accounts.get("student2InCourse1");
    nonInstrAccount.email = "newInstructor@gmail.com";
    nonInstrAccount.name = " newInstructor";
    nonInstrAccount.googleId = "newInstructorGoogleId";
    newIns = InstructorAttributes.builder(null, instructor.courseId, nonInstrAccount.name, nonInstrAccount.email).build();
    instructorsLogic.createInstructor(newIns);
    encryptedKey = instructorsLogic.getEncryptedKeyForInstructor(instructor.courseId, nonInstrAccount.email);
    accountsLogic.joinCourseForInstructor(encryptedKey, nonInstrAccount.googleId);
    joinedInstructor = instructorsLogic.getInstructorForEmail(instructor.courseId, nonInstrAccount.email);
    assertEquals(nonInstrAccount.googleId, joinedInstructor.googleId);
    instructorsLogic.verifyInstructorExists(nonInstrAccount.googleId);
    AccountAttributes instructorAccount = accountsLogic.getAccount(nonInstrAccount.googleId);
    assertEquals("TEAMMATES Test Institute 1", instructorAccount.institute);
    accountsLogic.deleteAccountCascade(nonInstrAccount.googleId);
    ______TS("failure: instructor already joined");
    nonInstrAccount = dataBundle.accounts.get("student1InCourse1");
    instructor = dataBundle.instructors.get("instructorNotYetJoinCourse");
    encryptedKey = instructorsLogic.getEncryptedKeyForInstructor(instructor.courseId, nonInstrAccount.email);
    joinedInstructor = instructorsLogic.getInstructorForEmail(instructor.courseId, nonInstrAccount.email);
    try {
        accountsLogic.joinCourseForInstructor(encryptedKey, joinedInstructor.googleId);
        signalFailureToDetectException();
    } catch (JoinCourseException e) {
        assertEquals(joinedInstructor.googleId + " has already joined this course", e.getMessage());
    }
    ______TS("failure: key belongs to a different user");
    try {
        accountsLogic.joinCourseForInstructor(encryptedKey, "otherUserId");
        signalFailureToDetectException();
    } catch (JoinCourseException e) {
        assertEquals("The join link used belongs to a different user whose " + "Google ID is stude..ourse1 (only part of the Google ID is " + "shown to protect privacy). If that Google ID is owned by you, " + "please logout and re-login using that Google account. " + "If it doesn’t belong to you, please " + "<a href=\"mailto:" + Config.SUPPORT_EMAIL + "?" + "body=Your name:%0AYour course:%0AYour university:\">" + "contact us</a> so that we can investigate.", e.getMessage());
    }
    ______TS("failure: invalid key");
    String invalidKey = StringHelper.encrypt("invalidKey");
    try {
        accountsLogic.joinCourseForInstructor(invalidKey, loggedInGoogleId);
        signalFailureToDetectException();
    } catch (JoinCourseException e) {
        assertEquals("You have used an invalid join link: " + "/page/instructorCourseJoin?key=" + invalidKey, e.getMessage());
    }
}
Also used : AccountAttributes(teammates.common.datatransfer.attributes.AccountAttributes) AccountsDb(teammates.storage.api.AccountsDb) JoinCourseException(teammates.common.exception.JoinCourseException) InstructorAttributes(teammates.common.datatransfer.attributes.InstructorAttributes) Test(org.testng.annotations.Test)

Example 60 with AccountAttributes

use of teammates.common.datatransfer.attributes.AccountAttributes in project teammates by TEAMMATES.

the class StudentFeedbackResultsPageDataTest method testAll.

@Test
public void testAll() throws EntityDoesNotExistException {
    ______TS("typical success case");
    AccountAttributes account = dataBundle.accounts.get("student1InCourse1");
    StudentAttributes student = dataBundle.students.get("student1InCourse1");
    assertNotNull(student);
    String dummyKey = "key123";
    student.key = dummyKey;
    Logic logic = new Logic();
    StudentFeedbackResultsPageData pageData = new StudentFeedbackResultsPageData(account, student, dummySessionToken);
    Map<FeedbackQuestionAttributes, List<FeedbackResponseAttributes>> questionsWithResponses = new LinkedHashMap<>();
    FeedbackQuestionAttributes question1 = dataBundle.feedbackQuestions.get("qn1InSession1InCourse1");
    assertNotNull(question1);
    FeedbackQuestionAttributes question2 = dataBundle.feedbackQuestions.get("qn2InSession1InCourse1");
    assertNotNull(question2);
    List<FeedbackResponseAttributes> responsesForQ1 = new ArrayList<>();
    List<FeedbackResponseAttributes> responsesForQ2 = new ArrayList<>();
    /* Question 1 with responses */
    responsesForQ1.add(dataBundle.feedbackResponses.get("response1ForQ1S1C1"));
    questionsWithResponses.put(question1, responsesForQ1);
    /* Question 2 with responses */
    responsesForQ2.add(dataBundle.feedbackResponses.get("response1ForQ2S1C1"));
    responsesForQ2.add(dataBundle.feedbackResponses.get("response2ForQ2S1C1"));
    questionsWithResponses.put(question2, responsesForQ2);
    // need to obtain questionId and responseId as methods in FeedbackSessionResultsBundle require them
    questionsWithResponses = getActualQuestionsAndResponsesWithId(logic, questionsWithResponses);
    pageData.setBundle(logic.getFeedbackSessionResultsForStudent(question1.feedbackSessionName, question1.courseId, student.email));
    pageData.init(questionsWithResponses);
    StudentFeedbackResultsQuestionWithResponses questionBundle1 = pageData.getFeedbackResultsQuestionsWithResponses().get(0);
    StudentFeedbackResultsQuestionWithResponses questionBundle2 = pageData.getFeedbackResultsQuestionsWithResponses().get(1);
    assertNotNull(pageData.getFeedbackResultsQuestionsWithResponses());
    assertEquals(2, pageData.getFeedbackResultsQuestionsWithResponses().size());
    assertEquals("You are viewing feedback results as <span class='text-danger text-bold text-large'>" + "student1 In Course1</td></div>'\"</span>. You may submit feedback for sessions that are " + "currently open and view results without logging in. " + "To access other features you need <a href='/page/studentCourseJoinAuthentication?" + "key=" + StringHelper.encrypt(dummyKey) + "&studentemail=student1InCourse1%40gmail.tmt&courseid=idOfTypicalCourse1' class='link'>" + "to login using a Google account</a> (recommended).", pageData.getRegisterMessage());
    assertNotNull(questionBundle1.getQuestionDetails());
    assertNotNull(questionBundle2.getQuestionDetails());
    assertEquals("1", questionBundle1.getQuestionDetails().getQuestionIndex());
    assertEquals("2", questionBundle2.getQuestionDetails().getQuestionIndex());
    assertEquals("", questionBundle1.getQuestionDetails().getAdditionalInfo());
    assertEquals("", questionBundle2.getQuestionDetails().getAdditionalInfo());
    assertNotNull(questionBundle1.getResponseTables());
    assertNotNull(questionBundle2.getResponseTables());
    assertEquals("You", questionBundle1.getResponseTables().get(0).getRecipientName());
    assertNotNull(questionBundle1.getResponseTables().get(0).getResponses());
    assertEquals("You", questionBundle1.getResponseTables().get(0).getResponses().get(0).getGiverName());
    assertEquals("Student 1 self feedback.", questionBundle1.getResponseTables().get(0).getResponses().get(0).getAnswer());
    ______TS("student in unregistered course");
    student = dataBundle.students.get("student1InUnregisteredCourse");
    pageData = new StudentFeedbackResultsPageData(account, student, dummySessionToken);
    Map<FeedbackQuestionAttributes, List<FeedbackResponseAttributes>> questionsWithResponsesUnregistered = new LinkedHashMap<>();
    pageData.init(questionsWithResponsesUnregistered);
    assertTrue(pageData.getFeedbackResultsQuestionsWithResponses().isEmpty());
    assertEquals("regKeyForStuNotYetJoinCourse", student.key);
    assertEquals("idOfUnregisteredCourse", student.course);
    assertEquals("student1InUnregisteredCourse@gmail.tmt", student.email);
    assertEquals("You are viewing feedback results as " + "<span class='text-danger text-bold text-large'>student1 In " + "unregisteredCourse</span>. You may submit feedback for sessions that are currently open " + "and view results without logging in. To access other features you need " + "<a href='/page/studentCourseJoinAuthentication?key=" + StringHelper.encrypt("regKeyForStuNotYetJoinCourse") + "&studentemail=student1InUnregisteredCourse%40gmail.tmt&courseid=idOfUnregisteredCourse' " + "class='link'>to login using a Google account</a> (recommended).", pageData.getRegisterMessage());
}
Also used : AccountAttributes(teammates.common.datatransfer.attributes.AccountAttributes) ArrayList(java.util.ArrayList) StudentFeedbackResultsPageData(teammates.ui.pagedata.StudentFeedbackResultsPageData) StudentAttributes(teammates.common.datatransfer.attributes.StudentAttributes) LinkedHashMap(java.util.LinkedHashMap) StudentFeedbackResultsQuestionWithResponses(teammates.ui.template.StudentFeedbackResultsQuestionWithResponses) FeedbackResponseAttributes(teammates.common.datatransfer.attributes.FeedbackResponseAttributes) FeedbackQuestionAttributes(teammates.common.datatransfer.attributes.FeedbackQuestionAttributes) ArrayList(java.util.ArrayList) List(java.util.List) Logic(teammates.logic.api.Logic) Test(org.testng.annotations.Test)

Aggregations

AccountAttributes (teammates.common.datatransfer.attributes.AccountAttributes)84 Test (org.testng.annotations.Test)53 InstructorAttributes (teammates.common.datatransfer.attributes.InstructorAttributes)28 CourseAttributes (teammates.common.datatransfer.attributes.CourseAttributes)16 StudentAttributes (teammates.common.datatransfer.attributes.StudentAttributes)15 ArrayList (java.util.ArrayList)13 StudentProfileAttributes (teammates.common.datatransfer.attributes.StudentProfileAttributes)11 HashMap (java.util.HashMap)7 FeedbackSessionAttributes (teammates.common.datatransfer.attributes.FeedbackSessionAttributes)6 InvalidParametersException (teammates.common.exception.InvalidParametersException)6 UnauthorizedAccessException (teammates.common.exception.UnauthorizedAccessException)5 CourseDetailsBundle (teammates.common.datatransfer.CourseDetailsBundle)4 EntityDoesNotExistException (teammates.common.exception.EntityDoesNotExistException)4 EmailWrapper (teammates.common.util.EmailWrapper)4 RedirectResult (teammates.ui.controller.RedirectResult)4 List (java.util.List)3 EmailGenerator (teammates.logic.api.EmailGenerator)3 AccountsDb (teammates.storage.api.AccountsDb)3 Account (teammates.storage.entity.Account)3 ShowPageResult (teammates.ui.controller.ShowPageResult)3