1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace ArcherZdip\LaravelApiAuth\Test; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Faker\Factory; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use ArcherZdip\LaravelApiAuth\ApiAuth; |
10
|
|
|
use Illuminate\Support\Facades\Artisan; |
11
|
|
|
use ArcherZdip\LaravelApiAuth\Models\AppClient; |
12
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions; |
13
|
|
|
|
14
|
|
|
class ApiAuthTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
use DatabaseTransactions; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string $name |
20
|
|
|
*/ |
21
|
|
|
protected $name; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var AppClient $appClient |
25
|
|
|
*/ |
26
|
|
|
protected $appClient; |
27
|
|
|
|
28
|
|
|
protected function setUp() |
29
|
|
|
{ |
30
|
|
|
parent::setUp(); |
31
|
|
|
|
32
|
|
|
$this->name = Factory::create()->md5; |
33
|
|
|
|
34
|
|
|
Artisan::call('apikey:generate', [ |
35
|
|
|
'name' => $this->name |
36
|
|
|
]); |
37
|
|
|
|
38
|
|
|
$this->appClient = AppClient::where('name', '=', $this->name)->first(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* test genereate token |
43
|
|
|
* |
44
|
|
|
* @throws \Exception |
45
|
|
|
*/ |
46
|
|
|
public function test_generate_token() |
47
|
|
|
{ |
48
|
|
|
$token = ApiAuth::generateToken($this->appClient->appid, time()); |
49
|
|
|
|
50
|
|
|
echo $token; |
51
|
|
|
$this->assertInternalType('string', $token); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* test get appid |
56
|
|
|
* |
57
|
|
|
* @throws \Exception |
58
|
|
|
*/ |
59
|
|
|
public function test_get_appid() |
60
|
|
|
{ |
61
|
|
|
$appid = ApiAuth::getAppId($this->getGenerateToken()); |
62
|
|
|
|
63
|
|
|
echo $appid; |
64
|
|
|
|
65
|
|
|
$this->assertInternalType('string', $appid); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* test token is valid |
70
|
|
|
* |
71
|
|
|
* @throws \Exception |
72
|
|
|
*/ |
73
|
|
|
public function test_token_is_valid() |
74
|
|
|
{ |
75
|
|
|
$succToken = $this->getGenerateToken(); |
76
|
|
|
$failToken = '123456'; |
77
|
|
|
|
78
|
|
|
echo $succToken; |
79
|
|
|
|
80
|
|
|
$this->assertTrue(ApiAuth::isValid($succToken)); |
81
|
|
|
|
82
|
|
|
$this->assertFalse(ApiAuth::isValid($failToken)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get token for test |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
* @throws \Exception |
90
|
|
|
*/ |
91
|
|
|
private function getGenerateToken() |
92
|
|
|
{ |
93
|
|
|
return ApiAuth::generateToken($this->appClient->appid, time()); |
94
|
|
|
} |
95
|
|
|
} |