Codeception / module-yii2

Codeception module for Yii2 framework
MIT License
17 stars 36 forks source link

[ Yii2 - Connector ] User Identity not set after not authorized request #40

Closed wirwolf closed 2 years ago

wirwolf commented 7 years ago

What are you trying to achieve?

Mock user in functional test

Action code


use yii\base\Action;

class TestAction extends Action
{

    public function run() {
        Yii::error(\Yii::$app->user->getId(), 'User id is:');
        return [];
    }
}

Provide test source code if related

        $I->amLoggedInAs(new \tests\_data\functional\Modules\User\FakeIdentity(1));
        $I->sendGET('test/test');

        //$I->amLoggedInAs(null);
        $I->sendGET('test/test');

        $I->amLoggedInAs(new \tests\_data\functional\Modules\User\FakeIdentity(1));
        $I->sendGET('test/test');

Provide console output if related. Use -vvv mode for more details.

Functional Tests (1) ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Modules: Asserts, Yii2, REST
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FinanceParkingCest: Test
Signature: tests\functional\ExampleCest:test
Test: tests/functional/Modules/ExampleCest.php:test
Scenario --
  [yii\db\Connection::open] 'Opening DB connection: mysql:host=mariadb;port=3306;dbname=test'
  [Database] Transaction started
 I am logged in as "tests\\_data\\functional\\Modules\\User\\FakeIdentity"
  [yii\web\User::login] 'User \'1\' logged in from . Session not enabled.'
 I send get "/index-test.php/test/test",[]
  [Request] GET /index-test.php/test/test
  [Request Headers] []
  [yii\filters\RateLimiter::beforeAction] 'Rate limit skipped: "user" does not implement RateLimitInterface.'
  [User id is:] 1
  [Page] /index-test.php/test/test
  [Response] 200
  [Request Cookies] []
  [Response Headers] {"access-control-expose-headers":["X-Pagination-Total-Count, X-Pagination-Page-Count, X-Pagination-Current-Page, X-Pagination-Per-Page, token-type, access-token, userId"],"content-type":["application/json; charset=UTF-8"]}
  [Response] []
 I send get "/index-test.php/test/test",[]
  [Request] GET /index-test.php/test/test
  [Request Headers] []
  [yii\filters\RateLimiter::beforeAction] 'Rate limit skipped: user not logged in.'
  [User id is:] null
  [Page] /index-test.php/test/test
  [Response] 200
  [Request Cookies] []
  [Response Headers] {"access-control-expose-headers":["X-Pagination-Total-Count, X-Pagination-Page-Count, X-Pagination-Current-Page, X-Pagination-Per-Page, token-type, access-token, userId"],"content-type":["application/json; charset=UTF-8"]}
  [Response] []
 I am logged in as "tests\\_data\\functional\\Modules\\User\\FakeIdentity"
  [yii\web\User::login] 'User \'1\' logged in from . Session not enabled.'
 I send get "/index-test.php/test/test",[]
  [Request] GET /index-test.php/test/test
  [Request Headers] []
  [yii\filters\RateLimiter::beforeAction] 'Rate limit skipped: user not logged in.'
  [User id is:] null
  [Page] /index-test.php/test/test
  [Response] 200
  [Request Cookies] []
  [Response Headers] {"access-control-expose-headers":["X-Pagination-Total-Count, X-Pagination-Page-Count, X-Pagination-Current-Page, X-Pagination-Per-Page, token-type, access-token, userId"],"content-type":["application/json; charset=UTF-8"]}
  [Response] []
 PASSED 

  [Database] Transaction cancelled; all changes reverted.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Time: 383 ms, Memory: 24.00MB

OK (1 test, 0 assertions)

In the last query, I do not see the user id, but should

Details

* Suite configuration:

```yml
# Codeception Test Suite Configuration

# suite for functional (integration) tests.
# emulate web requests and make application process them.
# (tip: better to use with frameworks).

# RUN `build` COMMAND AFTER ADDING/REMOVING MODULES.
#basic/web/index.php
class_name: FunctionalTester
modules:
    enabled:
      - Asserts
      - Yii2
      - REST:
          depends: Yii2
          url:
    config:
        Yii2:
            configFile: 'config/tests/functional.php'
            cleanup: true # don't wrap test in transaction
wirwolf commented 6 years ago

Actual problem.