Commit e7d604a9 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

wooo auth, both doors and api

parent 2e35b145
Pipeline #1582 passed with stages
in 1 minute and 38 seconds
<?php
namespace Tests\Unit\Source\UseCases\Users\UpdateUser;
namespace Tests\Doubles;
use Source\Entities\User;
use Source\Gateways\Users\UsersRepository;
class InMemoryUsersRepositoryStub implements UsersRepository {
protected $user;
protected ?User $user;
public function setUserToReturnOnGet(User $user): void {
$this->user = $user;
......
......@@ -4,32 +4,34 @@
namespace Tests\Feature\Api\Users;
use Tests\TestCase;
use Source\Entities\User;
use Source\Gateways\Users\UsersRepository;
use Source\Exceptions\EntityNotFoundException;
use Illuminate\Foundation\Testing\TestResponse;
use Source\Gateways\Users\InMemoryUsersRepository;
use Illuminate\Contracts\Container\BindingResolutionException;
use Tests\Feature\AuthenticatesWithApplicationTestCase;
class CreateUserApiTest extends TestCase {
class CreateUserApiTest extends AuthenticatesWithApplicationTestCase {
protected TestResponse $response;
protected InMemoryUsersRepository $usersRepository;
/**
* @throws BindingResolutionException
* @param array $data
*/
public function setUp(): void {
parent::setUp();
$this->usersRepository = $this->app->make(UsersRepository::class);
protected function handleTest(array $data): void {
$this->response = $this->postJson(
'/users',
array_merge(
['api_token' => $this->authToken],
$data
)
);
}
/**
* @param array $data
* @test
*/
protected function handleTest(array $data): void {
$this->response = $this->postJson('/users', $data);
public function it_denies_unauthorized(): void {
$this->handleTest([]);
$this->response->assertStatus(401);
}
public function invalidUserProvider(): array {
......@@ -166,16 +168,20 @@ class CreateUserApiTest extends TestCase {
* @param array $data
* @param array $message
* @dataProvider invalidUserProvider
* @throws EntityNotFoundException
*/
public function it_tests_the_validation_rules(array $data, array $message): void {
$this->authorize();
$this->handleTest($data);
$this->response->assertJsonFragment($message);
}
/**
* @test
* @throws EntityNotFoundException
*/
public function it_creates_a_user(): void {
$this->authorize();
$this->handleTest(
[
'first_name' => 'first name',
......@@ -197,6 +203,6 @@ class CreateUserApiTest extends TestCase {
]
);
$this->assertCount(1, $this->usersRepository->all());
$this->assertCount(2, $this->usersRepository->all());
}
}
......@@ -4,34 +4,38 @@
namespace Tests\Feature\Api\Users;
use Tests\TestCase;
use Source\Entities\User;
use Source\Gateways\Users\UsersRepository;
use Source\Exceptions\EntityNotFoundException;
use Illuminate\Foundation\Testing\TestResponse;
use Source\Gateways\Users\InMemoryUsersRepository;
use Illuminate\Contracts\Container\BindingResolutionException;
use Tests\Unit\Source\UseCases\Users\UpdateUser\InMemoryUsersRepositoryStub;
use Tests\Feature\AuthenticatesWithApplicationTestCase;
class DeleteUserApiTest extends TestCase {
class DeleteUserApiTest extends AuthenticatesWithApplicationTestCase {
protected TestResponse $response;
protected InMemoryUsersRepository $usersRepository;
protected function handleTest(string $userId): void {
$this->response = $this->deleteJson(
'/users/' . $userId,
[
'api_token' => $this->authToken,
]
);
}
/**
* @throws BindingResolutionException
* @test
*/
public function setUp(): void {
parent::setUp();
public function it_denies_unauthorized(): void {
$this->handleTest('user id');
$this->usersRepository = $this->app->make(UsersRepository::class);
$this->response->assertStatus(401);
}
protected function handleTest(string $userId): void {
$this->response = $this->deleteJson('/users/' . $userId);
}
/** @test */
/**
* @test
* @throws EntityNotFoundException
*/
public function it_deletes_a_non_existent_user(): void {
$this->authorize();
$this->handleTest('asdfasfd non existent user');
$this->response->assertStatus(200);
......@@ -45,8 +49,13 @@ class DeleteUserApiTest extends TestCase {
);
}
/** @test */
/**
* @test
* @throws EntityNotFoundException
*/
public function it_deletes_a_user(): void {
$this->authorize();
$user = new User(69, '', '', '', null, '', '', '', null, null, null);
$this->usersRepository->create($user);
......@@ -61,14 +70,17 @@ class DeleteUserApiTest extends TestCase {
]
);
$this->assertCount(0, $this->usersRepository->all());
$this->assertCount(1, $this->usersRepository->all());
}
/** @test */
/**
* @test
* @throws EntityNotFoundException
*/
public function it_handles_a_failed_delete(): void {
$this->app->bind(UsersRepository::class, static function ($app) {
return new InMemoryUsersRepositoryStub();
});
$this->authorize();
$this->stubUsersRepository();
$this->handleTest('asdf');
$this->response->assertStatus(200);
......@@ -77,7 +89,6 @@ class DeleteUserApiTest extends TestCase {
'message' => 'Unable to delete user.',
'status' => 'error',
'code' => 200,
]
);
}
......
......@@ -4,68 +4,57 @@
namespace Tests\Feature\Api\Users;
use Throwable;
use Tests\TestCase;
use Source\Entities\User;
use Source\Gateways\Users\UsersRepository;
use Source\Exceptions\EntityNotFoundException;
use Illuminate\Foundation\Testing\TestResponse;
use Source\Gateways\Users\InMemoryUsersRepository;
use Illuminate\Contracts\Container\BindingResolutionException;
use Tests\Feature\AuthenticatesWithApplicationTestCase;
class GetAllUsersApiTest extends TestCase {
class GetAllUsersApiTest extends AuthenticatesWithApplicationTestCase {
protected TestResponse $response;
protected InMemoryUsersRepository $usersRepository;
protected function handleTest(): void {
$this->response = $this->getJson('/users?api_token=' . $this->authToken);
}
/**
* @throws BindingResolutionException
* @test
*/
public function setUp(): void {
parent::setUp();
$this->usersRepository = $this->app->make(UsersRepository::class);
}
public function it_denies_unauthorized(): void {
$this->handleTest();
protected function handleTest(): void {
$this->response = $this->getJson('/users');
$this->response->assertStatus(401);
}
/**
* @test
* @throws EntityNotFoundException
*/
public function it_returns_empty_when_no_users_are_added(): void {
$this->authorize();
$this->handleTest();
$this->response->assertStatus(200);
$this->response->assertExactJson(
$this->response->assertJson(
[
'status' => 'success',
'code' => 200,
'users' => []
'code' => 200,
]
);
}
/**
* @test
* @throws EntityNotFoundException
*/
public function it_returns_users_in_the_repository(): void {
$this->authorize();
$user = new User(69, '', '', '', null, '', '', '', null, null, null);
$this->usersRepository->create($user);
$this->handleTest();
$this->response->assertStatus(200);
$this->response->assertJsonCount(1, 'users');
$this->response->assertJsonCount(2, 'users');
$this->response->assertJson(['status' => 'success']);
}
/**
* @throws Throwable
*/
public function tearDown(): void {
parent::tearDown();
$this->usersRepository->clear();
}
}
......@@ -4,34 +4,33 @@
namespace Tests\Feature\Api\Users;
use Throwable;
use Tests\TestCase;
use Source\Entities\User;
use Source\Gateways\Users\UsersRepository;
use Source\Exceptions\EntityNotFoundException;
use Illuminate\Foundation\Testing\TestResponse;
use Source\Gateways\Users\InMemoryUsersRepository;
use Illuminate\Contracts\Container\BindingResolutionException;
use Tests\Feature\AuthenticatesWithApplicationTestCase;
class GetUserApiTest extends TestCase {
class GetUserApiTest extends AuthenticatesWithApplicationTestCase {
protected TestResponse $response;
protected InMemoryUsersRepository $usersRepository;
protected function handleTest(string $userId): void {
$this->response = $this->getJson('/users/' . $userId . '?api_token=' . $this->authToken);
}
/**
* @throws BindingResolutionException
* @test
*/
public function setUp(): void {
parent::setUp();
public function it_denies_unauthorized(): void {
$this->handleTest('asdf');
$this->usersRepository = $this->app->make(UsersRepository::class);
$this->response->assertStatus(401);
}
protected function handleTest(string $userId): void {
$this->response = $this->getJson('/users/' . $userId);
}
/** @test */
/**
* @test
* @throws EntityNotFoundException
*/
public function it_returns_404_on_non_existent_user(): void {
$this->authorize();
$this->handleTest('asdfasdfasdfasd');
$this->response->assertStatus(404);
......@@ -46,22 +45,16 @@ class GetUserApiTest extends TestCase {
/**
* @test
* @throws EntityNotFoundException
*/
public function it_returns_a_user(): void {
$this->authorize();
$user = new User(69, '', '', '', null, '', '', '', null, null, null);
$this->usersRepository->create($user);
$this->handleTest('69');
$this->response->assertStatus(200);
$this->response->assertJson(['status' => 'success']);
}
/**
* @throws Throwable
*/
public function tearDown(): void {
parent::tearDown();
$this->usersRepository->clear();
}
}
......@@ -4,35 +4,33 @@
namespace Tests\Feature\Api\Users;
use Tests\TestCase;
use Source\Entities\User;
use Source\Gateways\Users\UsersRepository;
use Source\Exceptions\EntityNotFoundException;
use Illuminate\Foundation\Testing\TestResponse;
use Source\Gateways\Users\InMemoryUsersRepository;
use Illuminate\Contracts\Container\BindingResolutionException;
use Tests\Feature\AuthenticatesWithApplicationTestCase;
class UpdateUserApiTest extends TestCase {
class UpdateUserApiTest extends AuthenticatesWithApplicationTestCase {
protected TestResponse $response;
protected InMemoryUsersRepository $usersRepository;
/**
* @throws BindingResolutionException
*/
public function setUp(): void {
parent::setUp();
$this->usersRepository = $this->app->make(UsersRepository::class);
}
/**
* @param string $userId
* @param array $data
*/
protected function handleTest(string $userId, array $data): void {
$this->response = $this->putJson('/users/' . $userId, $data);
$this->response = $this->putJson(
'/users/' . $userId,
array_merge(['api_token' => $this->authToken], $data)
);
}
/**
* @test
*/
public function it_denies_unauthorized(): void {
$this->handleTest('radlfj', []);
$this->response->assertStatus(401);
}
public function invalidUserProvider(): array {
$user = new User(0, 'first', 'last', 'display', null, 't@t.com', '', '43252643', null, null, null);
......@@ -154,17 +152,24 @@ class UpdateUserApiTest extends TestCase {
* @param array $data
* @param array $message
* @dataProvider invalidUserProvider
* @throws EntityNotFoundException
*/
public function it_tests_the_validation_rules(array $data, array $message): void {
$this->authorize();
$this->handleTest('69', $data);
$this->response->assertJsonFragment($message);
}
/** @test */
/**
* @test
* @throws EntityNotFoundException
*/
public function it_updates_a_user(): void {
$user = new User(69, '', '', '', null, '', '', '', null, null, null);
$this->usersRepository->create($user);
$this->authorize();
$this->handleTest(
'69',
[
......@@ -189,8 +194,12 @@ class UpdateUserApiTest extends TestCase {
$this->assertEquals($this->usersRepository->get('69')->getFirstName(), 'first name');
}
/** @test */
/**
* @test
* @throws EntityNotFoundException
*/
public function it_cannot_update_non_existient_user(): void {
$this->authorize();
$this->handleTest(
'asdf',
[
......
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Source\Entities\User;
use Source\Entities\Token;
use Source\Gateways\Users\UsersRepository;
use Source\Gateways\Tokens\TokensRepository;
use Source\Exceptions\EntityNotFoundException;
use Tests\Doubles\InMemoryUsersRepositoryStub;
use Source\Gateways\Users\InMemoryUsersRepository;
use Source\Gateways\Tokens\InMemoryTokensRepository;
use Illuminate\Contracts\Container\BindingResolutionException;
class AuthenticatesWithApplicationTestCase extends TestCase {
protected InMemoryUsersRepository $usersRepository;
protected InMemoryTokensRepository $tokens;
protected ?string $authToken = null;
protected ?User $authUser = null;
/**
* @throws BindingResolutionException
*/
public function setUp(): void {
parent::setUp();
$this->usersRepository = $this->app->make(UsersRepository::class);
$this->tokens = $this->app->make(TokensRepository::class);
}
/**
* @throws EntityNotFoundException
*/
protected function authorize(): void {
$this->authUser = new User(1, '', '', '', '', '', '', '', null, null, null);
$this->usersRepository->create($this->authUser);
$this->tokens->create(new Token(1, 1, 'token', 'name'));
$this->authToken = 'token';
}
protected function stubUsersRepository(): void {
$this->app->bind(
UsersRepository::class,
function ($app) {
$new = new InMemoryUsersRepositoryStub();
$new->setUserToReturnOnGet($this->authUser);
return $new;
}
);
}
}
......@@ -9,7 +9,7 @@ use PHPUnit\Framework\TestCase;
use Source\UseCases\Users\DeleteUser\DeleteUser;
use Source\Gateways\Users\InMemoryUsersRepository;
use Source\UseCases\Users\DeleteUser\ResponseModel;
use UseCases\Users\DeleteUser\DeleteFailedException;
use Source\UseCases\Users\DeleteUser\DeleteFailedException;
class UseCaseTest extends TestCase {
protected InMemoryUsersRepository $usersRepository;
......
......@@ -7,6 +7,7 @@ namespace Tests\Unit\Source\UseCases\Users\UpdateUser;
use Source\Entities\User;
use PHPUnit\Framework\TestCase;
use Source\Exceptions\EntityNotFoundException;
use Tests\Doubles\InMemoryUsersRepositoryStub;
use Source\UseCases\Users\UpdateUser\UpdateUser;
use Source\Gateways\Users\InMemoryUsersRepository;
use Source\UseCases\Users\UpdateUser\ResponseModel;
......
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