Passed
Push — nondestructive-enum ( eef0c9...88175c )
by Sam
07:14
created

testLiveBuildUnauthenticated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 0
dl 0
loc 21
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Core\Tests\Startup;
4
5
use SilverStripe\Control\HTTPApplication;
6
use SilverStripe\Control\HTTPRequest;
7
use SilverStripe\Control\HTTPResponse;
8
use SilverStripe\Control\Session;
9
use SilverStripe\Core\Kernel;
10
use SilverStripe\Core\Startup\ErrorControlChainMiddleware;
11
use SilverStripe\Core\Tests\Startup\ErrorControlChainMiddlewareTest\BlankKernel;
12
use SilverStripe\Dev\SapphireTest;
13
use SilverStripe\Security\Security;
14
15
class ErrorControlChainMiddlewareTest extends SapphireTest
16
{
17
    protected $usesDatabase = true;
18
19
    protected function setUp()
20
    {
21
        parent::setUp();
22
        Security::force_database_is_ready(true);
23
    }
24
25
    protected function tearDown()
26
    {
27
        Security::clear_database_is_ready();
28
        parent::tearDown();
29
    }
30
31
    public function testLiveFlushAdmin()
32
    {
33
        // Mock admin
34
        $adminID = $this->logInWithPermission('ADMIN');
35
        $this->logOut();
36
37
        // Mock app
38
        $app = new HTTPApplication(new BlankKernel(BASE_PATH));
39
        $app->getKernel()->setEnvironment(Kernel::LIVE);
40
41
        // Test being logged in as admin
42
        $chain = new ErrorControlChainMiddleware($app);
43
        $request = new HTTPRequest('GET', '/', ['flush' => 1]);
44
        $request->setSession(new Session(['loggedInAs' => $adminID]));
45
        $result = $chain->process($request, function () {
46
            return null;
47
        });
48
49
        $this->assertInstanceOf(HTTPResponse::class, $result);
50
        $location = $result->getHeader('Location');
51
        $this->assertContains('?flush=1&flushtoken=', $location);
52
        $this->assertNotContains('Security/login', $location);
53
    }
54
55
    public function testLiveFlushUnauthenticated()
56
    {
57
        // Mock app
58
        $app = new HTTPApplication(new BlankKernel(BASE_PATH));
59
        $app->getKernel()->setEnvironment(Kernel::LIVE);
60
61
        // Test being logged in as no one
62
        Security::setCurrentUser(null);
63
        $chain = new ErrorControlChainMiddleware($app);
64
        $request = new HTTPRequest('GET', '/', ['flush' => 1]);
65
        $request->setSession(new Session(['loggedInAs' => 0]));
66
        $result = $chain->process($request, function () {
67
            return null;
68
        });
69
70
        // Should be directed to login, not to flush
71
        $this->assertInstanceOf(HTTPResponse::class, $result);
72
        $location = $result->getHeader('Location');
73
        $this->assertNotContains('?flush=1&flushtoken=', $location);
74
        $this->assertContains('Security/login', $location);
75
    }
76
77
    public function testLiveBuildAdmin()
78
    {
79
        // Mock admin
80
        $adminID = $this->logInWithPermission('ADMIN');
81
        $this->logOut();
82
83
        // Mock app
84
        $app = new HTTPApplication(new BlankKernel(BASE_PATH));
85
        $app->getKernel()->setEnvironment(Kernel::LIVE);
86
87
        // Test being logged in as admin
88
        $chain = new ErrorControlChainMiddleware($app);
89
        $request = new HTTPRequest('GET', '/dev/build/');
90
        $request->setSession(new Session(['loggedInAs' => $adminID]));
91
        $result = $chain->process($request, function () {
92
            return null;
93
        });
94
95
        $this->assertInstanceOf(HTTPResponse::class, $result);
96
        $location = $result->getHeader('Location');
97
        $this->assertContains('/dev/build', $location);
98
        $this->assertContains('?devbuildtoken=', $location);
99
        $this->assertNotContains('Security/login', $location);
100
    }
101
102
    public function testLiveBuildUnauthenticated()
103
    {
104
        // Mock app
105
        $app = new HTTPApplication(new BlankKernel(BASE_PATH));
106
        $app->getKernel()->setEnvironment(Kernel::LIVE);
107
108
        // Test being logged in as no one
109
        Security::setCurrentUser(null);
110
        $chain = new ErrorControlChainMiddleware($app);
111
        $request = new HTTPRequest('GET', '/dev/build');
112
        $request->setSession(new Session(['loggedInAs' => 0]));
113
        $result = $chain->process($request, function () {
114
            return null;
115
        });
116
117
        // Should be directed to login, not to flush
118
        $this->assertInstanceOf(HTTPResponse::class, $result);
119
        $location = $result->getHeader('Location');
120
        $this->assertNotContains('/dev/build', $location);
121
        $this->assertNotContains('?devbuildtoken=', $location);
122
        $this->assertContains('Security/login', $location);
123
    }
124
125
    public function testLiveBuildAndFlushAdmin()
126
    {
127
        // Mock admin
128
        $adminID = $this->logInWithPermission('ADMIN');
129
        $this->logOut();
130
131
        // Mock app
132
        $app = new HTTPApplication(new BlankKernel(BASE_PATH));
133
        $app->getKernel()->setEnvironment(Kernel::LIVE);
134
135
        // Test being logged in as admin
136
        $chain = new ErrorControlChainMiddleware($app);
137
        $request = new HTTPRequest('GET', '/dev/build/', ['flush' => '1']);
138
        $request->setSession(new Session(['loggedInAs' => $adminID]));
139
        $result = $chain->process($request, function () {
140
            return null;
141
        });
142
143
        $this->assertInstanceOf(HTTPResponse::class, $result);
144
        $location = $result->getHeader('Location');
145
        $this->assertContains('/dev/build', $location);
146
        $this->assertContains('flush=1', $location);
147
        $this->assertContains('devbuildtoken=', $location);
148
        $this->assertContains('flushtoken=', $location);
149
        $this->assertNotContains('Security/login', $location);
150
    }
151
152
    public function testLiveBuildAndFlushUnauthenticated()
153
    {
154
        // Mock app
155
        $app = new HTTPApplication(new BlankKernel(BASE_PATH));
156
        $app->getKernel()->setEnvironment(Kernel::LIVE);
157
158
        // Test being logged in as no one
159
        Security::setCurrentUser(null);
160
        $chain = new ErrorControlChainMiddleware($app);
161
        $request = new HTTPRequest('GET', '/dev/build', ['flush' => '1']);
162
        $request->setSession(new Session(['loggedInAs' => 0]));
163
        $result = $chain->process($request, function () {
164
            return null;
165
        });
166
167
        // Should be directed to login, not to flush
168
        $this->assertInstanceOf(HTTPResponse::class, $result);
169
        $location = $result->getHeader('Location');
170
        $this->assertNotContains('/dev/build', $location);
171
        $this->assertNotContains('flush=1', $location);
172
        $this->assertNotContains('devbuildtoken=', $location);
173
        $this->assertNotContains('flushtoken=', $location);
174
        $this->assertContains('Security/login', $location);
175
    }
176
}
177