Commit 98dd8984 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

no deleting admin user

parent 50a0616f
Pipeline #2263 canceled with stages
in 1 minute and 12 seconds
......@@ -138,6 +138,7 @@ class UsersController extends ApiController
try {
$deleteUser->delete($userId, $presenter);
} catch (DeleteFailedException $e) {
$this->setStatusCode(400);
return $this->respondWithError($e->getMessage());
}
......
......@@ -23,8 +23,8 @@ class UsersSeeder extends Seeder
$user = $users->create(new User(
0,
'Admin',
'Admin',
'admin',
'',
'Admin User',
null,
'admin@admin.user',
......
......@@ -4,6 +4,7 @@ namespace Source\UseCases\Users\DeleteUser;
use Source\Gateways\Users\UsersRepository;
use Source\Exceptions\DeleteFailedException;
use Source\Exceptions\EntityNotFoundException;
class DeleteUser implements DeleteUserUseCase
{
......@@ -22,6 +23,16 @@ class DeleteUser implements DeleteUserUseCase
*/
public function delete(string $userId, Presenter $presenter): void
{
$user = $this->usersRepository->get($userId);
if (!$user) {
throw new EntityNotFoundException();
}
if ($user->getFirstName() === 'admin') {
throw new DeleteFailedException('Cannot delete super admin user.');
}
if (!$this->usersRepository->delete($userId)) {
throw new DeleteFailedException('Unable to delete user.');
}
......
......@@ -9,6 +9,7 @@ interface DeleteUserUseCase
* @param string $userId
* @param \Source\UseCases\Users\DeleteUser\Presenter $presenter
* @throws \Source\Exceptions\DeleteFailedException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function delete(string $userId, Presenter $presenter): void;
}
......@@ -49,20 +49,12 @@ class DeleteUserApiTest extends AuthenticatesWithApplicationTestCase
* @test
* @throws EntityNotFoundException
*/
public function it_deletes_a_non_existent_user(): void
public function it_does_not_delete_a_non_existent_user(): void
{
$this->authenticate();
$this->handleTest('asdfasfd non existent user');
$this->response->assertStatus(200);
$this->response->assertJson(
[
'message' => 'User deleted.',
'status' => 'success',
'code' => 200,
]
);
$this->response->assertStatus(404);
}
/**
......@@ -79,14 +71,11 @@ class DeleteUserApiTest extends AuthenticatesWithApplicationTestCase
$this->handleTest('69');
$this->response->assertStatus(200);
$this->response->assertJson(
[
'message' => 'User deleted.',
'status' => 'success',
'code' => 200,
]
);
$this->response->assertJson([
'message' => 'User deleted.',
'status' => 'success',
'code' => 200,
]);
$this->assertCount(1, $this->usersRepository->all());
}
......@@ -101,13 +90,11 @@ class DeleteUserApiTest extends AuthenticatesWithApplicationTestCase
$this->stubUsersRepository();
$this->handleTest('asdf');
$this->response->assertStatus(200);
$this->response->assertJson(
[
'message' => 'Unable to delete user.',
'status' => 'error',
'code' => 200,
]
);
$this->response->assertStatus(400);
$this->response->assertJson([
'message' => 'Unable to delete user.',
'status' => 'error',
'code' => 400,
]);
}
}
......@@ -32,6 +32,7 @@ class UseCaseTest extends TestCase
/**
* @param string $userId
* @throws DeleteFailedException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function handleTest(string $userId): void
{
......@@ -43,10 +44,13 @@ class UseCaseTest extends TestCase
/**
* @test
* @throws DeleteFailedException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function presenter_was_called(): void
{
$this->handleTest('123');
$user = new User(420, '', '', '', null, '', '', '', null, null, null);
$this->usersRepository->create($user);
$this->handleTest('420');
$this->assertTrue($this->presenter->wasPresenterCalled());
}
......@@ -54,6 +58,7 @@ class UseCaseTest extends TestCase
/**
* @test
* @throws DeleteFailedException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_deletes_a_user(): void
{
......@@ -68,4 +73,17 @@ class UseCaseTest extends TestCase
$this->assertCount(1, $this->usersRepository->all());
$this->assertEquals('420', $this->usersRepository->get('420')->getId());
}
/**
* @test
* @throws \Source\Exceptions\DeleteFailedException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_wont_delete_admin_user(): void
{
$admin = new User(1, 'admin', '', '', null, '',null, null);
$this->usersRepository->create($admin);
$this->expectException(DeleteFailedException::class);
$this->handleTest('1');
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment