Author: dashorst
Date: Mon Nov 26 02:38:33 2007
New Revision: 598210

URL: http://svn.apache.org/viewvc?rev=598210&view=rev
Log:
Added a simple spam prevention to our guestbook to prevent automated spam bots 
of flooding our guestbook

Modified:
    
wicket/trunk/jdk-1.5/wicket-examples/src/main/java/org/apache/wicket/examples/guestbook/GuestBook.html
    
wicket/trunk/jdk-1.5/wicket-examples/src/main/java/org/apache/wicket/examples/guestbook/GuestBook.java
    wicket/trunk/jdk-1.5/wicket-examples/src/main/webapp/style.css
    
wicket/trunk/jdk-1.5/wicket-examples/src/test/java/org/apache/wicket/examples/guestbook/GuestbookTest.java

Modified: 
wicket/trunk/jdk-1.5/wicket-examples/src/main/java/org/apache/wicket/examples/guestbook/GuestBook.html
URL: 
http://svn.apache.org/viewvc/wicket/trunk/jdk-1.5/wicket-examples/src/main/java/org/apache/wicket/examples/guestbook/GuestBook.html?rev=598210&r1=598209&r2=598210&view=diff
==============================================================================
--- 
wicket/trunk/jdk-1.5/wicket-examples/src/main/java/org/apache/wicket/examples/guestbook/GuestBook.html
 (original)
+++ 
wicket/trunk/jdk-1.5/wicket-examples/src/main/java/org/apache/wicket/examples/guestbook/GuestBook.html
 Mon Nov 26 02:38:33 2007
@@ -10,8 +10,9 @@
     Add your comment here:
     <p>
     <textarea wicket:id = "text">This is a comment</textarea>
+    <input type="text" wicket:id="comment" class="nospam" 
onfocus="getElementById('formsubmit').focus();"/>
     <p>
-    <input type = "submit" value = "Submit"/>
+    <input type = "submit" value = "Submit" id="formsubmit"/>
   </form>
   <p/>
   <span wicket:id = "comments" id="comments">

Modified: 
wicket/trunk/jdk-1.5/wicket-examples/src/main/java/org/apache/wicket/examples/guestbook/GuestBook.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/jdk-1.5/wicket-examples/src/main/java/org/apache/wicket/examples/guestbook/GuestBook.java?rev=598210&r1=598209&r2=598210&view=diff
==============================================================================
--- 
wicket/trunk/jdk-1.5/wicket-examples/src/main/java/org/apache/wicket/examples/guestbook/GuestBook.java
 (original)
+++ 
wicket/trunk/jdk-1.5/wicket-examples/src/main/java/org/apache/wicket/examples/guestbook/GuestBook.java
 Mon Nov 26 02:38:33 2007
@@ -20,14 +20,17 @@
 import java.util.Date;
 import java.util.List;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.wicket.examples.WicketExamplePage;
 import org.apache.wicket.markup.html.basic.Label;
 import org.apache.wicket.markup.html.basic.MultiLineLabel;
 import org.apache.wicket.markup.html.form.Form;
 import org.apache.wicket.markup.html.form.TextArea;
+import org.apache.wicket.markup.html.form.TextField;
 import org.apache.wicket.markup.html.list.ListItem;
 import org.apache.wicket.markup.html.list.PropertyListView;
 import org.apache.wicket.model.CompoundPropertyModel;
+import org.apache.wicket.util.value.ValueMap;
 
 
 /**
@@ -55,6 +58,7 @@
                // Add commentListView of existing comments
                add(new PropertyListView("comments", commentList)
                {
+                       @Override
                        public void populateItem(final ListItem listItem)
                        {
                                listItem.add(new Label("date"));
@@ -79,30 +83,42 @@
                public CommentForm(final String id)
                {
                        // Construct form with no validation listener
-                       super(id, new CompoundPropertyModel(new Comment()));
+                       super(id, new CompoundPropertyModel(new ValueMap()));
 
                        // this is just to make the unit test happy
                        setMarkupId("commentForm");
 
                        // Add text entry widget
                        add(new TextArea("text"));
+
+                       // Add simple automated spam prevention measure.
+                       add(new TextField("comment"));
                }
 
                /**
                 * Show the resulting valid edit
                 */
+               @Override
                public final void onSubmit()
                {
+                       ValueMap values = (ValueMap)getModelObject();
+
+                       // check if the honey pot is filled
+                       if 
(StringUtils.isNotBlank((String)values.get("comment")))
+                       {
+                               error("Caught a spammer!!!");
+                               return;
+                       }
                        // Construct a copy of the edited comment
-                       final Comment comment = (Comment)getModelObject();
-                       final Comment newComment = new Comment(comment);
+                       Comment comment = new Comment();
 
                        // Set date of comment to add
-                       newComment.setDate(new Date());
-                       commentList.add(0, newComment);
+                       comment.setDate(new Date());
+                       comment.setText((String)values.get("text"));
+                       commentList.add(0, comment);
 
                        // Clear out the text component
-                       comment.setText("");
+                       values.put("text", "");
                }
        }
 

