+ Start a Discussion
Praveen Kumar BonaluPraveen Kumar Bonalu 

TEST Class for Custom JIT (just in time provisioning) controller -SAML

Hi,

I would like to know how to write a test class for the custom Jit controller.

Controller:
global class StandardUserHandler implements Auth.SamlJitHandler {
    private class JitException extends Exception{}
    private void handleUser(boolean create, User u, Map<String, String> attributes,
        String federationIdentifier, boolean isStandard) {
        if(create && attributes.containsKey('User.Username')) {
            u.Username = attributes.get('User.Username');
        }
        if(create) {
            if(attributes.containsKey('User.FederationIdentifier')) {
                u.FederationIdentifier = attributes.get('User.FederationIdentifier');
            } else {
                u.FederationIdentifier = federationIdentifier;
            }
        }
        if(attributes.containsKey('User.ProfileId')) {
            String profileId = attributes.get('User.ProfileId');
            Profile p = [SELECT Id FROM Profile WHERE Id=:profileId];
            u.ProfileId = p.Id;
        }
        if(attributes.containsKey('User.UserRoleId')) {
            String userRole = attributes.get('User.UserRoleId');
            UserRole r = [SELECT Id FROM UserRole WHERE Id=:userRole];
            u.UserRoleId = r.Id;
        }
        if(attributes.containsKey('User.Phone')) {
            u.Phone = attributes.get('User.Phone');
        }
        if(attributes.containsKey('User.Email')) {
            u.Email = attributes.get('User.Email');
        }


        if(!create) {
            update(u);
        }
    }

    private void handleJit(boolean create, User u, Id samlSsoProviderId, Id communityId, Id portalId,
        String federationIdentifier, Map<String, String> attributes, String assertion) {
        if(communityId != null || portalId != null) {
            String account = handleAccount(create, u, attributes);
            handleContact(create, account, u, attributes);
            handleUser(create, u, attributes, federationIdentifier, false);
        } else {
            handleUser(create, u, attributes, federationIdentifier, true);
        }
    }

    global User createUser(Id samlSsoProviderId, Id communityId, Id portalId,
        String federationIdentifier, Map<String, String> attributes, String assertion) {
        User u = new User();
        handleJit(true, u, samlSsoProviderId, communityId, portalId,
            federationIdentifier, attributes, assertion);
        return u;
    }

    global void updateUser(Id userId, Id samlSsoProviderId, Id communityId, Id portalId,
        String federationIdentifier, Map<String, String> attributes, String assertion) {
        User u = [SELECT Id FROM User WHERE Id=:userId];
        handleJit(false, u, samlSsoProviderId, communityId, portalId,
            federationIdentifier, attributes, assertion);
    }
}
anyone help and comments are much appreciated.

Thank you  
Raj VakatiRaj Vakati
Sample code
 
@isTest
private class StandardUserRegistrationHandlerTest {
    static testMethod void testCreateAndUpdateUser() {
        StandardUserHandler handler = new StandardUserHandler();
        Auth.UserData sampleData = new Auth.UserData('testId', 'testFirst', 'testLast',
                                                     'testFirst testLast', 'testuser@example.org', null, 'testuserlong', 'en_US', 'facebook',
                                                     null, new Map<String, String>{'language' => 'en_US'});
        String pid =[Select Id from Profile Where name ='Standard User'].Id ;
        Map<string,String> tempUser=  new Map<String, String>
        {'language' => 'en_US' ,
            'Username'=>'testaskdhjkasdj@sdjkfhsjd.com' ,
            'LastName'=>'asdjhgasjhd',
            'Email'=>'askdasgdhgas@sdjkfd.com',
            'Alias'=>'asdhagsd',
            'CommunityNickname'=>'asdjasgdjhahsdhasd',
            'TimeZoneSidKey'=>'America/Los_Angeles',
            'LocaleSidKey'=>'en_US',
            'EmailEncodingKey'=>'UTF-8',
            'ProfileId'=>pid,
            'LanguageLocaleKey'=>'en_US' }; 
                
                
                
                User u = handler.createUser(null,null,null,'', tempUser ,null);
        
        insert(u);
        String uid = u.id;
        
        sampleData = new Auth.UserData('testNewId', 'testNewFirst', 'testNewLast',
                                       'testNewFirst testNewLast', 'testnewuser@example.org', null, 'testnewuserlong', 'en_US', 'facebook',
                                       null, new Map<String, String>{});
        handler.updateUser(uid,null,null,null, '', tempUser,null);
        
        User updatedUser = [SELECT userName, email, firstName, lastName, alias FROM user WHERE id=:uid];
        
    }
}

 
Praveen Kumar BonaluPraveen Kumar Bonalu
Thank you Raj