2018-07-01 11:31:02 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* AttachmentControllerTest.php
|
|
|
|
* Copyright (c) 2018 thegrumpydictator@gmail.com
|
|
|
|
*
|
|
|
|
* This file is part of Firefly III.
|
|
|
|
*
|
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Tests\Api\V1\Controllers;
|
|
|
|
|
|
|
|
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
|
|
|
|
use FireflyIII\Models\Attachment;
|
|
|
|
use FireflyIII\Models\TransactionJournal;
|
|
|
|
use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface;
|
2018-09-04 09:47:01 -05:00
|
|
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
2018-07-01 11:31:02 -05:00
|
|
|
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
|
|
|
use Laravel\Passport\Passport;
|
|
|
|
use Log;
|
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Class AttachmentControllerTest
|
|
|
|
*/
|
|
|
|
class AttachmentControllerTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
Passport::actingAs($this->user());
|
2018-09-03 11:52:46 -05:00
|
|
|
Log::info(sprintf('Now in %s.', \get_class($this)));
|
2018-07-01 11:31:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy account over API.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
|
|
|
|
*/
|
|
|
|
public function testDelete(): void
|
|
|
|
{
|
|
|
|
// mock stuff:
|
2018-12-10 14:45:44 -06:00
|
|
|
$repository = $this->mock(AttachmentRepositoryInterface::class);
|
2018-09-04 09:47:01 -05:00
|
|
|
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
2018-07-01 11:31:02 -05:00
|
|
|
// mock calls:
|
|
|
|
$repository->shouldReceive('setUser')->once();
|
|
|
|
$repository->shouldReceive('destroy')->once()->andReturn(true);
|
|
|
|
|
|
|
|
// get attachment:
|
|
|
|
$attachment = $this->user()->attachments()->first();
|
|
|
|
|
|
|
|
// call API
|
|
|
|
$response = $this->delete('/api/v1/attachments/' . $attachment->id);
|
|
|
|
$response->assertStatus(204);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download attachment
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
|
|
|
|
*/
|
|
|
|
public function testDownload(): void
|
|
|
|
{
|
|
|
|
// mock stuff:
|
2018-12-10 14:45:44 -06:00
|
|
|
$repository = $this->mock(AttachmentRepositoryInterface::class);
|
2018-09-04 09:47:01 -05:00
|
|
|
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
|
|
|
|
2018-12-10 14:45:44 -06:00
|
|
|
$content = 'Attachment content ' . random_int(100, 1000);
|
2018-07-01 11:31:02 -05:00
|
|
|
// mock calls:
|
|
|
|
$repository->shouldReceive('setUser')->once();
|
|
|
|
$repository->shouldReceive('exists')->andReturn(true)->once();
|
|
|
|
$repository->shouldReceive('getContent')->andReturn($content)->once();
|
|
|
|
|
|
|
|
// get attachment:
|
|
|
|
$attachment = $this->user()->attachments()->first();
|
|
|
|
|
|
|
|
// call API
|
|
|
|
$response = $this->get('/api/v1/attachments/' . $attachment->id . '/download');
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertSee($content);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download attachment but file doesn't exist.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
|
|
|
|
*/
|
|
|
|
public function testDownloadNotExisting(): void
|
|
|
|
{
|
|
|
|
// mock stuff:
|
2018-12-10 14:45:44 -06:00
|
|
|
$repository = $this->mock(AttachmentRepositoryInterface::class);
|
2018-09-04 09:47:01 -05:00
|
|
|
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
|
|
|
|
2018-12-10 14:45:44 -06:00
|
|
|
$content = 'Attachment content ' . random_int(100, 1000);
|
2018-07-01 11:31:02 -05:00
|
|
|
// mock calls:
|
|
|
|
$repository->shouldReceive('setUser')->once();
|
|
|
|
$repository->shouldReceive('exists')->andReturn(false)->once();
|
|
|
|
|
|
|
|
// get attachment:
|
|
|
|
$attachment = $this->user()->attachments()->first();
|
|
|
|
|
|
|
|
// call API
|
|
|
|
$response = $this->get('/api/v1/attachments/' . $attachment->id . '/download');
|
|
|
|
|
|
|
|
$response->assertStatus(500);
|
|
|
|
$response->assertSee('Could not find the indicated attachment. The file is no longer there.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-01 11:58:41 -05:00
|
|
|
* Download attachment but no file uploaded
|
2018-07-01 11:31:02 -05:00
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
|
|
|
|
*/
|
|
|
|
public function testDownloadNotUploaded(): void
|
|
|
|
{
|
|
|
|
// mock stuff:
|
2018-12-10 14:45:44 -06:00
|
|
|
$repository = $this->mock(AttachmentRepositoryInterface::class);
|
2018-09-04 09:47:01 -05:00
|
|
|
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
|
|
|
|
2018-07-01 11:31:02 -05:00
|
|
|
// mock calls:
|
|
|
|
$repository->shouldReceive('setUser')->once();
|
|
|
|
|
|
|
|
// create attachment
|
|
|
|
$attachment = Attachment::create(
|
|
|
|
[
|
|
|
|
'user_id' => $this->user()->id,
|
|
|
|
'attachable_id' => 1,
|
|
|
|
'attachable_type' => TransactionJournal::class,
|
2018-07-14 09:41:07 -05:00
|
|
|
'md5' => md5('Hello' . random_int(1, 10000)),
|
2018-07-01 11:31:02 -05:00
|
|
|
'filename' => 'some name',
|
|
|
|
'mime' => 'text/plain',
|
|
|
|
'size' => 5,
|
|
|
|
'uploaded' => false,
|
|
|
|
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
// call API
|
|
|
|
$response = $this->get('/api/v1/attachments/' . $attachment->id . '/download');
|
|
|
|
|
|
|
|
$response->assertStatus(500);
|
|
|
|
$response->assertSee('No file has been uploaded for this attachment (yet).');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List all attachments.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
|
|
|
|
*/
|
|
|
|
public function testIndex(): void
|
|
|
|
{
|
|
|
|
// create stuff
|
|
|
|
$attachments = factory(Attachment::class, 10)->create();
|
|
|
|
|
|
|
|
// mock stuff:
|
2018-12-10 14:45:44 -06:00
|
|
|
$repository = $this->mock(AttachmentRepositoryInterface::class);
|
2018-09-04 09:47:01 -05:00
|
|
|
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
|
|
|
|
2018-07-01 11:31:02 -05:00
|
|
|
|
|
|
|
// mock calls:
|
2018-09-27 06:11:31 -05:00
|
|
|
$repository->shouldReceive('setUser');
|
2018-07-01 11:31:02 -05:00
|
|
|
$repository->shouldReceive('get')->once()->andReturn($attachments);
|
2018-09-27 06:11:31 -05:00
|
|
|
$repository->shouldReceive('getNoteText')->andReturn('Hi There');
|
2018-07-01 11:31:02 -05:00
|
|
|
|
|
|
|
// test API
|
|
|
|
$response = $this->get('/api/v1/attachments');
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertJson(['data' => [],]);
|
|
|
|
$response->assertJson(['meta' => ['pagination' => ['total' => 10, 'count' => 10, 'per_page' => true, 'current_page' => 1, 'total_pages' => 1]],]);
|
|
|
|
$response->assertJson(['links' => ['self' => true, 'first' => true, 'last' => true,],]);
|
|
|
|
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List one attachment.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
|
|
|
|
*/
|
|
|
|
public function testShow(): void
|
|
|
|
{
|
|
|
|
/** @var Attachment $attachment */
|
|
|
|
$attachment = $this->user()->attachments()->first();
|
|
|
|
|
|
|
|
// mock stuff:
|
2018-12-10 14:45:44 -06:00
|
|
|
$repository = $this->mock(AttachmentRepositoryInterface::class);
|
2018-09-04 09:47:01 -05:00
|
|
|
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
|
|
|
|
2018-07-01 11:31:02 -05:00
|
|
|
|
|
|
|
// mock calls:
|
2018-09-27 06:11:31 -05:00
|
|
|
$repository->shouldReceive('setUser');
|
|
|
|
$repository->shouldReceive('getNoteText')->andReturn('Hi There');
|
2018-07-01 11:31:02 -05:00
|
|
|
|
|
|
|
// test API
|
|
|
|
$response = $this->get('/api/v1/attachments/' . $attachment->id);
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertJson(['data' => ['type' => 'attachments', 'links' => true],]);
|
|
|
|
$response->assertSee($attachment->filename); // attachment file name
|
|
|
|
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a new attachment.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
|
2018-08-23 11:33:39 -05:00
|
|
|
* @covers \FireflyIII\Api\V1\Requests\AttachmentRequest
|
2018-07-01 11:31:02 -05:00
|
|
|
*/
|
|
|
|
public function testStore(): void
|
|
|
|
{
|
|
|
|
/** @var Attachment $attachment */
|
|
|
|
$attachment = $this->user()->attachments()->first();
|
|
|
|
|
|
|
|
// mock stuff:
|
2018-12-10 14:45:44 -06:00
|
|
|
$repository = $this->mock(AttachmentRepositoryInterface::class);
|
|
|
|
$journalRepos = $this->mock(JournalRepositoryInterface::class);
|
2018-09-04 09:47:01 -05:00
|
|
|
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
|
|
|
|
2018-07-01 11:31:02 -05:00
|
|
|
|
|
|
|
// mock calls:
|
2018-09-27 06:11:31 -05:00
|
|
|
$repository->shouldReceive('setUser')->atLeast()->once();
|
2018-07-01 11:31:02 -05:00
|
|
|
$repository->shouldReceive('store')->once()->andReturn($attachment);
|
2018-09-27 06:11:31 -05:00
|
|
|
$repository->shouldReceive('getNoteText')->andReturn('Hi There');
|
2018-07-01 11:31:02 -05:00
|
|
|
$journalRepos->shouldReceive('setUser')->once();
|
|
|
|
$journalRepos->shouldReceive('findNull')->once()->andReturn($this->user()->transactionJournals()->find(1));
|
|
|
|
|
|
|
|
// data to submit
|
|
|
|
$data = [
|
|
|
|
'filename' => 'Some new att',
|
2018-07-14 09:41:07 -05:00
|
|
|
'description' => sprintf('Attempt #%d', random_int(1, 10000)),
|
2018-07-01 11:31:02 -05:00
|
|
|
'model' => TransactionJournal::class,
|
|
|
|
'model_id' => 1,
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
// test API
|
|
|
|
$response = $this->post('/api/v1/attachments', $data);
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertJson(['data' => ['type' => 'attachments', 'links' => true],]);
|
|
|
|
$response->assertSee($attachment->filename); // the file name.
|
|
|
|
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an attachment.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
|
|
|
|
* @covers \FireflyIII\Api\V1\Requests\AttachmentRequest
|
|
|
|
*/
|
|
|
|
public function testUpdate(): void
|
|
|
|
{
|
|
|
|
// mock repositories
|
2018-12-10 14:45:44 -06:00
|
|
|
$repository = $this->mock(AttachmentRepositoryInterface::class);
|
2018-09-04 09:47:01 -05:00
|
|
|
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
|
|
|
|
2018-07-01 11:31:02 -05:00
|
|
|
|
|
|
|
/** @var Attachment $attachment */
|
|
|
|
$attachment = $this->user()->attachments()->first();
|
|
|
|
|
|
|
|
// mock calls:
|
2018-09-27 06:11:31 -05:00
|
|
|
$repository->shouldReceive('setUser')->atLeast()->once();
|
2018-07-01 11:31:02 -05:00
|
|
|
$repository->shouldReceive('update')->once()->andReturn($attachment);
|
2018-09-27 06:11:31 -05:00
|
|
|
$repository->shouldReceive('getNoteText')->andReturn('Hi There');
|
2018-07-01 11:31:02 -05:00
|
|
|
// data to submit
|
|
|
|
$data = [
|
|
|
|
'filename' => $attachment->filename,
|
2018-07-14 09:41:07 -05:00
|
|
|
'description' => sprintf('Attempt #%d', random_int(1, 10000)),
|
2018-07-01 11:31:02 -05:00
|
|
|
'model' => TransactionJournal::class,
|
|
|
|
'model_id' => 1,
|
|
|
|
];
|
|
|
|
|
|
|
|
// test API
|
|
|
|
$response = $this->put('/api/v1/attachments/' . $attachment->id, $data, ['Accept' => 'application/json']);
|
|
|
|
$response->assertStatus(200);
|
|
|
|
$response->assertJson(['data' => ['type' => 'attachments', 'links' => true],]);
|
|
|
|
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
|
|
|
$response->assertSee($attachment->description);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-07-01 11:58:41 -05:00
|
|
|
/**
|
|
|
|
* Upload file for attachment.
|
|
|
|
*
|
|
|
|
* @covers \FireflyIII\Api\V1\Controllers\AttachmentController
|
|
|
|
*
|
|
|
|
*/
|
2018-07-01 11:31:02 -05:00
|
|
|
public function testUpload(): void
|
|
|
|
{
|
2018-12-10 14:45:44 -06:00
|
|
|
$repository = $this->mock(AttachmentRepositoryInterface::class);
|
2018-09-04 09:47:01 -05:00
|
|
|
$currencyRepos = $this->mock(CurrencyRepositoryInterface::class);
|
|
|
|
$repository->shouldReceive('setUser')->once();
|
|
|
|
|
|
|
|
|
2018-07-01 11:31:02 -05:00
|
|
|
/** @var Attachment $attachment */
|
|
|
|
$attachment = $this->user()->attachments()->first();
|
2018-07-01 11:58:41 -05:00
|
|
|
$content = 'Hello there';
|
2018-07-01 11:31:02 -05:00
|
|
|
// mock helper:
|
2018-07-01 11:58:41 -05:00
|
|
|
$helper = $this->mock(AttachmentHelperInterface::class);
|
2018-07-01 11:31:02 -05:00
|
|
|
$helper->shouldReceive('saveAttachmentFromApi')->once();
|
|
|
|
|
2018-07-01 11:58:41 -05:00
|
|
|
$response = $this->call('POST', '/api/v1/attachments/' . $attachment->id . '/upload', [], [], [], [], $content);
|
2018-07-01 11:31:02 -05:00
|
|
|
//$response = $this->post('/api/v1/attachments/' . $attachment->id . '/upload',$content, ['Accept' => 'application/json']);
|
|
|
|
$response->assertStatus(204);
|
|
|
|
}
|
2018-07-22 13:33:17 -05:00
|
|
|
}
|