diff --git a/app/Models/Configuration.php b/app/Models/Configuration.php index 3a0617d92f..8f29c16dc0 100644 --- a/app/Models/Configuration.php +++ b/app/Models/Configuration.php @@ -29,6 +29,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; * Class Configuration. * * @property string $data + * @property string $name */ class Configuration extends Model { diff --git a/routes/api.php b/routes/api.php index 8083a43212..37a33015f7 100644 --- a/routes/api.php +++ b/routes/api.php @@ -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']); } ); diff --git a/tests/Api/V1/Controllers/BudgetLimitControllerTest.php b/tests/Api/V1/Controllers/BudgetLimitControllerTest.php index 1af35c2f91..47a222a80f 100644 --- a/tests/Api/V1/Controllers/BudgetLimitControllerTest.php +++ b/tests/Api/V1/Controllers/BudgetLimitControllerTest.php @@ -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.'); } } \ No newline at end of file diff --git a/tests/Api/V1/Controllers/CategoryControllerTest.php b/tests/Api/V1/Controllers/CategoryControllerTest.php new file mode 100644 index 0000000000..4782bac76c --- /dev/null +++ b/tests/Api/V1/Controllers/CategoryControllerTest.php @@ -0,0 +1,175 @@ +. + */ + +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); + } + + +} \ No newline at end of file diff --git a/tests/Api/V1/Controllers/ConfigurationControllerTest.php b/tests/Api/V1/Controllers/ConfigurationControllerTest.php new file mode 100644 index 0000000000..ab958a1f2f --- /dev/null +++ b/tests/Api/V1/Controllers/ConfigurationControllerTest.php @@ -0,0 +1,185 @@ +. + */ + +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.'); + } + + +} \ No newline at end of file