Search in sources :

Example 1 with UserNameMapper

use of org.wso2.carbon.apimgt.core.api.UserNameMapper in project carbon-apimgt by wso2.

the class DefaultIdentityProviderImplTestCase method testGetIdOfUser.

@Test
public void testGetIdOfUser() throws Exception {
    SCIMServiceStub scimServiceStub = Mockito.mock(SCIMServiceStub.class);
    UserNameMapper userNameMapper = Mockito.mock(UserNameMapperImpl.class);
    Mockito.when(userNameMapper.getLoggedInUserIDFromPseudoName("invalid_user")).thenReturn("invalid_user");
    Mockito.when(userNameMapper.getLoggedInUserIDFromPseudoName("John")).thenReturn("John");
    Mockito.when(userNameMapper.getLoggedInUserIDFromPseudoName("invalid_user_giving_null_response")).thenReturn("invalid_user_giving_null_response");
    DefaultIdentityProviderImpl idpImpl = new DefaultIdentityProviderImpl(scimServiceStub, userNameMapper);
    String validUserName = "John";
    final String validUserSearchQuery = "userName Eq " + validUserName;
    final String expectedUserId = "cfbde56e-8422-498e-b6dc-85a6f1f8b058";
    String invalidUserName = "invalid_user";
    final String invalidUserSearchQuery = "userName Eq " + invalidUserName;
    String userReturningNullResponse = "invalid_user_giving_null_response";
    final String userReturningNullResponseSearchQuery = "userName Eq " + userReturningNullResponse;
    // happy path
    String responseBody = "{\"totalResults\":1,\"schemas\":[\"urn:scim:schemas:core:1.0\"],\"Resources\":" + "[{\"meta\":{\"created\":\"2017-06-02T10:12:26\",\"location\":" + "\"https://localhost:9443/wso2/scim/Users/cfbde56e-8422-498e-b6dc-85a6f1f8b058\",\"lastModified\":" + "\"2017-06-02T10:12:26\"},\"id\":\"cfbde56e-8422-498e-b6dc-85a6f1f8b058\",\"userName\":\"John\"}]}";
    Response createdResponse = Response.builder().status(APIMgtConstants.HTTPStatusCodes.SC_200_OK).headers(new HashMap<>()).body(responseBody.getBytes()).build();
    Mockito.when(scimServiceStub.searchUsers(validUserSearchQuery)).thenReturn(createdResponse);
    try {
        String userId = idpImpl.getIdOfUser(validUserName);
        Assert.assertEquals(userId, expectedUserId);
    } catch (Exception ex) {
        Assert.fail(ex.getMessage());
    }
    // error path
    // Assuming the user cannot be found - When the request did not return a 200 OK response
    String errorResponse = "{\"Errors\":[{\"code\":\"404\",\"description\":\"User not found in the user " + "store.\"}]}";
    Response createdResponseNoSuchUser = Response.builder().status(APIMgtConstants.HTTPStatusCodes.SC_404_NOT_FOUND).headers(new HashMap<>()).body(errorResponse.getBytes()).build();
    Mockito.when(scimServiceStub.searchUsers(invalidUserSearchQuery)).thenReturn(createdResponseNoSuchUser);
    try {
        idpImpl.getIdOfUser(invalidUserName);
    } catch (Exception ex) {
        Assert.assertTrue(ex instanceof IdentityProviderException);
        Assert.assertEquals(ex.getMessage(), "Error occurred while retrieving Id of user " + invalidUserName + ". Error : User not found in the user store.");
    }
    // error path
    // Assuming the response is null
    Mockito.when(scimServiceStub.searchUsers(userReturningNullResponseSearchQuery)).thenReturn(null);
    try {
        idpImpl.getIdOfUser(userReturningNullResponse);
    } catch (Exception ex) {
        Assert.assertTrue(ex instanceof IdentityProviderException);
        Assert.assertEquals(ex.getMessage(), "Error occurred while retrieving Id of user " + userReturningNullResponse + ". Error : Response is null.");
    }
}
Also used : Response(feign.Response) UserNameMapper(org.wso2.carbon.apimgt.core.api.UserNameMapper) SCIMServiceStub(org.wso2.carbon.apimgt.core.auth.SCIMServiceStub) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException) Test(org.testng.annotations.Test)

