Search in sources :

Example 1 with SessionMock

use of com.enonic.xp.session.SessionMock in project xp by enonic.

the class LoginHandlerTest method initialize.

@Override
public void initialize() throws Exception {
    super.initialize();
    this.securityService = Mockito.mock(SecurityService.class);
    addService(SecurityService.class, this.securityService);
    ContextAccessor.current().getLocalScope().setSession(new SessionMock());
}
Also used : SecurityService(com.enonic.xp.security.SecurityService) SessionMock(com.enonic.xp.session.SessionMock)

Example 2 with SessionMock

use of com.enonic.xp.session.SessionMock in project xp by enonic.

the class LoginHandlerTest method testSessionInvalidatedOnLogin.

@Test
public void testSessionInvalidatedOnLogin() {
    final AuthenticationInfo authInfo = TestDataFixtures.createAuthenticationInfo();
    final IdProviders idProviders = IdProviders.from(IdProvider.create().displayName("system").key(IdProviderKey.from("system")).build());
    Mockito.when(this.securityService.authenticate(Mockito.any())).thenReturn(authInfo);
    Mockito.when(this.securityService.getIdProviders()).thenReturn(idProviders);
    final SessionMock session = Mockito.spy(new SessionMock());
    ContextAccessor.current().getLocalScope().setSession(session);
    runScript("/lib/xp/examples/auth/login.js");
    verify(session, times(5)).invalidate();
}
Also used : IdProviders(com.enonic.xp.security.IdProviders) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) SessionMock(com.enonic.xp.session.SessionMock) Test(org.junit.jupiter.api.Test)

Example 3 with SessionMock

use of com.enonic.xp.session.SessionMock in project xp by enonic.

the class IdProviderRequestWrapperTest method isUserInRole.

@Test
void isUserInRole() {
    final User user = User.create().key(PrincipalKey.ofUser(IdProviderKey.createDefault(), "userId")).login("usr").build();
    final AuthenticationInfo authenticationInfo = AuthenticationInfo.create().user(user).principals(RoleKeys.ADMIN).build();
    final Context context = ContextBuilder.create().build();
    final Session session = new SessionMock();
    context.getLocalScope().setSession(session);
    session.setAttribute(authenticationInfo);
    final Boolean isAdmin = context.callWith(() -> new IdProviderRequestWrapper(request).isUserInRole(RoleKeys.ADMIN.getId()));
    assertTrue(isAdmin);
    verifyNoInteractions(request);
}
Also used : Context(com.enonic.xp.context.Context) User(com.enonic.xp.security.User) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Session(com.enonic.xp.session.Session) SessionMock(com.enonic.xp.session.SessionMock) Test(org.junit.jupiter.api.Test)

Example 4 with SessionMock

use of com.enonic.xp.session.SessionMock in project xp by enonic.

the class IdProviderRequestWrapperTest method getUserPrincipal.

@Test
void getUserPrincipal() {
    final User user = User.create().key(PrincipalKey.ofUser(IdProviderKey.createDefault(), "userId")).login("usr").build();
    final AuthenticationInfo authenticationInfo = AuthenticationInfo.create().user(user).build();
    final Context context = ContextBuilder.create().build();
    final Session session = new SessionMock();
    context.getLocalScope().setSession(session);
    session.setAttribute(authenticationInfo);
    final Principal principal = context.callWith(() -> new IdProviderRequestWrapper(request).getUserPrincipal());
    assertEquals(principal, user);
    verifyNoInteractions(request);
}
Also used : Context(com.enonic.xp.context.Context) User(com.enonic.xp.security.User) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Principal(java.security.Principal) Session(com.enonic.xp.session.Session) SessionMock(com.enonic.xp.session.SessionMock) Test(org.junit.jupiter.api.Test)

Example 5 with SessionMock

use of com.enonic.xp.session.SessionMock in project xp by enonic.

the class HasRoleHandlerTest method initialize.

@Override
public void initialize() throws Exception {
    super.initialize();
    this.session = new SessionMock();
    ContextAccessor.current().getLocalScope().setSession(session);
}
Also used : SessionMock(com.enonic.xp.session.SessionMock)

Aggregations

SessionMock (com.enonic.xp.session.SessionMock)15 Test (org.junit.jupiter.api.Test)9 AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)5 User (com.enonic.xp.security.User)4 Session (com.enonic.xp.session.Session)4 Context (com.enonic.xp.context.Context)3 SecurityService (com.enonic.xp.security.SecurityService)2 LocalScope (com.enonic.xp.context.LocalScope)1 IndexService (com.enonic.xp.index.IndexService)1 JsonMapGenerator (com.enonic.xp.script.serializer.JsonMapGenerator)1 IdProviders (com.enonic.xp.security.IdProviders)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Principal (java.security.Principal)1