Search in sources :

Example 6 with UserNameMapper

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

the class DefaultIdentityProviderImplTestCase method testRegisterUser.

@Test
public void testRegisterUser() throws Exception {
    SCIMServiceStub scimServiceStub = Mockito.mock(SCIMServiceStub.class);
    UserNameMapper userNameMapper = Mockito.mock(UserNameMapperImpl.class);
    DefaultIdentityProviderImpl idpImpl = new DefaultIdentityProviderImpl(scimServiceStub, userNameMapper);
    // happy path
    User user = new User();
    user.setFirstName("john");
    user.setLastName("doe");
    user.setUsername("johnd");
    user.setEmail("john@wso2.com");
    user.setPassword(new char[] { 'p', 'a', 's', 's' });
    SCIMUser scimUser = new SCIMUser();
    SCIMUser.SCIMName scimName = new SCIMUser.SCIMName();
    scimName.setGivenName(user.getFirstName());
    scimName.setFamilyName(user.getLastName());
    scimUser.setName(scimName);
    SCIMUser.SCIMUserEmails scimUserEmails = new SCIMUser.SCIMUserEmails(user.getEmail(), "home", true);
    List<SCIMUser.SCIMUserEmails> scimUserEmailList = new ArrayList<>();
    scimUserEmailList.add(scimUserEmails);
    scimUser.setEmails(scimUserEmailList);
    scimUser.setUsername(user.getUsername());
    scimUser.setPassword(String.valueOf(user.getPassword()));
    Response createdResponse = Response.builder().status(APIMgtConstants.HTTPStatusCodes.SC_201_CREATED).headers(new HashMap<>()).build();
    Mockito.when(scimServiceStub.addUser(scimUser)).thenReturn(createdResponse);
    try {
        idpImpl.registerUser(user);
        Assert.assertTrue(true);
    } catch (Exception ex) {
        Assert.fail(ex.getMessage());
    }
    // error path
    final int errorSc = APIMgtConstants.HTTPStatusCodes.SC_409_CONFLICT;
    final String errorMsg = "{\"Errors\":[{\"code\":\"409\",\"description\":\"Error in adding the user: test to " + "the user store.\"}]}";
    Response errorResponse = Response.builder().status(errorSc).headers(new HashMap<>()).body(errorMsg.getBytes()).build();
    Mockito.when(scimServiceStub.addUser(any(SCIMUser.class))).thenReturn(errorResponse);
    try {
        idpImpl.registerUser(user);
        Assert.fail("Exception was expected, but wasn't thrown");
    } catch (IdentityProviderException ex) {
        Assert.assertTrue(ex.getMessage().startsWith("Error occurred while creating user."));
    }
}
Also used : SCIMUser(org.wso2.carbon.apimgt.core.auth.dto.SCIMUser) User(org.wso2.carbon.apimgt.core.models.User) SCIMUser(org.wso2.carbon.apimgt.core.auth.dto.SCIMUser) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException) 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) Test(org.testng.annotations.Test)

Example 7 with UserNameMapper

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

the class DefaultIdentityProviderImplTestCase method testGetRoleNamesOfUser.

@Test
public void testGetRoleNamesOfUser() 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> roleNames = new ArrayList<>();
    roleNames.add(roleName1);
    roleNames.add(roleName2);
    roleNames.add(roleName3);
    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.getRoleNamesOfUser(validUserId);
    Assert.assertEquals(roleNames.size(), roles.size());
    roles.forEach(roleName -> Assert.assertTrue(roleNames.contains(roleName)));
    // Error case - When response is null
    String invalidUserIdResponseNull = "invalidUserId_Response_Null";
    Mockito.when(scimServiceStub.getUser(invalidUserIdResponseNull)).thenReturn(null);
    try {
        idpImpl.getRoleNamesOfUser(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.getRoleNamesOfUser(invalidUserIdNot200OK);
    } catch (IdentityProviderException ex) {
        Assert.assertEquals(ex.getMessage(), "Error occurred while retrieving role names 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.getRoleNamesOfUser(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 8 with UserNameMapper

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

the class UserDataMappingImplTestCase method testGetIdOfUser.

@Test
public void testGetIdOfUser() throws Exception {
    UserMappingDAO userMappingDAO = Mockito.mock(UserMappingDAO.class);
    Mockito.when(userMappingDAO.getPseudoNameByUserID("test_user")).thenReturn("XXXXX");
    UserNameMapper userNameMapper = new UserNameMapperImpl(userMappingDAO);
    String pseudoName = userNameMapper.getLoggedInPseudoNameFromUserID("test_user");
    Assert.assertNotNull(pseudoName);
    Assert.assertEquals("XXXXX", pseudoName);
    userNameMapper.getLoggedInUserIDFromPseudoName("XXXXX");
}
Also used : UserMappingDAO(org.wso2.carbon.apimgt.core.dao.UserMappingDAO) UserNameMapper(org.wso2.carbon.apimgt.core.api.UserNameMapper) 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