JWSEncoderTest::getJWSProviderMock()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
1
<?php
2
/*
3
 * This file is part of the Guarded Authentication package.
4
 *
5
 * (c) Jafar Jabr <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Jafar\Bundle\GuardedAuthenticationBundle\Tests\Api\JWSEncoder;
12
13
use Jafar\Bundle\GuardedAuthenticationBundle\Api\JWSCreator\JWSCreator;
14
use Jafar\Bundle\GuardedAuthenticationBundle\Api\JWSEncoder\JWSEncoder;
15
use Jafar\Bundle\GuardedAuthenticationBundle\Api\JWSProvider\JWSProviderInterface;
16
use Jafar\Bundle\GuardedAuthenticationBundle\Api\KeyLoader\LoadedJWS;
17
use PHPUnit\Framework\TestCase;
18
19
/**
20
 * Class JWSEncoderTest.
21
 *
22
 * @author Jafar Jabr <[email protected]>
23
 */
24
class JWSEncoderTest extends TestCase
25
{
26
    /**
27
     * Tests calling JWSEncoder::decode() with a valid signature and payload.
28
     */
29
    public function testDecodeFromValidJWS()
30
    {
31
        $payload = [
32
            'username' => 'jafaronly',
33
            'exp'      => time() + 3600,
34
        ];
35
36
        $loadedJWS   = new LoadedJWS($payload, true);
37
        $jwsProvider = $this->getJWSProviderMock();
38
        $jwsProvider
39
            ->expects($this->once())
40
            ->method('load')
41
            ->willReturn($loadedJWS);
42
43
        $encoder = new JWSEncoder($jwsProvider);
44
45
        $this->assertSame($payload, $encoder->decode('jwt'));
46
    }
47
48
    /**
49
     * Tests calling JWSEncoder::encode() with a signed token.
50
     */
51
    public function testEncodeFromValidJWS()
52
    {
53
        $createdJWS  = new JWSCreator('jwt', true);
54
        $jwsProvider = $this->getJWSProviderMock();
55
        $jwsProvider
56
            ->expects($this->once())
57
            ->method('create')
58
            ->willReturn($createdJWS);
59
60
        $encoder = new JWSEncoder($jwsProvider);
61
62
        $this->assertSame('jwt', $encoder->encode([]));
63
    }
64
65
    /**
66
     * Tests that calling JWSEncoder::encode() with an unsigned JWS correctly fails.
67
     *
68
     * @expectedException \Jafar\Bundle\GuardedAuthenticationBundle\Exception\ApiException
69
     */
70
    public function testEncodeFromUnsignedJWS()
71
    {
72
        $jwsProvider = $this->getJWSProviderMock();
73
        $jwsProvider
74
            ->expects($this->once())
75
            ->method('create')
76
            ->willReturn(new JWSCreator('jwt', false));
77
78
        $encoder = new JWSEncoder($jwsProvider);
79
        $encoder->encode([]);
80
    }
81
82
    /**
83
     * Tests that calling JWSEncoder::decode() with an unverified signature correctly fails.
84
     *
85
     * @expectedException \Jafar\Bundle\GuardedAuthenticationBundle\Exception\ApiException
86
     */
87
    public function testDecodeFromUnverifiedJWS()
88
    {
89
        $jwsProvider = $this->getJWSProviderMock();
90
        $jwsProvider
91
            ->expects($this->once())
92
            ->method('load')
93
            ->willReturn(new LoadedJWS([], false));
94
95
        $encoder = new JWSEncoder($jwsProvider);
96
        $encoder->decode('something');
97
    }
98
99
    /**
100
     * Tests that calling JWSEncoder::decode() with an expired payload correctly fails.
101
     *
102
     * @expectedException        \Jafar\Bundle\GuardedAuthenticationBundle\Exception\ApiException
103
     * @expectedExceptionMessage Expired JWT Token
104
     */
105
    public function testDecodeFromExpiredPayload()
106
    {
107
        $loadedJWS   = new LoadedJWS(['exp' => time() - 3600], true);
108
        $jwsProvider = $this->getJWSProviderMock();
109
        $jwsProvider
110
            ->expects($this->once())
111
            ->method('load')
112
            ->willReturn($loadedJWS);
113
114
        $encoder = new JWSEncoder($jwsProvider);
115
        $encoder->decode('jwt');
116
    }
117
118
    /**
119
     * Tests that calling JWSEncoder::decode() with an iat set in the future correctly fails.
120
     *
121
     * @expectedException        \Jafar\Bundle\GuardedAuthenticationBundle\Exception\ApiException
122
     * @expectedExceptionMessage Invalid JWT Token
123
     */
124
    public function testDecodeWithInvalidIssudAtClaimInPayload()
125
    {
126
        $loadedJWS   = new LoadedJWS(['exp' => time() + 3600, 'iat' => time() + 3600], true);
127
        $jwsProvider = $this->getJWSProviderMock();
128
        $jwsProvider
129
            ->expects($this->once())
130
            ->method('load')
131
            ->willReturn($loadedJWS);
132
        $encoder = new JWSEncoder($jwsProvider);
133
        $encoder->decode('jwt');
134
    }
135
136
    /**
137
     * @return \PHPUnit\Framework\MockObject\MockObject
138
     */
139
    private function getJWSProviderMock()
140
    {
141
        return $this->getMockBuilder(JWSProviderInterface::class)
142
            ->disableOriginalConstructor()
143
            ->getMock();
144
    }
145
}
146