Skip to content

Commit

Permalink
[#12048] Migrate Reset Account Action (#12204)
Browse files Browse the repository at this point in the history
  • Loading branch information
domlimm authored and samuelfangjw committed Mar 20, 2023
1 parent 3d8219a commit d82c685
Show file tree
Hide file tree
Showing 11 changed files with 577 additions and 30 deletions.
150 changes: 150 additions & 0 deletions src/it/java/teammates/it/sqllogic/core/UsersLogicIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package teammates.it.sqllogic.core;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import teammates.common.datatransfer.InstructorPermissionRole;
import teammates.common.datatransfer.InstructorPrivileges;
import teammates.common.exception.EntityAlreadyExistsException;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.common.util.Const;
import teammates.it.test.BaseTestCaseWithSqlDatabaseAccess;
import teammates.sqllogic.core.AccountsLogic;
import teammates.sqllogic.core.CoursesLogic;
import teammates.sqllogic.core.UsersLogic;
import teammates.storage.sqlentity.Account;
import teammates.storage.sqlentity.Course;
import teammates.storage.sqlentity.Instructor;
import teammates.storage.sqlentity.Student;

/**
* SUT: {@link UsersLogic}.
*/
public class UsersLogicIT extends BaseTestCaseWithSqlDatabaseAccess {

private final UsersLogic usersLogic = UsersLogic.inst();

private final AccountsLogic accountsLogic = AccountsLogic.inst();

private final CoursesLogic coursesLogic = CoursesLogic.inst();

private Course course;

private Account account;

@BeforeMethod
@Override
protected void setUp() throws Exception {
super.setUp();

course = getTypicalCourse();
coursesLogic.createCourse(course);

account = getTypicalAccount();
accountsLogic.createAccount(account);
}

@Test
public void testResetInstructorGoogleId()
throws InvalidParametersException, EntityAlreadyExistsException, EntityDoesNotExistException {
Instructor instructor = getTypicalInstructor();
instructor.setCourse(course);
instructor.setAccount(account);

String email = instructor.getEmail();
String courseId = instructor.getCourseId();
String googleId = instructor.getGoogleId();

______TS("success: reset instructor that does not exist");
assertThrows(EntityDoesNotExistException.class,
() -> usersLogic.resetInstructorGoogleId(email, courseId, googleId));

______TS("success: reset instructor that exists");
usersLogic.createInstructor(instructor);
usersLogic.resetInstructorGoogleId(email, courseId, googleId);

assertNull(instructor.getAccount());
assertEquals(0, accountsLogic.getAccountsForEmail(email).size());

______TS("found at least one other user with same googleId, should not delete account");
Account anotherAccount = getTypicalAccount();
accountsLogic.createAccount(anotherAccount);

instructor.setCourse(course);
instructor.setAccount(anotherAccount);

Student anotherUser = getTypicalStudent();
anotherUser.setCourse(course);
anotherUser.setAccount(anotherAccount);

usersLogic.createStudent(anotherUser);
usersLogic.resetInstructorGoogleId(email, courseId, googleId);

assertNull(instructor.getAccount());
assertEquals(anotherAccount, accountsLogic.getAccountForGoogleId(googleId));
}

@Test
public void testResetStudentGoogleId()
throws InvalidParametersException, EntityAlreadyExistsException, EntityDoesNotExistException {
Student student = getTypicalStudent();
student.setCourse(course);
student.setAccount(account);

String email = student.getEmail();
String courseId = student.getCourseId();
String googleId = student.getGoogleId();

______TS("success: reset student that does not exist");
assertThrows(EntityDoesNotExistException.class,
() -> usersLogic.resetStudentGoogleId(email, courseId, googleId));

______TS("success: reset student that exists");
usersLogic.createStudent(student);
usersLogic.resetStudentGoogleId(email, courseId, googleId);

assertNull(student.getAccount());
assertEquals(0, accountsLogic.getAccountsForEmail(email).size());

______TS("found at least one other user with same googleId, should not delete account");
Account anotherAccount = getTypicalAccount();
accountsLogic.createAccount(anotherAccount);

student.setCourse(course);
student.setAccount(anotherAccount);

Instructor anotherUser = getTypicalInstructor();
anotherUser.setCourse(course);
anotherUser.setAccount(anotherAccount);

usersLogic.createInstructor(anotherUser);
usersLogic.resetStudentGoogleId(email, courseId, googleId);

assertNull(student.getAccount());
assertEquals(anotherAccount, accountsLogic.getAccountForGoogleId(googleId));
}

private Course getTypicalCourse() {
return new Course("course-id", "course-name", Const.DEFAULT_TIME_ZONE, "teammates");
}

private Student getTypicalStudent() {
return new Student(course, "student-name", "[email protected]", "comments");
}

private Instructor getTypicalInstructor() {
InstructorPrivileges instructorPrivileges =
new InstructorPrivileges(Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER);
InstructorPermissionRole role = InstructorPermissionRole
.getEnum(Const.InstructorPermissionRoleNames.INSTRUCTOR_PERMISSION_ROLE_COOWNER);

return new Instructor(course, "instructor-name", "[email protected]",
true, Const.DEFAULT_DISPLAY_NAME_FOR_INSTRUCTOR, role, instructorPrivileges);
}

private Account getTypicalAccount() {
return new Account("google-id", "name", "[email protected]");
}

}
105 changes: 105 additions & 0 deletions src/it/java/teammates/it/ui/webapi/ResetAccountActionIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package teammates.it.ui.webapi;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import teammates.common.util.Const;
import teammates.common.util.HibernateUtil;
import teammates.storage.sqlentity.Course;
import teammates.storage.sqlentity.Instructor;
import teammates.storage.sqlentity.Student;
import teammates.ui.output.MessageOutput;
import teammates.ui.webapi.EntityNotFoundException;
import teammates.ui.webapi.JsonResult;
import teammates.ui.webapi.ResetAccountAction;

/**
* SUT: {@link ResetAccountAction}.
*/
public class ResetAccountActionIT extends BaseActionIT<ResetAccountAction> {

@Override
@BeforeMethod
protected void setUp() throws Exception {
super.setUp();
persistDataBundle(typicalBundle);
HibernateUtil.flushSession();
}

@Override
protected String getActionUri() {
return Const.ResourceURIs.ACCOUNT_RESET;
}

@Override
protected String getRequestMethod() {
return PUT;
}

@Test
@Override
protected void testExecute() {
Instructor instructor = typicalBundle.instructors.get("instructor1OfCourse1");
Student student = typicalBundle.students.get("student1InCourse1");

loginAsAdmin();

______TS("Typical Success Case with Student Email param given and Student exists");
String[] params = new String[] {
Const.ParamsNames.STUDENT_EMAIL, student.getEmail(),
Const.ParamsNames.COURSE_ID, student.getCourseId(),
};

ResetAccountAction resetAccountAction = getAction(params);
JsonResult actionOutput = getJsonResult(resetAccountAction);
MessageOutput response = (MessageOutput) actionOutput.getOutput();

assertEquals(response.getMessage(), "Account is successfully reset.");
assertNotNull(student);
assertNull(student.getAccount());
assertNull(student.getGoogleId());

______TS("Student Email param given but Student is non existent");
String invalidEmail = "[email protected]";
String[] invalidParams = new String[] {
Const.ParamsNames.STUDENT_EMAIL, invalidEmail,
Const.ParamsNames.COURSE_ID, student.getCourseId(),
};

EntityNotFoundException enfe = verifyEntityNotFound(invalidParams);
assertEquals("Student does not exist.", enfe.getMessage());

______TS("Typical Success Case with Instructor Email param given and Instructor exists");
params = new String[] {
Const.ParamsNames.INSTRUCTOR_EMAIL, instructor.getEmail(),
Const.ParamsNames.COURSE_ID, instructor.getCourseId(),
};

resetAccountAction = getAction(params);
actionOutput = getJsonResult(resetAccountAction);
response = (MessageOutput) actionOutput.getOutput();

assertEquals(response.getMessage(), "Account is successfully reset.");
assertNotNull(instructor);
assertNull(instructor.getAccount());
assertNull(instructor.getGoogleId());

______TS("Instructor Email param given but Instructor is non existent");
invalidParams = new String[] {
Const.ParamsNames.INSTRUCTOR_EMAIL, invalidEmail,
Const.ParamsNames.COURSE_ID, instructor.getCourseId(),
};

enfe = verifyEntityNotFound(invalidParams);
assertEquals("Instructor does not exist.", enfe.getMessage());
}

@Test
@Override
protected void testAccessControl() throws Exception {
Course course = typicalBundle.courses.get("course1");

verifyOnlyAdminCanAccess(course);
}

}
26 changes: 26 additions & 0 deletions src/main/java/teammates/sqllogic/api/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,32 @@ public List<Notification> getAllNotifications() {
return notificationsLogic.getAllNotifications();
}

/**
* Resets the googleId associated with the instructor.
*
* <br/>Preconditions: <br/>
* * All parameters are non-null.
*
* @throws EntityDoesNotExistException If instructor cannot be found with given email and courseId.
*/
public void resetInstructorGoogleId(String email, String courseId, String googleId)
throws EntityDoesNotExistException {
usersLogic.resetInstructorGoogleId(email, courseId, googleId);
}

/**
* Resets the googleId associated with the student.
*
* <br/>Preconditions: <br/>
* * All parameters are non-null.
*
* @throws EntityDoesNotExistException If student cannot be found with given email and courseId.
*/
public void resetStudentGoogleId(String email, String courseId, String googleId)
throws EntityDoesNotExistException {
usersLogic.resetStudentGoogleId(email, courseId, googleId);
}

/**
* Returns active notification for general users and the specified {@code targetUser}.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/teammates/sqllogic/core/LogicStarter.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static void initializeDependencies() {
fqLogic.initLogicDependencies(FeedbackQuestionsDb.inst());
notificationsLogic.initLogicDependencies(NotificationsDb.inst());
usageStatisticsLogic.initLogicDependencies(UsageStatisticsDb.inst());
usersLogic.initLogicDependencies(UsersDb.inst());
usersLogic.initLogicDependencies(UsersDb.inst(), accountsLogic);
log.info("Initialized dependencies between logic classes");
}

Expand Down
54 changes: 53 additions & 1 deletion src/main/java/teammates/sqllogic/core/UsersLogic.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package teammates.sqllogic.core;

import static teammates.common.util.Const.ERROR_UPDATE_NON_EXISTENT;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.UUID;

import teammates.common.exception.EntityAlreadyExistsException;
import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.storage.sqlapi.UsersDb;
import teammates.storage.sqlentity.Instructor;
Expand All @@ -24,6 +27,8 @@ public final class UsersLogic {

private UsersDb usersDb;

private AccountsLogic accountsLogic;

private UsersLogic() {
// prevent initialization
}
Expand All @@ -32,8 +37,9 @@ public static UsersLogic inst() {
return instance;
}

void initLogicDependencies(UsersDb usersDb) {
void initLogicDependencies(UsersDb usersDb, AccountsLogic accountsLogic) {
this.usersDb = usersDb;
this.accountsLogic = accountsLogic;
}

/**
Expand Down Expand Up @@ -205,6 +211,52 @@ public List<User> getAllUsersByGoogleId(String googleId) {
return usersDb.getAllUsersByGoogleId(googleId);
}

/**
* Resets the googleId associated with the instructor.
*/
public void resetInstructorGoogleId(String email, String courseId, String googleId)
throws EntityDoesNotExistException {
assert email != null;
assert courseId != null;
assert googleId != null;

Instructor instructor = getInstructorForEmail(courseId, email);

if (instructor == null) {
throw new EntityDoesNotExistException(ERROR_UPDATE_NON_EXISTENT
+ "Instructor [courseId=" + courseId + ", email=" + email + "]");
}

instructor.setAccount(null);

if (usersDb.getAllUsersByGoogleId(googleId).isEmpty()) {
accountsLogic.deleteAccountCascade(googleId);
}
}

/**
* Resets the googleId associated with the student.
*/
public void resetStudentGoogleId(String email, String courseId, String googleId)
throws EntityDoesNotExistException {
assert email != null;
assert courseId != null;
assert googleId != null;

Student student = getStudentForEmail(courseId, email);

if (student == null) {
throw new EntityDoesNotExistException(ERROR_UPDATE_NON_EXISTENT
+ "Student [courseId=" + courseId + ", email=" + email + "]");
}

student.setAccount(null);

if (usersDb.getAllUsersByGoogleId(googleId).isEmpty()) {
accountsLogic.deleteAccountCascade(googleId);
}
}

/**
* Sorts the instructors list alphabetically by name.
*/
Expand Down
Loading

0 comments on commit d82c685

Please sign in to comment.