Modified: wicket/trunk/jdk-1.5/wicket-examples/src/main/webapp/style.css
URL: 
http://svn.apache.org/viewvc/wicket/trunk/jdk-1.5/wicket-examples/src/main/webapp/style.css?rev=598210&r1=598209&r2=598210&view=diff
==============================================================================
--- wicket/trunk/jdk-1.5/wicket-examples/src/main/webapp/style.css (original)
+++ wicket/trunk/jdk-1.5/wicket-examples/src/main/webapp/style.css Mon Nov 26 
02:38:33 2007
@@ -61,6 +61,9 @@
        list-style: circle;
        font-weight: bold;
 }
+.nospam {
+       display : none;
+}
 
 .block {
        padding: 10px;

Modified: 
wicket/trunk/jdk-1.5/wicket-examples/src/test/java/org/apache/wicket/examples/guestbook/GuestbookTest.java
URL: 
http://svn.apache.org/viewvc/wicket/trunk/jdk-1.5/wicket-examples/src/test/java/org/apache/wicket/examples/guestbook/GuestbookTest.java?rev=598210&r1=598209&r2=598210&view=diff
==============================================================================
--- 
wicket/trunk/jdk-1.5/wicket-examples/src/test/java/org/apache/wicket/examples/guestbook/GuestbookTest.java
 (original)
+++ 
wicket/trunk/jdk-1.5/wicket-examples/src/test/java/org/apache/wicket/examples/guestbook/GuestbookTest.java
 Mon Nov 26 02:38:33 2007
@@ -16,48 +16,32 @@
  */
 package org.apache.wicket.examples.guestbook;
 
-import junit.framework.Test;
+import java.util.ArrayList;
+
+import junit.framework.TestCase;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.wicket.examples.WicketWebTestCase;
+import org.apache.wicket.markup.html.form.Form;
+import org.apache.wicket.util.tester.FormTester;
+import org.apache.wicket.util.tester.WicketTester;
 
 
 /**
  * jWebUnit test for Hello World.
  */
-public class GuestbookTest extends WicketWebTestCase
+public class GuestbookTest extends TestCase
 {
        private static final Log log = LogFactory.getLog(GuestbookTest.class);
 
        /**
-        * 
-        * @return Test
-        */
-       public static Test suite()
-       {
-               return suite(GuestbookTest.class);
-       }
-
-       /**
-        * Construct.
-        * 
-        * @param name
-        *            name of test
-        */
-       public GuestbookTest(String name)
-       {
-               super(name);
-       }
-
-       /**
         * Sets up the test.
         * 
         * @throws Exception
         */
+       @Override
        public void setUp() throws Exception
        {
-               super.setUp();
                GuestBook.clear();
        }
 
@@ -68,33 +52,33 @@
         */
        public void test_1() throws Exception
        {
-               beginAt("/guestbook");
+               GuestBookApplication book = new GuestBookApplication();
+               WicketTester tester = new WicketTester(book);
+
+               tester.startPage(GuestBook.class);
+               tester.assertContains("Wicket Examples - guestbook");
 
-               assertTitleEquals("Wicket Examples - guestbook");
-               // this.assertXpathNodeNotPresent("//[EMAIL 
PROTECTED]:id='comments']");
-               this.assertElementNotPresent("comments");
-
-               assertFormPresent("commentForm");
-               this.assertFormElementPresent("text");
-               this.setFormElement("text", "test-1");
-               this.submit();
-
-               assertTitleEquals("Wicket Examples - guestbook");
-               assertFormPresent("commentForm");
-               this.assertFormElementPresent("text");
-               this.assertElementPresent("comments");
-               // assertTextInElement() seems to be buggy
-               // this.assertTextInElement("text", "test-1");
-               this.assertTextPresent("test-1");
-               this.setFormElement("text", "test-2");
-               this.submit();
-
-               assertTitleEquals("Wicket Examples - guestbook");
-               this.assertElementPresent("comments");
-               // assertTextInElement() seems to be buggy
-               // this.assertTextInElement("text", "test-1");
-               this.assertTextPresent("test-1");
-               // this.assertTextInElement("text", "test-2");
-               this.assertTextPresent("test-2");
+               // check if the list of comments is empty
+               tester.assertListView("comments", new ArrayList());
+               tester.assertComponent("commentForm", Form.class);
+               FormTester formTester = tester.newFormTester("commentForm");
+               formTester.setValue("text", "test-1");
+               formTester.submit();
+
+               tester.assertModelValue("comments:0:text", "test-1");
+
+               formTester = tester.newFormTester("commentForm");
+               formTester.setValue("text", "test-2");
+               formTester.submit();
+               tester.assertModelValue("comments:0:text", "test-2");
+               tester.assertModelValue("comments:1:text", "test-1");
+
+               formTester = tester.newFormTester("commentForm");
+               formTester.setValue("text", "test-3");
+               formTester.setValue("comment", "test-3");
+               formTester.submit();
+               tester.assertModelValue("comments:0:text", "test-2");
+               tester.assertModelValue("comments:1:text", "test-1");
+               tester.assertErrorMessages(new String[] { "Caught a spammer!!!" 
});
        }
 }


Reply via email to