Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

creating feature tests #102

Merged
merged 11 commits into from
Nov 1, 2024
Merged
9 changes: 9 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,14 @@ jobs:
- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Create env
run: php qt core:env

- name: Create APP key
run: php qt core:key --length=64

- name: Install demo
run: php qt install:demo --yes

- name: Run tests
run: composer test
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"autoload-dev": {
"psr-4": {
"Quantum\\Tests\\": "tests/unit/"
"Quantum\\Tests\\": "tests/"
}
},
"scripts": {
Expand Down
20 changes: 14 additions & 6 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php"
colors="true"
verbose="true"
stopOnFailure="false">
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
stderr="true"
processIsolation="true"
>
<testsuites>
<testsuite name="Quantum Test Suite">
<directory>./tests</directory>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
</phpunit>
248 changes: 248 additions & 0 deletions tests/Feature/Api/AuthControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
<?php

namespace Quantum\Tests\Feature\Api;

use Quantum\Tests\Feature\AppTestCase;
use Quantum\Libraries\Hasher\Hasher;
use Quantum\Factory\ModelFactory;
use Shared\Models\User;

class AuthControllerTest extends AppTestCase
{
/**
* @var string
*/
protected $email = '[email protected]';

/**
* @var string
*/
protected $password = 'password';

public function setUp(): void
{
parent::setUp();
}

public function testSignInApi()
{
$response = $this->request('post', '/api/en/signin', [
'email' => $this->email,
'password' => $this->password
]);

$this->assertIsObject($response);
$this->assertEquals('success', $response->get('status'));
$this->assertArrayHasKey('tokens', $response->all());
}

public function testSignInWithIncorrectRequestApi()
{
$response = $this->request('post', '/api/en/signin', [
'email' => '[email protected]',
'password' => 'password'
]);

$this->assertIsObject($response);
$this->assertEquals('error', $response->get('status'));
$this->assertEquals('Incorrect credentials', $response->get('message'));
}

public function testMeApi()
{
$tokens = $this->signInAndReturnTokens();

$response = $this->request('get', '/api/en/me', [], ['Authorization' => 'Bearer ' . $tokens['access_token']]);

$this->assertIsObject($response);
$this->assertEquals('success', $response->get('status'));
$this->assertIsArray($response->all());
$this->assertArrayHasKey('data', $response->all());
$this->assertIsArray($response->get('data'));
$this->assertArrayHasKey('firstname', $response->get('data'));
$this->assertArrayHasKey('lastname', $response->get('data'));
$this->assertArrayHasKey('email', $response->get('data'));
}

public function testMeIncorrectApi()
{
$response = $this->request('get', '/api/en/me');

$this->assertIsObject($response);
$this->assertEquals('error', $response->get('status'));
$this->assertEquals('Unauthorized request', $response->get('message'));
}

public function testSignoutApi()
{
$tokens = $this->signInAndReturnTokens();

$response = $this->request('get', '/api/en/signout', [], [
'Authorization' => 'Bearer ' . $tokens['access_token'],
'refresh_token' => $tokens['refresh_token']
]);

$this->assertEquals('success', $response->get('status'));
}

public function testForgetApi()
{
$response = $this->request('post', '/api/en/forget', ['email' => $this->email]);

$this->assertEquals('success', $response->get('status'));
$this->assertEquals('Check your email to reset your password', $response->get('message'));
}

public function testSignupApi()
{
$response = $this->request('post', '/api/en/signup', [
'email' => '[email protected]',
'password' => $this->password,
'firstname' => 'firstname',
'lastname' => 'lastname'
]);

$this->assertEquals('success', $response->get('status'));
$this->assertEquals('Successfully signed up', $response->get('message'));

ModelFactory::get(User::class)->findOneBy('email', '[email protected]')->delete();
}

public function testSignupIncorrectApi()
{
$response = $this->request('post', '/api/en/signup', [
'email' => $this->email,
'password' => $this->password,
'firstname' => 'firstname',
'lastname' => 'lastname'
]);

$this->assertEquals('error', $response->get('status'));
$this->assertEquals('The value of Email field is already exists in our database', $response->get('message')['email'][0]);
}

public function testActivateApi()
{
$userModel = ModelFactory::get(User::class)->create();
$activationToken = base64_encode((new Hasher())->hash('password'));
$userModel->fillObjectProps([
'email' => $this->email,
'password' => (new Hasher())->hash($this->password),
'firstname' => 'firstname',
'lastname' => 'lastname',
'activation_token' => $activationToken
]);
$userModel->save();

$response = $this->request('get', '/api/en/activate/' . $activationToken);

$this->assertEquals('success', $response->get('status'));
$this->assertEquals('Account is activated', $response->get('message'));
}

public function testActivateIncorrectApi()
{
$response = $this->request('get', '/api/en/activate/incorrect-activation-token');

$this->assertEquals('error', $response->get('status'));
$this->assertEquals('There is no record matched to token', $response->get('message')[0]);
}

public function testResetApi()
{
$userModel = ModelFactory::get(User::class)->create();
$resetToken = base64_encode((new Hasher())->hash('password'));
$userModel->fillObjectProps([
'email' => $this->email,
'password' => (new Hasher())->hash($this->password),
'firstname' => 'firstname',
'lastname' => 'lastname',
'reset_token' => $resetToken
]);
$userModel->save();

$response = $this->request('post', '/api/en/reset/' . $resetToken, [
'password' => $this->password,
'repeat_password' => $this->password,
]);

$this->assertEquals('success', $response->get('status'));
}

public function testResetIncorrectApi()
{
$response = $this->request('post', '/api/en/reset/incorrect-activation-token');

$this->assertEquals('error', $response->get('status'));
$this->assertEquals('There is no record matched to token', $response->get('message')[0]);
}

public function testResendApi()
{

$userModel = ModelFactory::get(User::class)->create();
$otpToken = base64_encode((new Hasher())->hash($this->email));
$userModel->fillObjectProps([
'email' => $this->email,
'password' => (new Hasher())->hash($this->password),
'firstname' => 'firstname',
'lastname' => 'lastname',
'otp_token' => $otpToken
]);
$userModel->save();
$response = $this->request('get', '/api/en/resend/' . $otpToken);

$this->assertEquals('success', $response->get('status'));
$this->assertArrayHasKey('code', $response->all());
$this->assertIsString($response->get('code'));
}

public function testResendIncorrectApi()
{
$response = $this->request('get', '/api/en/resend/incorrect-otp-token');

$this->assertEquals('error', $response->get('status'));
$this->assertEquals('Incorrect credentials', $response->get('message'));
}

public function testVerifyApi()
{
$userModel = ModelFactory::get(User::class)->create();
$otpToken = base64_encode((new Hasher())->hash($this->email));
$userModel->fillObjectProps([
'email' => $this->email,
'password' => (new Hasher())->hash($this->password),
'firstname' => 'firstname',
'lastname' => 'lastname',
'otp_token' => $otpToken,
'otp' => $otpToken
]);
$userModel->save();

$response = $this->request('post', '/api/en/verify', [
'otp' => $otpToken,
'code' => $otpToken,
]);

$this->assertEquals('success', $response->get('status'));
$this->assertArrayHasKey('tokens', $response->all());
$this->assertArrayHasKey('refresh_token', $response->get('tokens'));
$this->assertArrayHasKey('access_token', $response->get('tokens'));
}

public function testVerifyIncorrectApi()
{
$response = $this->request('post', '/api/en/verify', [
'otp' => 'incorrect-otp',
'code' => 'incorrect-code',
]);

$this->assertEquals('error', $response->get('status'));
$this->assertEquals('Incorrect verification code.', $response->get('message'));
}

public function tearDown(): void
{
parent::tearDown();
}
}
Loading
Loading