Skip to content

Commit

Permalink
Improve tests, mark test skipped if it cannot be run
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesDPC committed Jan 8, 2025
1 parent 5d22e80 commit 26e18be
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions tests/PasswordStrengthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use NSWDPC\Authentication\Rules\DictionaryWordRule;
use NSWDPC\Authentication\Rules\RepetitiveCharacterRule;
use NSWDPC\Authentication\Rules\SequentialCharacterRule;
use NSWDPC\Authentication\Services\NISTPasswordValidator;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
Expand All @@ -17,6 +18,18 @@ class PasswordStrengthTest extends SapphireTest
{
protected $usesDatabase = true;

public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
$validator = Injector::inst()->get(PasswordValidator::class);
Member::set_password_validator($validator);
}

public function testHasValidator(): void {
$validator = Member::password_validator();
$this->assertInstanceOf(NISTPasswordValidator::class, $validator, "PasswordValidator is a NISTPasswordValidator");
}

public function testContextualWords(): bool
{
$strings = [
Expand Down Expand Up @@ -106,6 +119,8 @@ public function testDictionaryWords(): bool
}
}

} else {
$this->markTestSkipped( 'This test could not be run because the enchant extension was not available' );
}

return true;
Expand Down Expand Up @@ -192,7 +207,7 @@ public function testVerifier(): void

$validator = Member::password_validator();

$this->assertTrue($validator instanceof PasswordValidator, "Member password validator is not an instance of PasswordValidator");
$this->assertInstanceOf(PasswordValidator::class, $validator, "Member password validator is an instance of PasswordValidator");

// Bob wants to set his password to this... it should fail
$repetitive_password = "abcd12345defgh";
Expand Down Expand Up @@ -232,9 +247,14 @@ public function testRandomPasswords(): void
'Surname' => 'Smith',
]);

$validator = Member::password_validator();
$minLength = $validator->getMinLength();
if(!$minLength) {
$minLength = 8;
}
$password_count = 5;
for($i = 0;$i < $password_count;$i++) {
$keys = array_rand($letters, 8);
$keys = array_rand($letters, $minLength);
$password = "";
foreach($keys as $key) {
$password .= $letters[ $key ];
Expand Down

0 comments on commit 26e18be

Please sign in to comment.