Skip to content

Instantly share code, notes, and snippets.

@imdong
Created November 25, 2020 06:16
Show Gist options
  • Save imdong/b59dd8bed35969aea1c9dd22d006f620 to your computer and use it in GitHub Desktop.
Save imdong/b59dd8bed35969aea1c9dd22d006f620 to your computer and use it in GitHub Desktop.
登录功能测试
<?php
/**
* 登录功能测试
*
* @author ImDong ([email protected])
* @created 2020-11-20 16:06:16
* @updated 2020-11-24 17:30:14
*/
namespace Tests\Feature;
use App\Exceptions\ApiException;
use App\Exceptions\AuthException;
use App\Exceptions\CommonException;
use App\Helpers\RedisHelper;
use App\Http\Services\VerificationCode;
use App\Models\User;
use Tests\ApiFeatureBaseTest;
/**
* Class UserLoginTest
*
* @package Tests\Feature
*/
class UserLoginTest extends ApiFeatureBaseTest
{
/**
* @var string 获取验证码的接口地址
*/
private $api_code = '/v1/sms/code';
/**
* @var string 登录用的接口地址
*/
private $api_login = '/v1/user/login';
/**
* 删除用户
*
* @throws \Exception
*/
public function delUser()
{
// 删除用户
$user = User::query()->where(['phone' => $this->phone])->first();
if ($user) {
$user->delete();
}
}
/**
* @throws \Exception
*/
public function setUp(): void
{
parent::setUp();
// 删除用户
$this->delUser();
// 清空 Redis 库
RedisHelper::get()->flushdb();
// 修改接口地址
$this->api_code = $this->baseApiUrl . $this->api_code;
$this->api_login = $this->baseApiUrl . $this->api_login;
}
/**
* 无验证码登录
*/
public function testNoneCode()
{
// 登录用数据
$login_data = [
'type' => 'code', // 使用验证码登录
'phone' => $this->phone,
'code' => '0000'
];
// 先使用错误验证码登录
$this->postJson($this->api_login, $login_data)->assertJson([
'code' => CommonException::CODE_INVALID
]);
}
/**
* 获取 并 使用错误验证码登录(获取验证码频繁)
*/
public function testCodeError()
{
// 吊销验证码
VerificationCode::revokeCodeByPhone($this->phone, 'login');
// 获取短信验证码
$code_data = [
'type' => 'login',
'phone' => $this->phone
];
$this->postJson($this->api_code, $code_data)->assertJson(['code' => ApiException::SUCCESS]);
// 获取验证码过于频繁
$this->postJson($this->api_code, $code_data)->assertJson([
'code' => CommonException::OPERATION_FREQUENT
]);
// 登录失败 验证码错误
$this->postJson($this->api_login, [
'type' => 'code',
'phone' => $this->phone,
'code' => '0000'
])->assertJson([
'code' => CommonException::CODE_ERROR
]);
}
/**
* 登录成功 (首次登录)
*
* @throws \Exception
*/
public function testLoginSuccessFirst()
{
// 删除用户
$this->delUser();
// 吊销验证码
VerificationCode::revokeCodeByPhone($this->phone, 'login');
// 获取短信验证码
$code_data = [
'type' => 'login',
'phone' => $this->phone
];
$this->postJson($this->api_code, $code_data)->assertJson(['code' => ApiException::SUCCESS]);
// 这次应该能登录成功 (新用户)
$this->postJson($this->api_login, [
'type' => 'code',
'phone' => $this->phone,
'code' => RedisHelper::get()->get(RedisHelper::getSmsCodeKey($this->phone, 'login'))
])->assertJson([
'code' => ApiException::SUCCESS,
'data' => [
'new_user' => true
]
]);
}
/**
* 登录成功 非首次
*
* @group Dev
*/
public function testLoginSuccessNoFirst()
{
// 保证一定有用户
User::findOrNew($this->phone);
// 吊销验证码
VerificationCode::revokeCodeByPhone($this->phone, 'login');
// 获取短信验证码
$code_data = [
'type' => 'login',
'phone' => $this->phone
];
$this->postJson($this->api_code, $code_data)->assertJson(['code' => ApiException::SUCCESS]);
// 这次应该能登录成功 (非新用户)
$this->postJson($this->api_login, [
'type' => 'code',
'phone' => $this->phone,
'code' => RedisHelper::get()->get(RedisHelper::getSmsCodeKey($this->phone, 'login'))
])->assertJson([
'code' => ApiException::SUCCESS,
'data' => [
'new_user' => false
]
]);
}
/**
* 未设置密码的登录
*
* @throws \App\Exceptions\ApiException
*/
public function testLoginByPasswordNoSet()
{
// 创建用户
User::findOrNew($this->phone);
$post = [
'type' => 'password',
'phone' => $this->phone,
'password' => $this->password
];
$this->postJson($this->api_login, $post)->assertJson([
'code' => AuthException::PASSWORD_ERROR
]);
}
/**
* 设置密码 但使用错误密码登录
*
* @throws \App\Exceptions\ApiException
*/
public function testLoginByPasswordError()
{
// 创建用户
$user = User::findOrNew($this->phone);
$user->setPassword($this->password);
$post = [
'type' => 'password',
'phone' => $this->phone,
'password' => $this->password,
];
$this->postJson($this->api_login, $post)->assertJson([
'code' => AuthException::PASSWORD_ERROR
]);
}
/**
* 使用密码登录成功
*
* @throws \App\Exceptions\ApiException
*/
public function testLoginByPasswordSuccess()
{
// 创建用户
$user = User::findOrNew($this->phone);
$user->setPassword($this->password);
$post = [
'type' => 'password',
'phone' => $this->phone,
'password' => $this->password,
];
$this->postJson($this->api_login, $post)->assertJson([
'code' => AuthException::PASSWORD_ERROR
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment