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

215 lines
6.6 KiB
PHP
Raw Normal View History

2018-03-03 01:12:18 -06:00
<?php
/**
* UserControllerTest.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;
2018-03-03 03:15:39 -06:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
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
*/
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());
Log::debug(sprintf('Now in %s.', \get_class($this)));
2018-03-03 03:15:39 -06:00
}
/**
* Delete a user.
*
2018-07-01 02:27:22 -05:00
* @covers \FireflyIII\Api\V1\Controllers\UserController
2018-03-03 03:15:39 -06:00
* @covers \FireflyIII\Api\V1\Requests\UserRequest
*/
2018-05-11 12:58:10 -05:00
public function testDelete(): void
2018-03-03 03:15:39 -06:00
{
// create a user first:
$user = User::create(['email' => 'some@newu' . random_int(1, 10000) . 'ser.nl', 'password' => 'hello', 'blocked' => 0]);
2018-03-03 03:15:39 -06:00
// call API
$response = $this->delete('/api/v1/users/' . $user->id);
$response->assertStatus(204);
$this->assertDatabaseMissing('users', ['id' => $user->id]);
}
/**
* Delete a user as non admin
*
2018-07-01 02:27:22 -05:00
* @covers \FireflyIII\Api\V1\Controllers\UserController
2018-03-03 03:15:39 -06:00
* @covers \FireflyIII\Api\V1\Requests\UserRequest
*/
2018-05-11 12:58:10 -05:00
public function testDeleteNoAdmin(): void
2018-03-03 03:15:39 -06:00
{
Passport::actingAs($this->emptyUser());
// create a user first:
$user = User::create(['email' => 'some@newu' . random_int(1, 10000) . 'ser.nl', 'password' => 'hello', 'blocked' => 0]);
2018-03-03 03:15:39 -06:00
// call API
$response = $this->delete('/api/v1/users/' . $user->id);
$response->assertStatus(302);
$this->assertDatabaseHas('users', ['id' => $user->id]);
}
/**
2018-07-01 02:27:22 -05:00
* Show list of users.
*
* @covers \FireflyIII\Api\V1\Controllers\UserController
2018-03-03 03:15:39 -06:00
*/
2018-05-11 12:58:10 -05:00
public function testIndex(): void
2018-03-03 03:15:39 -06:00
{
// create stuff
$users = factory(User::class, 10)->create();
// mock stuff:
$repository = $this->mock(UserRepositoryInterface::class);
2018-07-12 23:12:39 -05:00
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->once()->andReturn(true);
2018-03-03 03:15:39 -06:00
// mock calls:
$repository->shouldReceive('all')->withAnyArgs()->andReturn($users)->once();
// test API
$response = $this->get('/api/v1/users');
$response->assertStatus(200);
$response->assertJson(['data' => [],]);
2018-03-03 07:24:06 -06:00
$response->assertJson(['meta' => ['pagination' => ['total' => 10, 'count' => 10, 'current_page' => 1, 'total_pages' => 1]],]);
2018-03-03 03:15:39 -06:00
$response->assertJson(
['links' => ['self' => true, 'first' => true, 'last' => true,],]
);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
2018-07-01 02:27:22 -05:00
* Show single user.
*
* @covers \FireflyIII\Api\V1\Controllers\UserController
2018-03-03 03:15:39 -06:00
*/
2018-05-11 12:58:10 -05:00
public function testShow(): void
2018-03-03 03:15:39 -06:00
{
2018-07-12 23:12:39 -05:00
$user = User::first();
$repository = $this->mock(UserRepositoryInterface::class);
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->once()->andReturn(true);
2018-03-03 03:15:39 -06:00
// test API
$response = $this->get('/api/v1/users/' . $user->id);
$response->assertStatus(200);
$response->assertSee($user->email);
}
/**
2018-07-01 02:27:22 -05:00
* Store new user.
*
* @covers \FireflyIII\Api\V1\Controllers\UserController
2018-03-03 03:15:39 -06:00
* @covers \FireflyIII\Api\V1\Requests\UserRequest
*/
2018-05-11 12:58:10 -05:00
public function testStoreBasic(): void
2018-03-03 03:15:39 -06:00
{
$data = [
'email' => 'some_new@user' . random_int(1, 10000) . '.com',
2018-03-03 03:15:39 -06:00
'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
$userRepos->shouldReceive('store')->once()->andReturn($this->user());
// test API
$response = $this->post('/api/v1/users', $data);
$response->assertStatus(200);
$response->assertSee($this->user()->email);
}
/**
2018-07-01 02:27:22 -05:00
* Store user with info already used.
*
* @covers \FireflyIII\Api\V1\Controllers\UserController
2018-03-03 03:15:39 -06:00
* @covers \FireflyIII\Api\V1\Requests\UserRequest
*/
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('/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-07-01 02:27:22 -05:00
* Update user.
*
2018-07-01 08:48:02 -05:00
* @covers \FireflyIII\Api\V1\Controllers\UserController
2018-03-03 03:15:39 -06:00
* @covers \FireflyIII\Api\V1\Requests\UserRequest
*/
2018-05-11 12:58:10 -05:00
public function testUpdate(): void
2018-03-03 03:15:39 -06:00
{
// create a user first:
$user = User::create(['email' => 'some@newu' . random_int(1, 10000) . 'ser.nl', 'password' => 'hello', 'blocked' => 0]);
2018-03-03 03:15:39 -06:00
// data:
$data = [
'email' => 'some-new@email' . random_int(1, 10000) . '.com',
2018-03-03 03:15:39 -06:00
'blocked' => 0,
];
// mock
$userRepos = $this->mock(UserRepositoryInterface::class);
$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
// call API
$response = $this->put('/api/v1/users/' . $user->id, $data);
$response->assertStatus(200);
}
2018-03-04 08:14:29 -06:00
}