Created
August 27, 2015 17:15
-
-
Save sergiorgiraldo/46efe15f5c0de9c3b29e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//PWD.JS | |
var Ret=function(){ | |
this.status = true; | |
this.message = ""; | |
}; | |
function validateThePassword(vl){ | |
var ret=new Ret(); | |
if (vl.length < 8){ | |
ret.status =false; | |
ret.message += "length must be greater than 8\n"; | |
} | |
if (!(/[a-z]/.test(vl))){ | |
ret.status =false; | |
ret.message += "must have one lowercase\n"; | |
} | |
if (!(/[A-Z]/.test(vl))){ | |
ret.status =false; | |
ret.message += "must have one uppercase\n"; | |
} | |
if (!(/[0-9]/.test(vl))){ | |
ret.status =false; | |
ret.message += "must have one number\n"; | |
} | |
if (!(/[!@#$%&*()\-=+,.;:~^]/.test(vl))){ | |
ret.status =false; | |
ret.message += "must have one of this !@#$%&*()-=+,.;:~^\n"; | |
} | |
return ret; | |
} | |
//PWD.HTML | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<script type="text/javascript" src="./pwd.js"></script> | |
<script type="text/javascript" src="./jquery-2.1.3.min.js"></script> | |
<script type="text/javascript"> | |
function checkPassword(){ | |
var ret = validateThePassword($("#pwd").val()); | |
if (!ret.status) | |
$("#lblResult").html(ret.message.replace(/\n/g, "<br/>")); | |
else | |
$("#lblResult").html("good password"); | |
} | |
function setup(){ | |
$("#pwd").on("keypress", function(e){if (e.keyCode == 13) checkPassword();}); | |
$("#btnValidate").on("click", function(){checkPassword();}); | |
} | |
$(document).ready(function(){ | |
setup(); | |
}); | |
</script> | |
</head> | |
<body> | |
Your password:: <input type="text" id="pwd" /><br/> | |
<button id="btnValidate">validate</button> | |
<p><label id="lblResult"></label> | |
</body> | |
</html> | |
//PWDTESTS.HTML | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>password unit tests</title> | |
<link rel="stylesheet" type="text/css" href="jasmine/jasmine.css"> | |
<script type="text/javascript" src="./jquery-2.1.3.min.js"></script> | |
<script type="text/javascript" src="./jasmine/jasmine.js"></script> | |
<script type="text/javascript" src="./jasmine/jasmine-html.js"></script> | |
<script type="text/javascript" src="./jasmine/boot.js"></script> | |
<script type="text/javascript" src="./jasmine/jasmine-jquery.js"></script> | |
<script type="text/javascript" src="./pwd.js"></script><!-- SUT --> | |
<script type="text/javascript"> | |
console.clear(); | |
describe("wrong passwords tests", function(){ | |
it( "password must be at least 8 in length", function() { | |
var pwd="pwd"; | |
var ret = validateThePassword(pwd); | |
expect(ret.status).toBeFalsy(); | |
expect(ret.message).toContain("length must be greater than 8"); | |
}); | |
it( "password must have at least one lowercase", function() { | |
var pwd="PWD"; | |
var ret = validateThePassword(pwd); | |
expect(ret.status).toBeFalsy(); | |
expect(ret.message).toContain("must have one lowercase"); | |
}); | |
it( "password must have at least one uppercase", function() { | |
var pwd="pwd"; | |
var ret = validateThePassword(pwd); | |
expect(ret.status).toBeFalsy(); | |
expect(ret.message).toContain("must have one uppercase"); | |
}); | |
it( "password must have at least one number", function() { | |
var pwd="pwd"; | |
var ret = validateThePassword(pwd); | |
expect(ret.status).toBeFalsy(); | |
expect(ret.message).toContain("must have one number"); | |
}); | |
it( "password must have at least one non alphanumeric character", function() { | |
var pwd="pwd"; | |
var ret = validateThePassword(pwd); | |
expect(ret.status).toBeFalsy(); | |
expect(ret.message).toContain("must have one of this !@#$%&*()-=+,.;:~^"); | |
}); | |
it( "must not contain length error nor lowercase error", function() { | |
var pwd="pwdfa3dsa3dsdj"; | |
var ret = validateThePassword(pwd); | |
expect(ret.status).toBeFalsy(); | |
expect(ret.message).not.toContain("must have one lowercase"); | |
expect(ret.message).not.toContain("length must be greater than 8"); | |
expect(ret.message).not.toContain("must have one number"); | |
}); | |
it( "must not contain length error nor lowercase error nor uppercase error", function() { | |
var pwd="pwdfa3dsa3dSdj"; | |
var ret = validateThePassword(pwd); | |
expect(ret.status).toBeFalsy(); | |
expect(ret.message).not.toContain("must have one lowercase"); | |
expect(ret.message).not.toContain("must have one uppercase"); | |
expect(ret.message).not.toContain("length must be greater than 8"); | |
expect(ret.message).not.toContain("must have one number"); | |
}); | |
}); | |
describe("right passwords tests", function(){ | |
it( "Password1!", function() { | |
var pwd="Password1!"; | |
var ret = validateThePassword(pwd); | |
expect(ret.status).toBeTruthy(); | |
expect(ret.message).toEqual(""); | |
}); | |
it( "wRPasswd1!", function() { | |
var pwd="wRPasswd1!"; | |
var ret = validateThePassword(pwd); | |
expect(ret.status).toBeTruthy(); | |
expect(ret.message).toEqual(""); | |
}); | |
it( "#$#$#2rF", function() { | |
var pwd="#$#$#2rF"; | |
var ret = validateThePassword(pwd); | |
expect(ret.status).toBeTruthy(); | |
expect(ret.message).toEqual(""); | |
}); | |
it( "Fr#$#$#2", function() { | |
var pwd="Fr#$#$#2"; | |
var ret = validateThePassword(pwd); | |
expect(ret.status).toBeTruthy(); | |
expect(ret.message).toEqual(""); | |
}); | |
it( "2Fr#$#$#", function() { | |
var pwd="2Fr#$#$#"; | |
var ret = validateThePassword(pwd); | |
expect(ret.status).toBeTruthy(); | |
expect(ret.message).toEqual(""); | |
}); | |
}); | |
describe("UI tests", function(){ | |
beforeEach(function() { | |
jasmine.getFixtures().fixturesPath = "."; | |
loadFixtures("pwd.html"); | |
setup(); | |
}); | |
it( "button click must call validate", function() { | |
spyOn(window, "validateThePassword").and.callFake(function(){return new Ret();}); | |
$("#btnValidate").click(); | |
expect(window.validateThePassword).toHaveBeenCalled(); | |
}); | |
it( "enter key press on password field must call validate", function() { | |
spyOn(window, "validateThePassword").and.callFake(function(){return new Ret();}); | |
var e = jQuery.Event("keypress", { keyCode: 13 }); | |
$("#pwd").trigger(e); | |
expect(window.validateThePassword).toHaveBeenCalled(); | |
}); | |
it( "if result status is true, change label to 'good password'", function() { | |
spyOn(window, "validateThePassword").and.callFake(function(){ | |
var ret = new Ret(); | |
ret.status = true; | |
return ret; | |
}); | |
$("#btnValidate").click(); | |
expect($("#lblResult")).toHaveText("good password"); | |
}); | |
it( "if result status is false, change label to result message", function() { | |
spyOn(window, "validateThePassword").and.callFake(function(){ | |
var ret = new Ret(); | |
ret.status = false; | |
ret.message = "error message"; | |
return ret; | |
}); | |
$("#btnValidate").click(); | |
expect($("#lblResult")).toHaveText("error message"); | |
}); | |
}); | |
</script> | |
</head> | |
<body></body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment