Skip to content

Commit

Permalink
feat: add user creation without config endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyTWF committed Apr 23, 2024
1 parent 15c2ac1 commit 04eb65c
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 3 deletions.
25 changes: 25 additions & 0 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Exceptions\TooManyTokensException;
use App\Exceptions\UserAlreadyExistsException;
use App\Http\Requests\User\CreateUser;
use App\Models\User\Admin;
use App\Providers\AuthServiceProvider;
use App\Services\UserConfigService;
Expand Down Expand Up @@ -85,6 +86,30 @@ public function banUser(int $cid) : JsonResponse
*/
public function createUserWithPluginConfig(int $cid) : JsonResponse
{
try {
return response()->json(
$this->userService->createUserWithConfig($cid)
)->setStatusCode(201);
} catch (UserAlreadyExistsException $e) {
Log::error('Unable to create user with CID ' . $cid . ', already exists');
return response()->json(
[
'message' => 'User with CID ' . $cid . ' already exists',
]
)->setStatusCode(422);
}
}

/**
* Creates a user with the given CID and also
* returns their plugin config.
*
* @param integer $cid
* @return JsonResponse
*/
public function createUser(CreateUser $request) : JsonResponse
{
$cid = $request->validated('cid');
try {
return response()->json(
$this->userService->createUser($cid)
Expand Down
19 changes: 19 additions & 0 deletions app/Http/Requests/User/CreateUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Requests\User;

use App\Rules\User\VatsimCid;
use Illuminate\Foundation\Http\FormRequest;

class CreateUser extends FormRequest
{
public function rules()
{
return [
'cid' => [
'required',
new VatsimCid(),
],
];
}
}
8 changes: 8 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ function () {
]
)->where('cid', '[0-9]+');

// Create without config
Route::post(
'user',
[
'uses' => 'UserController@createUser',
]
);

// Reactivate user account
Route::put(
'user/{cid}/reactivate',
Expand Down
47 changes: 44 additions & 3 deletions tests/app/Http/Controllers/UserControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
use App\Models\Notification\Notification;
use App\Models\User\Admin;
use App\Models\User\User;
use App\Models\User\UserStatus;
use App\Providers\AuthServiceProvider;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;

class UserControllerTest extends BaseApiTestCase
{
const USER_CREATE_URI = 'user/1203532';
const USER_CREATE_NO_CONFIG_URI = 'user';
const ADMIN_LOGIN_URI = 'admin/login';
const ADMIN_EMAIL = '[email protected]';

Expand All @@ -29,12 +31,51 @@ public function testItConstructs()
}

public function testItRequiresUserAdminScope()
{
$this->regenerateAccessToken([], static::$tokenUser);
$this->makeAuthenticatedApiRequest(self::METHOD_POST, self::USER_CREATE_NO_CONFIG_URI, ['cid' => 800001])->assertStatus(403);
}

public function testCreateUserReturnsCreatedOnSuccess()
{
$this->makeAuthenticatedApiRequest(self::METHOD_POST, self::USER_CREATE_NO_CONFIG_URI, ['cid' => 800001])->assertStatus(201);
}

public function testCreateUserReturnsUnprocessableOnInvalidCid()
{
$this->makeAuthenticatedApiRequest(self::METHOD_POST, self::USER_CREATE_NO_CONFIG_URI, ['cid' => 'abc'])->assertUnprocessable();
}

public function testCreateUserReturnsUnprocessableOnNoCid()
{
$this->makeAuthenticatedApiRequest(self::METHOD_POST, self::USER_CREATE_NO_CONFIG_URI, [])->assertUnprocessable();
}

public function testCreateUserReturnsUnprocessableOnAlreadyExists()
{
$this->makeAuthenticatedApiRequest(self::METHOD_POST, self::USER_CREATE_NO_CONFIG_URI, ['cid' => 800001])->assertStatus(201);
$this->makeAuthenticatedApiRequest(self::METHOD_POST, self::USER_CREATE_NO_CONFIG_URI, ['cid' => 800001])->assertStatus(422);
}

public function testCreatesUser()
{
$this->makeAuthenticatedApiRequest(self::METHOD_POST, self::USER_CREATE_NO_CONFIG_URI, ['cid' => 800001])->assertStatus(201);
$this->assertDatabaseHas(
'user',
[
'id' => 800001,
'status' => UserStatus::ACTIVE
]
);
}

public function testItRequiresUserAdminScopeWithConfig()
{
$this->regenerateAccessToken([], static::$tokenUser);
$this->makeAuthenticatedApiRequest(self::METHOD_POST, self::USER_CREATE_URI)->assertStatus(403);
}

public function testCreateUserReturnsTheCorrectJsonStructure()
public function testCreateWithConfigUserReturnsTheCorrectJsonStructure()
{
$this->makeAuthenticatedApiRequest(self::METHOD_POST, self::USER_CREATE_URI)->assertJsonStructure(
[
Expand All @@ -44,12 +85,12 @@ public function testCreateUserReturnsTheCorrectJsonStructure()
);
}

public function testCreateUserReturnsCreatedOnSuccess()
public function testCreateUserWithConfigReturnsCreatedOnSuccess()
{
$this->makeAuthenticatedApiRequest(self::METHOD_POST, self::USER_CREATE_URI)->assertStatus(201);
}

public function testCreateUserReturnsUnprocessableOnAlreadyExists()
public function testCreateUserWithConfigReturnsUnprocessableOnAlreadyExists()
{
$this->makeAuthenticatedApiRequest(self::METHOD_POST, self::USER_CREATE_URI)->assertStatus(201);
$this->makeAuthenticatedApiRequest(self::METHOD_POST, self::USER_CREATE_URI)->assertStatus(422);
Expand Down

0 comments on commit 04eb65c

Please sign in to comment.