firefly-iii/tests/Api/V1/Controllers/UserControllerTest.php

251 lines
9.3 KiB
PHP
Raw Normal View History

2018-03-03 01:12:18 -06:00
<?php
/**
* UserControllerTest.php
2020-02-16 06:59:55 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2018-03-03 01:12:18 -06:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-03-03 01:12:18 -06:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2018-03-03 01:12:18 -06:00
*
* This program is distributed in the hope that it will be useful,
2018-03-03 01:12:18 -06:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2018-03-03 01:12:18 -06:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-03-03 01:12:18 -06:00
*/
declare(strict_types=1);
namespace Tests\Api\V1\Controllers;
2018-03-03 03:15:39 -06:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
2018-12-16 06:55:19 -06:00
use FireflyIII\Transformers\UserTransformer;
2018-03-03 03:15:39 -06:00
use FireflyIII\User;
use Laravel\Passport\Passport;
use Log;
2018-07-12 23:12:39 -05:00
use Mockery;
2018-03-03 01:12:18 -06:00
use Tests\TestCase;
/**
* Class UserControllerTest
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2018-03-03 01:12:18 -06:00
*/
class UserControllerTest extends TestCase
{
2018-03-03 03:15:39 -06:00
/**
*
*/
2018-09-02 13:27:26 -05:00
public function setUp(): void
2018-03-03 03:15:39 -06:00
{
parent::setUp();
Passport::actingAs($this->user());
2020-03-15 09:32:09 -05:00
$this->mockDefaultConfiguration();
2019-04-09 13:05:20 -05:00
Log::info(sprintf('Now in %s.', get_class($this)));
2018-03-03 03:15:39 -06:00
}
/**
2018-07-01 02:27:22 -05:00
* Store new user.
*
* @covers \FireflyIII\Api\V1\Controllers\UserController
* @covers \FireflyIII\Api\V1\Requests\UserStoreRequest
2018-03-03 03:15:39 -06:00
*/
2018-05-11 12:58:10 -05:00
public function testStoreBasic(): void
2018-03-03 03:15:39 -06:00
{
$data = [
2019-06-16 06:16:46 -05:00
'email' => 'some_new@user' . $this->randomInt() . '.com',
2018-12-03 00:18:05 -06:00
];
// mock
$userRepos = $this->mock(UserRepositoryInterface::class);
2018-12-16 06:55:19 -06:00
$transformer = $this->mock(UserTransformer::class);
2018-12-03 00:18:05 -06:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true);
$userRepos->shouldReceive('store')->once()->andReturn($this->user());
2018-12-16 06:55:19 -06:00
// mock transformer
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
2018-12-03 00:18:05 -06:00
// test API
$response = $this->post(route('api.v1.users.store'), $data, ['Content-Type' => 'application/x-www-form-urlencoded']);
2018-12-03 00:18:05 -06:00
$response->assertStatus(200);
}
/**
* Store new user using JSON.
*
* @covers \FireflyIII\Api\V1\Controllers\UserController
* @covers \FireflyIII\Api\V1\Requests\UserStoreRequest
2018-12-03 00:18:05 -06:00
*/
public function testStoreBasicJson(): void
{
$data = [
2019-06-16 06:16:46 -05:00
'email' => 'some_new@user' . $this->randomInt() . '.com',
2018-12-03 00:18:05 -06:00
'blocked' => true,
'blocked_code' => 'email_changed',
2018-03-03 03:15:39 -06:00
];
// mock
$userRepos = $this->mock(UserRepositoryInterface::class);
$transformer = $this->mock(UserTransformer::class);
2018-07-12 23:12:39 -05:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true);
2018-03-03 03:15:39 -06:00
$userRepos->shouldReceive('store')->once()->andReturn($this->user());
2018-12-16 06:55:19 -06:00
// mock transformer
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
2018-03-03 03:15:39 -06:00
// test API
2018-12-03 00:18:05 -06:00
$response = $this->postJson('/api/v1/users', $data, ['Accept' => 'application/json']);
2018-03-03 03:15:39 -06:00
$response->assertStatus(200);
}
/**
2018-07-01 02:27:22 -05:00
* Store user with info already used.
*
* @covers \FireflyIII\Api\V1\Controllers\UserController
* @covers \FireflyIII\Api\V1\Requests\UserStoreRequest
2018-03-03 03:15:39 -06:00
*/
2018-05-11 12:58:10 -05:00
public function testStoreNotUnique(): void
2018-03-03 03:15:39 -06:00
{
$data = [
'email' => $this->user()->email,
'blocked' => 0,
];
// mock
$userRepos = $this->mock(UserRepositoryInterface::class);
2018-07-12 23:12:39 -05:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true);
2018-03-03 03:15:39 -06:00
// test API
$response = $this->post(route('api.v1.users.store'), $data, ['Accept' => 'application/json']);
2018-03-03 03:15:39 -06:00
$response->assertStatus(422);
$response->assertExactJson(
[
'message' => 'The given data was invalid.',
'errors' => [
'email' => [
'The email address has already been taken.',
],
],
]
);
}
2018-12-03 00:18:05 -06:00
/**
* Store user with info already used.
*
* @covers \FireflyIII\Api\V1\Controllers\UserController
* @covers \FireflyIII\Api\V1\Requests\UserStoreRequest
2018-12-03 00:18:05 -06:00
*/
public function testStoreNotUniqueJson(): void
{
$data = [
'email' => $this->user()->email,
'blocked' => 0,
];
// mock
$userRepos = $this->mock(UserRepositoryInterface::class);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true);
// test API
$response = $this->postJson('/api/v1/users', $data, ['Accept' => 'application/json']);
$response->assertStatus(422);
$response->assertExactJson(
[
'message' => 'The given data was invalid.',
'errors' => [
'email' => [
'The email address has already been taken.',
],
],
]
);
}
2018-03-03 03:15:39 -06:00
/**
2018-07-01 02:27:22 -05:00
* Update user.
*
2018-07-01 08:48:02 -05:00
* @covers \FireflyIII\Api\V1\Controllers\UserController
* @covers \FireflyIII\Api\V1\Requests\UserStoreRequest
2018-03-03 03:15:39 -06:00
*/
2018-05-11 12:58:10 -05:00
public function testUpdate(): void
2018-03-03 03:15:39 -06:00
{
// create a user first:
2019-06-16 06:16:46 -05:00
$user = User::create(['email' => 'some@newu' . $this->randomInt() . 'ser.nl', 'password' => 'hello', 'blocked' => 0]);
2018-03-03 03:15:39 -06:00
// data:
$data = [
2019-06-16 06:16:46 -05:00
'email' => 'some-new@email' . $this->randomInt() . '.com',
2018-03-03 03:15:39 -06:00
'blocked' => 0,
];
// mock
$userRepos = $this->mock(UserRepositoryInterface::class);
2018-12-16 06:55:19 -06:00
$transformer = $this->mock(UserTransformer::class);
2018-03-03 03:15:39 -06:00
$userRepos->shouldReceive('update')->once()->andReturn($user);
2018-07-12 23:12:39 -05:00
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true);
2018-03-03 03:15:39 -06:00
2018-12-16 06:55:19 -06:00
// mock transformer
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
2018-03-03 03:15:39 -06:00
// call API
2019-06-09 03:27:11 -05:00
$response = $this->put(route('api.v1.users.update', $user->id), $data, ['Accept' => 'application/json']);
2018-03-03 03:15:39 -06:00
$response->assertStatus(200);
2018-12-03 00:18:05 -06:00
}
2018-03-03 03:15:39 -06:00
2018-12-03 00:18:05 -06:00
/**
* Update user.
*
* @covers \FireflyIII\Api\V1\Controllers\UserController
* @covers \FireflyIII\Api\V1\Requests\UserStoreRequest
2018-12-03 00:18:05 -06:00
*/
public function testUpdateJson(): void
{
// create a user first:
2019-06-16 06:16:46 -05:00
$user = User::create(['email' => 'some@newu' . $this->randomInt() . 'ser.nl', 'password' => 'hello', 'blocked' => 0]);
2018-12-03 00:18:05 -06:00
// data:
$data = [
2019-06-16 06:16:46 -05:00
'email' => 'some-new@email' . $this->randomInt() . '.com',
2018-12-03 00:18:05 -06:00
'blocked' => 0,
];
// mock
$userRepos = $this->mock(UserRepositoryInterface::class);
2018-12-16 06:55:19 -06:00
$transformer = $this->mock(UserTransformer::class);
2018-12-03 00:18:05 -06:00
$userRepos->shouldReceive('update')->once()->andReturn($user);
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true);
2018-12-16 06:55:19 -06:00
// mock transformer
$transformer->shouldReceive('setParameters')->withAnyArgs()->atLeast()->once();
$transformer->shouldReceive('setCurrentScope')->withAnyArgs()->atLeast()->once()->andReturnSelf();
$transformer->shouldReceive('getDefaultIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('getAvailableIncludes')->withAnyArgs()->atLeast()->once()->andReturn([]);
$transformer->shouldReceive('transform')->atLeast()->once()->andReturn(['id' => 5]);
2018-12-03 00:18:05 -06:00
// call API
$response = $this->putJson('/api/v1/users/' . $user->id, $data, ['Accept' => 'application/json']);
$response->assertStatus(200);
2018-03-03 03:15:39 -06:00
}
2018-03-04 08:14:29 -06:00
}