Skip to content

Commit

Permalink
Tests and cchangelog
Browse files Browse the repository at this point in the history
  • Loading branch information
tobytwigger committed Apr 6, 2020
1 parent 94138ff commit ec7d81e
Show file tree
Hide file tree
Showing 2 changed files with 283 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.0.6] - (06/04/2020)

### Added
- Allow admins to upload files on behalf of a user

## [1.0.5] - (04/04/2020)

### Added
Expand Down
278 changes: 278 additions & 0 deletions tests/Http/Controllers/AdminApi/FileControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
namespace BristolSU\Module\Tests\UploadFile\Http\Controllers\AdminApi;

use BristolSU\Module\Tests\UploadFile\TestCase;
use BristolSU\Module\UploadFile\Events\DocumentUploaded;
use BristolSU\Module\UploadFile\Models\File;
use BristolSU\Support\ActivityInstance\ActivityInstance;
use BristolSU\Support\ModuleInstance\Settings\ModuleInstanceSetting;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Storage;

class FileControllerTest extends TestCase
{
Expand Down Expand Up @@ -86,4 +92,276 @@ public function show_returns_the_file(){
'title' => $file->title
]);
}


/** @test */
public function store_allows_a_single_file_to_be_uploaded()
{
ModuleInstanceSetting::create([
'key' => 'allowed_extensions', 'value' => ['jpg', 'png'], 'module_instance_id' => $this->getModuleInstance()->id()
]);
$activityInstance = factory(ActivityInstance::class)->create();

$this->bypassAuthorization();

Storage::fake();
$file = UploadedFile::fake()->create('filename.png', 58, 'image/png');

$response = $this->postJson($this->adminApiUrl('/file'), [
'file' => [$file],
'title' => 'ATitle',
'description' => 'ADescription',
'activity_instance_id' => $activityInstance->id
]);

$response->assertStatus(200);

$this->assertDatabaseHas('uploadfile_files', [
'title' => 'ATitle',
'description' => 'ADescription',
'filename' => 'filename.png',
'mime' => 'image/png',
'uploaded_by' => $this->getControlUser()->id(),
'activity_instance_id' => $activityInstance->id,
'module_instance_id' => $this->getModuleInstance()->id()
]);

$file = File::where('title', 'ATitle')->get()->first();
$this->assertInstanceOf(File::class, $file);

Storage::assertExists($file->path);

}

/** @test */
public function store_returns_the_file_meta_data_as_an_array()
{
ModuleInstanceSetting::create([
'key' => 'allowed_extensions', 'value' => ['jpg', 'png'], 'module_instance_id' => $this->getModuleInstance()->id()
]);

$activityInstance = factory(ActivityInstance::class)->create();

$this->bypassAuthorization();

Storage::fake();
$file = UploadedFile::fake()->create('filename.png', 58, 'image/png');

$response = $this->postJson($this->adminApiUrl('/file'), [
'file' => [$file],
'title' => 'ATitle',
'description' => 'ADescription',
'activity_instance_id' => $activityInstance->id
]);
$response->assertStatus(200);
$response->assertJsonCount(1);

$response->assertJsonFragment([
'title' => 'ATitle',
'description' => 'ADescription',
'filename' => 'filename.png',
'mime' => 'image/png',
'activity_instance_id' => $activityInstance->id
]);
}

/** @test */
public function store_allows_multiple_files_to_be_uploaded()
{
ModuleInstanceSetting::create([
'key' => 'allowed_extensions', 'value' => ['jpg', 'png'], 'module_instance_id' => $this->getModuleInstance()->id()
]);
$activityInstance = factory(ActivityInstance::class)->create();

$this->bypassAuthorization();

Storage::fake();

$file1 = UploadedFile::fake()->create('filename.png', 58, 'image/png');
$file2 = UploadedFile::fake()->create('filename2.png', 58, 'image/png');

$response = $this->postJson($this->adminApiUrl('/file'), [
'file' => [$file1, $file2],
'title' => 'ATitle',
'description' => 'ADescription',
'activity_instance_id' => $activityInstance->id
]);
$response->assertStatus(200);
$response->assertJsonCount(2);
$response->assertJsonFragment([
'title' => 'ATitle',
'description' => 'ADescription',
'filename' => 'filename.png',
'mime' => 'image/png',
'activity_instance_id' => $activityInstance->id
]);
$response->assertJsonFragment([
'title' => 'ATitle',
'description' => 'ADescription',
'filename' => 'filename2.png',
'mime' => 'image/png',
'activity_instance_id' => $activityInstance->id
]);

$this->assertDatabaseHas('uploadfile_files', [
'title' => 'ATitle',
'description' => 'ADescription',
'filename' => 'filename.png',
'mime' => 'image/png',
'uploaded_by' => $this->getControlUser()->id(),
'activity_instance_id' => $activityInstance->id,
'module_instance_id' => $this->getModuleInstance()->id()
]);
$this->assertDatabaseHas('uploadfile_files', [
'title' => 'ATitle',
'description' => 'ADescription',
'filename' => 'filename2.png',
'mime' => 'image/png',
'uploaded_by' => $this->getControlUser()->id(),
'activity_instance_id' => $activityInstance->id,
'module_instance_id' => $this->getModuleInstance()->id()
]);

$files = File::where('title', 'ATitle')->get();
$this->assertCount(2, $files);

$this->assertInstanceOf(File::class, $files[0]);
Storage::assertExists($files[0]->path);
$this->assertInstanceOf(File::class, $files[1]);
Storage::assertExists($files[1]->path);
}

