Skip to content
This repository was archived by the owner on Feb 18, 2023. It is now read-only.

Commit 0e477b4

Browse files
authored
Merge pull request #82 from joselfonseca/feature/77-user-registration
User Registration endpoint
2 parents c5b4133 + b773698 commit 0e477b4

File tree

10 files changed

+390
-5
lines changed

10 files changed

+390
-5
lines changed

.github/workflows/run-tests.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ jobs:
3737
php artisan passport:keys
3838
- name: Execute tests
3939
run: vendor/bin/phpunit
40+
41+
- name: Test Code Style php-cs-fixer
42+
run: PHP_CS_FIXER_IGNORE_ENV=true vendor/bin/php-cs-fixer fix --config=.php_cs.dist -v --dry-run --stop-on-violation --using-cache=no
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api\Auth;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Models\User;
7+
use App\Transformers\Users\UserTransformer;
8+
use Illuminate\Auth\Events\Registered;
9+
use Illuminate\Http\Request;
10+
11+
class RegisterController extends Controller
12+
{
13+
protected $model;
14+
15+
public function __construct(User $model)
16+
{
17+
$this->model = $model;
18+
}
19+
20+
public function store(Request $request)
21+
{
22+
$this->validate($request, [
23+
'name' => 'required',
24+
'email' => 'required|email|unique:users,email',
25+
'password' => 'required|min:8|confirmed',
26+
]);
27+
$user = $this->model->create($request->all());
28+
$user->assignRole('User');
29+
event(new Registered($user));
30+
31+
return fractal($user, new UserTransformer())->respond(201);
32+
}
33+
}

database/seeders/Users/RoleTableSeeder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class RoleTableSeeder extends Seeder
1313
*/
1414
public $roles = [
1515
['name' => 'Administrator'],
16+
['name' => 'User'],
1617
];
1718

