Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#12048] Migrate Tests for FeedbackSessionOpenedRemindersActionTest #13215

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package teammates.sqlui.webapi;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import java.util.List;

import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import teammates.common.util.Const;
import teammates.common.util.EmailWrapper;
import teammates.common.util.RequestTracer;
import teammates.storage.sqlentity.FeedbackSession;
import teammates.ui.output.MessageOutput;
import teammates.ui.webapi.FeedbackSessionOpenedRemindersAction;

/**
* SUT: {@link FeedbackSessionOpenedRemindersAction}.
*/
public class FeedbackSessionOpenedRemindersActionTest extends BaseActionTest<FeedbackSessionOpenedRemindersAction> {

private FeedbackSession session;
private FeedbackSession session2;

@Override
protected String getActionUri() {
return Const.CronJobURIs.AUTOMATED_FEEDBACK_OPENED_REMINDERS;
}

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

@BeforeMethod
public void setUp() {
Mockito.reset(mockLogic, mockSqlEmailGenerator);

session = mock(FeedbackSession.class);
session2 = mock(FeedbackSession.class);
EmailWrapper mockEmail = mock(EmailWrapper.class);
EmailWrapper mockEmail2 = mock(EmailWrapper.class);

when(mockSqlEmailGenerator.generateFeedbackSessionOpenedEmails(session)).thenReturn(List.of(mockEmail));
when(mockSqlEmailGenerator.generateFeedbackSessionOpenedEmails(session2)).thenReturn(List.of(mockEmail2));
}

@Test
void testExecute_allSessionsOpened_emailsSent() {
when(mockLogic.getFeedbackSessionsWhichNeedOpenedEmailsToBeSent()).thenReturn(List.of(session, session2));

try (MockedStatic<RequestTracer> mockRequestTracer = mockStatic(RequestTracer.class)) {
FeedbackSessionOpenedRemindersAction action = getAction();
MessageOutput actionOutput = (MessageOutput) getJsonResult(action).getOutput();

verify(mockLogic, times(1)).getFeedbackSessionsWhichNeedOpenedEmailsToBeSent();
mockRequestTracer.verify(RequestTracer::checkRemainingTime, times(2));
verify(mockSqlEmailGenerator, times(1)).generateFeedbackSessionOpenedEmails(session);
verify(mockSqlEmailGenerator, times(1)).generateFeedbackSessionOpenedEmails(session2);
verify(session, times(1)).setOpenedEmailSent(true);
verify(session2, times(1)).setOpenedEmailSent(true);

verifySpecifiedTasksAdded(Const.TaskQueue.SEND_EMAIL_QUEUE_NAME, 2);
verifyNoMoreInteractions(mockLogic, mockSqlEmailGenerator, session, session2);
assertEquals("Successful", actionOutput.getMessage());
}
}

@Test
void testExecute_oneSessionOpened_emailsSent() {
when(mockLogic.getFeedbackSessionsWhichNeedOpenedEmailsToBeSent()).thenReturn(List.of(session));

try (MockedStatic<RequestTracer> mockRequestTracer = mockStatic(RequestTracer.class)) {
FeedbackSessionOpenedRemindersAction action = getAction();
MessageOutput actionOutput = (MessageOutput) getJsonResult(action).getOutput();

verify(mockLogic, times(1)).getFeedbackSessionsWhichNeedOpenedEmailsToBeSent();
mockRequestTracer.verify(RequestTracer::checkRemainingTime, times(1));
verify(mockSqlEmailGenerator, times(1)).generateFeedbackSessionOpenedEmails(session);
verify(session, times(1)).setOpenedEmailSent(true);

verifySpecifiedTasksAdded(Const.TaskQueue.SEND_EMAIL_QUEUE_NAME, 1);
verifyNoMoreInteractions(mockLogic, mockSqlEmailGenerator, session, session2);
assertEquals("Successful", actionOutput.getMessage());
}
}

@Test
void testExecute_noSessionsOpened_noEmailsSent() {
when(mockLogic.getFeedbackSessionsWhichNeedOpenedEmailsToBeSent()).thenReturn(List.of());

try (MockedStatic<RequestTracer> mockRequestTracer = mockStatic(RequestTracer.class)) {
FeedbackSessionOpenedRemindersAction action = getAction();
MessageOutput actionOutput = (MessageOutput) getJsonResult(action).getOutput();

verify(mockLogic, times(1)).getFeedbackSessionsWhichNeedOpenedEmailsToBeSent();
mockRequestTracer.verify(RequestTracer::checkRemainingTime, never());

verifyNoTasksAdded();
verifyNoMoreInteractions(mockLogic, mockSqlEmailGenerator, session, session2);
assertEquals("Successful", actionOutput.getMessage());
}
}

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

@Test
void testSpecificAccessControl_instructor_cannotAccess() {
loginAsInstructor("instructor-googleId");
verifyCannotAccess();
}

@Test
void testSpecificAccessControl_student_cannotAccess() {
loginAsStudent("student-googleId");
verifyCannotAccess();
}

@Test
void testSpecificAccessControl_loggedOut_cannotAccess() {
logoutUser();
verifyCannotAccess();
}
}