Search in sources :

Example 1 with Key

use of io.lavagna.model.Key in project lavagna by digitalfondue.

the class AnonymousUserFilterTest method testAnonymousUserEnabled.

@Test
public void testAnonymousUserEnabled() throws IOException, ServletException {
    AnonymousUserFilter auf = new AnonymousUserFilter();
    MockHttpServletRequest request = new MockHttpServletRequest();
    Map<Key, String> conf = new EnumMap<>(Key.class);
    conf.put(Key.SETUP_COMPLETE, "true");
    conf.put(Key.ENABLE_ANON_USER, "true");
    when(configurationRepository.findConfigurationFor(Mockito.<Set<Key>>any())).thenReturn(conf);
    when(sessionHandler.isUserAuthenticated(request)).thenReturn(Boolean.FALSE);
    when(users.findUserByName("system", "anonymous")).thenReturn(user);
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain chain = new MockFilterChain();
    auf.init(filterConfig);
    auf.doFilterInternal(request, response, chain);
    Mockito.verify(users).findUserByName("system", "anonymous");
    Mockito.verify(sessionHandler).isUserAuthenticated(request);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockFilterChain(org.springframework.mock.web.MockFilterChain) EnumMap(java.util.EnumMap) Key(io.lavagna.model.Key) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 2 with Key

use of io.lavagna.model.Key in project lavagna by digitalfondue.

the class AnonymousUserFilterTest method testAnonymousUserNotEnabled.

@Test
public void testAnonymousUserNotEnabled() throws IOException, ServletException {
    AnonymousUserFilter auf = new AnonymousUserFilter();
    MockHttpServletRequest request = new MockHttpServletRequest();
    Map<Key, String> conf = new EnumMap<>(Key.class);
    conf.put(Key.SETUP_COMPLETE, "true");
    conf.put(Key.ENABLE_ANON_USER, "false");
    when(configurationRepository.findConfigurationFor(Mockito.<Set<Key>>any())).thenReturn(conf);
    when(sessionHandler.isUserAuthenticated(request)).thenReturn(Boolean.TRUE);
    when(sessionHandler.isUserAnonymous(request)).thenReturn(Boolean.TRUE);
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain chain = new MockFilterChain();
    auf.init(filterConfig);
    auf.doFilterInternal(request, response, chain);
    Mockito.verify(sessionHandler).isUserAuthenticated(request);
    Mockito.verify(sessionHandler).isUserAnonymous(request);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockFilterChain(org.springframework.mock.web.MockFilterChain) EnumMap(java.util.EnumMap) Key(io.lavagna.model.Key) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 3 with Key

use of io.lavagna.model.Key in project lavagna by digitalfondue.

the class RememberMeFilterTest method testUnconfiguredSetup.

@Test
public void testUnconfiguredSetup() throws IOException, ServletException {
    RememberMeFilter rmf = new RememberMeFilter();
    MockHttpServletRequest request = new MockHttpServletRequest();
    Map<Key, String> conf = new EnumMap<>(Key.class);
    conf.put(Key.SETUP_COMPLETE, "false");
    when(configurationRepository.findConfigurationFor(Mockito.<Set<Key>>any())).thenReturn(conf);
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain chain = new MockFilterChain();
    rmf.init(filterConfig);
    rmf.doFilterInternal(request, response, chain);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockFilterChain(org.springframework.mock.web.MockFilterChain) EnumMap(java.util.EnumMap) Key(io.lavagna.model.Key) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 4 with Key

use of io.lavagna.model.Key in project lavagna by digitalfondue.

the class RememberMeFilterTest method testRememberMeTokenNotExists.

@Test
public void testRememberMeTokenNotExists() throws IOException, ServletException {
    RememberMeFilter rmf = new RememberMeFilter();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.getSession().setAttribute(AUTH_KEY, false);
    Cookie cookie = new Cookie(CookieNames.getRememberMeCookieName(), "2/056a8421-7448-4753-a932-13dc7e4cd510");
    request.setCookies(cookie);
    Map<Key, String> conf = new EnumMap<>(Key.class);
    conf.put(Key.SETUP_COMPLETE, "true");
    when(userRepository.rememberMeTokenExists(Mockito.eq(2), Mockito.eq("056a8421-7448-4753-a932-13dc7e4cd510"))).thenReturn(Boolean.FALSE);
    when(configurationRepository.findConfigurationFor(Mockito.<Set<Key>>any())).thenReturn(conf);
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain chain = new MockFilterChain();
    rmf.init(filterConfig);
    rmf.doFilterInternal(request, response, chain);
    Mockito.verify(userRepository).rememberMeTokenExists(Mockito.eq(2), Mockito.eq("056a8421-7448-4753-a932-13dc7e4cd510"));
}
Also used : Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockFilterChain(org.springframework.mock.web.MockFilterChain) EnumMap(java.util.EnumMap) Key(io.lavagna.model.Key) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 5 with Key

use of io.lavagna.model.Key in project lavagna by digitalfondue.

the class ConfigurationRepository method findConfigurationFor.

public Map<Key, String> findConfigurationFor(Set<Key> keys) {
    Set<String> s = new HashSet<>();
    Map<Key, String> res = new EnumMap<>(Key.class);
    for (Key k : keys) {
        s.add(k.toString());
        res.put(k, null);
    }
    for (ConfigurationKeyValue kv : queries.findConfigurationFor(s)) {
        res.put(kv.getFirst(), kv.getSecond());
    }
    return res;
}
Also used : ConfigurationKeyValue(io.lavagna.model.ConfigurationKeyValue) Key(io.lavagna.model.Key)

Aggregations

Key (io.lavagna.model.Key)8 EnumMap (java.util.EnumMap)7 Test (org.junit.Test)7 MockFilterChain (org.springframework.mock.web.MockFilterChain)7 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)7 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)7 Cookie (javax.servlet.http.Cookie)2 ConfigurationKeyValue (io.lavagna.model.ConfigurationKeyValue)1