Failed Conditions
Pull Request — master (#262)
by Guilherme
10:25 queued 04:26
created

RemoteClaimParserTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 8
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
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 LoginCidadao\RemoteClaimsBundle\Tests\Parser;
12
13
use Emarref\Jwt\Jwt;
14
use Emarref\Jwt\Token;
15
use Emarref\Jwt\Claim;
16
use Emarref\Jwt\Encryption;
17
use Emarref\Jwt\Algorithm;
18
use LoginCidadao\OAuthBundle\Entity\Client;
19
use LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaim;
20
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimInterface;
21
use LoginCidadao\RemoteClaimsBundle\Parser\RemoteClaimParser;
22
use PHPUnit\Framework\TestCase;
23
24
class RemoteClaimParserTest extends TestCase
25
{
26
    public static $claimMetadata = [
27
        'claim_version' => 1,
28
        'claim_name' => 'tag:example.com,2017:my_claim',
29
        'claim_display_name' => 'My Claim',
30
        'claim_description' => 'My very nice claim description.',
31
        'claim_provider_recommended_scope' => ['scope1', 'scope2', 'scope3'],
32
        'claim_provider_essential_scope' => ['scope2'],
33
        'claim_provider' => [
34
            'client_id' => '123_r4nd0mid',
35
            'client_name' => 'The Provider',
36
            'redirect_uris' => ['https://redirect.uri'],
37
            'bar' => 'foo',
38
        ],
39
        'foo' => 'bar',
40
    ];
41
42
    public function testParseClaimString()
43
    {
44
        $claim = RemoteClaimParser::parseClaim(json_encode(self::$claimMetadata), new RemoteClaim(), new Client());
45
46
        $this->assertClaimAndProvider($claim);
47
    }
48
49
    public function testParseClaimArray()
50
    {
51
        $claim = RemoteClaimParser::parseClaim(self::$claimMetadata, new RemoteClaim(), new Client());
52
53
        $this->assertClaimAndProvider($claim);
54
    }
55
56
    public function testParseClaimObject()
57
    {
58
        $claim = RemoteClaimParser::parseClaim((object)self::$claimMetadata, new RemoteClaim(), new Client());
59
60
        $this->assertClaimAndProvider($claim);
61
    }
62
63
    public function testParseJwt()
64
    {
65
        $token = new Token();
66
        $token->addClaim(new Claim\Audience(['lc_aud']));
67
        $token->addClaim(new Claim\Expiration(new \DateTime('30 minutes')));
68
        $token->addClaim(new Claim\IssuedAt(new \DateTime()));
69
        $token->addClaim(new Claim\Issuer(self::$claimMetadata['claim_provider']['redirect_uris'][0]));
70
        foreach (self::$claimMetadata as $name => $value) {
71
            $token->addClaim(new Claim\PublicClaim($name, $value));
72
        }
73
74
        $secret = 'my_jwt_secret';
75
        $encryption = Encryption\Factory::create(new Algorithm\Hs256($secret));
76
        $jwt = (new Jwt())->serialize($token, $encryption);
77
78
        $claim = RemoteClaimParser::parseJwt($jwt, new RemoteClaim(), new Client());
79
80
        $this->assertClaimAndProvider($claim);
81
    }
82
83
    public function testInvalidJwt()
84
    {
85
        $this->expectException('\InvalidArgumentException');
86
87
        RemoteClaimParser::parseJwt('INVALID JWT', new RemoteClaim(), new Client());
88
    }
89
90
    public function testParseInvalidClaim()
91
    {
92
        $this->expectException('\InvalidArgumentException');
93
94
        RemoteClaimParser::parseClaim(null, new RemoteClaim());
95
    }
96
97
    private function assertClaimAndProvider(RemoteClaimInterface $claim)
98
    {
99
        $this->assertEquals(self::$claimMetadata['claim_name'], $claim->getName());
100
        $this->assertEquals(self::$claimMetadata['claim_display_name'], $claim->getDisplayName());
101
        $this->assertEquals(self::$claimMetadata['claim_description'], $claim->getDescription());
102
        $this->assertEquals(self::$claimMetadata['claim_provider_recommended_scope'], $claim->getRecommendedScope());
103
        $this->assertEquals(self::$claimMetadata['claim_provider_essential_scope'], $claim->getEssentialScope());
104
105
        $expectedProvider = self::$claimMetadata['claim_provider'];
106
        $this->assertEquals($expectedProvider['client_id'], $claim->getProvider()->getClientId());
107
        $this->assertEquals($expectedProvider['client_name'], $claim->getProvider()->getName());
108
        $this->assertEquals($expectedProvider['redirect_uris'], $claim->getProvider()->getRedirectUris());
109
    }
110
}
111