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
pchadha20pchadha20 

Plz help me out to write the Test method for Controller Class

Hi Guys,

I am trying to write the test method for below mention controller but not able to write. This class is calling ReCaptcha service. I am new to this , please help me out.

 

public  class SA_CFX_Password_Reset_Controller {

    private static string secret = '6LdecwwAAAAAALtIuEKANEv7lgzQNGzMAluFXSHk ';
    public string publicKey { get { return '6LdecwwAAAAAAIqUiXkcEJ_-V1U99uwCm-S4OXuY ' ; }} 

    private static string baseUrl = 'http://api-verify.recaptcha.net/verify'; 
     
    public string challenge {get; set;} { challenge = null; } 
    public string response {get; set; }  { response = null; } 
    public string message {get; set;} { message = null; } 

    public Boolean correctResponse { get; private set; } { correctResponse = false; } 
    
    public Account acc;
       
    public void SA_CFX_Password_Reset_Controller()
    {
        //Constructor
    }
    
    public Account getacc()
    { 
        if (acc == null) 
            acc = new Account();
        return acc;
    }
 
    public PageReference verify() {
      
    try { 
          if ( challenge == null || response == null ) { 
            
            return null; 
          }
                    
          HttpResponse r = makeRequest(  baseUrl ,
               'privatekey='+ secret + 
               '&remoteip=' + remoteHost + 
               '&challenge=' + challenge +
               '&response=' + response +
               '&error=incorrect-captcha-sol'
          );
        
          if ( r != null ) {  // is null when test methods run
            correctResponse = ( r.getBody().contains('true') );
          }
          
          if(correctResponse == true) {              
                Account Acct = [select Id, SA_CFX_Primary_Contact_eMail__c, SA_CFX_Reset_OTP__c, (Select Id,Password__c, User_Id__c, Flag__c  From CFX_Trading_Accounts__r) from Account where SA_CFX_Primary_Contact_eMail__c = :acc.SA_CFX_Primary_Contact_eMail__c ];
                CFX_Trading_Account__c[] tdacc = Acct.CFX_Trading_Accounts__r;
                
                list <CFX_Trading_Account__c> tacc = new list<CFX_Trading_Account__c>();
                for(Integer i=0; i<tdacc.size(); i++)
                {   
                    if(i == 0){
                      tdacc[i].Flag__c = true;
                    }
                    SA_CFXPasswordGenerator PassGen = new SA_CFXPasswordGenerator();
                    String pd = PassGen.randomPassword();
                    tdacc[i].Password__c = pd;
                    tacc.add(tdacc[i]);         
                    
                }
                                
                if(tacc.size()>0){                  
                    Database.SaveResult[] lsr = Database.update(tacc,true);
                    
                    for(Database.SaveResult sr : lsr){
                        if(sr.isSuccess()){
                          
              return Page.SA_CFX_Password_Reset_Successful;
              
                        }
                        else{
                            return Page.SA_CFX_Password_Reset_Failure;
                        }
                    }
                }
                else {
                    
                    return Page.SA_CFX_Password_Reset_Failure;
                }
                
          }  
        response = null;
        challenge = null;  
        message = 'Yes';
        return null;
   }
   
   catch (Exception e)  {
     ApexPages.addMessages(e);
     return null;
     
   }
        
 }    
       
   

    public PageReference reset() {
        challenge = null;
        response = null; 
        return null; 
    }   

    public static HttpResponse makeRequest(string url, string body)  {
          HttpRequest req = new HttpRequest();   
          HttpResponse response = null;
          req.setEndpoint( url );
          req.setMethod('POST');
          req.setBody ( body);
          try {
            Http http = new Http();
            response = http.send(req);
            System.debug('response: '+ response);
            System.debug('body: '+ response.getBody());
           
          } catch( System.Exception e) {
             System.debug('ERROR: '+ e);
          }
          return response;
        }   
    
    public PageReference close()
    {
        return null;
    }
  
    public PageReference close1()
    {   
        PageReference pg = page.ResetOTP ;
        pg.setRedirect(true);
        return pg; 
        return null;
    }
    
    
    public string remoteHost { get { string ret = '127.0.0.1';
        // also could use x-original-remote-host 
        map<string , string> hdrs = ApexPages.currentPage().getHeaders();
        if ( hdrs.get('x-original-remote-addr') != null)
            ret =  hdrs.get('x-original-remote-addr');
        else if ( hdrs.get('X-Salesforce-SIP') != null)
            ret =  hdrs.get('X-Salesforce-SIP');
        return ret;
    } }
    

 

Many Thanks,

pchadha20

snievassnievas

What is the error you are getting? Is it because of the web callouts that are ending the test method prematurely?

 

If so, you'll need to adjust your test method to execute the other portions of the code without actually making a webservice callout. The docs should have more info on this when writing test methods.