Skip to content

Commit bddedaa

Browse files
committed
edit tests case
1 parent 9ca46a0 commit bddedaa

File tree

7 files changed

+257
-19
lines changed

7 files changed

+257
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ for execute only the unit test execute
178178
``` ./vendor/bin/codecept run unit ```
179179

180180
run test of only file
181-
``` ./vendor/bin/codecept run tests/api/UserLoginCept.php```
181+
``` ./vendor/bin/codecept run tests/api/UserCest.php```
182182

183183

184184

api/config/main.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@
6565
}
6666
},
6767
],
68+
'db' => [
69+
'class' => 'yii\db\Connection',
70+
'dsn' => 'mysql:host=127.0.0.1;port=3306;dbname=yii_app',
71+
'username' => 'root',
72+
'password' => 'm4r14l3j',
73+
'charset' => 'utf8',
74+
75+
]
6876
],
6977
'params' => $params,
7078
'controllerMap' => [

api/config/test.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818
return [
1919
'id' => 'app-api-tests',
2020
'basePath' => dirname(__DIR__),
21+
'components' => [
22+
'db2' => [
23+
'class' => 'yii\db\Connection',
24+
'dsn' => 'mysql:host=127.0.0.1;dbname=yii_app_test',
25+
'username' => 'root',
26+
'password' => 'm4r14l3j',
27+
'charset' => 'utf8',
28+
]
29+
]
2130
];
2231

2332

api/config/urlRules.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
'extraPatterns' => [
77
'GET status' => 'status',
88
'POST signup' => 'signup',
9+
'POST register' => 'register'
910
],
1011
'except' => ['create', 'delete', 'update', 'view', 'index'],
1112
],

api/modules/v1/controllers/UserController.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use api\modules\v1\models\User;
77
use base\rest\ActiveController;
8+
use phpDocumentor\Reflection\Types\Null_;
89
use Yii;
910
use yii\filters\auth\CompositeAuth;
1011
use yii\filters\auth\HttpBearerAuth;
@@ -20,7 +21,7 @@ public function behaviors()
2021
[
2122
'class' => CompositeAuth::class,
2223
// Status endpoint is inherited from the ActiveController class
23-
'except' => ['status', 'signup'],
24+
'except' => ['status', 'signup', 'register'],
2425
'authMethods' => [
2526
HttpBearerAuth::class,
2627
],
@@ -42,4 +43,31 @@ public function actionSignup()
4243

4344
}
4445

46+
public function actionRegister(){
47+
$model = new User();
48+
$request = Yii::$app->request->post();
49+
50+
$model->username = $request["username"];
51+
$model->password = $request["password"];
52+
$model->email = $request["username"];
53+
54+
$user = User::find()
55+
->where(['username' => $model->username])
56+
->one();
57+
58+
if($user == null){
59+
$register = User::register($model);
60+
return $register;
61+
62+
}
63+
else{
64+
return ["message" => "username is used by other user"];
65+
66+
}
67+
$response = $model->register();
68+
69+
return $response;
70+
71+
}
72+
4573
}

api/modules/v1/models/User.php

Lines changed: 194 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,28 @@
1313
*
1414
* {@inheritdoc}
1515
*/
16-
class User extends ActiveRecord
16+
17+
/**
18+
* User model
19+
*
20+
* @property integer $id
21+
* @property string $username
22+
* @property string $password_hash
23+
* @property string $password_reset_token
24+
* @property string $email
25+
* @property string $auth_key
26+
* @property string $access_token
27+
* @property string $refresh_token
28+
* @property integer $status
29+
* @property integer $created_at
30+
* @property integer $updated_at
31+
* @property string $created_by
32+
* @property string $updated_by
33+
* @property string $password write-only password
34+
*/
35+
class User extends ActiveRecord implements IdentityInterface
1736
{
18-
public $username;
37+
public $userna;
1938
public $password;
2039
public $rememberMe = true;
2140

@@ -37,11 +56,13 @@ public function rules()
3756
{
3857
return [
3958
// username and password are both required
40-
[['username', 'password'], 'required'],
59+
[['username', 'password_hash'], 'required'],
60+
[['username', 'password_hash'], 'safe'],
61+
[['username', 'password_hash'], 'string'],
4162
// rememberMe must be a boolean value
4263
['rememberMe', 'boolean'],
4364
// password is validated by validatePassword()
44-
['password', 'validatePassword'],
65+
['password_hash', 'validatePassword'],
4566
];
4667
}
4768

@@ -54,22 +75,146 @@ public function getId()
5475
}
5576

