Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial commit for the AF password box core component #1090

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,6 @@ private FormConstants() {
public static final String REQ_ATTR_REFERENCED_PATH = "referencedPage";

public static final String PROP_FRAGMENT_PATH = "fragmentPath";

public static final String RT_FD_FORM_PASSWORD_V1 = RT_FD_FORM_PREFIX + "password/v1/password";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Copyright 2024 Adobe
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

package com.adobe.cq.forms.core.components.internal.models.v1.form;

import javax.annotation.Nullable;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.InjectionStrategy;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

import com.adobe.cq.export.json.ComponentExporter;
import com.adobe.cq.export.json.ExporterConstants;
import com.adobe.cq.forms.core.components.internal.form.FormConstants;
import com.adobe.cq.forms.core.components.models.form.Password;
import com.adobe.cq.forms.core.components.util.AbstractFieldImpl;

@Model(
adaptables = { SlingHttpServletRequest.class, Resource.class },
adapters = { Password.class, ComponentExporter.class },
resourceType = { FormConstants.RT_FD_FORM_PASSWORD_V1 })
@Exporter(
name = ExporterConstants.SLING_MODEL_EXPORTER_NAME,
extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class PasswordImpl extends AbstractFieldImpl implements Password {

@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL)
@Nullable
private String validationPattern;

@Override
public String getFieldType() {
return super.getFieldType();
}

@Override
public Integer getMinLength() {
return minLength;
}

@Override
public Integer getMaxLength() {
return maxLength;
}

@Override
public Long getMinimum() {
return minimum;
}

@Override
public Long getMaximum() {
return maximum;
}

@Override
public Long getExclusiveMaximum() {
return exclusiveMaximum;
}

@Override
public Long getExclusiveMinimum() {
return exclusiveMinimum;
}

@Override
public String getFormat() {
return displayFormat;
}

@Override
public String getValidationPattern() {
return validationPattern;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public enum FieldType {
PANEL("panel"),
FORM("form"),
CHECKBOX_GROUP("checkbox-group"),
IMAGE("image");
IMAGE("image"),
PASSWORD("password");

private String value;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Copyright 2024 Adobe
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

package com.adobe.cq.forms.core.components.models.form;

import org.osgi.annotation.versioning.ConsumerType;

/**
* Interface for {@code Password} Sling Model used for the {@code /apps/core/fd/components/form/password/v1/password} component.
*
* @since com.adobe.cq.forms.core.components.models.form 2.0.0
*/
@ConsumerType
public interface Password extends Field, StringConstraint, NumberConstraint {

/**
* Returns the validation pattern (regex) for the password field.
*
* @return the validation pattern
* @since com.adobe.cq.forms.core.components.models.form 2.0.0
*/
String getValidationPattern();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ Copyright 2024 Adobe
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

package com.adobe.cq.forms.core.components.internal.models.v1.form;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import com.adobe.cq.forms.core.Utils;
import com.adobe.cq.forms.core.components.datalayer.FormComponentData;
import com.adobe.cq.forms.core.components.models.form.FieldType;
import com.adobe.cq.forms.core.components.models.form.Password;
import com.adobe.cq.forms.core.context.FormsCoreComponentTestContext;
import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;

import static org.junit.Assert.assertEquals;

@ExtendWith(AemContextExtension.class)
public class PasswordImplTest {

private static final String BASE = "/form/password";
private static final String CONTENT_ROOT = "/content";
private static final String PATH_PASSWORD_DATALAYER = CONTENT_ROOT + "/password-datalayer";
private static final String PATH_PASSWORD_CUSTOMIZED = CONTENT_ROOT + "/password-customized";

private final AemContext context = FormsCoreComponentTestContext.newAemContext();

@BeforeEach
void setUp() {
context.load().json(BASE + FormsCoreComponentTestContext.TEST_CONTENT_JSON, CONTENT_ROOT);
}

@Test
void testFieldType() {
Password password = Utils.getComponentUnderTest(PATH_PASSWORD_CUSTOMIZED, Password.class, context);
assertEquals(FieldType.PASSWORD.getValue(), password.getFieldType());
}

@Test
void testGetLabel() {
Password password = Utils.getComponentUnderTest(PATH_PASSWORD_CUSTOMIZED, Password.class, context);
assertEquals("pwd", password.getLabel().getValue());
}

@Test
void testPlaceholder() {
Password password = Utils.getComponentUnderTest(PATH_PASSWORD_CUSTOMIZED, Password.class, context);
assertEquals("Enter valid password", password.getPlaceHolder());
}

@Test
void testGetName() {
Password password = Utils.getComponentUnderTest(PATH_PASSWORD_CUSTOMIZED, Password.class, context);
assertEquals("password", password.getName());

}

@Test
void testDorProperties() {
Password password = Utils.getComponentUnderTest(PATH_PASSWORD_CUSTOMIZED, Password.class, context);
assertEquals(true, password.getDorProperties().get("dorExclusion"));
assertEquals("4", password.getDorProperties().get("dorColspan"));
assertEquals("Text1", password.getDorProperties().get("dorBindRef"));

}

@Test
void testGetDescription() {
Password password = Utils.getComponentUnderTest(PATH_PASSWORD_CUSTOMIZED, Password.class, context);
assertEquals("password field", password.getDescription());
}

@Test
void testGetRequired() {
Password password = Utils.getComponentUnderTest(PATH_PASSWORD_CUSTOMIZED, Password.class, context);
assertEquals(true, password.isRequired());
}

@Test
void testGetValidationPattern() {
Password password = Utils.getComponentUnderTest(PATH_PASSWORD_CUSTOMIZED, Password.class, context);
assertEquals("/^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}$/", password.getValidationPattern());

}

@Test
void testIsEnabled() {
Password password = Utils.getComponentUnderTest(PATH_PASSWORD_CUSTOMIZED, Password.class, context);
assertEquals(true, password.isEnabled());
}

@Test
void testIsEnabledForCustomized() {
Password password = Utils.getComponentUnderTest(PATH_PASSWORD_CUSTOMIZED, Password.class, context);
assertEquals(true, password.isEnabled());
}

@Test
void testIsReadOnly() {
Password password = Utils.getComponentUnderTest(PATH_PASSWORD_CUSTOMIZED, Password.class, context);
assertEquals(false, password.isReadOnly());
}

@Test
void testIsReadOnlyForCustomized() {
Password password = Utils.getComponentUnderTest(PATH_PASSWORD_CUSTOMIZED, Password.class, context);
assertEquals(false, password.isReadOnly());
}

@Test
void testMinLength() {
Password password = Utils.getComponentUnderTest(PATH_PASSWORD_CUSTOMIZED, Password.class, context);
assertEquals(5, password.getMinLength().intValue());
}

@Test
void testMaxLength() {
Password password = Utils.getComponentUnderTest(PATH_PASSWORD_CUSTOMIZED, Password.class, context);
assertEquals(10, password.getMaxLength().intValue());
}

@Test
void testGetExclusiveMinimum() {
Password password = Utils.getComponentUnderTest(PATH_PASSWORD_CUSTOMIZED, Password.class, context);
assertEquals(8, password.getExclusiveMinimum().intValue());
}

@Test
void testGetExclusiveMaximum() {
Password password = Utils.getComponentUnderTest(PATH_PASSWORD_CUSTOMIZED, Password.class, context);
assertEquals(16, password.getExclusiveMaximum().intValue());
}

@Test
void testGetMinimum() {
Password password = Utils.getComponentUnderTest(PATH_PASSWORD_CUSTOMIZED, Password.class, context);
assertEquals(1, password.getMinimum().intValue());
}

@Test
void testGetMaximum() {
Password password = Utils.getComponentUnderTest(PATH_PASSWORD_CUSTOMIZED, Password.class, context);
assertEquals(6, password.getMaximum().intValue());
}

@Test
void testGetDisplayFormat() throws Exception {
Password password = Utils.getComponentUnderTest(PATH_PASSWORD_CUSTOMIZED, Password.class, context);
assertEquals("password", password.getFormat());
}

@Test
void testDataLayerProperties() throws IllegalAccessException {
Password password = Utils.getComponentUnderTest(PATH_PASSWORD_DATALAYER, Password.class, context);
FieldUtils.writeField(password, "dataLayerEnabled", true, true);
FormComponentData dataObject = (FormComponentData) password.getData();
assert (dataObject != null);
assert (dataObject.getId()).equals("password-1c7bc238a6");
assert (dataObject.getType()).equals("core/fd/components/form/password/v1/password");
assert (dataObject.getTitle()).equals("Full Name");
assert (dataObject.getFieldType()).equals("password");
assert (dataObject.getDescription()).equals("Enter Full Name");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"id": "password-477c777f4e",
"fieldType": "password",
"name": "password1708629052628",
"visible": true,
"description": "<p>long desc</p>\r\n",
"tooltip": "<p>short desc</p>\r\n",
"type": "string",
"required": true,
"enabled": true,
"constraintMessages": {
"required": "Password is required",
"minLength": "min chars",
"maxLength": "max chars",
"pattern": "Invalid pwd"
},
"readOnly": false,
"minLength": 5,
"maxLength": 10,
"validationPattern": "/^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}$/",
"label": {
"value": "Password",
"visible": true
},
"properties": {
"afs:layout": {
"tooltipVisible": false
},
"fd:dor": {
"dorExclusion": false
},
"fd:path": "/content/password-customized"
},
"events": {
"custom:setProperty": [
"$event.payload"
]
},
"placeholder": "Enter valid password",
":type": "core/fd/components/form/password/v1/password"
}