Passed
Push — fix-9163 ( 4cfde3...07a516 )
by Ingo
17:14
created

testAuthenticate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 67
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 36
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 67
rs 9.344

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SilverStripe\Security\Tests;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Control\NullHTTPRequest;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Dev\SapphireTest;
10
use SilverStripe\ORM\FieldType\DBDatetime;
11
use SilverStripe\ORM\ValidationResult;
12
use SilverStripe\Security\Authenticator;
13
use SilverStripe\Security\DefaultAdminService;
14
use SilverStripe\Security\IdentityStore;
15
use SilverStripe\Security\LoginAttempt;
16
use SilverStripe\Security\Member;
17
use SilverStripe\Security\MemberAuthenticator\CMSMemberAuthenticator;
18
use SilverStripe\Security\MemberAuthenticator\CMSMemberLoginForm;
19
use SilverStripe\Security\MemberAuthenticator\MemberAuthenticator;
20
use SilverStripe\Security\MemberAuthenticator\MemberLoginForm;
21
use SilverStripe\Security\PasswordValidator;
22
use SilverStripe\Security\Security;
23
use SilverStripe\Versioned\Versioned;
24
25
/**
26
 * @skipUpgrade
27
 */
28
class VersionedMemberAuthenticatorTest extends SapphireTest
29
{
30
31
    protected $usesDatabase = true;
32
33
    protected static $required_extensions = [
34
        Member::class => [
35
            Versioned::class
36
        ]
37
    ];
38
39
    public function setUp()
40
    {
41
        parent::setUp();
42
43
        if (!class_exists(Versioned::class)) {
44
            $this->markTestSkipped("Versioned is required");
45
            return;
46
        }
47
48
        // Enforce dummy validation (this can otherwise be influenced by recipe config)
49
        PasswordValidator::singleton()
50
            ->setMinLength(0)
51
            ->setTestNames([]);
52
    }
53
54
    protected function tearDown()
55
    {
56
        $this->logOut();
57
        parent::tearDown();
58
    }
59
60
    public function testAuthenticate()
61
    {
62
        $mockDate1 = '2010-01-01 10:00:00';
63
        $readingMode = sprintf('Archive.%s.Stage', $mockDate1);
64
65
        /** @var Member $member */
66
        $member = DBDatetime::withFixedNow($mockDate1, function () {
67
            $member = Member::create();
68
            $member->update([
69
                'FirstName' => 'Jane',
70
                'Surname' => 'Doe',
71
                'Email' => '[email protected]'
72
            ]);
73
            $member->write();
74
            $member->changePassword('password', true);
75
76
            return $member;
77
        });
78
79
        $member->changePassword('new-password', true);
80
81
        /** @var ValidationResult $results */
82
        $results = Versioned::withVersionedMode(function () use ($readingMode) {
83
            Versioned::set_reading_mode($readingMode);
84
            $authenticator = new MemberAuthenticator();
85
86
            // Test correct login
87
            /** @var ValidationResult $message */
88
            $authenticator->authenticate(
89
                [
90
                    'Email' => '[email protected]',
91
                    'Password' => 'password'
92
                ],
93
                Controller::curr()->getRequest(),
94
                $result
95
            );
96
97
            return $result;
98
        });
99
100
        $this->assertFalse(
101
            $results->isValid(),
102
            'Authenticate using old credentials fails even when using an old reading mode'
103
        );
104
105
        /** @var ValidationResult $results */
106
        $results = Versioned::withVersionedMode(function () use ($readingMode) {
107
            Versioned::set_reading_mode($readingMode);
108
            $authenticator = new MemberAuthenticator();
109
110
            // Test correct login
111
            /** @var ValidationResult $message */
112
            $authenticator->authenticate(
113
                [
114
                    'Email' => '[email protected]',
115
                    'Password' => 'new-password'
116
                ],
117
                Controller::curr()->getRequest(),
118
                $result
119
            );
120
121
            return $result;
122
        });
123
124
        $this->assertTrue(
125
            $results->isValid(),
126
            'Authenticate using current credentials succeeds even when using an old reading mode'
127
        );
128
    }
129
130
    public function testAuthenticateAgainstLiveStage()
131
    {
132
        /** @var Member $member */
133
        $member = Member::create();
134
        $member->update([
135
            'FirstName' => 'Jane',
136
            'Surname' => 'Doe',
137
            'Email' => '[email protected]'
138
        ]);
139
        $member->write();
140
        $member->changePassword('password', true);
141
        $member->publishSingle();
0 ignored issues
show
Bug introduced by
The method publishSingle() does not exist on SilverStripe\Security\Member. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

141
        $member->/** @scrutinizer ignore-call */ 
142
                 publishSingle();
Loading history...
142
143
        $member->changePassword('new-password', true);
144
145
        /** @var ValidationResult $results */
146
        $results = Versioned::withVersionedMode(function () {
147
            Versioned::set_stage(Versioned::LIVE);
148
            $authenticator = new MemberAuthenticator();
149
150
            // Test correct login
151
            /** @var ValidationResult $message */
152
            $authenticator->authenticate(
153
                [
154
                    'Email' => '[email protected]',
155
                    'Password' => 'password'
156
                ],
157
                Controller::curr()->getRequest(),
158
                $result
159
            );
160
161
            return $result;
162
        });
163
164
        $this->assertFalse(
165
            $results->isValid(),
166
            'Authenticate using "published" credentials fails when draft credentials have changed'
167
        );
168
169
        /** @var ValidationResult $results */
170
        $results = Versioned::withVersionedMode(function () {
171
            Versioned::set_stage(Versioned::LIVE);
172
            $authenticator = new MemberAuthenticator();
173
174
            // Test correct login
175
            /** @var ValidationResult $message */
176
            $authenticator->authenticate(
177
                [
178
                    'Email' => '[email protected]',
179
                    'Password' => 'new-password'
180
                ],
181
                Controller::curr()->getRequest(),
182
                $result
183
            );
184
185
            return $result;
186
        });
187
188
        $this->assertTrue(
189
            $results->isValid(),
190
            'Authenticate using current credentials succeeds even when "published" credentials are different'
191
        );
192
    }
193
}
194