Example 2 with UserNameMapper

use of org.wso2.carbon.apimgt.core.api.UserNameMapper in project carbon-apimgt by wso2.

the class DefaultIdentityProviderImplTestCase method testGetRoleId.

@Test
public void testGetRoleId() throws Exception {
    SCIMServiceStub scimServiceStub = Mockito.mock(SCIMServiceStub.class);
    UserNameMapper userNameMapper = Mockito.mock(UserNameMapperImpl.class);
    DefaultIdentityProviderImpl idpImpl = new DefaultIdentityProviderImpl(scimServiceStub, userNameMapper);
    String validRoleName = "engineer";
    final String validRoleSearchQuery = "displayName Eq " + validRoleName;
    final String expectedRoleId = "ac093278-9343-466c-8a71-af47921a575b";
    String invalidRoleName = "invalid_role";
    final String invalidRoleSearchQuery = "displayName Eq " + invalidRoleName;
    String roleReturningNullResponse = "invalid_user_giving_null_response";
    final String roleReturningNullResponseSearchQuery = "displayName Eq " + roleReturningNullResponse;
    // happy path
    String responseBody = "{\"totalResults\":1,\"schemas\":[\"urn:scim:schemas:core:1.0\"],\"Resources\":" + "[{\"displayName\":\"PRIMARY/engineer\",\"meta\":{\"created\":\"2017-06-02T10:14:42\"," + "\"location\":\"https://localhost:9443/wso2/scim/Groups/ac093278-9343-466c-8a71-af47921a575b\"," + "\"lastModified\":\"2017-06-02T10:14:42\"},\"id\":\"ac093278-9343-466c-8a71-af47921a575b\"}]}";
    Response createdResponse = Response.builder().status(APIMgtConstants.HTTPStatusCodes.SC_200_OK).headers(new HashMap<>()).body(responseBody.getBytes()).build();
    Mockito.when(scimServiceStub.searchGroups(validRoleSearchQuery)).thenReturn(createdResponse);
    try {
        String roleId = idpImpl.getRoleId(validRoleName);
        Assert.assertEquals(roleId, expectedRoleId);
    } catch (Exception ex) {
        Assert.fail(ex.getMessage());
    }
    // error path
    // Assuming the role cannot be found - when returning a not 200 response
    String errorResponseBody = "{\"Errors\":[{\"code\":\"404\",\"description\":\"Group not found in the user store.\"}]}";
    Response createdResponseNoSuchRole = Response.builder().status(APIMgtConstants.HTTPStatusCodes.SC_404_NOT_FOUND).headers(new HashMap<>()).body(errorResponseBody.getBytes()).build();
    Mockito.when(scimServiceStub.searchGroups(invalidRoleSearchQuery)).thenReturn(createdResponseNoSuchRole);
    try {
        idpImpl.getRoleId(invalidRoleName);
    } catch (Exception ex) {
        Assert.assertTrue(ex instanceof IdentityProviderException);
        Assert.assertEquals(ex.getMessage(), "Error occurred while retrieving Id of role " + invalidRoleName + ". Error : Group not found in the user store.");
    }
    // error path
    // Assuming the response is null
    Mockito.when(scimServiceStub.searchGroups(roleReturningNullResponseSearchQuery)).thenReturn(null);
    try {
        idpImpl.getRoleId(roleReturningNullResponse);
    } catch (Exception ex) {
        Assert.assertTrue(ex instanceof IdentityProviderException);
        Assert.assertEquals(ex.getMessage(), "Error occurred while retrieving Id of role " + roleReturningNullResponse + ". Error : Response is null.");
    }
}
Also used : Response(feign.Response) UserNameMapper(org.wso2.carbon.apimgt.core.api.UserNameMapper) SCIMServiceStub(org.wso2.carbon.apimgt.core.auth.SCIMServiceStub) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException) Test(org.testng.annotations.Test)

