TokenValidatorTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 5
dl 0
loc 67
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 22 1
B validateFails() 0 29 2
1
<?php
2
3
namespace ChadicusTest\Psr\Http\ServerMiddleware\Hmac;
4
5
use Chadicus\Psr\Http\ServerMiddleware\AuthenticationException;
6
use Chadicus\Psr\Http\ServerMiddleware\Token;
7
use Chadicus\Psr\Http\ServerMiddleware\Hmac\TokenValidator;
8
use PHPUnit\Framework\TestCase;
9
use Zend\Diactoros\ServerRequest;
10
use Zend\Diactoros\Stream;
11
12
/**
13
 * @coversDefaultClass \Chadicus\Psr\Http\ServerMiddleware\Hmac\TokenValidator
14
 */
15
final class TokenValidatorTest extends TestCase
16
{
17
    /**
18
     * @test
19
     * @covers ::validate
20
     *
21
     * @return void
22
     */
23
    public function validate()
24
    {
25
        $json = json_encode(['foo' => 'bar', 'abc' => '123']);
26
        $stream = fopen('php://memory', 'r+');
27
        fwrite($stream, $json);
28
        rewind($stream);
29
30
        $privateKey = md5(microtime(true));
31
        $uri = 'https://example.com/foos';
32
        $now = time();
33
        $nonce = rand();
34
        $base64 = base64_encode($json);
35
        $data = "{$privateKey}POST{$uri}{$now}{$nonce}{$base64}";
36
        $signature = hash('sha256', $data);
37
        $publicKey = md5(microtime());
38
        $token = new Token($publicKey, $signature, $nonce, $now);
39
40
        $request = new ServerRequest([], [], $uri, 'POST', $stream);
41
42
        $validator = new TokenValidator();
43
        $this->assertTrue($validator->validate($privateKey, $token, $request));
44
    }
45
46
    /**
47
     * @test
48
     * @covers ::validate
49
     *
50
     * @return void
51
     */
52
    public function validateFails()
53
    {
54
        $json = json_encode(['foo' => 'bar', 'abc' => '123']);
55
        $stream = fopen('php://memory', 'r+');
56
        fwrite($stream, $json);
57
        rewind($stream);
58
59
        $privateKey = md5(microtime(true));
60
        $uri = 'https://example.com/foos';
61
        $now = time();
62
        $nonce = rand();
63
        $base64 = base64_encode($json);
64
        //encode data with GET method not expected POST
65
        $data = "{$privateKey}GET{$uri}{$now}{$nonce}{$base64}";
66
        $signature = hash('sha256', $data);
67
        $publicKey = md5(microtime());
68
        $token = new Token($publicKey, $signature, $nonce, $now);
69
70
        $request = new ServerRequest([], [], $uri, 'POST', $stream);
71
72
        $validator = new TokenValidator();
73
        try {
74
            $validator->validate($privateKey, $token, $request);
75
            $this->fail('No exception thrown');
76
        } catch (AuthenticationException $e) {
77
            $this->assertSame(401, $e->getStatusCode());
78
            $this->assertSame('Invalid Hash', $e->getReasonPhrase());
79
        }
80
    }
81
}
82