1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Library for confirmation tokens. |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/php-confirmator |
6
|
|
|
* @package php-confirmator |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\php\confirmator\tests\unit; |
12
|
|
|
|
13
|
|
|
use hiqdev\php\confirmator\FileStorage; |
14
|
|
|
use hiqdev\php\confirmator\Service; |
15
|
|
|
use hiqdev\php\confirmator\Token; |
16
|
|
|
|
17
|
|
|
class ServiceTest extends \PHPUnit\Framework\TestCase |
18
|
|
|
{ |
19
|
|
|
public $service; |
20
|
|
|
public $storage; |
21
|
|
|
|
22
|
|
|
public $action = 'restore-password'; |
23
|
|
|
public $username = 'somebody'; |
24
|
|
|
public $notAfter; |
25
|
|
|
public $notBefore; |
26
|
|
|
|
27
|
|
|
protected function setUp() |
28
|
|
|
{ |
29
|
|
|
$this->storage = new FileStorage(dirname(__DIR__) . '/tokens'); |
30
|
|
|
$this->service = new Service($this->storage); |
31
|
|
|
$this->notAfter = date('Y-m-d H:i:s', time() + 3600); |
32
|
|
|
$this->notBefore = date('Y-m-d H:i:s', time() - 3600); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function tearDown() |
36
|
|
|
{ |
37
|
|
|
$this->storage = null; |
38
|
|
|
$this->service = null; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function issueToken() |
42
|
|
|
{ |
43
|
|
|
return $this->service->issueToken([ |
44
|
|
|
'action' => $this->action, |
45
|
|
|
'username' => $this->username, |
46
|
|
|
'notAfter' => $this->notAfter, |
47
|
|
|
'notBefore' => $this->notBefore, |
48
|
|
|
]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testCheckToken() |
52
|
|
|
{ |
53
|
|
|
$token = $this->issueToken(); |
54
|
|
|
$this->assertTrue($this->service->checkToken($token, [ |
55
|
|
|
'action' => $this->action, |
56
|
|
|
'username' => $this->username, |
57
|
|
|
])); |
58
|
|
|
$this->assertTrue($this->service->checkToken((string) $token, [ |
59
|
|
|
'action' => $this->action, |
60
|
|
|
'username' => $this->username, |
61
|
|
|
])); |
62
|
|
|
$this->assertFalse($this->service->checkToken((string) $token, [ |
63
|
|
|
'action' => 'other', |
64
|
|
|
])); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testOutdatedToken() |
68
|
|
|
{ |
69
|
|
|
$token = $this->service->issueToken([ |
70
|
|
|
'notAfter' => $this->notBefore, |
71
|
|
|
]); |
72
|
|
|
$this->assertFalse($token->check([])); |
73
|
|
|
$token = $this->service->issueToken([ |
74
|
|
|
'notBefore' => $this->notAfter, |
75
|
|
|
]); |
76
|
|
|
$this->assertFalse($token->check([])); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testFindToken() |
80
|
|
|
{ |
81
|
|
|
$tokenString = (string) $this->issueToken(); |
82
|
|
|
$token = $this->service->findToken($tokenString); |
83
|
|
|
$this->assertInstanceOf(Token::class, $token); |
84
|
|
|
$this->assertSame($this->action, $token->get('action')); |
85
|
|
|
$this->assertSame($this->username, $token->get('username')); |
86
|
|
|
$this->assertSame(null, $token->get('nonExistentField')); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|