Skip to content

Commit 757c133

Browse files
committed
Added a Pester Unit Test Script to New-RandomPassword.ps1
1 parent 5c85846 commit 757c133

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Paolo Frigo, 2018
2+
# https://www.scriptinglibrary.com
3+
4+
. .\New-RandomPassword.ps1
5+
6+
Describe "New-RandomPassword" -Tags "Unit Tests" {
7+
it "Should generate a String" {
8+
(New-RandomPassword).GetType().Name | Should BeExactly "String"
9+
}
10+
11+
#Pick a password lenght between 0 and 7 and 33 and 100 characters
12+
$n = 0..1 + 33..100
13+
foreach ($length in $n){
14+
it "Should throw an exeption if the password lenght ($length) is out of the accepted range (8-32)" {
15+
{New-RandomPassword -Lenght 5} | Should throw
16+
}
17+
}
18+
#Pick a password lenght between 8 and 32 characters
19+
$n = 8..32
20+
foreach ($length in $n){
21+
It "Should generate a password $length character long" {
22+
(New-RandomPassword($length)).length | Should BeExactly "$length"
23+
}
24+
}
25+
#Test/Generate 300 passwords
26+
foreach ($attempt in (1..300)){
27+
It "Should generate a password complex enough for AD ($attempt/300 attempt)"{
28+
$n = 8..32 | Get-Random
29+
$RegEx = "(?=^.{8,32}$)((?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))^.*"
30+
(New-RandomPassword($n)) -match $RegEx
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)