Passed
Push — master ( c8946b...d7743b )
by Simon
01:41
created

PasswordValidatorExtensionTest::testGetSetParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Firesphere\HaveIBeenPwned\Tests;
4
5
use Firesphere\HaveIBeenPwned\Extensions\PasswordValidatorExtension;
6
use Firesphere\HaveIBeenPwned\Services\HaveIBeenPwnedService;
7
use GuzzleHttp\Handler\MockHandler;
8
use GuzzleHttp\Psr7\Response;
9
use SilverStripe\Core\Config\Config;
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\Dev\Debug;
12
use SilverStripe\Dev\SapphireTest;
13
use SilverStripe\ORM\ValidationResult;
14
use SilverStripe\Security\Member;
15
16
class PasswordValidatorExtensionTest extends SapphireTest
17
{
18
19
    public function testGetSetParams()
20
    {
21
        $mock = ['handler' => []];
22
23
        /** @var PasswordValidatorExtension $extension */
24
        $extension = Injector::inst()->get(PasswordValidatorExtension::class);
25
        $extension->setParams($mock);
26
27
        $this->assertEquals($mock, $extension->getParams());
28
    }
29
30
    public function testUpdateValidatePasswordAllowAll()
31
    {
32
        Config::modify()->set(HaveIBeenPwnedService::class, 'allow_pwnd', true);
33
        Config::modify()->set(HaveIBeenPwnedService::class, 'save_pwnd', false);
34
        $body = file_get_contents(__DIR__ . '/../fixtures/pwnd1234.txt');
35
        $body2 = file_get_contents(__DIR__ . '/../fixtures/breachmails.json');
36
        // This sets up the mock client to respond to the request it gets
37
        // with an HTTP 200 containing your mock body.
38
        $mock = new MockHandler([
39
            new Response(123, [], $body),
40
            new Response(123, [], $body2),
41
        ]);
42
43
        /** @var PasswordValidatorExtension $extension */
44
        $extension = Injector::inst()->get(PasswordValidatorExtension::class);
45
        $extension->setParams(['handler' => $mock]);
46
        /** @var Member $member */
47
        $member = Member::create();
48
        /** @var ValidationResult $valid */
49
        $valid = ValidationResult::create();
50
        $extension->updateValidatePassword('123', $member, $valid, null);
0 ignored issues
show
Unused Code introduced by
The call to Firesphere\HaveIBeenPwne...pdateValidatePassword() has too many arguments starting with null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        $extension->/** @scrutinizer ignore-call */ 
51
                    updateValidatePassword('123', $member, $valid, null);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
51
52
        $this->assertTrue($valid->isValid());
53
        $this->assertEquals(1014565, $member->PasswordIsPwnd);
54
        $this->assertEquals('', $member->BreachedSites);
55
    }
56
57
    public function testUpdatePasswordValidateTemporarily()
58
    {
59
        /** @var PasswordValidatorExtension $extension */
60
        $extension = Injector::inst()->get(PasswordValidatorExtension::class);
61
        /** @var Member $member */
62
        $member = Member::create(['PwndDisabled' => 'true']);
63
        /** @var ValidationResult $valid */
64
        $valid = ValidationResult::create();
65
        $extension->updateValidatePassword('password', $member, $valid, null);
0 ignored issues
show
Unused Code introduced by
The call to Firesphere\HaveIBeenPwne...pdateValidatePassword() has too many arguments starting with null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
        $extension->/** @scrutinizer ignore-call */ 
66
                    updateValidatePassword('password', $member, $valid, null);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
66
67
        $this->assertTrue($valid->isValid());
68
        $this->assertEquals(0, $member->PasswordIsPwnd);
69
        $this->assertEquals('', $member->BreachedSites);
70
    }
71
72
    public function testUpdateValidatePasswordDeny()
73
    {
74
        Config::modify()->set(HaveIBeenPwnedService::class, 'allow_pwnd', false);
75
        Config::modify()->set(HaveIBeenPwnedService::class, 'save_pwnd', false);
76
        /** @var PasswordValidatorExtension $extension */
77
        $extension = Injector::inst()->get(PasswordValidatorExtension::class);
78
79
        /** @var Member $member */
80
        $member = Member::create();
81
        /** @var ValidationResult $valid */
82
        $valid = ValidationResult::create();
83
84
        $body = file_get_contents(__DIR__ . '/../fixtures/pwnd123.txt');
85
        // This sets up the mock client to respond to the request it gets
86
        // with an HTTP 200 containing your mock body.
87
        $mock = new MockHandler([
88
            new Response(123, [], $body),
89
        ]);
90
91
        $extension->setParams(['handler' => $mock]);
92
        $extension->updateValidatePassword('123', $member, $valid, null);
0 ignored issues
show
Unused Code introduced by
The call to Firesphere\HaveIBeenPwne...pdateValidatePassword() has too many arguments starting with null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
        $extension->/** @scrutinizer ignore-call */ 
93
                    updateValidatePassword('123', $member, $valid, null);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
93
94
        $this->assertFalse($valid->isValid());
95
    }
96
97
    public function testStoredBreached()
98
    {
99
        Config::modify()->set(HaveIBeenPwnedService::class, 'allow_pwnd', false);
100
        Config::modify()->set(HaveIBeenPwnedService::class, 'save_pwnd', false);
101
102
103
        /** @var PasswordValidatorExtension $extension */
104
        $extension = Injector::inst()->get(PasswordValidatorExtension::class);
105
106
        /** @var Member $member */
107
        $member = Member::create(['Email' => '[email protected]']);
108
        /** @var ValidationResult $valid */
109
        $valid = ValidationResult::create();
110
111
        $body = file_get_contents(__DIR__ . '/../fixtures/pwnd1234.txt');
112
        $body2 = file_get_contents(__DIR__ . '/../fixtures/breachmails.json');
113
        // This sets up the mock client to respond to the request it gets
114
        // with an HTTP 200 containing your mock body.
115
        $mock = new MockHandler([
116
            new Response(123, [], $body),
117
            new Response(123, [], $body2),
118
        ]);
119
120
        $extension->setParams(['handler' => $mock]);
121
        $extension->updateValidatePassword('1234', $member, $valid, null);
0 ignored issues
show
Unused Code introduced by
The call to Firesphere\HaveIBeenPwne...pdateValidatePassword() has too many arguments starting with null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
        $extension->/** @scrutinizer ignore-call */ 
122
                    updateValidatePassword('1234', $member, $valid, null);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
122
123
        $messages = $valid->getMessages();
124
125
        $this->assertCount(1, $messages);
126
        $this->assertEmpty($member->BreachedSites);
127
128
        Config::modify()->set(HaveIBeenPwnedService::class, 'save_pwnd', true);
129
130
        /** @var ValidationResult $valid */
131
        $valid = ValidationResult::create();
132
133
        $mock = new MockHandler([
134
            new Response(123, [], $body),
135
            new Response(123, [], $body2),
136
        ]);
137
138
        $extension->setParams(['handler' => $mock]);
139
        $extension->updateValidatePassword('1234', $member, $valid, null);
140
141
        $this->assertContains('2fast4u', $member->BreachedSites);
142
    }
143
144
    protected function setUp()
145
    {
146
        return parent::setUp(); // TODO: Change the autogenerated stub
147
    }
148
149
    protected function tearDown()
150
    {
151
        /** @var Member|null $member */
152
        $member = Member::get()->filter(['Email' => '[email protected]'])->first();
153
        if ($member) {
0 ignored issues
show
introduced by
$member is of type SilverStripe\Security\Member, thus it always evaluated to true.
Loading history...
154
            $member->delete();
155
        }
156
        parent::tearDown(); // TODO: Change the autogenerated stub
157
    }
158
}
159