You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2023/04/13 11:12:51 UTC

[myfaces-tobago] branch main updated: demo(test): Test style nonce (#3964)

This is an automated email from the ASF dual-hosted git repository.

lofwyr pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git


The following commit(s) were added to refs/heads/main by this push:
     new c07665860d demo(test): Test style nonce (#3964)
c07665860d is described below

commit c07665860d8314b68c859f2245c15f57883f1c0c
Author: Udo Schnurpfeil <lo...@apache.org>
AuthorDate: Thu Apr 13 13:12:45 2023 +0200

    demo(test): Test style nonce (#3964)
    
    issue: TOBAGO-2210
---
 .../tobago/example/demo/NonceController.java       | 26 ++++++++
 .../900-test/40000-style/300-nonce/Nonce.test.js   | 47 ++++++++++++++
 .../900-test/40000-style/300-nonce/Nonce.xhtml     | 72 ++++++++++++++++++++++
 3 files changed, 145 insertions(+)

diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/NonceController.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/NonceController.java
new file mode 100644
index 0000000000..926dd0b6f2
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/NonceController.java
@@ -0,0 +1,26 @@
+package org.apache.myfaces.tobago.example.demo;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.enterprise.context.RequestScoped;
+import javax.inject.Named;
+import java.lang.invoke.MethodHandles;
+
+@RequestScoped
+@Named
+public class NonceController {
+
+  private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+  private boolean ajax = false;
+
+  public void ajax() {
+    LOG.info("AJAX action called.");
+    ajax = true;
+  }
+
+  public boolean isAjax() {
+    return ajax;
+  }
+}
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/content/900-test/40000-style/300-nonce/Nonce.test.js b/tobago-example/tobago-example-demo/src/main/webapp/content/900-test/40000-style/300-nonce/Nonce.test.js
new file mode 100644
index 0000000000..5a08248287
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/main/webapp/content/900-test/40000-style/300-nonce/Nonce.test.js
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+import {JasmineTestTool} from "/tobago/test/tobago-test-tool.js";
+import {elementByIdFn, querySelectorFn} from "/script/tobago-test.js";
+
+it("height by style tag normal", function (done) {
+  const box1Fn = elementByIdFn("page:mainForm:box1");
+
+  const test = new JasmineTestTool(done);
+  test.do(() => expect(getComputedStyle(box1Fn()).height).toBe("300px"));
+  test.start();
+});
+
+it("height by style tag ajax", function (done) {
+  const box2Fn = elementByIdFn("page:mainForm:box2");
+  const ajaxFn = elementByIdFn("page:mainForm:ajax");
+  const box2titleFn = querySelectorFn("#page\\:mainForm\\:box2 h3");
+
+  const test = new JasmineTestTool(done);
+  test.do(() => expect(getComputedStyle(box2Fn()).height).toBe("300px"));
+  test.event("click", ajaxFn, () => box2titleFn().innerHTML.trim() === "Box ajax=true");
+  test.do(() => expect(getComputedStyle(box2Fn()).height).toBe("300px"));
+  test.start();
+});
+
+it("height by style tag fake", function (done) {
+  const box3Fn = elementByIdFn("page:mainForm:box3");
+
+  const test = new JasmineTestTool(done);
+  test.do(() => expect(getComputedStyle(box3Fn()).height).not.toBe("300px"));
+  test.start();
+});
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/content/900-test/40000-style/300-nonce/Nonce.xhtml b/tobago-example/tobago-example-demo/src/main/webapp/content/900-test/40000-style/300-nonce/Nonce.xhtml
new file mode 100644
index 0000000000..2056215c2e
--- /dev/null
+++ b/tobago-example/tobago-example-demo/src/main/webapp/content/900-test/40000-style/300-nonce/Nonce.xhtml
@@ -0,0 +1,72 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+-->
+
+<ui:composition template="/main.xhtml"
+                xmlns="http://www.w3.org/1999/xhtml"
+                xmlns:tc="http://myfaces.apache.org/tobago/component"
+                xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
+                xmlns:f="http://xmlns.jcp.org/jsf/core">
+
+  <p>
+    This tests among others the fix of issue TOBAGO-2210.
+  </p>
+
+  <tc:section label="Style with nonce check after normal load">
+    <ul>
+      <li>Test if nonce attribute is set corretly, after first load.</li>
+      <li>Test if styles from style tag was applied corrretly, after first load.</li>
+    </ul>
+
+    <tc:box label="Box" id="box1">
+      <tc:style minHeight="300px"/>
+      Style: min-height: 300px
+    </tc:box>
+
+  </tc:section>
+
+  <tc:section label="Style with nonce check after AJAX load">
+    <ul>
+      <li>Test if nonce attribute is set corretly, after AJAX reload.</li>
+      <li>Test if styles from style tag was applied corrretly, after AJAX reload.</li>
+    </ul>
+
+    <tc:box label="Box ajax=#{nonceController.ajax}" id="box2">
+      <tc:style minHeight="300px"/>
+      Style: min-height: 300px
+    </tc:box>
+
+    <tc:button action="#{nonceController.ajax}" label="AJAX reload" id="ajax">
+      <f:ajax render="box2"/>
+    </tc:button>
+
+  </tc:section>
+
+  <tc:section label="Style with wrong nonce must not applied">
+    <ul>
+      <li>Test if styles from style tag was NOT applied.</li>
+    </ul>
+
+    <tc:box label="Box" id="box3">
+      <style nonce="invalid-dummy-nonce"> #page\:mainForm\:box3 { min-height:300px; } </style>
+      Style: min-height: 300px<br/>
+      Must not be applied, because of CSP. If it is applied, CSP is switched off, or it's just not working.
+    </tc:box>
+
+  </tc:section>
+</ui:composition>