Example 3 with UserNameMapper

use of org.wso2.carbon.apimgt.core.api.UserNameMapper in project carbon-apimgt by wso2.

the class DefaultIdentityProviderImplTestCase method testIsValidRole.

@Test
public void testIsValidRole() throws Exception {
    SCIMServiceStub scimServiceStub = Mockito.mock(SCIMServiceStub.class);
    UserNameMapper userNameMapper = Mockito.mock(UserNameMapperImpl.class);
    DefaultIdentityProviderImpl idpImpl = new DefaultIdentityProviderImpl(scimServiceStub, userNameMapper);
    final String validRole = "engineer";
    final String validRoleSearchQuery = "displayName Eq " + validRole;
    Response okResponse = Response.builder().status(APIMgtConstants.HTTPStatusCodes.SC_200_OK).headers(new HashMap<>()).build();
    Mockito.when(scimServiceStub.searchGroups(validRoleSearchQuery)).thenReturn(okResponse);
    Assert.assertTrue(idpImpl.isValidRole(validRole));
    final String invalidRole = "invalid-role";
    final String invalidRoleSearchQuery = "displayName Eq " + invalidRole;
    Response notFoundResponse = Response.builder().status(APIMgtConstants.HTTPStatusCodes.SC_404_NOT_FOUND).headers(new HashMap<>()).build();
    Mockito.when(scimServiceStub.searchGroups(invalidRoleSearchQuery)).thenReturn(notFoundResponse);
    Assert.assertFalse(idpImpl.isValidRole(invalidRole));
}
Also used : Response(feign.Response) UserNameMapper(org.wso2.carbon.apimgt.core.api.UserNameMapper) HashMap(java.util.HashMap) SCIMServiceStub(org.wso2.carbon.apimgt.core.auth.SCIMServiceStub) Test(org.testng.annotations.Test)

Example 4 with UserNameMapper

use of org.wso2.carbon.apimgt.core.api.UserNameMapper in project carbon-apimgt by wso2.

the class DefaultIdentityProviderImplTestCase method testGetRoleIdsOfUser.

