mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Various API updates.
This commit is contained in:
@@ -59,9 +59,8 @@ class UserControllerTest extends TestCase
|
||||
$userRepository = $this->mock(UserRepositoryInterface::class);
|
||||
$userRepository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
|
||||
$userRepository->shouldReceive('destroy')->once();
|
||||
// create a user first:
|
||||
// call API
|
||||
$response = $this->delete('/api/v1/users/' . $this->user()->id);
|
||||
$response = $this->delete('/api/v1/users/2');
|
||||
$response->assertStatus(204);
|
||||
}
|
||||
|
||||
@@ -81,11 +80,28 @@ class UserControllerTest extends TestCase
|
||||
$user = User::create(['email' => 'some@newu' . random_int(1, 10000) . 'ser.nl', 'password' => 'hello', 'blocked' => 0]);
|
||||
|
||||
// call API
|
||||
$response = $this->delete('/api/v1/users/' . $user->id);
|
||||
$response = $this->delete('/api/v1/users/' . $user->id, [], ['Accept' => 'application/json']);
|
||||
$response->assertStatus(302);
|
||||
$this->assertDatabaseHas('users', ['id' => $user->id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cannot delete yourself.
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\UserController
|
||||
* @covers \FireflyIII\Api\V1\Requests\UserRequest
|
||||
*/
|
||||
public function testDeleteYourself(): void
|
||||
{
|
||||
$userRepository = $this->mock(UserRepositoryInterface::class);
|
||||
$userRepository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->atLeast()->once()->andReturn(true);
|
||||
// create a user first:
|
||||
// call API
|
||||
$response = $this->delete('/api/v1/users/' . $this->user()->id, [], ['Accept' => 'application/json']);
|
||||
$response->assertStatus(500);
|
||||
$response->assertSee('No access to method.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show list of users.
|
||||
*
|
||||
@@ -103,13 +119,11 @@ class UserControllerTest extends TestCase
|
||||
$repository->shouldReceive('all')->withAnyArgs()->andReturn($users)->once();
|
||||
|
||||
// test API
|
||||
$response = $this->get('/api/v1/users');
|
||||
$response = $this->get('/api/v1/users', ['Accept' => 'application/json']);
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(['data' => [],]);
|
||||
$response->assertJson(['meta' => ['pagination' => ['total' => 10, 'count' => 10, 'current_page' => 1, 'total_pages' => 1]],]);
|
||||
$response->assertJson(
|
||||
['links' => ['self' => true, 'first' => true, 'last' => true,],]
|
||||
);
|
||||
$response->assertJson(['links' => ['self' => true, 'first' => true, 'last' => true,],]);
|
||||
$response->assertHeader('Content-Type', 'application/vnd.api+json');
|
||||
}
|
||||
|
||||
@@ -125,7 +139,7 @@ class UserControllerTest extends TestCase
|
||||
$repository->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->once()->andReturn(true);
|
||||
|
||||
// test API
|
||||
$response = $this->get('/api/v1/users/' . $user->id);
|
||||
$response = $this->get('/api/v1/users/' . $user->id, ['Accept' => 'application/json']);
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee($user->email);
|
||||
}
|
||||
@@ -139,8 +153,7 @@ class UserControllerTest extends TestCase
|
||||
public function testStoreBasic(): void
|
||||
{
|
||||
$data = [
|
||||
'email' => 'some_new@user' . random_int(1, 10000) . '.com',
|
||||
'blocked' => 0,
|
||||
'email' => 'some_new@user' . random_int(1, 10000) . '.com',
|
||||
];
|
||||
|
||||
// mock
|
||||
@@ -149,7 +162,32 @@ class UserControllerTest extends TestCase
|
||||
$userRepos->shouldReceive('store')->once()->andReturn($this->user());
|
||||
|
||||
// test API
|
||||
$response = $this->post('/api/v1/users', $data);
|
||||
$response = $this->post('/api/v1/users', $data, ['Content-Type' => 'application/x-www-form-urlencoded']);
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee($this->user()->email);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store new user using JSON.
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\UserController
|
||||
* @covers \FireflyIII\Api\V1\Requests\UserRequest
|
||||
*/
|
||||
public function testStoreBasicJson(): void
|
||||
{
|
||||
$data = [
|
||||
'email' => 'some_new@user' . random_int(1, 10000) . '.com',
|
||||
'blocked' => true,
|
||||
'blocked_code' => 'email_changed',
|
||||
];
|
||||
|
||||
// mock
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true);
|
||||
$userRepos->shouldReceive('store')->once()->andReturn($this->user());
|
||||
|
||||
// test API
|
||||
$response = $this->postJson('/api/v1/users', $data, ['Accept' => 'application/json']);
|
||||
$response->assertStatus(200);
|
||||
$response->assertSee($this->user()->email);
|
||||
}
|
||||
@@ -185,6 +223,37 @@ class UserControllerTest extends TestCase
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store user with info already used.
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\UserController
|
||||
* @covers \FireflyIII\Api\V1\Requests\UserRequest
|
||||
*/
|
||||
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.',
|
||||
],
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update user.
|
||||
*
|
||||
@@ -208,9 +277,35 @@ class UserControllerTest extends TestCase
|
||||
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true);
|
||||
|
||||
// call API
|
||||
$response = $this->put('/api/v1/users/' . $user->id, $data);
|
||||
$response = $this->put('/api/v1/users/' . $user->id, $data, ['Accept' => 'application/json']);
|
||||
$response->assertStatus(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update user.
|
||||
*
|
||||
* @covers \FireflyIII\Api\V1\Controllers\UserController
|
||||
* @covers \FireflyIII\Api\V1\Requests\UserRequest
|
||||
*/
|
||||
public function testUpdateJson(): void
|
||||
{
|
||||
// create a user first:
|
||||
$user = User::create(['email' => 'some@newu' . random_int(1, 10000) . 'ser.nl', 'password' => 'hello', 'blocked' => 0]);
|
||||
|
||||
// data:
|
||||
$data = [
|
||||
'email' => 'some-new@email' . random_int(1, 10000) . '.com',
|
||||
'blocked' => 0,
|
||||
];
|
||||
|
||||
// mock
|
||||
$userRepos = $this->mock(UserRepositoryInterface::class);
|
||||
$userRepos->shouldReceive('update')->once()->andReturn($user);
|
||||
$userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->twice()->andReturn(true);
|
||||
|
||||
// call API
|
||||
$response = $this->putJson('/api/v1/users/' . $user->id, $data, ['Accept' => 'application/json']);
|
||||
$response->assertStatus(200);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user