Browse files

validate: first prototype for password validation including password …

…strength meter

git-svn-id: http://jqueryjs.googlecode.com/svn/trunk/plugins/validate.password@5834 c715fcbe-d12f-0410-84c4-316a508785bb
  • Loading branch information...
1 parent 7d428f1 commit 8f3019e7520788a435ff11c89927f63f0f2a8666 joern.zaefferer committed Aug 19, 2008
Showing with 123 additions and 0 deletions.
  1. +33 −0 test/index.html
  2. +39 −0 test/rating.js
  3. +51 −0 test/strength-meter.js
View
33 test/index.html
@@ -0,0 +1,33 @@
+<html id="html">
+<head>
+ <title>jQuery Validation Plugin Password Extension Test Suite</title>
+ <link rel="Stylesheet" media="screen" href="qunit/testsuite.css" />
+ <script type="text/javascript" src="../../validate/lib/jquery.js"></script>
+ <script type="text/javascript" src="../lib/ui.core.js"></script>
+ <script type="text/javascript" src="../lib/ui.progressbar.js"></script>
+ <script type="text/javascript" src="qunit/testrunner.js"></script>
+ <script type="text/javascript" src="../../validate/jquery.validate.js"></script>
+ <script type="text/javascript" src="../jquery.validate.password.js"></script>
+ <script type="text/javascript" src="rating.js"></script>
+ <script type="text/javascript" src="strength-meter.js"></script>
+
+ <style type="text/css">
+ .xerror, .error { display: none }
+ </style>
+</head>
+<body id="body">
+ <h1><a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/">jQuery Validation Plugin Password Extension</a> Test Suite</h1>
+ <h2 id="banner"></h2>
+ <h2 id="userAgent"></h2>
+
+ <div id="main" style="display:none;">
+ <form id="form">
+ <input id="username" name="username" value="Peter" />
+ <input class="required password" name="password" id="password" />
+ </form>
+ </div>
+
+ <ol id="tests"></ol>
+
+</body>
+</html>
View
39 test/rating.js
@@ -0,0 +1,39 @@
+test("ratings", function() {
+
+ function rates(password, expectedMessage, expectedRating, username) {
+ var rating = $.validator.passwordRating(password, username);
+ equals(rating.rate, expectedRating, "rating for " + password);
+ equals(rating.messageKey, expectedMessage);
+ }
+
+ rates(null, "too-short", 0);
+ rates("", "too-short", 0);
+ rates("a", "too-short", 0);
+ rates("abcdefg", "too-short", 0);
+
+ // long enough, but too simple: very weak
+ rates("aaaaaaaa", "very-weak", 1);
+ rates("@@@@@@@@", "very-weak", 1);
+
+ // long enough and mixed characters: weak
+ rates("Computer", "weak", 2);
+ rates("computer", "weak", 2);
+ rates("abcdefgh", "weak", 2);
+
+ // letters and a number or upper and lowercase letters: good
+ rates("computer1", "good", 3);
+ rates("cOmputer", "good", 3);
+
+ // letters and numbers or upper/lower and number, or letter and special: strong
+ rates("computer12", "strong", 4);
+ rates("c@mputer", "strong", 4);
+ rates("cOmputer2", "strong", 4);
+
+ // long pass phrase
+ rates("my uncle ben has my password safe in his safe", "strong", 4);
+
+ // differ from username
+ rates("computer", "similar-to-username", 0, "computer");
+ rates("cOmputer1", "similar-to-username", 0, "computer");
+
+});
View
51 test/strength-meter.js
@@ -0,0 +1,51 @@
+(function() {
+
+var input;
+var messages = $.validator.passwordRating.messages;
+
+function check(messageKey) {
+ input.valid();
+ if (!messageKey) {
+ equals( input.next(":visible").text(), $.validator.messages.required );
+ } else {
+ equals( input.next(":visible").text(), messages[messageKey] );
+ }
+}
+
+test("basic password strength meter", function() {
+
+ $("#form").validate();
+
+ input = $("#password");
+ check();
+
+ input.val("a");
+ check("too-short");
+
+ input.val("");
+ check();
+
+ input.val("abc123@po");
+ check("strong");
+});
+
+test("check similar username", function() {
+
+ input = $("#password").removeClass();
+
+ $("#form").validate({
+ rules: {
+ password: {
+ required: true,
+ password: "#username"
+ }
+ }
+ });
+
+ check();
+
+ input.val("peterpeter");
+ check("similar-to-username");
+});
+
+})();

0 comments on commit 8f3019e

Please sign in to comment.