@Test
public void testGetRoleIdsOfUser() throws Exception {
    SCIMServiceStub scimServiceStub = Mockito.mock(SCIMServiceStub.class);
    UserNameMapper userNameMapper = Mockito.mock(UserNameMapperImpl.class);
    DefaultIdentityProviderImpl idpImpl = new DefaultIdentityProviderImpl(scimServiceStub, userNameMapper);
    String validUserId = "a42b4760-120d-432e-8042-4a7f12e3346c";
    String roleName1 = "subscriber";
    String roleId1 = "fb5aaf9c-1fdf-4b2d-86bc-6e3203b99618";
    String roleName2 = "manager";
    String roleId2 = "097435bc-c460-402b-9137-8ab65fd28c3e";
    String roleName3 = "engineer";
    String roleId3 = "ac093278-9343-466c-8a71-af47921a575b";
    List<String> roleIds = new ArrayList<>();
    roleIds.add(roleId1);
    roleIds.add(roleId2);
    roleIds.add(roleId3);
    String successResponseBody = "{\"emails\":[{\"type\":\"home\",\"value\":\"john_home.com\"},{\"type\":\"work\"" + ",\"value\":\"john_work.com\"}],\"meta\":{\"created\":\"2017-06-02T10:12:26\",\"location\":" + "\"https://localhost:9443/wso2/scim/Users/" + validUserId + "\",\"lastModified\":" + "\"2017-06-02T10:12:26\"},\"schemas\":[\"urn:scim:schemas:core:1.0\"],\"name\":{\"familyName\":" + "\"Smith\",\"givenName\":\"John\"},\"groups\":[{\"display\":\"" + roleName1 + "\",\"value\":\"" + roleId1 + "\"},{\"display\":\"" + roleName2 + "\",\"value\":\"" + roleId2 + "\"},{\"display\":\"" + roleName3 + "\",\"value\":\"" + roleId3 + "\"}],\"id\":\"" + validUserId + "\",\"userName\":" + "\"John\"}";
    Response successfulResponse = Response.builder().status(APIMgtConstants.HTTPStatusCodes.SC_200_OK).headers(new HashMap<>()).body(successResponseBody.getBytes()).build();
    Mockito.when(scimServiceStub.getUser(validUserId)).thenReturn(successfulResponse);
    List<String> roles = idpImpl.getRoleIdsOfUser(validUserId);
    Assert.assertEquals(roleIds.size(), roles.size());
    roles.forEach(roleId -> Assert.assertTrue(roleIds.contains(roleId)));
    // Error case - When response is null
    String invalidUserIdResponseNull = "invalidUserId_Response_Null";
    Mockito.when(scimServiceStub.getUser(invalidUserIdResponseNull)).thenReturn(null);
    try {
        idpImpl.getRoleIdsOfUser(invalidUserIdResponseNull);
    } catch (IdentityProviderException ex) {
        Assert.assertEquals(ex.getMessage(), "Error occurred while retrieving user with Id " + invalidUserIdResponseNull + ". Error : Response is null.");
    }
    // Error case - When the request did not return a 200 OK response
    String invalidUserIdNot200OK = "invalidUserId_Not_200_OK";
    String errorResponseBody = "{\"Errors\":[{\"code\":\"404\",\"description\":\"User not found in the user " + "store.\"}]}";
    Response errorResponse = Response.builder().status(APIMgtConstants.HTTPStatusCodes.SC_404_NOT_FOUND).headers(new HashMap<>()).body(errorResponseBody.getBytes()).build();
    Mockito.when(scimServiceStub.getUser(invalidUserIdNot200OK)).thenReturn(errorResponse);
    try {
        idpImpl.getRoleIdsOfUser(invalidUserIdNot200OK);
    } catch (IdentityProviderException ex) {
        Assert.assertEquals(ex.getMessage(), "Error occurred while retrieving role Ids of user with Id " + invalidUserIdNot200OK + ". Error : User not found in the user store.");
    }
    // Error case - When response body is empty
    String invalidUserIdResponseEmpty = "invalidUserId_Response_Empty";
    Response emptyResponse = Response.builder().status(APIMgtConstants.HTTPStatusCodes.SC_200_OK).headers(new HashMap<>()).body("".getBytes()).build();
    Mockito.when(scimServiceStub.getUser(invalidUserIdResponseEmpty)).thenReturn(emptyResponse);
    try {
        idpImpl.getRoleIdsOfUser(invalidUserIdResponseEmpty);
    } catch (IdentityProviderException ex) {
        Assert.assertEquals(ex.getMessage(), "Error occurred while retrieving user with user Id " + invalidUserIdResponseEmpty + " from SCIM endpoint. Response body is null or empty.");
    }
}
Also used : Response(feign.Response) UserNameMapper(org.wso2.carbon.apimgt.core.api.UserNameMapper) ArrayList(java.util.ArrayList) SCIMServiceStub(org.wso2.carbon.apimgt.core.auth.SCIMServiceStub) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException) Test(org.testng.annotations.Test)

Example 5 with UserNameMapper

use of org.wso2.carbon.apimgt.core.api.UserNameMapper in project carbon-apimgt by wso2.

the class DefaultIdentityProviderImplTestCase method testGetRoleName.

