|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com). |
| 3 | + * |
| 4 | + * WSO2 LLC. licenses this file to you under the Apache License, |
| 5 | + * Version 2.0 (the "License"); you may not use this file except |
| 6 | + * in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, |
| 12 | + * software distributed under the License is distributed on an |
| 13 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | + * KIND, either express or implied. See the License for the |
| 15 | + * specific language governing permissions and limitations |
| 16 | + * under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.wso2.carbon.identity.conditional.auth.functions.user; |
| 20 | + |
| 21 | +import org.mockito.Mock; |
| 22 | +import org.testng.Assert; |
| 23 | +import org.testng.annotations.AfterMethod; |
| 24 | +import org.testng.annotations.BeforeMethod; |
| 25 | +import org.testng.annotations.Test; |
| 26 | +import org.wso2.carbon.context.PrivilegedCarbonContext; |
| 27 | +import org.wso2.carbon.identity.application.authentication.framework.UserSessionManagementService; |
| 28 | +import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.JsAuthenticatedUser; |
| 29 | +import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.js.graaljs.JsGraalAuthenticatedUser; |
| 30 | +import org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceDataHolder; |
| 31 | +import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser; |
| 32 | +import org.wso2.carbon.identity.application.authentication.framework.model.UserSession; |
| 33 | +import org.wso2.carbon.identity.common.testng.WithCarbonHome; |
| 34 | +import org.wso2.carbon.identity.common.testng.WithRealmService; |
| 35 | +import org.wso2.carbon.identity.conditional.auth.functions.user.internal.UserFunctionsServiceHolder; |
| 36 | +import org.wso2.carbon.identity.conditional.auth.functions.user.model.JsUserSession; |
| 37 | +import org.wso2.carbon.identity.core.util.IdentityTenantUtil; |
| 38 | +import org.wso2.carbon.identity.organization.management.service.OrganizationManager; |
| 39 | +import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException; |
| 40 | + |
| 41 | +import java.util.ArrayList; |
| 42 | +import java.util.List; |
| 43 | + |
| 44 | +import static org.mockito.Mockito.mock; |
| 45 | +import static org.mockito.Mockito.when; |
| 46 | +import static org.mockito.MockitoAnnotations.initMocks; |
| 47 | + |
| 48 | +/** |
| 49 | + * Unit tests for GetUserSessionsFunctionImpl class. |
| 50 | + */ |
| 51 | +@WithCarbonHome |
| 52 | +@WithRealmService(injectToSingletons = {UserFunctionsServiceHolder.class, IdentityTenantUtil.class, |
| 53 | + FrameworkServiceDataHolder.class}) |
| 54 | +public class GetUserSessionsFunctionImplTest { |
| 55 | + |
| 56 | + private static final String TEST_USER_NAME = "testUser"; |
| 57 | + private static final String TENANT_DOMAIN_CARBON_SUPER = "carbon.super"; |
| 58 | + private static final String USER_STORE_DOMAIN_PRIMARY = "PRIMARY"; |
| 59 | + private static final String TEST_USER_ID_123 = "test-user-id-123"; |
| 60 | + private static final String TEST_USER_ID_456 = "test-user-id-456"; |
| 61 | + private static final String TEST_USER_ID_ORG_123 = "test-user-id-org-123"; |
| 62 | + private static final String TEST_USER_ID_ORG_456 = "test-user-id-org-456"; |
| 63 | + private static final String ORG_ID_123 = "org123"; |
| 64 | + private static final String ORG_TENANT_DOMAIN = "org.tenant.com"; |
| 65 | + private static final String INVALID_ORG = "invalidOrg"; |
| 66 | + private static final String ORG_NOT_FOUND_MESSAGE = "Organization not found"; |
| 67 | + |
| 68 | + @Mock |
| 69 | + private UserSessionManagementService userSessionManagementService; |
| 70 | + @Mock |
| 71 | + private OrganizationManager organizationManager; |
| 72 | + private GetUserSessionsFunctionImpl getUserSessionsFunction; |
| 73 | + |
| 74 | + @BeforeMethod |
| 75 | + public void setUp() { |
| 76 | + |
| 77 | + initMocks(this); |
| 78 | + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(TENANT_DOMAIN_CARBON_SUPER, true); |
| 79 | + |
| 80 | + getUserSessionsFunction = new GetUserSessionsFunctionImpl(); |
| 81 | + UserFunctionsServiceHolder.getInstance().setUserSessionManagementService(userSessionManagementService); |
| 82 | + UserFunctionsServiceHolder.getInstance().setOrganizationManager(organizationManager); |
| 83 | + } |
| 84 | + |
| 85 | + @AfterMethod |
| 86 | + public void tearDown() { |
| 87 | + |
| 88 | + PrivilegedCarbonContext.destroyCurrentContext(); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testGetUserSessionsSuccess() throws Exception { |
| 93 | + |
| 94 | + // Create test user. |
| 95 | + AuthenticatedUser authenticatedUser = new AuthenticatedUser(); |
| 96 | + authenticatedUser.setUserName(TEST_USER_NAME); |
| 97 | + authenticatedUser.setTenantDomain(TENANT_DOMAIN_CARBON_SUPER); |
| 98 | + authenticatedUser.setUserStoreDomain(USER_STORE_DOMAIN_PRIMARY); |
| 99 | + authenticatedUser.setUserId(TEST_USER_ID_123); |
| 100 | + |
| 101 | + JsAuthenticatedUser jsUser = new JsGraalAuthenticatedUser(authenticatedUser); |
| 102 | + |
| 103 | + // Create mock user sessions. |
| 104 | + List<UserSession> mockSessions = new ArrayList<>(); |
| 105 | + UserSession session1 = mock(UserSession.class); |
| 106 | + UserSession session2 = mock(UserSession.class); |
| 107 | + mockSessions.add(session1); |
| 108 | + mockSessions.add(session2); |
| 109 | + |
| 110 | + // Mock the session management service. |
| 111 | + when(userSessionManagementService.getSessionsByUserId(TEST_USER_ID_123, TENANT_DOMAIN_CARBON_SUPER)) |
| 112 | + .thenReturn(mockSessions); |
| 113 | + |
| 114 | + // Execute the function. |
| 115 | + List<JsUserSession> result = getUserSessionsFunction.getUserSessions(jsUser); |
| 116 | + |
| 117 | + // Verify results. |
| 118 | + Assert.assertNotNull(result); |
| 119 | + Assert.assertEquals(result.size(), 2); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testGetUserSessionsWithEmptyResult() throws Exception { |
| 124 | + |
| 125 | + // Create test user. |
| 126 | + AuthenticatedUser authenticatedUser = new AuthenticatedUser(); |
| 127 | + authenticatedUser.setUserName(TEST_USER_NAME); |
| 128 | + authenticatedUser.setTenantDomain(TENANT_DOMAIN_CARBON_SUPER); |
| 129 | + authenticatedUser.setUserStoreDomain(USER_STORE_DOMAIN_PRIMARY); |
| 130 | + authenticatedUser.setUserId(TEST_USER_ID_456); |
| 131 | + |
| 132 | + JsAuthenticatedUser jsUser = new JsGraalAuthenticatedUser(authenticatedUser); |
| 133 | + |
| 134 | + // Mock empty session list. |
| 135 | + when(userSessionManagementService.getSessionsByUserId(TEST_USER_ID_456, TENANT_DOMAIN_CARBON_SUPER)) |
| 136 | + .thenReturn(new ArrayList<>()); |
| 137 | + |
| 138 | + // Execute the function. |
| 139 | + List<JsUserSession> result = getUserSessionsFunction.getUserSessions(jsUser); |
| 140 | + |
| 141 | + // Verify results. |
| 142 | + Assert.assertNotNull(result); |
| 143 | + Assert.assertEquals(result.size(), 0); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testGetUserSessionsWithOrganization() throws Exception { |
| 148 | + |
| 149 | + // Create test user with accessing organization. |
| 150 | + AuthenticatedUser authenticatedUser = new AuthenticatedUser(); |
| 151 | + authenticatedUser.setUserName(TEST_USER_NAME); |
| 152 | + authenticatedUser.setTenantDomain(TENANT_DOMAIN_CARBON_SUPER); |
| 153 | + authenticatedUser.setUserStoreDomain(USER_STORE_DOMAIN_PRIMARY); |
| 154 | + authenticatedUser.setUserId(TEST_USER_ID_ORG_123); |
| 155 | + authenticatedUser.setAccessingOrganization(ORG_ID_123); |
| 156 | + |
| 157 | + JsAuthenticatedUser jsUser = new JsGraalAuthenticatedUser(authenticatedUser); |
| 158 | + |
| 159 | + // Mock organization manager. |
| 160 | + when(organizationManager.resolveTenantDomain(ORG_ID_123)).thenReturn(ORG_TENANT_DOMAIN); |
| 161 | + |
| 162 | + // Create mock user sessions |
| 163 | + List<UserSession> mockSessions = new ArrayList<>(); |
| 164 | + UserSession session = mock(UserSession.class); |
| 165 | + mockSessions.add(session); |
| 166 | + |
| 167 | + // Mock the session management service with resolved tenant domain. |
| 168 | + when(userSessionManagementService.getSessionsByUserId(TEST_USER_ID_ORG_123, ORG_TENANT_DOMAIN)) |
| 169 | + .thenReturn(mockSessions); |
| 170 | + |
| 171 | + // Execute the function. |
| 172 | + List<JsUserSession> result = getUserSessionsFunction.getUserSessions(jsUser); |
| 173 | + |
| 174 | + // Verify results. |
| 175 | + Assert.assertNotNull(result); |
| 176 | + Assert.assertEquals(result.size(), 1); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + public void testGetUserSessionsWithOrganizationManagementException() throws Exception { |
| 181 | + |
| 182 | + // Create test user with accessing organization. |
| 183 | + AuthenticatedUser authenticatedUser = new AuthenticatedUser(); |
| 184 | + authenticatedUser.setUserName(TEST_USER_NAME); |
| 185 | + authenticatedUser.setTenantDomain(TENANT_DOMAIN_CARBON_SUPER); |
| 186 | + authenticatedUser.setUserStoreDomain(USER_STORE_DOMAIN_PRIMARY); |
| 187 | + authenticatedUser.setUserId(TEST_USER_ID_ORG_456); |
| 188 | + authenticatedUser.setAccessingOrganization(INVALID_ORG); |
| 189 | + |
| 190 | + JsAuthenticatedUser jsUser = new JsGraalAuthenticatedUser(authenticatedUser); |
| 191 | + |
| 192 | + // Mock organization manager to throw exception. |
| 193 | + when(organizationManager.resolveTenantDomain(INVALID_ORG)) |
| 194 | + .thenThrow(new OrganizationManagementException(ORG_NOT_FOUND_MESSAGE)); |
| 195 | + |
| 196 | + // Execute the function. |
| 197 | + List<JsUserSession> result = getUserSessionsFunction.getUserSessions(jsUser); |
| 198 | + Assert.assertNull(result); |
| 199 | + } |
| 200 | +} |
0 commit comments