Skip to content

Commit 036fe17

Browse files
committed
test:添加测试用例
1 parent 8d6b4a4 commit 036fe17

File tree

6 files changed

+31
-4
lines changed

6 files changed

+31
-4
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ composer.lock
44
*.log
55
/hooks
66
.idea
7-
/runtime
7+
/runtime
8+
config

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"hyperf/command": "^2.0",
4141
"hyperf/config": "^2.0",
4242
"hyperf/database": "^2.0",
43+
"hyperf/event": "^2.2",
4344
"hyperf/framework": "^2.0",
4445
"hyperf/redis": "^2.0",
4546
"hyperf/session": "^2.0",

src/Guard/JwtGuard.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function login(Authenticatable $user)
7575

7676
/**
7777
* 获取用于存到 context 的 key.
78+
*
7879
* @param $token
7980
* @return string
8081
*/
@@ -178,9 +179,10 @@ public function getJwtManager(): JWTManager
178179

179180
/**
180181
* 获取 token 标识.
182+
*
183+
* @throws \Qbhy\SimpleJwt\Exceptions\TokenExpiredException
181184
* @throws \Qbhy\SimpleJwt\Exceptions\InvalidTokenException
182185
* @throws \Qbhy\SimpleJwt\Exceptions\SignatureException
183-
* @throws \Qbhy\SimpleJwt\Exceptions\TokenExpiredException
184186
* @return mixed|string
185187
*/
186188
protected function getJti(string $token): string

src/Guard/SsoGuard.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Hyperf\HttpServer\Contract\RequestInterface;
1515
use Hyperf\Redis\Redis;
16+
use Hyperf\Utils\Context;
1617
use Psr\EventDispatcher\EventDispatcherInterface;
1718
use Qbhy\HyperfAuth\Authenticatable;
1819
use Qbhy\HyperfAuth\Events\ForcedOfflineEvent;
@@ -33,6 +34,7 @@ class SsoGuard extends JwtGuard
3334
public function __construct(array $config, string $name, UserProvider $userProvider, RequestInterface $request)
3435
{
3536
parent::__construct($config, $name, $userProvider, $request);
37+
$this->eventDispatcher = make(EventDispatcherInterface::class);
3638

3739
// 初始化redis实例
3840
$this->redis = is_callable($config['redis']) ? call_user_func_array($config['redis'], []) : make(Redis::class);
@@ -51,7 +53,8 @@ public function login(Authenticatable $user, string $client = null)
5153

5254
if (! empty($previousToken = $this->redis->hGet($redisKey, $client))) {
5355
// 如果存在上一个 token,就给他拉黑,也就是强制下线
54-
$this->getJwtManager()->addBlacklist($token);
56+
Context::set($this->resultKey($previousToken), 0);
57+
$this->getJwtManager()->addBlacklist($previousToken);
5558
$this->redis->hDel($redisKey, $client);
5659
$this->eventDispatcher->dispatch(new ForcedOfflineEvent($user, $client));
5760
}

tests/Cases/ExampleTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,27 @@ public function testSessionGuard()
9292

9393
public function testSsoGuard()
9494
{
95+
/** @var SsoGuard $guard */
9596
$guard = $this->auth()->guard('sso');
9697
$this->assertTrue($guard instanceof SsoGuard);
9798
$this->assertTrue($guard->getProvider() instanceof EloquentProvider);
98-
// todo: sso guard 未测试
99+
$user = $this->user(10);
100+
$token = $guard->login($user, 'pc');
101+
$this->assertTrue(is_string($token));
102+
$this->assertTrue($guard->check($token));
103+
104+
// // 抢线登录
105+
// $newToken = $guard->login($user, 'pc');
106+
// var_dump('抢线的token', $newToken);
107+
// $this->assertTrue($newToken != $token);
108+
// $this->assertTrue($guard->check($newToken));
109+
//
110+
// // 测试旧 token 还能不能用
111+
// $this->assertTrue($guard->guest($token));
112+
//
113+
// // 第二个设备登录
114+
// $weappToken = $guard->login($user, 'weapp');
115+
// $this->assertTrue($guard->check($weappToken));
99116
}
100117

101118
protected function auth()

tests/bootstrap.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@
7272

7373
// 自定义 redis key,必须包含 {uid},{uid} 会被替换成用户ID
7474
'redis_key' => 'u:token:{uid}',
75+
// 'cache' => function () {
76+
// return make(\Qbhy\HyperfAuth\HyperfRedisCache::class);
77+
// },
7578
]),
7679
'jwt' => $jwtConfig,
7780
'session' => [

0 commit comments

Comments
 (0)