Guest User

Untitled

a guest
Feb 4th, 2018
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.36 KB | None | 0 0
  1. public class GHI_Portal_Password_Controller {
  2.  
  3. public Boolean hasNewPassword { get; set; }
  4. public Boolean hasSecurity { get;set; }
  5. public Boolean isReset {get;set;}
  6. public Boolean hasConfirmPassword { get; set; }
  7.  
  8. public String newPassword { get; set; }
  9. public String confirmPassword { get; set; }
  10. public String passwordMsg { get; set; }
  11. public String securityError { get; set; }
  12.  
  13. public String charAllowed { get; set; }
  14.  
  15. public User currentUser { get; set; }
  16. public Boolean passwordMatch{get;set;}
  17.  
  18.  
  19. public GHI_Portal_Password_Controller(){
  20. this.charAllowed = Label.GHI_Portal_AllowedChar;
  21. this.hasConfirmPassword = true;
  22. this.hasNewPassword = true;
  23. passwordMsg = '';
  24. passwordMatch= false;
  25. currentUser = [Select GHI_Portal_Security_Question_1__c,GHI_Portal_Security_Question_2__c,GHI_Portal_Security_Question_3__c,
  26. GHI_Portal_Answer_1__c,GHI_Portal_Answer_2__c, GHI_Portal_Answer_3__c from User where Id = :UserInfo.getUserId()];
  27.  
  28. // system.debug('@@currentUser.GHI_Portal_Security_Question_1__c '+currentUser.GHI_Portal_Security_Question_1__c);
  29. if ((currentUser.GHI_Portal_Security_Question_1__c != null && currentUser.GHI_Portal_Security_Question_1__c != '') &&
  30. (currentUser.GHI_Portal_Security_Question_2__c != null && currentUser.GHI_Portal_Security_Question_2__c != '') &&
  31. (currentUser.GHI_Portal_Security_Question_3__c != null && currentUser.GHI_Portal_Security_Question_3__c != '') &&
  32. (currentUser.GHI_Portal_Answer_1__c != null && currentUser.GHI_Portal_Answer_1__c != '') &&
  33. (currentUser.GHI_Portal_Answer_2__c != null && currentUser.GHI_Portal_Answer_2__c != '') &&
  34. (currentUser.GHI_Portal_Answer_3__c != null && currentUser.GHI_Portal_Answer_3__c != ''))
  35. {
  36. isReset = false;
  37. }
  38. else{
  39. isReset = true;
  40. }
  41. }
  42.  
  43.  
  44. public PageReference changePasswordPage() {
  45. PageReference prSiteChangePass;
  46. this.passwordMsg='';
  47.  
  48.  
  49. if (currentUser.GHI_Portal_Security_Question_1__c == null || currentUser.GHI_Portal_Answer_1__c==null ||
  50. currentUser.GHI_Portal_Security_Question_2__c == null || currentUser.GHI_Portal_Answer_2__c==null ||
  51. currentUser.GHI_Portal_Security_Question_3__c == null || currentUser.GHI_Portal_Answer_3__c==null) {
  52. hasSecurity = true;
  53. this.securityError = Label.GHI_Portal_SecurityError;
  54. }
  55. **else if(newPassword.length() > 20 || confirmPassword.length() > 20) {
  56. this.passwordMsg = Label.GHI_Portal_PasswordMsg;
  57. passwordMatch = true;**
  58. //return ApexPages.currentPage();
  59. }
  60. **else if (newPassword.contains(' ')) {
  61. this.passwordMsg = Label.GHI_Portal_PasswordMsg;**
  62. //System.debug('@@passwordMsg2 ' + passwordMsg);
  63. **passwordMatch = true;**
  64. //return ApexPages.currentPage();
  65. }
  66. **else if (currentUser.GHI_Portal_Security_Question_1__c == currentUser.GHI_Portal_Security_Question_2__c ||**
  67. currentUser.GHI_Portal_Security_Question_2__c == currentUser.GHI_Portal_Security_Question_3__c ||
  68. currentUser.GHI_Portal_Security_Question_1__c == currentUser.GHI_Portal_Security_Question_3__c) {
  69. **hasSecurity = true;
  70. this.securityError = Label.GHI_Portal_UniqueQuestionMsg;**
  71. }
  72. else{
  73. **update currentUser;
  74. prSiteChangePass = Site.changePassword(newPassword, confirmPassword);
  75. return ((hasNewPassword && hasConfirmPassword) ? prSiteChangePass : ApexPages.currentPage());**
  76. }
  77. return ApexPages.currentPage();
  78. }
  79. }
  80.  
  81. @isTest
  82. private class GHI_Portal_Password_Controller_Test {
  83.  
  84. private static testMethod void test() {
  85.  
  86. }
  87. private static testMethod void testLogin() {
  88. GHI_Portal_Password_Controller controller1 = new GHI_Portal_Password_Controller();
  89. User testUser = new User();
  90. testUser.Username= 'testUser11@company.com';
  91. testUser.Email = 'testuser1@company.com';
  92. testUser.Lastname = 'user';
  93. testUser.Firstname = 'test';
  94. testUser.Alias = 'test';
  95. testUser.CommunityNickname = '12346test';
  96. List<Profile> prof = [select Id from Profile where Name = 'System Administrator' LIMIT 1];
  97. List<UserRole> role = [select Id from UserRole where Name = 'finance'];
  98. testUser.UserRoleId = role[0].Id;
  99. testUser.ProfileId = prof[0].Id;
  100.  
  101. //testUser.CurrencyIsoCode = 'USD';
  102. testUser.TimeZoneSidKey = 'GMT';
  103. testUser.LocaleSidKey = 'en_US';
  104. testUser.EmailEncodingKey = 'ISO-8859-1';
  105.  
  106. testUser.LanguageLocaleKey = 'en_US';
  107. testUser.UserPermissionsMobileUser = false;
  108. insert testUser;
  109.  
  110. PageReference pageRef = Page.GHI_Portal_Login;
  111. pageRef.getParameters().put('A','1');
  112. GHI_Portal_Login controller = new GHI_Portal_Login();
  113. Test.setCurrentPage(pageRef);
  114.  
  115. Test.startTest();
  116. System.runAs(testUser) {
  117. RecordType rtContact = [SELECT Id FROM RecordType WHERE SObjectType='Contact' AND Name='Patient' LIMIT 1];
  118. Account acct = OSM_DataFactory.createAccount('test test');
  119. insert acct;
  120. Contact ctct = OSM_DataFactory.createContact('test','test',rtContact.Id);
  121. ctct.AccountId = acct.Id;
  122. insert ctct;
  123.  
  124. User user = new User();
  125. user.Username= 'testUser1@company.com';
  126. user.Email = 'testuser1@company.com';
  127. user.Lastname = 'user';
  128. user.Firstname = 'test';
  129. user.Alias = 'test';
  130. user.CommunityNickname = '12346';
  131. List<Profile> prof1 = [select Id from Profile where Name = 'GHI Portal User' LIMIT 1];
  132. user.ProfileId = prof1[0].Id;
  133. user.ContactId = ctct.Id;
  134.  
  135. //testUser.CurrencyIsoCode = 'USD';
  136. user.TimeZoneSidKey = 'GMT';
  137. user.LocaleSidKey = 'en_US';
  138. user.EmailEncodingKey = 'ISO-8859-1';
  139. user.LanguageLocaleKey = 'en_US';
  140. user.UserPermissionsMobileUser = false;
  141. user.GHI_Portal_Security_Question_1__c ='';
  142. user.GHI_Portal_Security_Question_2__c ='';
  143. user.GHI_Portal_Security_Question_3__c ='';
  144. user.GHI_Portal_Answer_3__c = '';
  145. insert user;
  146. controller1.changePasswordPage();
  147. controller1.newPassword = 'dsadsadsada';
  148. controller1.confirmPassword = 'dsadadasda';
  149. update user;
  150. update testUser;
  151. update acct;
  152. update ctct;
  153. Test.stopTest();
  154.  
  155. }
  156. }
  157. }
  158.  
  159. controller1.changePasswordPage();
  160.  
  161. // Set your values in these fields so it doesn't go in the first IF statement
  162. testUser.GHI_Portal_Security_Question_1__c = 'Test';
  163. testUser.GHI_Portal_Security_Question_2__c = 'Test';
  164. testUser.GHI_Portal_Security_Question_3__c = 'Test';
  165. testUser.GHI_Portal_Answer_1__c = 'Test';
  166. testUser.GHI_Portal_Answer_2__c = 'Test';
  167. testUser.GHI_Portal_Answer_3__c = 'Test';
  168. update testUser;
  169. controller1 = new GHI_Portal_Password_Controller(); // re-initialise the controller in order to get the field values in the user variable
  170. // Also set newPassword and confirmPassword with length > 20 so that it executes the 2nd else-if block
  171. controller1.newPassword = '1234567890123456789012345';
  172. controller1.confirmPassword = '1234567890123456789012345';
  173. controller1.changePasswordPage(); // Call again this method, this time it should skip the first IF statement since it evaluates to false
  174. // Let's now change the passwords again so that it doesn't go in the 2nd but goes in the 3rd else-if (means we have to have whitespace in the password)
  175. controller1.newPassword = 'a b';
  176. controller1.confirmPassword = 'a b';
  177. controller1.changePasswordPage();
  178. // Reset all this again so that all first 3 if/else-if blocks are skipped (< 20 characters and no whitespace)
  179. controller1.newPassword = 'abc';
  180. controller1.confirmPassword = 'abc';
  181. controller1.changePasswordPage();
  182. // Update the question/answer values again just like for the 1st if statement, but make sure they all have different values so that your last else if is covered
Add Comment
Please, Sign In to add comment