Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
grada84 committed Nov 4, 2024
1 parent 5a5aba0 commit b086c6f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/main/java/net/lacnic/portal/auth/client/UtilsLogin.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UtilsLogin {

private static final Logger logger = LoggerFactory.getLogger(UtilsLogin.class);

public static LoginData login(String username, String password) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
Expand All @@ -17,7 +22,7 @@ public static LoginData login(String username, String password) {
LoginData infoLDAP = PortalWSClient.getLoginData(username, password);
return infoLDAP;
} catch (Exception e) {
e.printStackTrace();
logger.error("An error occurred: {}", e.getMessage(), e);
return new LoginData("Error: verifique usuario y/o contraseña");
}
}
Expand Down
74 changes: 74 additions & 0 deletions src/test/java/net/lacnic/portal/auth/client/UtilsLoginTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package net.lacnic.portal.auth.client;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

import java.security.MessageDigest;
import java.util.Base64;

import javax.servlet.http.HttpServletRequest;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

public class UtilsLoginTest {

private HttpServletRequest mockRequest;

@BeforeEach
void setUp() {
mockRequest = mock(HttpServletRequest.class);
}

@Test
void testLogin_successfulLogin() throws Exception {
String username = "testUser";
String password = "testPassword";
String encodedPassword = "{SHA256}" + Base64.getEncoder().encodeToString(MessageDigest.getInstance("SHA-256").digest(password.getBytes()));

LoginData mockLoginData = new LoginData();
try (MockedStatic<PortalWSClient> mockedPortalWSClient = mockStatic(PortalWSClient.class); MockedStatic<MessageDigest> mockedMessageDigest = mockStatic(MessageDigest.class)) {

MessageDigest mockDigest = mock(MessageDigest.class);
when(mockDigest.digest()).thenReturn(Base64.getDecoder().decode(encodedPassword.substring(8)));
mockedMessageDigest.when(() -> MessageDigest.getInstance("SHA-256")).thenReturn(mockDigest);
mockedPortalWSClient.when(() -> PortalWSClient.getLoginData(username, encodedPassword)).thenReturn(mockLoginData);

LoginData result = UtilsLogin.login(username, password);

assertNotNull(result);
assertEquals(mockLoginData, result);
}
}

@Test
void testLogin_exceptionInDigest() {
String username = "testUser";
String password = "testPassword";

try (MockedStatic<MessageDigest> mockedMessageDigest = mockStatic(MessageDigest.class)) {
mockedMessageDigest.when(() -> MessageDigest.getInstance("SHA-256")).thenThrow(new RuntimeException("Digest error"));

LoginData result = UtilsLogin.login(username, password);

assertNotNull(result);
assertFalse(result.isAuthenticated());
assertEquals("Error: verifique usuario y/o contraseña", result.getError());
}
}

@Test
void testGetBearer_validToken() throws Exception {
when(mockRequest.getHeader("Authorization")).thenReturn("Bearer validToken");

String token = UtilsLogin.getBearer(mockRequest);

assertEquals("validToken", token);
}

}

0 comments on commit b086c6f

Please sign in to comment.