Skip to content

Commit

Permalink
Merge branch 'master' into migrate-delete-account-request-action-test
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonqiu212 authored Feb 24, 2025
2 parents 3a55718 + d1930b5 commit e7581ca
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/teammates/ui/webapi/GetAccountsAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* Gets all accounts with the given email.
*/
class GetAccountsAction extends AdminOnlyAction {
public class GetAccountsAction extends AdminOnlyAction {

@Override
public JsonResult execute() {
Expand Down
128 changes: 128 additions & 0 deletions src/test/java/teammates/sqlui/webapi/GetAccountsActionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package teammates.sqlui.webapi;

import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.testng.annotations.Test;

import teammates.common.util.Const;
import teammates.storage.sqlentity.Account;
import teammates.ui.output.AccountData;
import teammates.ui.output.AccountsData;
import teammates.ui.webapi.GetAccountsAction;

/**
* SUT: {@link GetAccountsAction}.
*/
public class GetAccountsActionTest extends BaseActionTest<GetAccountsAction> {

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

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

private void verifyAccounts(List<AccountData> accountDataList, List<Account> accounts) {
assertEquals(accountDataList.size(), accounts.size());
for (int i = 0; i < accountDataList.size(); i++) {
AccountData accountData = accountDataList.get(i);
Account account = accounts.get(i);
assertEquals(accountData.getGoogleId(), account.getGoogleId());
assertEquals(accountData.getEmail(), account.getEmail());
assertEquals(accountData.getName(), account.getName());
}
}

@Test
void testExecute_multipleAccountsWithEmail_multipleAccountsFetched() {
List<Account> accounts = new ArrayList<>();
Account accountStub = getTypicalAccount();
Account anotherAccountStub = new Account("anotherGoogleId", "anotherEmail",
accountStub.getEmail());
accounts.add(accountStub);
accounts.add(anotherAccountStub);
when(mockLogic.getAccountsForEmail(accountStub.getEmail())).thenReturn(accounts);
String[] params = {
Const.ParamsNames.USER_EMAIL, accountStub.getEmail(),
};
GetAccountsAction action = getAction(params);
AccountsData actionOutput = (AccountsData) getJsonResult(action).getOutput();
List<AccountData> accountDataList = actionOutput.getAccounts();
verifyAccounts(accountDataList, accounts);
}

@Test
void testExecute_oneAccountWithEmail_accountFetched() {
List<Account> accounts = new ArrayList<>();
Account accountStub = getTypicalAccount();
accounts.add(accountStub);
when(mockLogic.getAccountsForEmail(accountStub.getEmail())).thenReturn(accounts);
String[] params = {
Const.ParamsNames.USER_EMAIL, accountStub.getEmail(),
};
GetAccountsAction action = getAction(params);
AccountsData actionOutput = (AccountsData) getJsonResult(action).getOutput();
List<AccountData> accountDataList = actionOutput.getAccounts();
verifyAccounts(accountDataList, accounts);
}

@Test
void testExecute_noAccountsWithEmail_noAccountsFetched() {
when(mockLogic.getAccountsForEmail("email")).thenReturn(Collections.emptyList());
String[] params = {
Const.ParamsNames.USER_EMAIL, "email",
};
GetAccountsAction action = getAction(params);
AccountsData actionOutput = (AccountsData) getJsonResult(action).getOutput();
List<AccountData> accountDataList = actionOutput.getAccounts();
verifyAccounts(accountDataList, Collections.emptyList());
}

@Test
void textExecute_invalidParams_throwsInvalidParametersException() {
String[] params = {
Const.ParamsNames.USER_EMAIL, null,
};
verifyHttpParameterFailure(params);

String[] params2 = {};

verifyHttpParameterFailure(params2);
}

@Test
void testSpecificAccessControl_admin_canAccess() {
loginAsAdmin();
verifyCanAccess();
}

@Test
void testSpecificAccessControl_instructor_cannotAccess() {
loginAsInstructor(getTypicalAccount().getGoogleId());
verifyCannotAccess();
}

@Test
void testSpecificAccessControl_student_cannotAccess() {
loginAsStudent(getTypicalAccount().getGoogleId());
verifyCannotAccess();
}

@Test
void testSpecificAccessControl_loggedOut_cannotAccess() {
verifyCannotAccess();

loginAsAdmin();
verifyCanAccess();

logoutUser();
verifyCannotAccess();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package teammates.sqlui.webapi;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

import java.util.List;
import java.util.UUID;

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

import teammates.common.exception.EntityDoesNotExistException;
import teammates.common.exception.InvalidParametersException;
import teammates.common.util.Const;
import teammates.storage.sqlentity.Instructor;
import teammates.storage.sqlentity.Notification;
import teammates.ui.output.ReadNotificationsData;
import teammates.ui.request.InvalidHttpRequestBodyException;
import teammates.ui.request.MarkNotificationAsReadRequest;
import teammates.ui.webapi.EntityNotFoundException;
import teammates.ui.webapi.JsonResult;
import teammates.ui.webapi.MarkNotificationAsReadAction;

/**
* SUT: {@link MarkNotificationAsReadAction}.
*/
public class MarkNotificationAsReadActionTest extends BaseActionTest<MarkNotificationAsReadAction> {
Instructor instructor;
String instructorId;
Notification testNotification;

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

@Override
String getRequestMethod() {
return POST;
}

@BeforeMethod
void setUp() {
instructor = getTypicalInstructor();
instructorId = instructor.getGoogleId();
testNotification = getTypicalNotificationWithId();
loginAsInstructor(instructorId);
}

@Test
protected void testExecute_markNotificationAsRead_shouldSucceed() throws Exception {
when(mockLogic.updateReadNotifications(
instructorId,
testNotification.getId(),
testNotification.getEndTime()
)).thenReturn(List.of(testNotification.getId()));

MarkNotificationAsReadRequest reqBody = new MarkNotificationAsReadRequest(
testNotification.getId().toString(), testNotification.getEndTime().toEpochMilli());

MarkNotificationAsReadAction action = getAction(reqBody);
JsonResult actionOutput = getJsonResult(action);

ReadNotificationsData response = (ReadNotificationsData) actionOutput.getOutput();
List<String> readNotifications = response.getReadNotifications();

assertTrue(readNotifications.contains(testNotification.getId().toString()));
}

@Test
protected void testExecute_markInvalidNotificationAsRead_shouldThrowIllegalArgumentError() {
MarkNotificationAsReadRequest reqBody =
new MarkNotificationAsReadRequest("invalid id", testNotification.getEndTime().toEpochMilli());

MarkNotificationAsReadAction action = getAction(reqBody);

assertThrows(IllegalArgumentException.class, () -> action.execute());
}

@Test
protected void testExecute_markNonExistentNotificationAsRead_shouldFail() throws Exception {
UUID nonExistentNotificationId = UUID.randomUUID();

MarkNotificationAsReadRequest reqBody =
new MarkNotificationAsReadRequest(
nonExistentNotificationId.toString(),
testNotification.getEndTime().toEpochMilli());

when(mockLogic.updateReadNotifications(
any(),
eq(nonExistentNotificationId),
eq(testNotification.getEndTime()))).thenThrow(new EntityDoesNotExistException(""));

MarkNotificationAsReadAction action = getAction(reqBody);

assertThrows(EntityNotFoundException.class, () -> action.execute());
}

@Test
protected void testExecute_notificationEndTimeIsZero_shouldFail() {
MarkNotificationAsReadRequest reqBody =
new MarkNotificationAsReadRequest(testNotification.getId().toString(), 0L);
verifyHttpRequestBodyFailure(reqBody);
}

@Test
protected void testExecute_markExpiredNotificationAsRead_shouldFail() throws Exception {
when(mockLogic.updateReadNotifications(
instructorId,
testNotification.getId(),
testNotification.getEndTime()
)).thenThrow(new InvalidParametersException("Trying to mark an expired notification as read."));

MarkNotificationAsReadRequest reqBody = new MarkNotificationAsReadRequest(
testNotification.getId().toString(),
testNotification.getEndTime().toEpochMilli());

MarkNotificationAsReadAction action = getAction(reqBody);

assertThrows(InvalidHttpRequestBodyException.class, () -> action.execute());
}
}

0 comments on commit e7581ca

Please sign in to comment.