View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.fit.enduser;
20  
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  
24  import org.apache.syncope.client.enduser.pages.Login;
25  import org.apache.syncope.client.enduser.pages.SelfPasswordReset;
26  import org.apache.syncope.client.enduser.pages.SelfRegistration;
27  import org.apache.syncope.client.enduser.pages.SelfResult;
28  import org.apache.syncope.client.lib.SyncopeClient;
29  import org.apache.syncope.client.ui.commons.Constants;
30  import org.apache.syncope.common.lib.SyncopeConstants;
31  import org.apache.syncope.common.lib.request.StringReplacePatchItem;
32  import org.apache.syncope.common.lib.request.UserUR;
33  import org.apache.syncope.common.lib.to.SecurityQuestionTO;
34  import org.apache.syncope.common.lib.to.UserTO;
35  import org.apache.syncope.common.rest.api.beans.AnyQuery;
36  import org.apache.wicket.util.tester.FormTester;
37  import org.junit.jupiter.api.Test;
38  
39  public class AnonymousITCase extends AbstractEnduserITCase {
40  
41      @Test
42      public void selfCreate() {
43          String username = "testUser";
44  
45          TESTER.startPage(Login.class);
46          TESTER.assertRenderedPage(Login.class);
47  
48          TESTER.clickLink("self-registration");
49          TESTER.assertRenderedPage(SelfRegistration.class);
50  
51          String form = "body:contentWrapper:content:selfRegistrationPanel:form";
52          FormTester formTester = TESTER.newFormTester(form);
53  
54          formTester.setValue("userDetailsPanelCard:contentPanel:username:textField", username);
55  
56          formTester.setValue(
57                  "userDetailsPanelCard:contentPanel:password:passwordPanel:passwordInnerForm:password:passwordField",
58                  "Password123");
59          formTester.setValue(
60                  "userDetailsPanelCard:contentPanel:password:passwordPanel:passwordInnerForm:confirmPassword:"
61                  + "passwordField",
62                  "Password123");
63  
64          formTester.setValue(
65                  "plainAttrsPanelCard:contentPanel:plainSchemas:schemas:6:panel:textField",
66                  "Fullname");
67  
68          formTester.setValue(
69                  "plainAttrsPanelCard:contentPanel:plainSchemas:schemas:12:panel:textField",
70                  "Surname");
71  
72          formTester.setValue(
73                  "plainAttrsPanelCard:contentPanel:plainSchemas:schemas:14:panel:textField",
74                  username + "@syncope.apache.org");
75  
76          try {
77              TESTER.executeAjaxEvent(form + ":submit", Constants.ON_CLICK);
78  
79              TESTER.assertRenderedPage(SelfResult.class);
80  
81              assertFalse(USER_SERVICE.search(new AnyQuery.Builder().realm(SyncopeConstants.ROOT_REALM).
82                      fiql(SyncopeClient.getUserSearchConditionBuilder().is("username").equalTo(username).query()).
83                      build()).getResult().isEmpty());
84  
85              assertNotNull(USER_SERVICE.read(username));
86  
87              TESTER.cleanupFeedbackMessages();
88          } finally {
89              // cleanup
90              try {
91                  USER_SERVICE.delete(username);
92              } catch (Exception e) {
93                  // ignore
94              }
95          }
96      }
97  
98      @Test
99      public void selfPasswordReset() {
100         SecurityQuestionTO question = SECURITY_QUESTION_SERVICE.read("887028ea-66fc-41e7-b397-620d7ea6dfbb");
101 
102         UserTO selfpwdreset = USER_SERVICE.read("selfpwdreset");
103         USER_SERVICE.update(new UserUR.Builder(selfpwdreset.getKey()).
104                 securityQuestion(new StringReplacePatchItem.Builder().value(question.getKey()).build()).
105                 securityAnswer(new StringReplacePatchItem.Builder().value("ananswer").build()).
106                 build());
107 
108         TESTER.startPage(Login.class);
109         TESTER.assertRenderedPage(Login.class);
110 
111         TESTER.clickLink("self-pwd-reset");
112 
113         TESTER.assertRenderedPage(SelfPasswordReset.class);
114 
115         String pwdResetForm = "body:contentWrapper:content:selfPwdResetForm";
116         FormTester formTester = TESTER.newFormTester(pwdResetForm);
117 
118         // 1. set username to selfpwdreset
119         formTester.setValue("selfPasswordResetPanelCard:contentPanel:username", "selfpwdreset");
120         TESTER.executeAjaxEvent(
121                 pwdResetForm + ":selfPasswordResetPanelCard:contentPanel:username", Constants.ON_BLUR);
122 
123         // 2. check that the question has been populated
124         TESTER.assertModelValue(
125                 pwdResetForm + ":selfPasswordResetPanelCard:contentPanel:securityQuestion",
126                 question.getContent());
127 
128         // 3. submit form and receive an error
129         TESTER.executeAjaxEvent(pwdResetForm + ":submit", Constants.ON_CLICK);
130         TESTER.assertErrorMessages("Invalid Security Answer");
131         TESTER.cleanupFeedbackMessages();
132 
133         // 3.1 set the correct answer
134         formTester = TESTER.newFormTester(pwdResetForm);
135         formTester.setValue("selfPasswordResetPanelCard:contentPanel:username", "selfpwdreset");
136         TESTER.executeAjaxEvent(
137                 pwdResetForm + ":selfPasswordResetPanelCard:contentPanel:username", Constants.ON_BLUR);
138         TESTER.assertModelValue(
139                 pwdResetForm + ":selfPasswordResetPanelCard:contentPanel:securityQuestion",
140                 question.getContent());
141         formTester.setValue("selfPasswordResetPanelCard:contentPanel:securityAnswer", "ananswer");
142         TESTER.executeAjaxEvent(
143                 pwdResetForm + ":selfPasswordResetPanelCard:contentPanel:securityAnswer", Constants.ON_CHANGE);
144 
145         // 4. submit form
146         TESTER.executeAjaxEvent(pwdResetForm + ":submit", Constants.ON_CLICK);
147 
148         TESTER.assertRenderedPage(SelfResult.class);
149 
150         TESTER.cleanupFeedbackMessages();
151     }
152 }