Skip to content

Commit

Permalink
test/main: covered all new auth method with unit tests; removed all u…
Browse files Browse the repository at this point in the history
…nnecessary code; renaming variables; ignoring framework classes on unit tests; added token expirated validation to auth;
  • Loading branch information
2amjsouza committed Aug 10, 2023
1 parent 7b3ddce commit e8e23b8
Show file tree
Hide file tree
Showing 16 changed files with 284 additions and 235 deletions.
8 changes: 0 additions & 8 deletions .env.testing
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=mail_api_test
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
Expand All @@ -32,4 +25,3 @@ REDIS_PASSWORD=null
REDIS_PORT=6379

TOKEN_TIME=60
JWT_SECRET=iDSfvEBNlTUELHBvmJa0QnNuSYrJvK3m2y3KVp89knAG6GVuHAvKwFIqkGCP55US
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ yarn-error.log
/.fleet
/.idea
/.vscode
/xdebug
9 changes: 5 additions & 4 deletions app/Console/Commands/CreateSignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,17 @@ public function handle()
return 0;
}

$timezone = Carbon::now()->utc()->toIso8601String();
$timeStamp = Carbon::now()->utc()->toIso8601String();

$this->info('ts: ' . $timeStamp);

$this->info('tz: ' . $timezone);
$signature = AuthorizationProvider::signToken(
appKey: $tokenAttributes['appKey'],
appSecret: $tokenAttributes['appSecret'],
timeStamp: $timezone,
timeStamp: $timeStamp,
);

$this->info($signature);
$this->info('Signature: ' . $signature);

return 1;
}
Expand Down
3 changes: 0 additions & 3 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ class Kernel extends HttpKernel
* @var array<int, class-string|string>
*/
protected $middleware = [
\App\Http\Middleware\TrustProxies::class,
\Illuminate\Http\Middleware\HandleCors::class,
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];

Expand Down
17 changes: 0 additions & 17 deletions app/Http/Middleware/PreventRequestsDuringMaintenance.php

This file was deleted.

19 changes: 0 additions & 19 deletions app/Http/Middleware/TrimStrings.php

This file was deleted.

28 changes: 0 additions & 28 deletions app/Http/Middleware/TrustProxies.php

This file was deleted.

8 changes: 7 additions & 1 deletion app/Jobs/EmailDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public function handle()
/**
* returns the given template in case template file exists
* returns the default app template in case template file does not exists
*
* @param string $template
* @return string
*/
private function getTemplate(string $template): string
{
Expand All @@ -73,8 +76,11 @@ private function getTemplate(string $template): string
/**
* returns the given language in case language path exists
* returns the default language in case language path does not exists
*
* @param string $language
* @return string
*/
private function getLanguage(string $language): string
public function getLanguage(string $language): string
{
return file_exists(lang_path($language))
? $language
Expand Down
5 changes: 3 additions & 2 deletions app/Providers/AuthorizationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ public static function checkSignature(string $accessKey, string $signedKey, stri
}

/**
* @param string $token
* @param string $appKey
* @param string $appSecret
* @param string $timeStamp
* @param string $secret
* @return string
* @throws Exception
*/
public static function signToken(string $appKey, string $appSecret, string $timeStamp): string
{
Expand Down
7 changes: 7 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
</include>
<exclude>
<file>./app/Providers/BroadcastServiceProvider.php</file>
<file>./app/Providers/AppServiceProvider.php</file>
<file>./app/Providers/EventServiceProvider.php</file>
<file>./app/Providers/RouteServiceProvider.php</file>
<file>./app/Models/User.php</file>
<file>./app/Console/Kernel.php</file>
<file>./app/Http/Controllers/Controller.php</file>
<file>./app/Http/Kernel.php</file>
</exclude>
</coverage>
<php>
Expand Down
File renamed without changes.
29 changes: 18 additions & 11 deletions tests/Unit/CommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,26 @@

class CommandsTest extends TestCase
{
use RefreshDatabase;

public function test_create_user_command()
public function test_create_app_key_signature_command()
{
$email = fake()->email;

$this->artisan('app:create-user')
->expectsQuestion('Define an Name', fake()->name)
->expectsQuestion('Define an Email', $email)
->expectsQuestion('Define a Password', '123456')
$this->artisan('app:create-signature')
->expectsOutput('Laravel API Service')
->expectsOutput('Signed access token generation script.')
->expectsOutput('Before moving ahead, define your access key in ./config/laravel-mail-api-token.php')
->expectsQuestion('Inform the Token Access Key', 'tests')
->expectsOutputToContain('ts')
->expectsOutputToContain('Signature:')
->assertExitCode(1);

$this->assertNotNull(User::whereEmail($email));
}

public function test_create_app_key_invalid_access_key()
{
$this->artisan('app:create-signature')
->expectsOutput('Laravel API Service')
->expectsOutput('Signed access token generation script.')
->expectsOutput('Before moving ahead, define your access key in ./config/laravel-mail-api-token.php')
->expectsQuestion('Inform the Token Access Key', 'x')
->expectsOutput('Invalid access token.')
->assertExitCode(0);
}
}
Loading

0 comments on commit e8e23b8

Please sign in to comment.