Skip to content

Commit 913b06e

Browse files
committed
Added test of posts
1 parent dbc7798 commit 913b06e

File tree

6 files changed

+69
-2
lines changed

6 files changed

+69
-2
lines changed

rest/versions/v1/models/Post.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ class Post extends CommonPost
88
{
99
const STATUS_DELETED = 3;
1010

11+
public function beforeSave($insert)
12+
{
13+
if (parent::beforeSave($insert)) {
14+
if ($insert) {
15+
$this->author_id = \Yii::$app->user->identity->getId();
16+
}
17+
return true;
18+
} else {
19+
return false;
20+
}
21+
}
22+
1123
public function delete()
1224
{
1325
$result = false;

rest/versions/v1/models/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function getRateLimit($request, $action)
1919
return [1, 30];
2020
}
2121

22-
return [1, 1];
22+
return [2, 1];
2323
}
2424

2525
/**

tests/codeception/common/_support/FixtureHelper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace tests\codeception\common\_support;
44

5+
use tests\codeception\common\fixtures\PostFixture;
56
use tests\codeception\common\fixtures\UserFixture;
67
use Codeception\Module;
78
use yii\test\FixtureTrait;
@@ -55,6 +56,9 @@ public function fixtures()
5556
'class' => UserFixture::className(),
5657
'dataFile' => '@tests/codeception/common/fixtures/data/init_login.php',
5758
],
59+
'post' => [
60+
'class' => PostFixture::className(),
61+
],
5862
];
5963
}
6064
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace tests\codeception\common\fixtures;
4+
5+
use yii\test\ActiveFixture;
6+
7+
/**
8+
* User fixture
9+
*/
10+
class PostFixture extends ActiveFixture
11+
{
12+
public $modelClass = 'common\models\Post';
13+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php use tests\codeception\rest\FunctionalTester;
2+
3+
/** @var \Codeception\Scenario $scenario */
4+
$I = new FunctionalTester($scenario);
5+
$token = '?access-token=tUu1qHcde0diwUol3xeI-18MuHkkprQI';
6+
$I->wantTo('ensure that posts access only for auth users');
7+
8+
$I->sendGET('/v1/posts');
9+
$I->seeResponseCodeIs(401);
10+
$I->seeResponseIsJson();
11+
12+
$I->wantTo('create post via API');
13+
$I->sendPOST(
14+
'/v1/posts' . $token,
15+
['title' => 'My first post', 'content' => 'There are many words....', 'status' => 2]
16+
);
17+
$I->seeResponseCodeIs(201);
18+
$I->seeResponseIsJson();
19+
$I->seeResponseContainsJson(['title' => 'My first post']);
20+
21+
$I->wantTo('get my post');
22+
$I->sendGET('/v1/posts' . $token . '&title=My first post');
23+
$I->seeResponseCodeIs(200);
24+
$I->seeResponseIsJson();
25+
$I->seeResponseContainsJson(['title' => 'My first post', 'id' => 1, 'author' => ['username' => 'erau']]);
26+
27+
$I->wantTo('update my post to draft');
28+
$I->haveHttpHeader('Content-Type', 'application/json');
29+
$I->sendPUT('/v1/posts/1' . $token, ['title' => 'My second post']);
30+
$I->seeResponseCodeIs(200);
31+
$I->seeResponseIsJson();
32+
$I->seeResponseContainsJson(['title' => 'My second post']);

tests/codeception/rest/functional/UserApiCept.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@
77
$I->sendPOST('/v1/user/login', ['username' => 'erau', 'password' => 'password_0']);
88
$I->seeResponseCodeIs(200);
99
$I->seeResponseIsJson();
10-
$I->seeResponseContains('tUu1qHcde0diwUol3xeI-18MuHkkprQI');
10+
$I->seeResponseContains('tUu1qHcde0diwUol3xeI-18MuHkkprQI');
11+
12+
$I->wantTo('test invalid query');
13+
$I->sendPOST('/v1/user/login', ['username' => 'user', 'password' => 'password']);
14+
$I->seeResponseCodeIs(422);
15+
$I->seeResponseIsJson();
16+
$I->seeResponseContainsJson(['message' => 'Incorrect username or password.']);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy