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
RobertCRobertC 

How to write a Test Class for the ConnectedAppPlugin Class?

I used the sample posted here in the help docs https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Auth_ConnectedAppPlugin.htm

I was able to update the customAttributes method to update the SAML response with additional attributes and this works well.  However I have been searching everywhere to find the sample test class to go with the extension sample.  Has anyone done this that can provide a basic sample?
Best Answer chosen by RobertC
Raj VakatiRaj Vakati
Try like this
 
@IsTest
public class ConnectedAppPluginExampleTest {

    @isTest
    public static void customAttributesTest(){
        Map<String,String> mapstr = new Map<String,String>();
        Test.startTest();
        ConnectedAppPluginExample  cap = new ConnectedAppPluginExample ();
        Map<String,String> respMapStr = cap.customAttributes(UserInfo.getUserId(),mapstr);
        Test.stopTest();
        
        Map<String,String> mapstrempty = new Map<String,String>();
        System.assertNotEquals(respMapStr,mapstrempty);
        
    }
}

 

All Answers

Raj VakatiRaj Vakati
Try like this
 
@IsTest
public class ConnectedAppPluginExampleTest {

    @isTest
    public static void customAttributesTest(){
        Map<String,String> mapstr = new Map<String,String>();
        Test.startTest();
        ConnectedAppPluginExample  cap = new ConnectedAppPluginExample ();
        Map<String,String> respMapStr = cap.customAttributes(UserInfo.getUserId(),mapstr);
        Test.stopTest();
        
        Map<String,String> mapstrempty = new Map<String,String>();
        System.assertNotEquals(respMapStr,mapstrempty);
        
    }
}

 
This was selected as the best answer
Raj VakatiRaj Vakati
You need to pass the Connected app and Auth Contect to below code
 
@IsTest
public class ConnectedAppPluginExampleTest {

    @isTest
    public static void customAttributesTest(){
        Map<String,String> mapstr = new Map<String,String>();
        Test.startTest();
        ConnectedAppPluginExample  cap = new ConnectedAppPluginExample ();
        Map<String,String> respMapStr = cap.customAttributes(UserInfo.getUserId(),mapstr);
		
		
		
        Test.stopTest();
        
        Map<String,String> mapstrempty = new Map<String,String>();
        System.assertNotEquals(respMapStr,mapstrempty);
		
		
		cap.refresh(UserInfo.getUserId(),'Connect API ID' , Auth.InvocationContext);
        		cap.authorize(UserInfo.getUserId(),'Connect API ID' , true , Auth.InvocationContext);

    }
}

 
RobertCRobertC
Thank you Raj, when I use that test it fails the assert, I simplified the method to include only this to make sure that there weren't any dependencies missing but it still fails the assert so I am not sure that the method is being called at all.

global override Map<String,String> customAttributes(Id userId, Id connectedAppId, Map<String,String>
        formulaDefinedAttributes, Auth.InvocationContext context)         
    {  
formulaDefinedAttributes.put('PermissionSets', 'test');
        return formulaDefinedAttributes;   
}
Raj VakatiRaj Vakati
Give me your class and test class 
RobertCRobertC
global class ConnectedAppPluginAttributes extends Auth.ConnectedAppPlugin {
     // Authorize the app if the user has achieved quota tracked in a custom field
    global override Boolean authorize(Id userId, Id connectedAppId, Boolean isAdminApproved, Auth.InvocationContext context) 
    {
         User u = [select id, isActive from User where id =: userId].get(0);
         return u.isActive;
      
    }      


    global override Map<String,String> customAttributes(Id userId, Id connectedAppId, Map<String,String>
        formulaDefinedAttributes, Auth.InvocationContext context)         
    {  

     //   List<User> relatedcontact = [Select Id,Contact.Account.Name,Contact.AccountId,Contact.Account.Partner_Level__c,Contact.Account.RecordType.Name,Contact.Account.Partner_Status__c from User
     //   where Id=:userId LIMIT 1];

     //   formulaDefinedAttributes.put('AccountRecordType',relatedcontact[0].Contact.Account.RecordType.Name);
     //   formulaDefinedAttributes.put('PartnerStatus',relatedcontact[0].Contact.Account.Partner_Status__c);
     //   formulaDefinedAttributes.put('AccountId',relatedcontact[0].Contact.AccountId);
     //   formulaDefinedAttributes.put('AccountName',relatedcontact[0].Contact.Account.Name);
     //   formulaDefinedAttributes.put('PartnerLevel',relatedcontact[0].Contact.Account.Partner_Level__c);

      
        formulaDefinedAttributes.put('PermissionSets', 'test');

        return formulaDefinedAttributes;   
     }



}
 
@isTest
public class ConnectedAppPluginAttributes_Test  {

 @isTest
   public static void customAttributesTest(){
       Map<String,String> mapstr = new Map<String,String>();
       List<ConnectedApplication> caid = [select Id from ConnectedApplication
where Name Like '%bob%' LIMIT 1];
           
       Test.startTest();
       ConnectedAppPluginAttributes  cap = new ConnectedAppPluginAttributes();
       Map<String,String> respMapStr = cap.customAttributes(UserInfo.getUserId(),mapstr);
       Test.stopTest();

       Map<String,String> mapstrempty = new Map<String,String>();
       System.assertNotEquals(respMapStr,mapstrempty);

    // commented the below, keep getting Compile error: Variable does not exist: Auth
      //cap.refresh(UserInfo.getUserId(),caid[0].Id , Auth.InvocationContext);
      // cap.authorize(UserInfo.getUserId(),caid[0].Id , true , Auth.InvocationContext);

      
   }

}

 
RobertCRobertC
First off thank you Raj Vakati for assisting, more than I can say for SFDC support.  Anyway we figured out how to test this.

The line in you inital sample:  
Map<String,String> respMapStr = cap.customAttributes(UserInfo.getUserId(),mapstr);

Needed to be this in format to work:
Map<String,String> respMapStr = cap.customAttributes(UserInfo.getUserId(),'connectedappid',mapstr,NULL);

Hopefully this helps others that are trying to figure this out as well.
Paul ChuangPaul Chuang
Following the codes the above (thank you all so much), I came up with mine that is very similiar:
 
@isTest
public class ConnectedAppPluginExample_Test {

    @isTest
    public static void customerAttributeTest(){
        Map<String,String> mapStr = new Map<String, String>();
        List<ConnectedApplication> connectedApp = [select Id from ConnectedApplication where Name Like '%YOURCONNECTEDAPPAME%' LIMIT 1];
        String connectedAppId = '';
        if(connectedApp.size() > 0){
            connectedAppId = connectedApp[0].Id;
        }
        
        Test.startTest();
        ConnectedAppPluginExample cap = new GlobalPRM_ConnectedAppPlugin();
        Map<String, String> respMapStr = cap.customAttributes(UserInfo.getUserId(), connectedAppId, mapstr, Auth.InvocationContext.SAML_ASSERTION);
        Test.stopTest();
        Map<String, String> mapEmptyStr = new Map<String,String>();
        System.assertNotEquals(respMapStr, mapEmptyStr);
    }
}


 
Virendra Singh NarukaVirendra Singh Naruka
Hi I am not able to get the Custom Attribures  using   global class ConnectedAppPluginAttributes extends Auth.ConnectedAppPlugin .. Even not ablel to see debug log.   I am using OAuth  for authentication ..  .. 

Also I am not able to  recieve Custom attributes configured with connected app..  without ConnectedAppPlugin  class too..  I  am using Node.js as Client ..  Help ..