@Test
public void testGetRoleName() throws Exception {
    SCIMServiceStub scimServiceStub = Mockito.mock(SCIMServiceStub.class);
    UserNameMapper userNameMapper = Mockito.mock(UserNameMapperImpl.class);
    DefaultIdentityProviderImpl idpImpl = new DefaultIdentityProviderImpl(scimServiceStub, userNameMapper);
    String validRoleId = "ac093278-9343-466c-8a71-af47921a575b";
    String expectedRoleName = "engineer";
    // happy path
    String successfulResponseBody = "{\"displayName\":\"" + expectedRoleName + "\",\"meta\":{\"created\":" + "\"2017-06-26T16:30:42\",\"location\":\"https://localhost:9443/wso2/scim/Groups/" + validRoleId + "\"" + ",\"lastModified\":\"2017-06-26T16:30:42\"},\"schemas\":[\"urn:scim:schemas:core:1.0\"],\"id\":\"" + validRoleId + "\"}";
    Response successfulResponse = Response.builder().status(APIMgtConstants.HTTPStatusCodes.SC_200_OK).headers(new HashMap<>()).body(successfulResponseBody.getBytes()).build();
    Mockito.when(scimServiceStub.getGroup(validRoleId)).thenReturn(successfulResponse);
    try {
        String roleName = idpImpl.getRoleName(validRoleId);
        Assert.assertEquals(roleName, expectedRoleName);
    } catch (Exception ex) {
        Assert.fail(ex.getMessage());
    }
    // error path
    // When response is null
    String invalidRoleIdResponseNull = "invalidRoleId_Response_Null";
    Mockito.when(scimServiceStub.getGroup(invalidRoleIdResponseNull)).thenReturn(null);
    try {
        idpImpl.getRoleName(invalidRoleIdResponseNull);
    } catch (IdentityProviderException ex) {
        Assert.assertEquals(ex.getMessage(), "Error occurred while retrieving name of role with Id " + invalidRoleIdResponseNull + ". Error : Response is null.");
    }
    // error path
    // When the request did not return a 200 OK response
    String invalidRoleIdNot200OK = "invalidRoleId_Not_200_OK";
    String errorResponseBody = "{\"Errors\":[{\"code\":\"404\",\"description\":\"Group not found in the user " + "store.\"}]}";
    Response errorResponse = Response.builder().status(APIMgtConstants.HTTPStatusCodes.SC_404_NOT_FOUND).headers(new HashMap<>()).body(errorResponseBody.getBytes()).build();
    Mockito.when(scimServiceStub.getGroup(invalidRoleIdNot200OK)).thenReturn(errorResponse);
    try {
        idpImpl.getRoleName(invalidRoleIdNot200OK);
    } catch (IdentityProviderException ex) {
        Assert.assertEquals(ex.getMessage(), "Error occurred while retrieving name of role with Id " + invalidRoleIdNot200OK + ". Error : Group not found in the user store.");
    }
    // Error case - When response body is empty
    String invalidRoleIdResponseEmpty = "invalidRoleId_Response_Empty";
    Response emptyResponse = Response.builder().status(APIMgtConstants.HTTPStatusCodes.SC_200_OK).headers(new HashMap<>()).body("".getBytes()).build();
    Mockito.when(scimServiceStub.getGroup(invalidRoleIdResponseEmpty)).thenReturn(emptyResponse);
    try {
        idpImpl.getRoleName(invalidRoleIdResponseEmpty);
    } catch (IdentityProviderException ex) {
        Assert.assertEquals(ex.getMessage(), "Error occurred while retrieving role name with role Id " + invalidRoleIdResponseEmpty + " from SCIM endpoint. " + "Response body is null or empty.");
    }
}
Also used : Response(feign.Response) UserNameMapper(org.wso2.carbon.apimgt.core.api.UserNameMapper) SCIMServiceStub(org.wso2.carbon.apimgt.core.auth.SCIMServiceStub) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)8 UserNameMapper (org.wso2.carbon.apimgt.core.api.UserNameMapper)8 Response (feign.Response)7 SCIMServiceStub (org.wso2.carbon.apimgt.core.auth.SCIMServiceStub)7 IdentityProviderException (org.wso2.carbon.apimgt.core.exception.IdentityProviderException)6 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)2 SCIMUser (org.wso2.carbon.apimgt.core.auth.dto.SCIMUser)1 UserMappingDAO (org.wso2.carbon.apimgt.core.dao.UserMappingDAO)1 User (org.wso2.carbon.apimgt.core.models.User)1