mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Implement category and configuration tests.
This commit is contained in:
parent
d97fdc3393
commit
3bca9b06f8
@ -29,6 +29,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
* Class Configuration.
|
||||
*
|
||||
* @property string $data
|
||||
* @property string $name
|
||||
*/
|
||||
class Configuration extends Model
|
||||
{
|
||||
|
@ -130,7 +130,7 @@ Route::group(
|
||||
|
||||
// Configuration API routes:
|
||||
Route::get('', ['uses' => 'ConfigurationController@index', 'as' => 'index']);
|
||||
Route::put('', ['uses' => 'ConfigurationController@update', 'as' => 'update']);
|
||||
Route::post('', ['uses' => 'ConfigurationController@update', 'as' => 'update']);
|
||||
}
|
||||
);
|
||||
|
||||
|
@ -249,6 +249,7 @@ class BudgetLimitControllerTest extends TestCase
|
||||
// call API
|
||||
$response = $this->post('/api/v1/budget_limits', $data);
|
||||
$response->assertStatus(500);
|
||||
$response->assertSee('Unknown budget.');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -324,6 +325,7 @@ class BudgetLimitControllerTest extends TestCase
|
||||
// call API
|
||||
$response = $this->put('/api/v1/budget_limits/' . $budgetLimit->id, $data);
|
||||
$response->assertStatus(500);
|
||||
$response->assertSee('Unknown budget.');
|
||||
}
|
||||
|
||||
}
|
175
tests/Api/V1/Controllers/CategoryControllerTest.php
Normal file
175
tests/Api/V1/Controllers/CategoryControllerTest.php
Normal file
@ -0,0 +1,175 @@
|
||||
<?php
|
||||
/**
|
||||
* CategoryControllerTest.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\Models\Budget;
|
||||
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
||||
use Laravel\Passport\Passport;
|
||||
use Log;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class CategoryControllerTest
|
||||
*/
|
||||
class CategoryControllerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Passport::actingAs($this->user());
|
||||
Log::debug(sprintf('Now in %s.', \get_class($this)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a category.
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\CategoryController
|
||||
*/
|
||||
public function testDelete(): void
|
||||
{
|
||||
// mock stuff:
|
||||
$repository = $this->mock(CategoryRepositoryInterface::class);
|
||||
|
||||
// mock calls:
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('destroy')->once()->andReturn(true);
|
||||
|
||||
// get category:
|
||||
$category = $this->user()->categories()->first();
|
||||
|
||||
// call API
|
||||
$response = $this->delete('/api/v1/categories/' . $category->id);
|
||||
$response->assertStatus(204);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show all categories
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\CategoryController
|
||||
*/
|
||||
public function testIndex(): void
|
||||
{
|
||||
$categories = $this->user()->categories()->get();
|
||||
// mock stuff:
|
||||
$repository = $this->mock(CategoryRepositoryInterface::class);
|
||||
|
||||
// mock calls:
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('getCategories')->once()->andReturn($categories);
|
||||
|
||||
// call API
|
||||
$response = $this->get('/api/v1/categories');
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee($categories->first()->name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a single category.
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\CategoryController
|
||||
*/
|
||||
public function testShow(): void
|
||||
{
|
||||
$category = $this->user()->categories()->first();
|
||||
// mock stuff:
|
||||
$repository = $this->mock(CategoryRepositoryInterface::class);
|
||||
|
||||
// mock calls:
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
|
||||
// call API
|
||||
$response = $this->get('/api/v1/categories/' . $category->id);
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee($category->name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a new category.
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\CategoryController
|
||||
*/
|
||||
public function testStore(): void
|
||||
{
|
||||
/** @var Budget $category */
|
||||
$category = $this->user()->categories()->first();
|
||||
|
||||
// mock stuff:
|
||||
$repository = $this->mock(CategoryRepositoryInterface::class);
|
||||
|
||||
// mock calls:
|
||||
$repository->shouldReceive('setUser')->once();
|
||||
$repository->shouldReceive('store')->once()->andReturn($category);
|
||||
|
||||
// data to submit
|
||||
$data = [
|
||||
'name' => 'Some category',
|
||||
'active' => '1',
|
||||
];
|
||||
|
||||
// test API
|
||||
$response = $this->post('/api/v1/categories', $data);
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(['data' => ['type' => 'categories', 'links' => true],]);
|
||||
$response->assertSee($category->name); // the amount
|
||||
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a category.
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\CategoryController
|
||||
*/
|
||||
public function testUpdate(): void
|
||||
{
|
||||
// mock repositories
|
||||
$repository = $this->mock(CategoryRepositoryInterface::class);
|
||||
|
||||
/** @var Budget $category */
|
||||
$category = $this->user()->categories()->first();
|
||||
|
||||
// mock calls:
|
||||
$repository->shouldReceive('setUser');
|
||||
$repository->shouldReceive('update')->once()->andReturn($category);
|
||||
|
||||
// data to submit
|
||||
$data = [
|
||||
'name' => 'Some new category',
|
||||
'active' => '1',
|
||||
];
|
||||
|
||||
// test API
|
||||
$response = $this->put('/api/v1/categories/' . $category->id, $data, ['Accept' => 'application/json']);
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(['data' => ['type' => 'categories', 'links' => true],]);
|
||||
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
||||
$response->assertSee($category->name);
|
||||
}
|
||||
|
||||
|
||||
}
|
185
tests/Api/V1/Controllers/ConfigurationControllerTest.php
Normal file
185
tests/Api/V1/Controllers/ConfigurationControllerTest.php
Normal file
@ -0,0 +1,185 @@
|
||||
<?php
|
||||
/**
|
||||
* ConfigurationControllerTest.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 FireflyConfig;
|
||||
use FireflyIII\Models\Configuration;
|
||||
use Laravel\Passport\Passport;
|
||||
use Log;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class ConfigurationControllerTest
|
||||
*/
|
||||
class ConfigurationControllerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Passport::actingAs($this->user());
|
||||
Log::debug(sprintf('Now in %s.', \get_class($this)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get configuration variables.
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\ConfigurationController
|
||||
*/
|
||||
public function testIndex(): void
|
||||
{
|
||||
$demoConfig = new Configuration;
|
||||
$demoConfig->name = 'is_demo_site';
|
||||
$demoConfig->data = false;
|
||||
|
||||
$permConfig = new Configuration;
|
||||
$permConfig->name = 'permission_update_check';
|
||||
$permConfig->data = -1;
|
||||
|
||||
$lastConfig = new Configuration;
|
||||
$lastConfig->name = 'last_update_check';
|
||||
$lastConfig->data = 123456789;
|
||||
|
||||
$singleConfig = new Configuration;
|
||||
$singleConfig->name = 'single_user_mode';
|
||||
$singleConfig->data = true;
|
||||
|
||||
FireflyConfig::shouldReceive('get')->withArgs(['is_demo_site'])->andReturn($demoConfig)->once();
|
||||
FireflyConfig::shouldReceive('get')->withArgs(['permission_update_check'])->andReturn($permConfig)->once();
|
||||
FireflyConfig::shouldReceive('get')->withArgs(['last_update_check'])->andReturn($lastConfig)->once();
|
||||
FireflyConfig::shouldReceive('get')->withArgs(['single_user_mode'])->andReturn($singleConfig)->once();
|
||||
|
||||
$expected = [
|
||||
'data' => [
|
||||
'is_demo_site' => false,
|
||||
'permission_update_check' => -1,
|
||||
'last_update_check' => 123456789,
|
||||
'single_user_mode' => true,
|
||||
],
|
||||
];
|
||||
|
||||
$response = $this->get('/api/v1/configuration');
|
||||
$response->assertStatus(200);
|
||||
$response->assertExactJson($expected);
|
||||
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get configuration variables.
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\ConfigurationController
|
||||
*/
|
||||
public function testIndexNotOwner(): void
|
||||
{
|
||||
Passport::actingAs($this->emptyUser());
|
||||
$response = $this->get('/api/v1/configuration');
|
||||
$response->assertStatus(500);
|
||||
$response->assertSee('No access to method.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set configuration variables.
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\ConfigurationController
|
||||
*/
|
||||
public function testUpdate(): void
|
||||
{
|
||||
$data = [
|
||||
'name' => 'permission_update_check',
|
||||
'value' => 1,
|
||||
|
||||
];
|
||||
|
||||
$demoConfig = new Configuration;
|
||||
$demoConfig->name = 'is_demo_site';
|
||||
$demoConfig->data = false;
|
||||
|
||||
$permConfig = new Configuration;
|
||||
$permConfig->name = 'permission_update_check';
|
||||
$permConfig->data = -1;
|
||||
|
||||
$lastConfig = new Configuration;
|
||||
$lastConfig->name = 'last_update_check';
|
||||
$lastConfig->data = 123456789;
|
||||
|
||||
$singleConfig = new Configuration;
|
||||
$singleConfig->name = 'single_user_mode';
|
||||
$singleConfig->data = true;
|
||||
|
||||
FireflyConfig::shouldReceive('get')->withArgs(['is_demo_site'])->andReturn($demoConfig)->once();
|
||||
FireflyConfig::shouldReceive('get')->withArgs(['permission_update_check'])->andReturn($permConfig)->once();
|
||||
FireflyConfig::shouldReceive('get')->withArgs(['last_update_check'])->andReturn($lastConfig)->once();
|
||||
FireflyConfig::shouldReceive('get')->withArgs(['single_user_mode'])->andReturn($singleConfig)->once();
|
||||
FireflyConfig::shouldReceive('set')->once()->withArgs(['permission_update_check',1]);
|
||||
|
||||
|
||||
$expected = [
|
||||
'data' => [
|
||||
'is_demo_site' => false,
|
||||
'permission_update_check' => -1,
|
||||
'last_update_check' => 123456789,
|
||||
'single_user_mode' => true,
|
||||
],
|
||||
];
|
||||
|
||||
$response = $this->post('/api/v1/configuration', $data);
|
||||
$response->assertStatus(200);
|
||||
$response->assertExactJson($expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set configuration variable that you're not allowed to change
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\ConfigurationController
|
||||
*/
|
||||
public function testUpdateInvalid(): void
|
||||
{
|
||||
$data = [
|
||||
'name' => 'last_update_check',
|
||||
'value' => 'true',
|
||||
];
|
||||
$response = $this->post('/api/v1/configuration', $data);
|
||||
$response->assertStatus(500);
|
||||
$response->assertSee('You cannot edit this configuration value.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set configuration variables but you're not the owner.
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\ConfigurationController
|
||||
*/
|
||||
public function testUpdateNotOwner(): void
|
||||
{
|
||||
Passport::actingAs($this->emptyUser());
|
||||
$response = $this->post('/api/v1/configuration');
|
||||
$response->assertStatus(500);
|
||||
$response->assertSee('No access to method.');
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user