File: ValidationNotPwnedVerifierTest.php

package info (click to toggle)
php-laravel-framework 10.48.29%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,188 kB
  • sloc: php: 232,347; sh: 167; makefile: 46
file content (146 lines) | stat: -rw-r--r-- 3,867 bytes parent folder | download | duplicates (3)
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
<?php

namespace Illuminate\Tests\Validation;

use Illuminate\Container\Container;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\Factory as HttpFactory;
use Illuminate\Http\Client\Response;
use Illuminate\Validation\NotPwnedVerifier;
use Mockery as m;
use PHPUnit\Framework\TestCase;

class ValidationNotPwnedVerifierTest extends TestCase
{
    protected function tearDown(): void
    {
        m::close();

        Container::setInstance(null);
    }

    public function testEmptyValues()
    {
        $httpFactory = m::mock(HttpFactory::class);
        $verifier = new NotPwnedVerifier($httpFactory);

        foreach (['', false, 0] as $password) {
            $this->assertFalse($verifier->verify([
                'value' => $password,
                'threshold' => 0,
            ]));
        }
    }

    public function testApiResponseGoesWrong()
    {
        $httpFactory = m::mock(HttpFactory::class);
        $response = m::mock(Response::class);

        $httpFactory = m::mock(HttpFactory::class);

        $httpFactory
            ->shouldReceive('withHeaders')
            ->once()
            ->with(['Add-Padding' => true])
            ->andReturn($httpFactory);

        $httpFactory
            ->shouldReceive('timeout')
            ->once()
            ->with(30)
            ->andReturn($httpFactory);

        $httpFactory->shouldReceive('get')
            ->once()
            ->andReturn($response);

        $response->shouldReceive('successful')
            ->once()
            ->andReturn(true);

        $response->shouldReceive('body')
            ->once()
            ->andReturn('');

        $verifier = new NotPwnedVerifier($httpFactory);

        $this->assertTrue($verifier->verify([
            'value' => 123123123,
            'threshold' => 0,
        ]));
    }

    public function testApiGoesDown()
    {
        $httpFactory = m::mock(HttpFactory::class);
        $response = m::mock(Response::class);

        $httpFactory
            ->shouldReceive('withHeaders')
            ->once()
            ->with(['Add-Padding' => true])
            ->andReturn($httpFactory);

        $httpFactory
            ->shouldReceive('timeout')
            ->once()
            ->with(30)
            ->andReturn($httpFactory);

        $httpFactory->shouldReceive('get')
            ->once()
            ->andReturn($response);

        $response->shouldReceive('successful')
            ->once()
            ->andReturn(false);

        $verifier = new NotPwnedVerifier($httpFactory);

        $this->assertTrue($verifier->verify([
            'value' => 123123123,
            'threshold' => 0,
        ]));
    }

    public function testDnsDown()
    {
        $container = Container::getInstance();
        $exception = new ConnectionException();

        $exceptionHandler = m::mock(ExceptionHandler::class);
        $exceptionHandler->shouldReceive('report')->once()->with($exception);
        $container->bind(ExceptionHandler::class, function () use ($exceptionHandler) {
            return $exceptionHandler;
        });

        $httpFactory = m::mock(HttpFactory::class);

        $httpFactory
            ->shouldReceive('withHeaders')
            ->once()
            ->with(['Add-Padding' => true])
            ->andReturn($httpFactory);

        $httpFactory
            ->shouldReceive('timeout')
            ->once()
            ->with(30)
            ->andReturn($httpFactory);

        $httpFactory
            ->shouldReceive('get')
            ->once()
            ->andThrow($exception);

        $verifier = new NotPwnedVerifier($httpFactory);
        $this->assertTrue($verifier->verify([
            'value' => 123123123,
            'threshold' => 0,
        ]));

        unset($container[ExceptionHandler::class]);
    }
}