Skip to content

Commit

Permalink
Merge pull request #3 from Oluwatobi-beebittech/chore/testing
Browse files Browse the repository at this point in the history
test: added testing to laravel modulr auth package
  • Loading branch information
Oluwatobi-beebittech authored Aug 2, 2022
2 parents b7adc4e + eb65bbf commit 2ef85ac
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 25 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/coverage
/vendor/
/composer.lock
/composer.lock
/.phpunit.result.cache
18 changes: 12 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"autoload-dev": {
"psr-4": {
"OluwatobiAkanji\\ModulrAuth\\Tests\\": "tests"
"OluwatobiAkanji\\ModulrAuth\\Tests\\": "tests/"
}
},
"authors": [
Expand All @@ -18,19 +18,25 @@
}
],
"require": {
"illuminate/support": "^9.20"
"illuminate/support": "^9.20",
"orchestra/testbench": "^7.6",
"illuminate/contracts": "^9.22"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
},
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html coverage"
},
"extra": {
"laravel": {
"providers": [
"OluwatobiAkanji\\ModulrAuth\\ModulrAuthServiceProvider"
],
"aliases": {
"AuthHeader": "OluwatobiAkanji\\ModulrAuth\\Facade\\AuthHeader"
"AuthHeader": "OluwatobiAkanji\\ModulrAuth\\Facades\\AuthHeader"
}
}
},
"require-dev": {
"phpunit/phpunit": "^9.5"
}
}
4 changes: 2 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</testsuite>
</testsuites>
<php>
<env name="DB_CONNECTION" value="testing"/>
<env name="APP_KEY" value="base64:2fl+Ktvkfl+Fuz4Qp/A75G2RTiWVA/ZoKZvp6fiiM10="/>
<env name="MODULR_API_KEY" value="TOKENKNOWN-"/>
<env name="MODULR_API_SECRET" value="NzAwZmIwMGQ0YTJiNDhkMzZjYzc3YjQ5OGQyYWMzOTI="/>
</php>
</phpunit>
2 changes: 0 additions & 2 deletions resources/config/modulr.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
* file that was distributed with this source code.
*/

use Illuminate\Support\Facades\Facade;

return [

'key' => env('MODULR_API_KEY'),
Expand Down
12 changes: 2 additions & 10 deletions src/Auth/AuthHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,12 @@ class AuthHeader
private ?string $nonce = null;
private ?string $timestamp = null;

public function getModulrRoute(): string {
$baseRoute = App::isProduction()
? config('modulr.live_base_url')
: config('modulr.local_base_url');

return config('modulr.base_url').$baseRoute;
}

public function getHeaders(?string $nonce=null, ?string $timestamp=null): array{
$apiKey = config('modulr.key');
$apiSecret = config('modulr.secret');

$nonce = $nonce ?? Str::uuid();
$timestamp = $timestamp ?? now()->toRfc7231String();
$nonce = $nonce ?? $this->nonce ?? Str::uuid();
$timestamp = $timestamp ?? $this->timestamp ?? now()->toRfc7231String();

$auth = new AuthSignatureGenerator($apiKey, $apiSecret);
return $auth->calculateHeaders($nonce, $timestamp)->getHTTPHeaders();
Expand Down
2 changes: 1 addition & 1 deletion src/Auth/AuthSignatureGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private function getSignature(string $nonce, string $timestamp): string {
return "date: $timestamp\nx-mod-nonce: $nonce";
}

public function calculateHeaders(string $nonce, string $timestamp): AuthorizationResult{
public function calculateHeaders(string $nonce, string $timestamp): AuthResult{
$apiKey = $this->getAPIKey();
$apiSecret = $this->getAPISecret();

Expand Down
2 changes: 1 addition & 1 deletion src/Facades/AuthHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Illuminate\Support\Facades\Facade;

class AuthHeaders extends Facade
class AuthHeader extends Facade
{
protected static function getFacadeAccessor()
{
Expand Down
2 changes: 1 addition & 1 deletion src/ModulrAuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ModulrAuthServiceProvider extends ServiceProvider
public function boot()
{
$this->publishes([
__DIR__.'/../resources/config/modulr.php' => config_path('modulr.php'),
dirname(__DIR__, 1).'/resources/config/modulr.php' => config_path('modulr.php'),
]);
}

Expand Down
76 changes: 76 additions & 0 deletions tests/Feature/AuthHeaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/*
* This file is part of the Laravel Modulr Auth package.
*
* (c) Oluwatobi Akanji <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace OluwatobiAkanji\ModulrAuth\Tests\Feature;

use OluwatobiAkanji\ModulrAuth\Facades\AuthHeader;
use OluwatobiAkanji\ModulrAuth\Tests\TestCase;

class AuthHeaderTest extends TestCase {

protected $apiKey;
protected $encodedSignature;
protected $expectedAuthorization;
protected $nonce;
protected $timestamp;

public function setUp(): void {
parent::setup();
$this->apiKey = config('modulr.key');
$this->encodedSignature ='WBMr%2FYdhysbmiIEkdTrf2hP7SfA%3D';
$this->nonce = '28154b2-9c62b93cc22a-24c9e2-5536d7d';
$this->timestamp = 'Mon, 25 Jul 2016 16:36:07 GMT';
$this->expectedAuthorization = "Signature keyId=\"$this->apiKey\",algorithm=\"hmac-sha1\",headers=\"date x-mod-nonce\",signature=\"$this->encodedSignature\"";
}

public function test_auth_header_facade(): void {
AuthHeader::shouldReceive('getHeaders')
->with($this->nonce, $this->timestamp)
->andReturn(["test"]);
$response = AuthHeader::getHeaders($this->nonce, $this->timestamp);

$this->assertEquals(["test"], $response);
}

public function test_auth_header(): void {
$authHeaders = AuthHeader::getHeaders($this->nonce, $this->timestamp);

$this->assertCount(3, $authHeaders);
$this->assertEquals($authHeaders['Date'], $this->timestamp);
$this->assertEquals($authHeaders['x-mod-nonce'], $this->nonce);
$this->assertEquals($authHeaders['Authorization'], $this->expectedAuthorization);
}

public function test_auth_header_using_current_time(): void {
$authHeaders = AuthHeader::getHeaders();

$this->assertCount(3, $authHeaders);
$this->assertIsString($authHeaders['Date']);
$this->assertIsString($authHeaders['x-mod-nonce']);
$this->assertIsString($authHeaders['Authorization']);
}

public function test_set_timestamp(): void {
AuthHeader::setTimestamp($this->timestamp);
$authHeaders = AuthHeader::getHeaders();

$this->assertCount(3, $authHeaders);
$this->assertEquals($authHeaders['Date'], $this->timestamp);
}

public function test_set_nonce(): void {
AuthHeader::setNonce($this->nonce);
$authHeaders = AuthHeader::getHeaders();

$this->assertCount(3, $authHeaders);
$this->assertEquals($authHeaders['x-mod-nonce'], $this->nonce);
}
}
3 changes: 2 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ protected function getPackageProviders($app)

protected function getEnvironmentSetUp($app)
{
// perform environment setup
$app['config']->set('modulr.key', 'KNOWN-TOKEN');
$app['config']->set('modulr.secret', 'NzAwZmIwMGQ0YTJiNDhkMzZjYzc3YjQ5OGQyYWMzOTI=');
}
}

0 comments on commit 2ef85ac

Please sign in to comment.