function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alexis Scott 16Alexis Scott 16 

autocreatedreghandler test class help

Hello! 

I am setting up Google SSO for Salesforce and I have this autoreghandler that I need a test class for. I'm not really a developer so I was wondering if someone could help me? I just need the code coverage so I can upload other things into production. 
 
//TODO:This autogenerated class includes the basics for a Registration
//Handler class. You will need to customize it to ensure it meets your needs and
//the data provided by the third party.

global class AutocreatedRegHandler1537799224416 implements Auth.RegistrationHandler{
global boolean canCreateUser(Auth.UserData data) {
	//TODO: Check whether we want to allow creation of a user with this data
	//Set<String> s = new Set<String>{'usernamea', 'usernameb', 'usernamec'};
	//if(s.contains(data.username)) {
		//return true;
	//}
	return false;
}

global User createUser(Id portalId, Auth.UserData data){
	if(!canCreateUser(data)) {
		//Returning null or throwing an exception fails the SSO flow
		return null;
	}
	if(data.attributeMap.containsKey('sfdc_networkid')) {
		//We have a community id, so create a user with community access
		//TODO: Get an actual account
		Account a = [SELECT Id FROM account WHERE name='Acme'];
		Contact c = new Contact();
		c.accountId = a.Id;
		c.email = data.email;
		c.firstName = data.firstName;
		c.lastName = data.lastName;
		insert(c);

		//TODO: Customize the username and profile. Also check that the username doesn't already exist and
		//possibly ensure there are enough org licenses to create a user. Must be 80 characters or less.
		User u = new User();
		Profile p = [SELECT Id FROM profile WHERE name='Customer Portal User'];
		u.username = data.username + '@acmecorp.com';
		u.email = data.email;
		u.lastName = data.lastName;
		u.firstName = data.firstName;
		String alias = data.username;
		//Alias must be 8 characters or less
		if(alias.length() > 8) {
			alias = alias.substring(0, 8);
		}
		u.alias = alias;
		u.languagelocalekey = UserInfo.getLocale();
		u.localesidkey = UserInfo.getLocale();
		u.emailEncodingKey = 'UTF-8';
		u.timeZoneSidKey = 'America/Los_Angeles';
		u.profileId = p.Id;
		u.contactId = c.Id;
		return u;
	} else {
		//This is not a community, so create a regular standard user
		User u = new User();
		Profile p = [SELECT Id FROM profile WHERE name='Standard User'];
		//TODO: Customize the username. Also check that the username doesn't already exist and
		//possibly ensure there are enough org licenses to create a user. Must be 80 characters
		//or less.
		u.username = data.username + '@myorg.com';
		u.email = data.email;
		u.lastName = data.lastName;
		u.firstName = data.firstName;
		String alias = data.username;
		//Alias must be 8 characters or less
		if(alias.length() > 8) {
			alias = alias.substring(0, 8);
		}
		u.alias = alias;
		u.languagelocalekey = UserInfo.getLocale();
		u.localesidkey = UserInfo.getLocale();
		u.emailEncodingKey = 'UTF-8';
		u.timeZoneSidKey = 'America/Los_Angeles';
		u.profileId = p.Id;
		return u;
	}
}

global void updateUser(Id userId, Id portalId, Auth.UserData data){
	User u = new User(id=userId);
	//TODO: Customize the username. Must be 80 characters or less.
	//u.username = data.username + '@myorg.com';
	u.email = data.email;
	u.lastName = data.lastName;
	u.firstName = data.firstName;
	//String alias = data.username;
	//Alias must be 8 characters or less
	//if(alias.length() > 8) {
		//alias = alias.substring(0, 8);
	//}
	//u.alias = alias;
	update(u);
}
}

I have this test class but it's only covering like 9%
 
@isTest
//https://help.salesforce.com/articleView?id=sso_authentication_providers.htm
private class StandardUserRegistrationHandlerTest3 {
static testMethod void testCreateAndUpdateUser() {
    AutocreatedRegHandler1537799224416 handler = new AutocreatedRegHandler1537799224416();
    // UserData class: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Auth_UserData.htm
    // https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_auth_plugin.htm
    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','site' => 'test'});
    Id portalId = null;
    User u = handler.createUser(portalId, sampleData);
    //System.assertEquals('testuserlong@myorg.com', u.userName);
    //System.assertEquals(true,sampleData.attributeMap.containsKey('language'));
    //System.debug(sampleData.attributeMap);
    
    System.assertEquals('testuser@example.org', u.email);
    System.assertEquals('testLast', u.lastName);
    System.assertEquals('testFirst', u.firstName);
    System.assertEquals('testuser', u.alias);
    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>{'language' => 'en_US'});
    handler.updateUser(uid, null, sampleData);
    
    
    User updatedUser = [SELECT userName, email, firstName, lastName, alias FROM user WHERE id=:uid];
    //System.assertEquals('testnewuserlong@acmecorp.com', updatedUser.userName);
    System.assertEquals('testnewuser@example.org', updatedUser.email);
    System.assertEquals('testNewLast', updatedUser.lastName);
    System.assertEquals('testNewFirst', updatedUser.firstName);
    //System.assertEquals('testnewu', updatedUser.alias);
}
}

 
Alex SelwynAlex Selwyn
if(!canCreateUser(data)) {   --> canCreateUser method always returns false, which will force the logic to go inside.
      //Returning null or throwing an exception fails the SSO flow
       return null; --> The flow breaks here.
  }

global boolean canCreateUser(Auth.UserData data) {
   //TODO: Check whether we want to allow creation of a user with this data
  //Set<String> s = new Set<String>{'usernamea', 'usernameb', 'usernamec'};
   if(s.contains(data.username)) { -->Uncomment these lines, as long as you have a user name it should execute the logic
        return true;
   }
12    return false;
13}

Just to clarify this is a sample code and this is not the one you would be deploying to production.