-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into migrate-delete-account-request-action-test
- Loading branch information
Showing
3 changed files
with
252 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
src/test/java/teammates/sqlui/webapi/GetAccountsActionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
src/test/java/teammates/sqlui/webapi/MarkNotificationAsReadActionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |