Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wing5wong committed Jul 20, 2022
1 parent 5294abd commit 5a327cf
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@ You MUST make sure you are authenticating requests to your application.
The example implementation will write json data files at `/storage/app/data`. These files are not publicly accessible by default.
Consider a task to tidy up these files once processed.

## Testing
Run tests with `php artisan test`

## KAMAR Directory Services Documentation
See https://directoryservices.kamar.nz/ for implementation details.
76 changes: 76 additions & 0 deletions tests/Feature/AuthenticationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class AuthenticationTest extends TestCase
{
public function test_unauthenticated_request_returns_403()
{
$response = $this->postJson('/kamar');

$response->assertJson(['SMSDirectoryData' => ['error' => 403]]);
$response->assertJson(['SMSDirectoryData' => ['result' => 'Authentication Failed']]);
$response->assertJsonMissing(['SMSDirectoryData' => ['service' => config('kamar.serviceName')]]);
$response->assertJsonMissing(['SMSDirectoryData' => ['version' => config('kamar.serviceVersion')]]);
}

public function test_unauthenticated_check_request_returns_403_with_service_details()
{
$response = $this->postJson('/kamar', ['SMSDirectoryData'=>['sync'=>'check']]);

$response->assertJson(['SMSDirectoryData' => ['error' => 403]]);
$response->assertJson(['SMSDirectoryData' => ['result' => 'Authentication Failed']]);
$response->assertJson(['SMSDirectoryData' => ['service' => config('kamar.serviceName')]]);
$response->assertJson(['SMSDirectoryData' => ['version' => config('kamar.serviceVersion')]]);
}

public function test_invalid_credentials_returns_403()
{
$response = $this->withHeaders([
'HTTP_AUTHORIZATION' => "Basic " . base64_encode('Username' . ':' . 'Password'),
])->postJson('/kamar');

$response->assertJson(['SMSDirectoryData' => ['error' => 403]]);
}

public function test_authenticated_request_with_missing_data_returns_401()
{
$response = $this->withHeaders([
'HTTP_AUTHORIZATION' => "Basic " . base64_encode(config('kamar.username') . ':' . config('kamar.password')),
])->postJson('/kamar');

$response->assertJson(['SMSDirectoryData' => ['error' => 401]]);
$response->assertJson(['SMSDirectoryData' => ['result' => 'Missing Data']]);
$response->assertJsonMissing(['SMSDirectoryData' => ['service' => config('kamar.serviceName')]]);
$response->assertJsonMissing(['SMSDirectoryData' => ['version' => config('kamar.serviceVersion')]]);
}

public function test_authenticated_sync_check_request_with_data_returns_0_and_service()
{
$response = $this->withHeaders([
'HTTP_AUTHORIZATION' => "Basic " . base64_encode(config('kamar.username') . ':' . config('kamar.password')),
])->postJson('/kamar', ['SMSDirectoryData'=>['sync'=>'check']]);

$response->assertJson(['SMSDirectoryData' => ['error' => 0]]);
$response->assertJson(['SMSDirectoryData' => ['result' => 'OK']]);
$response->assertJson(['SMSDirectoryData' => ['service' => config('kamar.serviceName')]]);
$response->assertJson(['SMSDirectoryData' => ['version' => config('kamar.serviceVersion')]]);
}

public function test_authenticated_request_with_data_returns_0_and_no_service_details()
{
$response = $this->withHeaders([
'HTTP_AUTHORIZATION' => "Basic " . base64_encode(config('kamar.username') . ':' . config('kamar.password')),
])->postJson('/kamar', ['SMSDirectoryData'=>['sync'=>'part']]);

$response->assertJson(['SMSDirectoryData' => ['error' => 0]]);
$response->assertJson(['SMSDirectoryData' => ['result' => 'OK']]);
$response->assertJsonMissing(['SMSDirectoryData' => ['service' => config('kamar.serviceName')]]);
$response->assertJsonMissing(['SMSDirectoryData' => ['version' => config('kamar.serviceVersion')]]);
}

}

0 comments on commit 5a327cf

Please sign in to comment.