1819
/**

docs/api/apiblueprint.apib

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ The API uses conventional HTTP response codes to indicate the success or failure
4343
# Data Structures
4444

4545
<!-- include(dataStructures/errors.apib) -->
46+
<!-- include(dataStructures/auth.apib) -->
4647
<!-- include(dataStructures/users.apib) -->
4748
<!-- include(dataStructures/roles.apib) -->
4849
<!-- include(dataStructures/permissions.apib) -->
4950
<!-- include(dataStructures/assets.apib) -->
5051

52+
<!-- include(routes/auth.apib) -->
5153
<!-- include(routes/users.apib) -->
5254
<!-- include(routes/assets.apib) -->
5355
### Asset Object (object)
@@ -59,7 +61,12 @@ The API uses conventional HTTP response codes to indicate the success or failure
5961
+ full: `https://laravelapi.test/api/assets/0c244c51-0a3b-4b86-829a-ee161c2f966f/render` (string) - The asset link for render full size
6062
+ thumb: `https://laravelapi.test/api/assets/0c244c51-0a3b-4b86-829a-ee161c2f966f/render?width=200&height=200` (string) - The asset link for render thumb size
6163
+ created_at : `1997-07-16T19:20:30+01:00` (string) - Date in format iso 8601
62-
## Error 404 (object)
64+
### Registration input (object)
65+
- name: `Jose Fonseca`(string, required) - The name of the user
66+
- email: `[email protected]` (string, required) - The email of the user
67+
- password: `Password123**` (string, required) - The password of the user
68+
- password_confirmation: `Password123**` (string, required) - The password confirmation of the user
69+
## Error 404 (object)
6370
- message: `404 Not found` (string)
6471
- status_code: 404 (number) `status code number`
6572

@@ -216,7 +223,41 @@ In the body of the requests you can send the raw binary data in base 64 encoded
216223
{
217224
"code": 413,
218225
"message": "The body is too large"
219-
}
226+
}# Group Auth
227+
228+
The auth API will allow you to work with the users registration and password management.
229+
230+
## Register [/api/register]
231+
Use this endpoint to register a new user from the client consuming the API.
232+
233+
### Register user [POST]
234+
This endpoint will allow you to handle the user registration in the API
235+
236+
+ Request (application/json)
237+
238+
+ Attributes (Registration input)
239+
240+
+ Response 201 (application/json)
241+
242+
+ Attributes
243+
+ data (User Object)
244+
245+
+ Response 422 (application/json)
246+
247+
+ Attributes (Error 422)
248+
249+
+ Response 401 (application/json)
250+
251+
+ Attributes (Error 401)
252+
253+
+ Response 403 (application/json)
254+
255+
+ Attributes (Error 403)
256+
257+
+ Response 404 (application/json)
258+
259+
+ Attributes (Error 404)
260+
220261
## Permissions resource [/api/permissions]
221262
It requires your user to have permissions to fetch, create, update or delete roles in the system depending on the request you want to make
222263

docs/api/blueprint/apidocs.apib

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ The API uses conventional HTTP response codes to indicate the success or failure
4343
# Data Structures
4444

4545
<!-- include(dataStructures/errors.apib) -->
46+
<!-- include(dataStructures/auth.apib) -->
4647
<!-- include(dataStructures/users.apib) -->
4748
<!-- include(dataStructures/roles.apib) -->
4849
<!-- include(dataStructures/permissions.apib) -->
4950
<!-- include(dataStructures/assets.apib) -->
5051

52+
<!-- include(routes/auth.apib) -->
5153
<!-- include(routes/users.apib) -->
5254
<!-- include(routes/assets.apib) -->
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
### Registration input (object)
2+
- name: `Jose Fonseca`(string, required) - The name of the user
3+
- email: `[email protected]` (string, required) - The email of the user
4+
- password: `Password123**` (string, required) - The password of the user
5+
- password_confirmation: `Password123**` (string, required) - The password confirmation of the user
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Group Auth
2+
3+
The auth API will allow you to work with the users registration and password management.
4+
5+
## Register [/api/register]
6+
Use this endpoint to register a new user from the client consuming the API.
7+
8+
### Register user [POST]
9+
This endpoint will allow you to handle the user registration in the API
10+
11+
+ Request (application/json)
12+
13+
+ Attributes (Registration input)
14+
15+
+ Response 201 (application/json)
16+
17+
+ Attributes
18+
+ data (User Object)
19+
20+
+ Response 422 (application/json)
21+
22+
+ Attributes (Error 422)
23+
24+
+ Response 401 (application/json)
25+
26+
+ Attributes (Error 401)
27+
28+
+ Response 403 (application/json)
29+
30+
+ Attributes (Error 403)
31+
32+
+ Response 404 (application/json)
33+
34+
+ Attributes (Error 404)

resources/views/apidocs.blade.php

Lines changed: 193 additions & 3 deletions
Large diffs are not rendered by default.

routes/api.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
Route::get('assets/{uuid}/render', 'Api\Assets\RenderFileController@show');
66

7+
Route::post('register', 'Api\Auth\RegisterController@store');
8+
79
Route::group(['middleware' => ['auth:api']], function () {
810
Route::group(['prefix' => 'users'], function () {
911
Route::get('/', 'Api\Users\UsersController@index');
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Tests\Feature\Auth;
4+
5+
use App\Models\User;
6+
use Illuminate\Auth\Events\Registered;
7+
use Illuminate\Foundation\Testing\RefreshDatabase;
8+
use Illuminate\Support\Facades\Event;
9+
use Spatie\Permission\PermissionRegistrar;
10+
use Tests\TestCase;
11+
12+
class RegisterTest extends TestCase
13+
{
14+
use RefreshDatabase;
15+
16+
public function setUp() : void
17+
{
18+
parent::setUp();
19+
$this->seed();
20+
$this->app->make(PermissionRegistrar::class)->registerPermissions();
21+
}
22+
23+
public function test_it_register_user_with_role()
24+
{
25+
Event::fake([Registered::class]);
26+
$response = $this->json('POST', 'api/register/', [
27+
'name' => 'John Doe',
28+
'email' => '[email protected]',
29+
'password' => '12345678',
30+
'password_confirmation' => '12345678',
31+
]);
32+
$response->assertStatus(201);
33+
$this->assertDatabaseHas('users', [
34+
'name' => 'John Doe',
35+
'email' => '[email protected]',
36+
]);
37+
$user = User::where('email', '[email protected]')->first();
38+
$this->assertTrue($user->hasRole('User'));
39+
Event::assertDispatched(Registered::class, function ($event) use ($user) {
40+
return $user->id === $event->user->id;
41+
});
42+
}
43+
44+
public function test_it_validates_input_for_registration()
45+
{
46+
Event::fake([Registered::class]);
47+
$response = $this->json('POST', 'api/register', [
48+
'name' => 'Some User',
49+
'email' => '[email protected]',
50+
'password' => '123456789qq',
51+
]);
52+
$response->assertStatus(422);
53+
$this->assertDatabaseMissing('users', [
54+
'name' => 'Some User',
55+
'email' => '[email protected]',
56+
]);
57+
Event::assertNotDispatched(Registered::class);
58+
}
59+
60+
public function test_it_returns_422_on_validation_error()
61+
{
62+
Event::fake([Registered::class]);
63+
$response = $this->json('POST', 'api/register', [
64+
'name' => 'Some User',
65+
]);
66+
$response->assertStatus(422);
67+
$this->assertEquals('{"message":"The given data was invalid.","status_code":422,"errors":{"email":["The email field is required."],"password":["The password field is required."]}}', $response->getContent());
68+
$this->assertDatabaseMissing('users', [
69+
'name' => 'Some User',
70+
'email' => '[email protected]',
71+
]);
72+
Event::assertNotDispatched(Registered::class);
73+
}
74+
}

0 commit comments

Comments
 (0)