package com.prueba.core.http.authentication;
public class AuthenticationInfo {
- private final String userName;
+ private final String username;
- public AuthenticationInfo(String userName) {
- this.userName = userName;
+ public AuthenticationInfo(String username) {
+ this.username = username;
}
- public String getUserName() {
- return userName;
+ public String getUsername() {
+ return username;
}
}
AuthenticationInfo authenticationInfo = BasicAuthenticationContext.getAuthentication();
if(authorizationService.isAuthorized(httpExchange.getRequestMethod(),
- httpExchange.getRequestURI().toString(), authenticationInfo.getUserName())) {
+ httpExchange.getRequestURI().toString(), authenticationInfo.getUsername())) {
apiController.handle(httpExchange);
protected void handleThrowable(HttpExchange httpExchange) throws IOException {
this.sessionHandler.handle(httpExchange);
- if (Sessions.getInstance().isValidSession(httpExchange)) {
+ if (Sessions.getInstance().isValidSession()) {
final SessionInfo sessionInfo = SessionContext.getSession();
if(authorizationService.isAuthorized(httpExchange.getRequestMethod(),
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.sun.net.httpserver.HttpExchange;
-
public class Sessions {
private static final Logger LOGGER = LoggerFactory.getLogger(Sessions.class);
sessions.remove(uuid);
}
- public boolean isValidSession(HttpExchange httpExchange) {
+ public boolean isValidSession() {
final SessionInfo sessionInfo = SessionContext.getSession();
boolean isValid = false;
final LoginFormImpl loginForm = new LoginFormImpl();
String html = "";
- if (Sessions.getInstance().isValidSession(httpExchange)) {
+ if (Sessions.getInstance().isValidSession()) {
html = loginForm.doNoRequiredLogin();
Sessions.getInstance().refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername());
} else {
public void processLoginPost(HttpExchange httpExchange) throws IOException {
final SessionInfo sessionInfo = SessionContext.getSession();
- if (!Sessions.getInstance().isValidSession(httpExchange)) {
+ if (!Sessions.getInstance().isValidSession()) {
String body = this.getBody(httpExchange);
String [] formData = body.split("&");
if (formData.length == 2) {
+++ /dev/null
-package com.prueba.core.context.security.handle;
-
-import static org.junit.Assert.*;
-
-import org.junit.Ignore;
-import org.junit.Test;
-
-@Ignore
-public class SessionHandleTest {
-
- @Test
- public void test() {
- fail("Not yet implemented");
- }
-
-}
--- /dev/null
+package com.prueba.core.http.authentication;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class AuthenticationInfoTest {
+ private static final String USERNAME = "GUMARTINM";
+
+ @Test
+ public void shouldCallGetters() {
+ AuthenticationInfo authenticationInfo = new AuthenticationInfo(USERNAME);
+
+ assertEquals(USERNAME, authenticationInfo.getUsername());
+ }
+
+}
package com.prueba.core.http.sessions;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
import java.time.LocalDateTime;
import java.util.UUID;
@Test
public void shouldCallGetters() {
- SessionInfo sessionInfo = new SessionInfo(UUID_VALUE, USERNAME, LAST_SESSION);
+ final SessionInfo sessionInfo = new SessionInfo(UUID_VALUE, USERNAME, LAST_SESSION);
assertEquals(UUID_VALUE, sessionInfo.getUUID());
assertEquals(USERNAME, sessionInfo.getUsername());
--- /dev/null
+package com.prueba.core.http.sessions;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.time.LocalDateTime;
+import java.util.UUID;
+
+import org.junit.Test;
+
+public class SessionsTest {
+ private static final String USERNAME = "GUMARTIN";
+
+ @Test
+ public void shouldCreateSessionSingleton() {
+ final Sessions sessions = Sessions.getInstance();
+
+ assertNotNull(sessions);
+ }
+
+ @Test
+ public void whenUUIDSessionIsNotPersistedThenReturnNull() {
+ final Sessions sessions = Sessions.getInstance();
+
+ assertNull(sessions.getSession(UUID.randomUUID()));
+ }
+
+ @Test
+ public void whenUUIDSessionIsPersistedThenReturnSession() {
+ final UUID uuidSession = UUID.randomUUID();
+ final Sessions sessions = Sessions.getInstance();
+ sessions.refreshSession(uuidSession, USERNAME);
+
+ SessionInfo sessionInfo = sessions.getSession(uuidSession);
+
+ assertEquals(USERNAME, sessionInfo.getUsername());
+ assertEquals(uuidSession, sessionInfo.getUUID());
+ }
+
+ @Test
+ public void whenThereIsNotSessionInThreadContextThenIsNotValidSession() {
+ final Sessions sessions = Sessions.getInstance();
+
+ assertFalse(sessions.isValidSession());
+ }
+
+ @Test
+ public void whenThereIsSessionInThreadContextAndSessionDidNotExpireThenIsValidSession() {
+ final SessionInfo sessionInfo = doSampleSessionInfo();
+ SessionContext.setSession(sessionInfo);
+ final Sessions sessions = Sessions.getInstance();
+
+ assertTrue(sessions.isValidSession());
+ }
+
+ private SessionInfo doSampleSessionInfo() {
+ return new SessionInfo(UUID.randomUUID(), USERNAME, LocalDateTime.now());
+ }
+}
--- /dev/null
+package com.prueba.model.domain;
+
+import static org.junit.Assert.*;
+
+import java.time.LocalDateTime;
+import java.util.UUID;
+
+import org.junit.Test;
+
+import com.prueba.core.http.sessions.SessionInfo;
+
+public class AccountResourceTest {
+ private static final String CODE = "GUMARTIN";
+ private static final String NAME = "Gustavo";
+ private static final String SURNAME = "Martin Morcuende";
+ private static final String PASSWORD = "lame";
+ private static final String ROLE = "MEGA_SUPER_ADMIN";
+
+ @Test
+ public void shouldCallGetters() {
+ final AccountResource accountResource = new AccountResource(CODE, NAME, SURNAME, PASSWORD, ROLE);
+
+ assertEquals(CODE, accountResource.getCode());
+ assertEquals(NAME, accountResource.getName());
+ assertEquals(SURNAME, accountResource.getSurname());
+ assertEquals(PASSWORD, accountResource.getPassword());
+ assertEquals(ROLE, accountResource.getRole());
+ }
+
+}
--- /dev/null
+package com.prueba.model.domain;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class ApplicationResourceResourceTest {
+ private static final String HTTPMETHOD = "GET";
+ private static final String URLPATTERN = "/app/pages/page_1.html";
+
+ @Test
+ public void shouldCallGetters() {
+ final ApplicationResourceResource applicationResource = new ApplicationResourceResource(URLPATTERN, HTTPMETHOD);
+
+ assertEquals(HTTPMETHOD, applicationResource.getHttpMethod());
+ assertEquals(URLPATTERN, applicationResource.getUrlPattern());
+ }
+}