File: CheckForMaintenanceModeTest.php

package info (click to toggle)
php-laravel-framework 6.20.14%2Bdfsg-2%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 10,932 kB
  • sloc: php: 122,752; sh: 136; javascript: 45; makefile: 44
file content (175 lines) | stat: -rw-r--r-- 5,022 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php

namespace Illuminate\Tests\Foundation\Http\Middleware;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Foundation\Http\Exceptions\MaintenanceModeException;
use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode;
use Illuminate\Http\Request;
use Mockery as m;
use PHPUnit\Framework\TestCase;

class CheckForMaintenanceModeTest extends TestCase
{
    /**
     * @var string
     */
    protected $storagePath;

    /**
     * @var string
     */
    protected $downFilePath;

    /**
     * @var \Illuminate\Filesystem\Filesystem
     */
    protected $files;

    protected function setUp(): void
    {
        if (is_null($this->files)) {
            $this->files = new Filesystem;
        }

        $this->storagePath = __DIR__.'/tmp';
        $this->downFilePath = $this->storagePath.'/framework/down';

        $this->files->makeDirectory($this->storagePath.'/framework', 0755, true);
    }

    protected function tearDown(): void
    {
        $this->files->deleteDirectory($this->storagePath);

        m::close();
    }

    public function testApplicationIsRunningNormally()
    {
        $app = m::mock(Application::class);
        $app->shouldReceive('isDownForMaintenance')->once()->andReturn(false);

        $middleware = new CheckForMaintenanceMode($app);

        $result = $middleware->handle(Request::create('/'), function ($request) {
            return 'Running normally.';
        });

        $this->assertSame('Running normally.', $result);
    }

    public function testApplicationAllowsSomeIPs()
    {
        $ips = ['127.0.0.1', '2001:0db8:85a3:0000:0000:8a2e:0370:7334'];

        // Check IPv4.
        $middleware = new CheckForMaintenanceMode($this->createMaintenanceApplication($ips));

        $request = m::mock(Request::class);
        $request->shouldReceive('ip')->once()->andReturn('127.0.0.1');

        $result = $middleware->handle($request, function ($request) {
            return 'Allowing [127.0.0.1]';
        });

        $this->assertSame('Allowing [127.0.0.1]', $result);

        // Check IPv6.
        $middleware = new CheckForMaintenanceMode($this->createMaintenanceApplication($ips));

        $request = m::mock(Request::class);
        $request->shouldReceive('ip')->once()->andReturn('2001:0db8:85a3:0000:0000:8a2e:0370:7334');

        $result = $middleware->handle($request, function ($request) {
            return 'Allowing [2001:0db8:85a3:0000:0000:8a2e:0370:7334]';
        });

        $this->assertSame('Allowing [2001:0db8:85a3:0000:0000:8a2e:0370:7334]', $result);
    }

    public function testApplicationDeniesSomeIPs()
    {
        $this->expectException(MaintenanceModeException::class);
        $this->expectExceptionMessage('This application is down for maintenance.');

        $middleware = new CheckForMaintenanceMode($this->createMaintenanceApplication());

        $middleware->handle(Request::create('/'), function ($request) {
            //
        });
    }

    public function testApplicationAllowsSomeURIs()
    {
        $app = $this->createMaintenanceApplication();

        $middleware = new class($app) extends CheckForMaintenanceMode {
            public function __construct($app)
            {
                parent::__construct($app);

                $this->except = ['foo/bar'];
            }
        };

        $result = $middleware->handle(Request::create('/foo/bar'), function ($request) {
            return 'Excepting /foo/bar';
        });

        $this->assertSame('Excepting /foo/bar', $result);
    }

    public function testApplicationDeniesSomeURIs()
    {
        $this->expectException(MaintenanceModeException::class);
        $this->expectExceptionMessage('This application is down for maintenance.');

        $middleware = new CheckForMaintenanceMode($this->createMaintenanceApplication());

        $middleware->handle(Request::create('/foo/bar'), function ($request) {
            //
        });
    }

    /**
     * Create a mock of maintenance application.
     *
     * @param  string|array  $ips
     * @return \Mockery\MockInterface
     */
    protected function createMaintenanceApplication($ips = null)
    {
        $this->makeDownFile($ips);

        $app = m::mock(Application::class);
        $app->shouldReceive('isDownForMaintenance')->once()->andReturn(true);
        $app->shouldReceive('storagePath')->once()->andReturn($this->storagePath);

        return $app;
    }

    /**
     * Make a down file with the given allowed ips.
     *
     * @param  string|array  $ips
     * @return array
     */
    protected function makeDownFile($ips = null)
    {
        $data = [
            'time' => time(),
            'retry' => 86400,
            'message' => 'This application is down for maintenance.',
        ];

        if ($ips !== null) {
            $data['allowed'] = $ips;
        }

        $this->files->put($this->downFilePath, json_encode($data, JSON_PRETTY_PRINT));

        return $data;
    }
}