+ Start a Discussion
Shiva SFDeveloperShiva SFDeveloper 

Test class for Transaction Security Policy

I am creating a Transaction Security Policy as per the example in the trail head. When creating the test class, it was giving me errors saying Platform field is not writeable. We also won't be able to create a new LoginHistory record. What is a better way to write the test class for this policy class to properly assert and verify the functionality? Any pointers would be appreciated. Thanks in advance!
 
global class BlockAndroidPolicyCondition implements TxnSecurity.PolicyCondition {
  public boolean evaluate(TxnSecurity.Event e) {
    LoginHistory eObj = [SELECT Platform FROM LoginHistory
                         WHERE Id = :e.data.get('LoginHistoryId')];
    if (eObj != null) {
      if (eObj.Platform.contains('Android') &&  
          eObj.Platform.compareTo('Android 5') < 0) { 
        return true;
      }
    }
    return false;
  }
}
@isTest
public class TestBlockAndroid {
  public static testMethod void testIsAndroid() {

    /* Create a history object that has Platform = Android 4. */
    LoginHistory loginHistoryObj = new LoginHistory();
    loginHistoryObj.Platform = 'Android 4';

    /* Create a map for the event we’re going to build. */
    Map<String, String> eventData = new Map<String, String>();

    /* Insert the LoginHistoryId into the event data map. */
    insert loginHistoryObj;
    eventData.put('LoginHistoryId', loginHistoryObj.id);

    TxnSecurity.Event e = new TxnSecurity.Event(
      '00Dxxx123123123', /* organizationId */
      '005xxx123123123', /* userId */
      'AuthSession', /* entityName */
      'Login', /* action */
      'LoginHistory', /* resourceType */
      '01pR00000009D2H', /* entityId */
      Datetime.newInstance(2016, 2, 15), /* timeStamp */
      eventData); /* data - Map with info about this event. */

    BlockAndroidPolicyCondition condition =
      new BlockAndroidPolicyCondition();

    System.assertEquals(true, condition.evaluate(e));
  }
}



 
Raj VakatiRaj Vakati
Hi , 

Use this code . 
@isTest(Seealldata=true)
public class TestBlockAndroid {
    public static testMethod void testIsAndroid() {
        System.Test.startTest() ;
        
        LoginHistory l = [ SELECT Platform FROM LoginHistory Limit 1];
        /* Create a map for the event we’re going to build. */
        Map<String, String> eventData = new Map<String, String>();
        eventData.put('LoginHistoryId', l.Id);
        
        TxnSecurity.Event e = new TxnSecurity.Event(
            UserInfo.getOrganizationId(), /* organizationId */
            UserInfo.getUserId(), /* userId */
            'AuthSession', /* entityName */
            'Login', /* action */
            'LoginHistory', /* resourceType */
            '01pR00000009D2H', /* entityId */
            Datetime.newInstance(2016, 2, 15), /* timeStamp */
            eventData); /* data - Map with info about this event. */
        
        BlockAndroidPolicyCondition111 condition =
            new BlockAndroidPolicyCondition111();
                condition.evaluate(e);

       // System.assertEquals(true, condition.evaluate(e));
        System.Test.stopTest() ;
        
    }
}
Shruti SShruti S
Please let me know the actual challenge in Trailhead. In that way, I can read the challenge and see what exactly is going on. 
Shiva SFDeveloperShiva SFDeveloper
Hi @rajamohan, Thank you for your response, really appreciate it. The test class provided by you gives good coverage; but I am trying to see if there is any way to cover both the true and false scenarios.