0

I have an Apex web service that takes a query string. I need to update this class to take an encrypted string, find the appropriate record in Salesforce, and then return an encrypted JSON response. I am struggling with writing the unit test for this. I have read through the Crypto Class documentation and am working on the trailhead but I expect I am missing something here.

In my unit test, I have encrypted the lastName param and added to the request.addParmater by calling String.valueOf of the encrypted Blob value. But when I try to decrypt in my API I get an error of Unrecognized base 64 character. This makes think I am misunderstanding how to pass the data to the web service and was hoping I could ask for some help here. The error happens when I call the EncodingUtil on the string from the request params.

I apologize if the code is a little awkward, just trying to add the encryption in.

Here is part of the GET endpoint

 @HttpGet
    global static void getPatient() {
        Blob cryptoKey = Blob.valueOf(Label.Okta_User_API_Cypto_Key);
        System.debug('crypto key value');

        RestResponse response = RestContext.response;
        String email = RestContext.request.params.get('email');
        String phone = RestContext.request.params.get('primaryPhone');
        String lastName = RestContext.request.params.get('lastName');
        String productSerNum = RestContext.request.params.get('serialNum');   

        ////test decrypted code here
        String lastNametoDecrypt = lastName;
        Blob lastNameData = null;
        if(lastNametoDecrypt != null){
            /////////Error line number here!!!!
            lastNameData = EncodingUtil.base64Decode(lastNametoDecrypt);
        }
        Blob decryptedLastNameData = Crypto.decryptWithManagedIV('AES256', cryptoKey , lastNameData);
        String decryptedLastName = decryptedLastNameData.toString();
        System.debug('value of decrypted last name ' + decryptedLastName);

        /// End test decrypt code
}

Below is the unit test

@IsTest
static void getPatient() {
        Patient__c testPatient = [SELECT First_Name__c, Last_Name__c, Email__c, Birthdate__c, Home_Phone__c
        FROM Patient__c LIMIT 1];
        // Set up a test request
        RestRequest request = new RestRequest();
        RestResponse response = new RestResponse();
         Blob cryptoKey = Blob.valueOf(Label.Okta_User_API_Cypto_Key);

        Blob lastname = Blob.valueOf('kirk');       
        Blob encrypted = Crypto.encryptWithManagedIV('AES256', cryptoKey, lastname);

        request.addParameter('email', testPatient.Email__c );       
        request.addParameter('lastName', String.valueOf(encrypted));
        request.addParameter('primaryPhone', testPatient.Home_Phone__c);
        request.addParameter('dob', '1950-01-01');
        request.addParameter('serialNum', '12345-54321');
        request.httpMethod = 'GET';
        RestContext.request = request;
        RestContext.response = response;

       Test.startTest();
       AccountClaimAPI.getPatient();
       Test.stopTest();

       System.assertNotEquals(null, response.responseBody);
       System.assertEquals(200, response.statusCode);
    }
identigral
  • 7,543
  • 29
  • 32
  • 42
Brooks Johnson
  • 1,443
  • 2
  • 21
  • 37

0 Answers0