Failed Conditions
Push — master ( 008a9e...af25b2 )
by Luís
11s
created

constructShouldRaiseExceptionOnNegativeLeeway()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Lcobucci\JWT\Validation\Constraint;
5
6
use DateInterval;
7
use DateTimeImmutable;
8
use Lcobucci\Clock\Clock;
9
use Lcobucci\Clock\FrozenClock;
10
use Lcobucci\JWT\Token\RegisteredClaims;
11
12
final class ValidAtTest extends ConstraintTestCase
13
{
14
    /**
15
     * @var Clock
16
     */
17
    private $clock;
18
19
    /**
20
     * @before
21
     */
22
    public function createDependencies(): void
23
    {
24
        $this->clock = new FrozenClock(new DateTimeImmutable());
25
    }
26
27
    /**
28
     * @test
29
     *
30
     * @expectedException \InvalidArgumentException
31
     *
32
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::__construct
33
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::guardLeeway
34
     */
35
    public function constructShouldRaiseExceptionOnNegativeLeeway(): void
36
    {
37
        $leeway         = new DateInterval('PT30S');
38
        $leeway->invert = 1;
39
40
        new ValidAt($this->clock, $leeway);
41
    }
42
43
    /**
44
     * @test
45
     *
46
     * @expectedException \Lcobucci\JWT\Validation\ConstraintViolation
47
     *
48
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::__construct
49
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::guardLeeway
50
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assert
51
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assertExpiration
52
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assertIssueTime
53
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assertMinimumTime
54
     *
55
     * @uses \Lcobucci\JWT\Token\DataSet
56
     * @uses \Lcobucci\JWT\Token\Plain
57
     * @uses \Lcobucci\JWT\Token\Signature
58
     */
59
    public function assertShouldRaiseExceptionWhenTokenIsExpired(): void
60
    {
61
        $now = $this->clock->now();
62
63
        $claims = [
64
            RegisteredClaims::ISSUED_AT => $now->modify('-20 seconds'),
65
            RegisteredClaims::NOT_BEFORE => $now->modify('-10 seconds'),
66
            RegisteredClaims::EXPIRATION_TIME => $now->modify('-10 seconds'),
67
        ];
68
69
        $constraint = new ValidAt($this->clock);
70
        $constraint->assert($this->buildToken($claims));
71
    }
72
73
    /**
74
     * @test
75
     *
76
     * @expectedException \Lcobucci\JWT\Validation\ConstraintViolation
77
     *
78
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::__construct
79
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::guardLeeway
80
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assert
81
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assertExpiration
82
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assertIssueTime
83
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assertMinimumTime
84
     *
85
     * @uses \Lcobucci\JWT\Token\DataSet
86
     * @uses \Lcobucci\JWT\Token\Plain
87
     * @uses \Lcobucci\JWT\Token\Signature
88
     */
89
    public function assertShouldRaiseExceptionWhenMinimumTimeIsNotMet(): void
90
    {
91
        $now = $this->clock->now();
92
93
        $claims = [
94
            RegisteredClaims::ISSUED_AT => $now->modify('-20 seconds'),
95
            RegisteredClaims::NOT_BEFORE => $now->modify('+40 seconds'),
96
            RegisteredClaims::EXPIRATION_TIME => $now->modify('+60 seconds'),
97
        ];
98
99
        $constraint = new ValidAt($this->clock);
100
        $constraint->assert($this->buildToken($claims));
101
    }
102
103
    /**
104
     * @test
105
     *
106
     * @expectedException \Lcobucci\JWT\Validation\ConstraintViolation
107
     *
108
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::__construct
109
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::guardLeeway
110
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assert
111
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assertExpiration
112
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assertIssueTime
113
     *
114
     * @uses \Lcobucci\JWT\Token\DataSet
115
     * @uses \Lcobucci\JWT\Token\Plain
116
     * @uses \Lcobucci\JWT\Token\Signature
117
     */
118
    public function assertShouldRaiseExceptionWhenTokenWasIssuedInTheFuture(): void
119
    {
120
        $now = $this->clock->now();
121
122
        $claims = [
123
            RegisteredClaims::ISSUED_AT => $now->modify('+20 seconds'),
124
            RegisteredClaims::NOT_BEFORE => $now->modify('+40 seconds'),
125
            RegisteredClaims::EXPIRATION_TIME => $now->modify('+60 seconds'),
126
        ];
127
128
        $constraint = new ValidAt($this->clock);
129
        $constraint->assert($this->buildToken($claims));
130
    }
131
132
    /**
133
     * @test
134
     * @doesNotPerformAssertions
135
     *
136
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::__construct
137
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::guardLeeway
138
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assert
139
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assertExpiration
140
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assertIssueTime
141
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assertMinimumTime
142
     *
143
     * @uses \Lcobucci\JWT\Token\DataSet
144
     * @uses \Lcobucci\JWT\Token\Plain
145
     * @uses \Lcobucci\JWT\Token\Signature
146
     */
147
    public function assertShouldNotRaiseExceptionWhenLeewayIsUsed(): void
148
    {
149
        $now = $this->clock->now();
150
151
        $claims = [
152
            RegisteredClaims::ISSUED_AT => $now->modify('+5 seconds'),
153
            RegisteredClaims::NOT_BEFORE => $now->modify('+5 seconds'),
154
            RegisteredClaims::EXPIRATION_TIME => $now->modify('-5 seconds'),
155
        ];
156
157
        $constraint = new ValidAt($this->clock, new DateInterval('PT5S'));
158
        $constraint->assert($this->buildToken($claims));
159
    }
160
161
    /**
162
     * @test
163
     * @doesNotPerformAssertions
164
     *
165
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::__construct
166
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::guardLeeway
167
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assert
168
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assertExpiration
169
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assertIssueTime
170
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assertMinimumTime
171
     *
172
     * @uses \Lcobucci\JWT\Token\DataSet
173
     * @uses \Lcobucci\JWT\Token\Plain
174
     * @uses \Lcobucci\JWT\Token\Signature
175
     */
176
    public function assertShouldNotRaiseExceptionWhenTokenIsUsedInTheRightMoment(): void
177
    {
178
        $constraint = new ValidAt($this->clock);
179
        $now        = $this->clock->now();
180
181
        $token = $this->buildToken(
182
            [
183
                RegisteredClaims::ISSUED_AT => $now->modify('-40 seconds'),
184
                RegisteredClaims::NOT_BEFORE => $now->modify('-20 seconds'),
185
                RegisteredClaims::EXPIRATION_TIME => $now->modify('+60 seconds'),
186
            ]
187
        );
188
189
        $constraint->assert($token);
190
191
        $token = $this->buildToken(
192
            [
193
                RegisteredClaims::ISSUED_AT => $now,
194
                RegisteredClaims::NOT_BEFORE => $now,
195
                RegisteredClaims::EXPIRATION_TIME => $now->modify('+60 seconds'),
196
            ]
197
        );
198
199
        $constraint->assert($token);
200
    }
201
202
    /**
203
     * @test
204
     * @doesNotPerformAssertions
205
     *
206
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::__construct
207
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::guardLeeway
208
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assert
209
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assertExpiration
210
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assertIssueTime
211
     * @covers \Lcobucci\JWT\Validation\Constraint\ValidAt::assertMinimumTime
212
     *
213
     * @uses \Lcobucci\JWT\Token\DataSet
214
     * @uses \Lcobucci\JWT\Token\Plain
215
     * @uses \Lcobucci\JWT\Token\Signature
216
     */
217
    public function assertShouldNotRaiseExceptionWhenTokenDoesNotHaveTimeClaims(): void
218
    {
219
        $token      = $this->buildToken();
220
        $constraint = new ValidAt($this->clock);
221
222
        $constraint->assert($token);
223
    }
224
}
225