5677
/**
57-
* Validates the password.
58-
* This method serves as the inline validation for password.
78+
* {@inheritdoc}
79+
*/
80+
public static function findIdentity($id)
81+
{
82+
return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
83+
}
84+
85+
/**
86+
* {@inheritdoc}
87+
*/
88+
public static function findIdentityByAccessToken($token, $type = null)
89+
{
90+
return static::findOne(['access_token' => $token, 'status' => self::STATUS_ACTIVE]);
91+
}
92+
93+
/**
94+
* Generates new access token
95+
*/
96+
public function generateAccessToken()
97+
{
98+
$this->access_token = Yii::$app->security->generateRandomString(32) . '_' . time();
99+
}
100+
101+
/**
102+
* Validates password
103+
*
104+
* @param string $password password to validate
105+
* @return bool if password provided is valid for current user
106+
*/
107+
public function validatePassword($password)
108+
{
109+
return Yii::$app->security->validatePassword($password, $this->password_hash);
110+
}
111+
112+
113+
/**
114+
* {@inheritdoc}
115+
*/
116+
public function validateAuthKey($authKey)
117+
{
118+
return $this->getAuthKey() === $authKey;
119+
}
120+
121+
/**
122+
* {@inheritdoc}
123+
*/
124+
public function getAuthKey()
125+
{
126+
return $this->auth_key;
127+
}
128+
129+
130+
/**
131+
* Generates password hash from password and sets it to the model
132+
*
133+
* @param string $password
134+
*/
135+
public function setPassword($password)
136+
{
137+
$this->password_hash = Yii::$app->security->generatePasswordHash($password);
138+
}
139+
140+
141+
/**
142+
* Generates "remember me" authentication key
143+
*/
144+
public function generateAuthKey()
145+
{
146+
$this->auth_key = Yii::$app->security->generateRandomString();
147+
}
148+
149+
/**
150+
* Generates new password reset token
151+
*/
152+
public function generatePasswordResetToken()
153+
{
154+
$this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
155+
}
156+
157+
/**
158+
* Removes password reset token
159+
*/
160+
public function removePasswordResetToken()
161+
{
162+
$this->password_reset_token = null;
163+
}
164+
165+
/**
166+
* Finds user by password reset token
167+
*
168+
* @param string $token password reset token
169+
* @return static|null
170+
*/
171+
public static function findByPasswordResetToken($token)
172+
{
173+
if (!static::isPasswordResetTokenValid($token)) {
174+
return null;
175+
}
176+
177+
return static::findOne([
178+
'password_reset_token' => $token,
179+
'status' => self::STATUS_ACTIVE,
180+
]);
181+
}
182+
183+
/**
184+
* Finds out if password reset token is valid
59185
*
60-
* @param string $attribute the attribute currently being validated
61-
* @param array $params the additional name-value pairs given in the rule
186+
* @param string $token password reset token
187+
* @return bool
62188
*/
63-
public function validatePassword($attribute, $params)
189+
public static function isPasswordResetTokenValid($token)
64190
{
65-
if (!$this->hasErrors()) {
66-
$user = $this->getUser();
67-
if (!$user || !$user->validatePassword($this->password)) {
68-
$this->addError($attribute, 'Incorrect username or password.');
69-
}
191+
if (empty($token)) {
192+
return false;
70193
}
194+
195+
$timestamp = (int)substr($token, strrpos($token, '_') + 1);
196+
$expire = Yii::$app->params['user.passwordResetTokenExpire'];
197+
return $timestamp + $expire >= time();
198+
}
199+
200+
/**
201+
* Generates new refresh token
202+
*/
203+
public function generateRefreshToken()
204+
{
205+
$this->refresh_token = Yii::$app->security->generateRandomString(32) . '_' . time();
206+
207+
}
208+
209+
/**
210+
* Removes access token
211+
*/
212+
public function removeRefreshToken()
213+
{
214+
$this->refresh_token = null;
71215
}
72216

217+
73218
/**
74219
* Finds user by [[username]]
75220
*
@@ -102,10 +247,43 @@ public static function findByUsername($username)
102247
*/
103248
public function login()
104249
{
105-
if ($this->validate()) {
106-
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
250+
$login = Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
251+
if($login == true){
252+
return $this->_user;
107253
}
254+
else
255+
return $login;
108256

257+
/*
258+
if ($this->validate()) {
259+
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
260+
}
109261
return false;
262+
*/
263+
}
264+
265+
public function register($data){
266+
267+
$user = new User();
268+
$user->id= null;
269+
$user->email= $data->username;
270+
$user->username = $data->username;
271+
$user->setPassword($data->password);
272+
$user->generateAuthKey();
273+
$user->generatePasswordResetToken();
274+
$user->generateAccessToken();
275+
$user->generateRefreshToken();
276+
$user->created_at = time() /1000;
277+
$user->updated_at = time() / 1000;
278+
279+
$user->isNewRecord = true;
280+
if(!$user->save()) {
281+
return $user->getErrors();
282+
}
283+
else {
284+
return $user;
285+
286+
}
110287
}
288+
111289
}

tests/api/UserCest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,28 @@ public function tryToTest(ApiTester $I)
2424
}
2525

2626
public function trySigUp(ApiTester $I){
27-
$I->sendGET( $this->path.'user/signup');
27+
$I->sendPOST( $this->path.'user/signup', ['username' => 'test@yopmail.com', 'password' => '123456']);
2828
$I->seeResponseCodeIs(HttpCode::OK); // 200
2929
$I->seeResponseIsJson();
3030
$I->canSeeResponseContainsJson([
3131
'success' => true,
3232
]);
33+
$I->seeResponseMatchesJsonType([
34+
'success' => 'boolean',
35+
'data' => 'array',
3336

37+
]);
3438

39+
}
40+
41+
public function tryRegister(ApiTester $I){
42+
$I->sendPOST( $this->path.'user/register', ['username' => 'test2@example.com', 'password' => '123456']);
43+
$I->seeResponseCodeIs(HttpCode::OK);
44+
$I->seeResponseIsJson();
45+
$I->seeResponseMatchesJsonType([
46+
'success' => 'boolean',
47+
'data' => 'array',
3548

49+
]);
3650
}
3751
}

0 commit comments

Comments
 (0)