• Pfooti
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies

So, I want to generate an email when Site.changePassword (old, new, confirm) is successful. As far as I can tell, on error (bad password or whatever), it returns null (and adds to the pagemessage queue). On success it returns my Site's top-level page.

 

My code works fine in regular use, I just say

 

PageReference retVal = Site.changePassword(newPassword, verifyNewPassword, oldpassword);

if (retVal != null) {

// send emails

}

return retVal;

 

The problem is that within the IsTest stuff, Site.changePassword seems to return Null, even if the password change is nominally a success. You can see my test code (run as SeeAllData=True) below - I create a contact in the appropriate account group, create a user from that contact, insert data, set the password on the user, and then try calling Site.changePassword with something that *should* work. 

 

Site.changePassword (according to my debug logs) runs and doesn't add anything to the ApexPages PageMessage queue. (parenthetically, it's really, really, really frustrating that errors in this method are handled that way instead of a sane way like throwing an exception).

 

So either I'm really missing something fundamental in how to do runAs in a test method, or Site.changePassword returns null on success when called inside a test environment. 

 

Any help is appreciated.

 

 

 

 

Contact c = new Contact(lastname = 'test',

                        firstname = 'test',

                      email = 'test@example.com',

                      accountId = '001e0000005PfVe');

insert(c);

Profile applicantProfile = [Select Id, Name from Profile where Name = 'Applicant Profile'];

User testUser = new User(lastname = 'test',

                        firstname = 'test',

                      email = 'test@example.com',

                      username= 'test@example.com, 

                      profileId= applicantProfile.id, 

                      contactId= c.id,

                      alias = 'test',

                      emailencodingkey='UTF-8',

                      languagelocalekey='en_US',

                      localesidkey='en_US',

                      timezonesidkey='America/Los_Angeles');

insert(testUser);

System.setPassword(testUser.Id, 'abc123456');

ChangePasswordController controller = new ChangePasswordController();

controller.oldPassword = 'abc123456';

controller.newPassword = 'qwerty1234'; 

controller.verifyNewPassword = 'qwerty1234';                

PageReference retVal = null;

System.runAs(testUser) {

retVal = controller.changePassword();

System.debug(retVal);

System.debug('RETURN URL '+Site.getOriginalURL());

System.assertNotEquals(retVal, null);

}

  • April 27, 2013
  • Like
  • 0