Passed
Push — pulls/manymanylist-add-callbac... ( 7e0693...0d7c5a )
by Ingo
09:03
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
49
    protected function tearDown()
50
    {
51
        $this->logOut();
52
        parent::tearDown();
53
    }
54
55
    public function testAuthenticate()
56
    {
57
        $mockDate1 = '2010-01-01 10:00:00';
58
        $readingMode = sprintf('Archive.%s.Stage', $mockDate1);
59
60
        /** @var Member $member */
61
        $member = DBDatetime::withFixedNow($mockDate1, function () {
62
            $member = Member::create();
63
            $member->update([
64
                'FirstName' => 'Jane',
65
                'Surname' => 'Doe',
66
                'Email' => '[email protected]'
67
            ]);
68
            $member->write();
69
            $member->changePassword('password', true);
70
71
            return $member;
72
        });
73
74
        $member->changePassword('new-password', true);
75
76
        /** @var ValidationResult $results */
77
        $results = Versioned::withVersionedMode(function () use ($readingMode) {
78
            Versioned::set_reading_mode($readingMode);
79
            $authenticator = new MemberAuthenticator();
80
81
            // Test correct login
82
            /** @var ValidationResult $message */
83
            $authenticator->authenticate(
84
                [
85
                    'Email' => '[email protected]',
86
                    'Password' => 'password'
87
                ],
88
                Controller::curr()->getRequest(),
89
                $result
90
            );
91
92
            return $result;
93
        });
94
95
        $this->assertFalse(
96
            $results->isValid(),
97
            'Authenticate using old credentials fails even when using an old reading mode'
98
        );
99
100
        /** @var ValidationResult $results */
101
        $results = Versioned::withVersionedMode(function () use ($readingMode) {
102
            Versioned::set_reading_mode($readingMode);
103
            $authenticator = new MemberAuthenticator();
104
105
            // Test correct login
106
            /** @var ValidationResult $message */
107
            $authenticator->authenticate(
108
                [
109
                    'Email' => '[email protected]',
110
                    'Password' => 'new-password'
111
                ],
112
                Controller::curr()->getRequest(),
113
                $result
114
            );
115
116
            return $result;
117
        });
118
119
        $this->assertTrue(
120
            $results->isValid(),
121
            'Authenticate using current credentials succeeds even when using an old reading mode'
122
        );
123
    }
124
125
    public function testAuthenticateAgainstLiveStage()
126
    {
127
        /** @var Member $member */
128
        $member = Member::create();
129
        $member->update([
130
            'FirstName' => 'Jane',
131
            'Surname' => 'Doe',
132
            'Email' => '[email protected]'
133
        ]);
134
        $member->write();
135
        $member->changePassword('password', true);
136
        $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

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