/** @test */
public function store_returns_a_200_if_the_permission_owned()
{
ModuleInstanceSetting::create([
'key' => 'allowed_extensions', 'value' => ['jpg', 'png'], 'module_instance_id' => $this->getModuleInstance()->id()
]);
$activityInstance = factory(ActivityInstance::class)->create();

$this->givePermissionTo('uploadfile.admin.file.store');

Storage::fake();
$file = UploadedFile::fake()->create('filename.png', 58, 'image/png');

$response = $this->postJson($this->adminApiUrl('/file'), [
'file' => [$file],
'title' => 'ATitle',
'description' => 'ADescription',
'activity_instance_id' => $activityInstance->id
]);

$response->assertStatus(200);
}

/** @test */
public function store_returns_a_403_if_the_permission_is_not_owned()
{
ModuleInstanceSetting::create([
'key' => 'allowed_extensions', 'value' => ['jpg', 'png'], 'module_instance_id' => $this->getModuleInstance()->id()
]);
$activityInstance = factory(ActivityInstance::class)->create();

$this->revokePermissionTo('uploadfile.admin.file.store');

Storage::fake();
$file = UploadedFile::fake()->create('filename.png', 58, 'image/png');

$response = $this->postJson($this->adminApiUrl('/file'), [
'file' => [$file],
'title' => 'ATitle',
'description' => 'ADescription',
'activity_instance_id' => $activityInstance->id
]);
$response->assertStatus(403);
}

/** @test */
public function store_fires_an_event_when_a_file_is_uploaded()
{
Event::fake(DocumentUploaded::class);
ModuleInstanceSetting::create([
'key' => 'allowed_extensions', 'value' => ['jpg', 'png'], 'module_instance_id' => $this->getModuleInstance()->id()
]);
$activityInstance = factory(ActivityInstance::class)->create();

$this->bypassAuthorization();

Storage::fake();
$file = UploadedFile::fake()->create('filename.png', 58, 'image/png');

$response = $this->postJson($this->adminApiUrl('/file'), [
'file' => [$file],
'title' => 'ATitle',
'description' => 'ADescription',
'activity_instance_id' => $activityInstance->id
]);
$response->assertStatus(200);

$fileMeta = File::where('title', 'ATitle')->get()->first();
$this->assertInstanceOf(File::class, $fileMeta);

Event::assertDispatched(DocumentUploaded::class, function($event) use ($fileMeta) {
return $event instanceof DocumentUploaded && $event->file->is($fileMeta);
});
}

/** @test */
public function store_fires_an_event_for_each_file_uploaded()
{
Event::fake(DocumentUploaded::class);
ModuleInstanceSetting::create([
'key' => 'allowed_extensions', 'value' => ['jpg', 'png'], 'module_instance_id' => $this->getModuleInstance()->id()
]);
$activityInstance = factory(ActivityInstance::class)->create();

$this->bypassAuthorization();

Storage::fake();

$file1 = UploadedFile::fake()->create('filename.png', 58, 'image/png');
$file2 = UploadedFile::fake()->create('filename2.png', 58, 'image/png');

$response = $this->postJson($this->adminApiUrl('/file'), [
'file' => [$file1, $file2],
'title' => 'ATitle',
'description' => 'ADescription',
'activity_instance_id' => $activityInstance->id
]);
$response->assertStatus(200);

$files = File::where('title', 'ATitle')->get();
$this->assertCount(2, $files);

Event::assertDispatched(DocumentUploaded::class, function($event) use ($files) {
return $event instanceof DocumentUploaded && ($event->file->is($files[0]) || $event->file->is($files[1]));
});
}

/** @test */
public function store_returns_422_if_file_type_not_allowed(){
Event::fake(DocumentUploaded::class);
$activityInstance = factory(ActivityInstance::class)->create();
ModuleInstanceSetting::create([
'key' => 'allowed_extensions', 'value' => [], 'module_instance_id' => $this->getModuleInstance()->id()
]);

$this->bypassAuthorization();

Storage::fake();

$file1 = UploadedFile::fake()->create('filename.png', 58, 'image/png');
$response = $this->postJson($this->adminApiUrl('/file'), [
'file' => [$file1],
'title' => 'ATitle',
'description' => 'ADescription',
'activity_instance_id' => $activityInstance->id
]);

$response->assertStatus(422);

$response->assertJsonValidationErrors(['file.0' => 'file of type:']);

}

}

0 comments on commit ec7d81e

Please sign in to comment.