Skip to content

Commit

Permalink
more coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
grada84 committed Nov 4, 2024
1 parent 38368a6 commit 7b5dbfa
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 72 deletions.
66 changes: 34 additions & 32 deletions src/test/java/net/lacnic/portal/auth/client/LoginDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;
Expand All @@ -20,55 +21,56 @@ void setUp() {
}

@Test
void defaultConstructor_initializesFieldsCorrectly() {
assertFalse(loginData.isAuthenticated());
assertEquals("", loginData.getUsername());
assertNotNull(loginData.getRoles());
assertTrue(loginData.getRoles().isEmpty());
assertEquals("", loginData.getError());
void testDefaultConstructor() {
assertFalse(loginData.isAuthenticated(), "Authenticated should be false by default");
assertEquals("", loginData.getUsername(), "Username should be an empty string by default");
assertNotNull(loginData.getRoles(), "Roles should be initialized");
assertTrue(loginData.getRoles().isEmpty(), "Roles should be empty by default");
assertEquals("", loginData.getError(), "Error should be an empty string by default");
}

@Test
void constructorWithError_initializesFieldsCorrectly() {
String errorMessage = "Authentication failed";
loginData = new LoginData(errorMessage);

assertFalse(loginData.isAuthenticated());
assertEquals("", loginData.getUsername());
assertNotNull(loginData.getRoles());
assertTrue(loginData.getRoles().isEmpty());
assertEquals(errorMessage, loginData.getError());
void testErrorConstructor() {
String error = "Invalid login";
loginData = new LoginData(error);
assertFalse(loginData.isAuthenticated(), "Authenticated should be false when error is set");
assertEquals("", loginData.getUsername(), "Username should be empty by default with error constructor");
assertNotNull(loginData.getRoles(), "Roles should be initialized");
assertTrue(loginData.getRoles().isEmpty(), "Roles should be empty by default with error constructor");
assertEquals(error, loginData.getError(), "Error should be set to the provided error message");
}

@Test
void setAuthenticated_setsAuthenticatedFieldCorrectly() {
void testSetAuthenticated() {
loginData.setAuthenticated(true);
assertTrue(loginData.isAuthenticated());

loginData.setAuthenticated(false);
assertFalse(loginData.isAuthenticated());
assertTrue(loginData.isAuthenticated(), "Authenticated should be true after setting it");
}

@Test
void setUsername_setsUsernameFieldCorrectly() {
String username = "testUser";
void testSetUsername() {
String username = "user1";
loginData.setUsername(username);
assertEquals(username, loginData.getUsername());
assertEquals(username, loginData.getUsername(), "Username should be set correctly");
}

@Test
void testSetRoles() {
loginData.setRoles(Arrays.asList("admin", "user"));
assertEquals(2, loginData.getRoles().size(), "Roles list size should match the assigned list");
assertTrue(loginData.getRoles().contains("admin"), "Roles should contain 'admin'");
assertTrue(loginData.getRoles().contains("user"), "Roles should contain 'user'");
}

@Test
void setRoles_setsRolesFieldCorrectly() {
loginData.setRoles(Arrays.asList("ROLE_USER", "ROLE_ADMIN"));
assertNotNull(loginData.getRoles());
assertEquals(2, loginData.getRoles().size());
assertTrue(loginData.getRoles().contains("ROLE_USER"));
assertTrue(loginData.getRoles().contains("ROLE_ADMIN"));
void testSetRolesWithNull() {
loginData.setRoles(null);
assertNull(loginData.getRoles(), "Roles should be null if set to null explicitly");
}

@Test
void setError_setsErrorFieldCorrectly() {
String error = "Some error";
void testSetError() {
String error = "Login failed";
loginData.setError(error);
assertEquals(error, loginData.getError());
assertEquals(error, loginData.getError(), "Error message should match the set value");
}
}
126 changes: 86 additions & 40 deletions src/test/java/net/lacnic/portal/auth/client/TokenDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;

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

class TokenDataTest {
public class TokenDataTest {

private TokenData tokenData;

@BeforeEach
Expand All @@ -19,64 +21,108 @@ void setUp() {
}

@Test
void defaultConstructor_initializesFieldsCorrectly() {
assertFalse(tokenData.isAuthenticated());
assertTrue(tokenData.getToken().isEmpty());
assertNotNull(tokenData.getRoles());
assertTrue(tokenData.getRoles().isEmpty());
assertTrue(tokenData.getError().isEmpty());
assertTrue(tokenData.getIpAllowed().isEmpty());
void testDefaultConstructor() {
assertFalse(tokenData.getAuthenticated(), "Authenticated should be false by default");
assertEquals("", tokenData.getToken(), "Token should be an empty string by default");
assertNotNull(tokenData.getRoles(), "Roles list should be initialized");
assertTrue(tokenData.getRoles().isEmpty(), "Roles list should be empty by default");
assertEquals("", tokenData.getIpAllowed(), "IP allowed should be an empty string by default");
assertEquals("", tokenData.getError(), "Error should be empty by default");
}

@Test
void constructorWithError_initializesFieldsCorrectly() {
String errorMessage = "Error occurred";
void testConstructorWithError() {
String errorMessage = "Test error";
tokenData = new TokenData(errorMessage);

assertFalse(tokenData.isAuthenticated());
assertTrue(tokenData.getToken().isEmpty());
assertNotNull(tokenData.getRoles());
assertTrue(tokenData.getRoles().isEmpty());
assertEquals(errorMessage, tokenData.getError());
assertTrue(tokenData.getIpAllowed().isEmpty());
assertFalse(tokenData.getAuthenticated(), "Authenticated should be false");
assertEquals("", tokenData.getToken(), "Token should be an empty string");
assertNotNull(tokenData.getRoles(), "Roles list should be initialized");
assertTrue(tokenData.getRoles().isEmpty(), "Roles list should be empty");
assertEquals("", tokenData.getIpAllowed(), "IP allowed should be an empty string");
assertEquals(errorMessage, tokenData.getError(), "Error should match the provided message");
}

@Test
void testConstructorWithRolesTokenIpAllowed() {
List<String> roles = new ArrayList<>();
roles.add("USER");
String token = "sampleToken";
String ipAllowed = "127.0.0.1";

tokenData = new TokenData(roles, token, ipAllowed);

assertTrue(tokenData.getAuthenticated(), "Authenticated should be true if roles are provided");
assertEquals(roles, tokenData.getRoles(), "Roles should match the provided list");
assertEquals(token, tokenData.getToken(), "Token should match the provided token");
assertEquals(ipAllowed, tokenData.getIpAllowed(), "IP allowed should match the provided IP");
assertEquals("", tokenData.getError(), "Error should be empty if authenticated");
}

@Test
void testConstructorWithEmptyRoles() {
List<String> roles = new ArrayList<>();
String token = "sampleToken";
String ipAllowed = "127.0.0.1";

tokenData = new TokenData(roles, token, ipAllowed);

assertFalse(tokenData.getAuthenticated(), "Authenticated should be false if roles are empty");
assertEquals("No existen roles asociados a este token", tokenData.getError(), "Error message should indicate no roles are associated");
}

@Test
void setAuthenticated_setsAuthenticatedFieldCorrectly() {
void testSetAuthenticated() {
tokenData.setAuthenticated(true);
assertTrue(tokenData.isAuthenticated());
assertTrue(tokenData.getAuthenticated(), "Authenticated should be true after setting it to true");
}

@Test
void testSetRoles() {
List<String> roles = new ArrayList<>();
roles.add("ADMIN");
tokenData.setRoles(roles);

tokenData.setAuthenticated(false);
assertFalse(tokenData.isAuthenticated());
assertEquals(roles, tokenData.getRoles(), "Roles should match the provided list");
}

@Test
void setToken_setsTokenFieldCorrectly() {
String token = "testToken";
tokenData.setToken(token);
assertEquals(token, tokenData.getToken());
void testSetError() {
String errorMessage = "Custom error";
tokenData.setError(errorMessage);

assertEquals(errorMessage, tokenData.getError(), "Error should match the provided message");
}

@Test
void setRoles_setsRolesFieldCorrectly() {
tokenData.setRoles(Arrays.asList("ROLE_USER", "ROLE_ADMIN"));
assertNotNull(tokenData.getRoles());
assertEquals(2, tokenData.getRoles().size());
assertTrue(tokenData.getRoles().contains("ROLE_USER"));
assertTrue(tokenData.getRoles().contains("ROLE_ADMIN"));
void testSetToken() {
String token = "newToken";
tokenData.setToken(token);

assertEquals(token, tokenData.getToken(), "Token should match the provided token");
}

@Test
void setError_setsErrorFieldCorrectly() {
String error = "Some error";
tokenData.setError(error);
assertEquals(error, tokenData.getError());
void testSetIpAllowed() {
String ipAllowed = "192.168.1.1";
tokenData.setIpAllowed(ipAllowed);

assertEquals(ipAllowed, tokenData.getIpAllowed(), "IP allowed should match the provided IP");
}

@Test
void setIpAllowed_setsIpAllowedFieldCorrectly() {
String ip = "192.168.0.1";
tokenData.setIpAllowed(ip);
assertEquals(ip, tokenData.getIpAllowed());
void testToString() {
List<String> roles = new ArrayList<>();
roles.add("USER");
tokenData.setRoles(roles);
tokenData.setAuthenticated(true);
tokenData.setToken("sampleToken");
tokenData.setError("No error");
tokenData.setIpAllowed("127.0.0.1");

assertTrue(tokenData.toString().contains("USER"));
assertTrue(tokenData.toString().contains("sampleToken"));
assertTrue(tokenData.toString().contains("No error"));
assertTrue(tokenData.toString().contains("127.0.0.1"));
}
}
}

0 comments on commit 7b5dbfa

Please sign in to comment.