Skip to content

Commit

Permalink
test: coverage to 77%
Browse files Browse the repository at this point in the history
  • Loading branch information
tobybatch committed Mar 12, 2024
1 parent 530ad9f commit d74349e
Show file tree
Hide file tree
Showing 18 changed files with 454 additions and 30 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/laravel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ jobs:

- name: Create DB
run: |
touch /tmp/data.db
php artisan migrate
env:
APP_ENV: testing

- name: Generate key
run: php artisan key:generate
env:
APP_ENV: testing

- name: Passport install
run: php .docker/passport-install.php .
Expand Down
9 changes: 5 additions & 4 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Handler extends ExceptionHandler
*/
protected function shallWeStackTrace(Throwable $e): bool
{
$x = config('app.env');
if (config('app.env') === 'production') {
foreach ($this->dontStackTrace as $type) {
if ($e instanceof $type) {
Expand Down Expand Up @@ -117,15 +118,15 @@ public function render($request, Throwable $exception)
->guest('/login')
// We anticipate expiry to be the most common reason.
->withErrors(['error_message' => trans('auth.expired')])
;
;
}

if ($exception instanceof MethodNotAllowedHttpException) {
// User somehow tried an HTTP verb a resource doesn't support
// Send them somewhere safe. '' is the "/" equivalent.
return redirect('');
// For some reason ->withErrors() won't "take";
// The session gets flashed, but it doesn't survive.
// For some reason ->withErrors() won't "take";
// The session gets flashed, but it doesn't survive.
}

return parent::render($request, $exception);
Expand All @@ -150,7 +151,7 @@ protected function unauthenticated($request, AuthenticationException $exception)
case 'api':
$login = route('api.login');
break;
//TODO: merge
//TODO: merge
case 'store':
$login = route('store.login');
break;
Expand Down
4 changes: 4 additions & 0 deletions app/Note.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

use Illuminate\Database\Eloquent\Model;

/**
* @property Family $family;
* @property CentreUser $user;
*/
class Note extends Model
{
/**
Expand Down
5 changes: 4 additions & 1 deletion app/Notifications/AdminPasswordResetNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ public function toMail($notifiable)
return (new MailMessage)
->subject('Password Reset Request Notification')
->greeting('Hi '. $this->name .',')
->line('You are receiving this email because we received a password reset request for your account on the Rosie admin service.')
->line(
'You are receiving this email because we received a password reset request for your account
on the Rosie admin service.'
)
->action(
'Reset Password',
('http://'
Expand Down
9 changes: 5 additions & 4 deletions app/Wrappers/SecretStreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ class SecretStreamWrapper
* We setup the resource we're wrapping and the cryptography we're going to rely on, deriving our key from
* self::getKey().
*
* @param $path the full path provided to @see fopen() or similar
* @param $mode only "r" is supported
* @param $options some options encoded as bits
* @param $open
* @param $path string the full path provided to @see fopen() or similar
* @param $mode string only "r" is supported
* @param $options int some options encoded as bits
* @param $open mixed not used?
*
* @return bool whether opening was successful
*/
public function stream_open($path, $mode, $options, &$open) {
Expand Down
1 change: 1 addition & 0 deletions tests/CreatesApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Hash;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Contracts\Foundation\Application;

trait CreatesApplication
{
Expand Down
1 change: 0 additions & 1 deletion tests/Unit/Console/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Testing\TestCase;
use Tests\CreatesApplication;

Expand Down
62 changes: 43 additions & 19 deletions tests/Unit/Controllers/Service/Admin/DeliveriesControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,44 @@

use App\AdminUser;
use App\Centre;
use App\Voucher;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Mockery;
use Mockery\MockInterface;
use Tests\TestCase;

class DeliveriesControllerTest extends TestCase
{
use DatabaseMigrations;

/** @var AdminUser $adminUser */
private $adminUser;

/** @var Centre $centre */
private $centre;

private $vouchersDeliveryroute;
private AdminUser $adminUser;
private Centre $centre;
private string $vouchersDeliveryroute;
private Collection $vouchers;

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

$this->adminUser = factory(AdminUser::class)->create();
$this->centre = factory(Centre::class)->create();
$this->vouchers = factory(Voucher::class, 5)->state('printed')->create();
$this->vouchersDeliveryroute = route('admin.deliveries.store');

// TODO The vouchers created have random code lengths,is that right?
$shortcode = $this->vouchers[0]->sponsor->shortcode;
foreach ($this->vouchers as $index => $voucher) {
$voucher->code = $shortcode . sprintf("%04d", $index);
$voucher->save();
}
}

/**
* @test
*
* @return void
*/
public function testStoreWithoutStartEndDateErrors()
public function testStoreWithoutStartEndDateErrors(): void
{
$this->actingAs($this->adminUser, 'admin')
->post($this->vouchersDeliveryroute, [
Expand All @@ -54,10 +61,8 @@ public function testStoreWithoutStartEndDateErrors()

/**
* @test
*
* @return void
*/
public function testStoreStartEndSwapped()
public function testStoreStartEndSwapped(): void
{
$this->actingAs($this->adminUser, 'admin')
->post($this->vouchersDeliveryroute, [
Expand All @@ -75,10 +80,8 @@ public function testStoreStartEndSwapped()

/**
* @test
*
* @return void
*/
public function testStoreCentreIsNotNumberErrors()
public function testStoreCentreIsNotNumberErrors(): void
{
$this->actingAs($this->adminUser, 'admin')
->post($this->vouchersDeliveryroute, [
Expand All @@ -94,10 +97,8 @@ public function testStoreCentreIsNotNumberErrors()

/**
* @test
*
* @return void
*/
public function testStoreStartIsNotTheSameSponsorAsEndErrors()
public function testStoreStartIsNotTheSameSponsorAsEndErrors(): void
{
$this->actingAs($this->adminUser, 'admin')
->post($this->vouchersDeliveryroute, [
Expand All @@ -112,4 +113,27 @@ public function testStoreStartIsNotTheSameSponsorAsEndErrors()
'voucher-end' => 'The voucher-end field must be the same sponsor as the voucher-start field.'
]);
}

/**
* @test
*/
public function testStore(): void
{
$vouchers = Voucher::all()->sortBy("code");
$firstVoucher = $vouchers->first();
$lastVoucher = $vouchers->last();
$response = $this->actingAs($this->adminUser, 'admin')
->post($this->vouchersDeliveryroute, [
'centre' => $this->centre->id,
'voucher-start' => $firstVoucher->code,
'voucher-end' => $lastVoucher->code,
'date-sent' => Carbon::now()->format('Y-m-d'),
])
->assertStatus(302)
->assertRedirectToRoute("admin.deliveries.create");

// TODO This needs loads more work, I can't get a deliverable range so the test never goes beyond here
// https://github.com/neontribe/ARCVService/blob/096bd1850c8a1a4baaf5e95d916a6ef6a062012f/app/Http/Controllers/Service/Admin/DeliveriesController.php#L63

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Tests\Unit\Controllers\Service\Auth;

use App\Http\Controllers\Service\Auth\ForgotPasswordController;
use Illuminate\Contracts\Auth\PasswordBroker;
use Illuminate\Contracts\View\View;
use Tests\TestCase;

class ForgotPasswordControllerTest extends TestCase
{

public function testBroker()
{
$fpc = new ForgotPasswordController();
$reflection = new \ReflectionClass($fpc);
$method = $reflection->getMethod("broker");
$method->setAccessible(true);
$result = $method->invoke($fpc);
$this->assertInstanceOf(PasswordBroker::class, $result);
}
public function testShowLinkRequestForm()
{
$fpc = new ForgotPasswordController();
$result = $fpc->showLinkRequestForm();
$this->assertInstanceOf(View::class, $result);
}
}
80 changes: 80 additions & 0 deletions tests/Unit/Controllers/Service/Auth/LoginControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Tests\Unit\Controllers\Service\Auth;

use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Tests\TestCase;

class LoginControllerTest extends TestCase
{
public function testLoginGood()
{
// Called in middleware
Auth::shouldReceive("guard->check")->once()->andReturn(false);
// Called in controller
Auth::shouldReceive("guard->attempt")->once()->andReturn(true);

/**
* @var RedirectResponse $response
*/
$response = $this->post(
route('admin.login'),
[
'email' => '[email protected]',
'password' => 'bdbdbd',
]
);
$this->assertEquals(302, $response->status());
$a = route('admin.dashboard');
$b = $response->getTargetUrl();
$this->assertEquals(route('admin.dashboard'), $response->getTargetUrl());
}

public function testLoginInvalidForm()
{
// Called in middleware
Auth::shouldReceive("guard->check")->once()->andReturn(false);

/**
* @var RedirectResponse $response
*/
$response = $this->post(
route('admin.login'),
[
'fu' => 'bar'
]
);
$this->assertEquals(302, $response->status());
$a = route('admin.dashboard');
$b = $response->getTargetUrl();
$this->assertEquals(route('admin.dashboard'), $response->getTargetUrl());
}

public function testLoginFail()
{
// Called in middleware
Auth::shouldReceive("guard->check")->once()->andReturn(false);
// Called in controller
Auth::shouldReceive("guard->attempt")->once()->andReturn(false);

/**
* @var RedirectResponse $response
*/
$response = $this->post(
route('admin.login'),
[
'email' => '[email protected]',
'password' => 'draconia',
]
);
$this->assertEquals(302, $response->status());
$a = route('admin.dashboard');
$b = $response->getTargetUrl();
$this->assertEquals(route('admin.dashboard'), $response->getTargetUrl());
}

// public function testLoginTooManyLogins()
// {
// }
}
25 changes: 25 additions & 0 deletions tests/Unit/Controllers/Service/Auth/LoginRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Tests\Unit\Controllers\Service\Auth;

use App\Http\Controllers\API\Auth\LoginRequest;
use Tests\TestCase;

class LoginRequestTest extends TestCase
{
public function testAuthorize()
{
$lr = new LoginRequest();
$this->assertTrue($lr->authorize());
}

public function testRules()
{
$lr = new LoginRequest();
$rules = $lr->rules();
$this->assertArrayHasKey('username', $rules);
$this->assertArrayHasKey('password', $rules);
$this->assertEquals("required|email", $rules["username"]);
$this->assertEquals("required", $rules["password"]);
}
}
21 changes: 21 additions & 0 deletions tests/Unit/Controllers/Service/DashboardControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Tests\Unit\Controllers\Service;

use App\Http\Controllers\Service\DashboardController;
use Illuminate\View\View;
use Tests\TestCase;

class DashboardControllerTest extends TestCase
{
public function testDashboard()
{
// TODO I can't find App\Http\Controllers\Service\DashboardController in the routes
// is it actually used?
// $response = $this->get(route('????'))->assertStatus(200);

$dc = new DashboardController();
$response = $dc->index();
$this->assertInstanceOf(View::class, $response);
}
}
15 changes: 15 additions & 0 deletions tests/Unit/Controllers/Service/VersionControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Tests\Unit\Controllers\Service;

use Tests\TestCase;

class VersionControllerTest extends TestCase
{
public function testVersion()
{
$response = $this->get(route('version'))->assertStatus(200);
$json = $response->json();
$this->assertArrayHasKey("Service/API", $json);
}
}
Loading

0 comments on commit d74349e

Please sign in to comment.