Skip to main content

Helpers

Doc:https://codeception.com/docs

Helpers

Case UserAuthgenerate

php vendor/bin/codecept generate:helper Auth

Generate Helpers

<?php
declare(strict_types=1);
namespace Tests\Support\Helper;

// here you can define custom actions
// all public methods declared in helper class will be available in $I

use App\Factory\JwtTokenFactory;

class Auth extends \Codeception\Module
{
protected array $requiredFields = ['uid', 'username'];
protected array $config = [
'app_name' => "PAYMENT_API",
'app_env' => "test",
'api_key' => "ACPDeveloperApiKey",
'token_iss' => "knowledge_payment_api",
'subs' => [
'web' => 'knowledge_payment_user',
],
'token_alg' => "HS256"
];

public function GenerateAuthToken(): string
{
$tokenFactory = new JwtTokenFactory(
$this->config['api_key'],
$this->config['app_name'],
$this->config['app_env'],
$this->config['token_iss'],
$this->config['subs'],
$this->config['token_alg']
);
return $tokenFactory->createToken($this->config['uid'], $this->config['username'])['token'];
}
}

Config

api.suite.yml

actor: ApiTester
suite_namespace: Tests\Api
modules:
enabled:
- Tests\Support\Helper\Auth:
uid : 1
username : admin
- REST:
url: nginx:8081
depends: PhpBrowser
part: Json

Case

<?php

namespace Tests\Api;

use \Tests\Support\ApiTester;

class CreateUserCest
{
public function _before(ApiTester $I)
{
}

// tests
public function tryToTest(ApiTester $I)
{
$token = $I->GenerateAuthToken(2,'admin');
$I->haveHttpHeader('Authorization', 'Bearer '.$token);
$I->sendPost('/api/user');
}
}