• Santhosh Gaddam 17
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
public with sharing class CSSChangePasswordController {
    @AuraEnabled
    public static String changePassword(String newPassword, String verifyNewPassword, String oldPassword) {
        String returnMessage = '';
        try{
            PageReference pr = changeExistingPassword(newPassword, verifyNewPassword, oldPassword);
            if (pr != null) {
                returnMessage = 'SUCCESS';
            } else {
                returnMessage = 'Error changing password. Contact Admin.';
            } 
            if(Test.isRunningTest()){
                throw new auraException();
            }
            
        } catch (Exception ex) {
            returnMessage = ex.getMessage();
        }
        return returnMessage;
    }
    
    public static PageReference changeExistingPassword(String newPassword, String verifyNewPassword, String oldPassword) {
        PageReference pf = Site.changePassword(newPassword, verifyNewPassword, oldpassword);
        return pf;
    }
}

=====here is my test class======
@isTest
public class CSSChangePasswordController_Test {
    public static List<User> userList=new List<User>();
    public static List<Contact> contactList=new List<Contact>();
    
    @testSetup static void setUp(){
        Profile profile=[Select id from Profile Where Name='System Administrator' LIMIT 1];
        User user=new User();
        user.JCI_Global_ID__c='GlobalId01';
        user.FirstName='First Name1';
        user.LastName='Last Name1';
        user.ProfileId=profile.id;
        user.Email='Dummymail@jci.com';
        user.Username='Dummymail@jci.com';
        user.Alias = 'Alias 1' ;
        user.TimeZoneSidKey = 'America/Los_Angeles';
        user.LocaleSidKey = 'en_US';
        user.EmailEncodingKey = 'UTF-8';
        user.LanguageLocaleKey = 'en_US';
        user.isActive = true;
        userList.add(user);
        insert userList;
        system.setPassword(userList[0].id, 'Abc@123456');
    }
    
    
    static testMethod void testChangePassword(){
        List<User> user1= [Select id,Email,Username from User where Username= 'Dummymail@jci.com' AND isActive=true ];
        Test.startTest();
        PageReference pf=null;
        String returnMessage=null;
        pf=CSSChangePasswordController.changeExistingPassword('Qwerty@1234', 'Qwerty@1234', 'Abc@123456');
        returnMessage= CSSChangePasswordController.changePassword('Qwerty@1234', 'Qwerty@1234', 'Abc@123456');
       
        Test.stopTest();
    }
}