Copilot commented on code in PR #13871:
URL: https://github.com/apache/cloudstack/pull/13871#discussion_r3782507709


##########
server/src/test/java/com/cloud/api/ApiServletTest.java:
##########
@@ -326,8 +327,25 @@ public void testDoNotSkip2FAcheckForAPIs() {
 
     @Test
     public void testSkip2FAcheckForUserWhenAlreadyVerified() {
-        Mockito.when(session.getAttribute("userid")).thenReturn(1L);
-        
Mockito.when(session.getAttribute(ApiConstants.IS_2FA_VERIFIED)).thenReturn(true);
+        when(session.getAttribute("userid")).thenReturn(1L);
+        
when(session.getAttribute(ApiConstants.IS_2FA_VERIFIED)).thenReturn(true);
+
+        boolean result = servlet.skip2FAcheckForUser(session);
+        Assert.assertEquals(true, result);
+    }
+
+    @Test
+    public void testSkip2FAcheckForUserWhenVerifiedAttributeIsAbsent() {
+        servlet.accountMgr = accountMgr;
+        when(session.getAttribute("userid")).thenReturn(1L);
+        
when(session.getAttribute(ApiConstants.IS_2FA_VERIFIED)).thenReturn(null);
+        when(accountMgr.getUserAccountById(1L)).thenReturn(userAccount);
+        when(userAccount.getDomainId()).thenReturn(1L);
+        when(userAccount.isUser2faEnabled()).thenReturn(false);
+
+        ConfigKey<Boolean> enableUserTwoFactorAuthentication = 
Mockito.mock(ConfigKey.class);
+        AccountManagerImpl.enableUserTwoFactorAuthentication = 
enableUserTwoFactorAuthentication;
+        when(enableUserTwoFactorAuthentication.valueIn(1L)).thenReturn(false);
 
         boolean result = servlet.skip2FAcheckForUser(session);
         Assert.assertEquals(true, result);

Review Comment:
   This test mutates a global static 
(`AccountManagerImpl.enableUserTwoFactorAuthentication`) but doesn’t restore 
it, which can leak state across tests and cause order-dependent/flaky behavior. 
Save the previous value and restore it in a `try/finally` block within the test 
or in an `@After` method (and apply the same pattern anywhere else in this test 
class where static config keys are overwritten).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to