parent
							
								
									ea120c8c9c
								
							
						
					
					
						commit
						17b76707b3
					
				@ -0,0 +1,75 @@
 | 
				
			||||
<?php
 | 
				
			||||
 | 
				
			||||
namespace App\Http\Controllers\API\v2;
 | 
				
			||||
 | 
				
			||||
use App\Models\User;
 | 
				
			||||
use Illuminate\Http\Request;
 | 
				
			||||
 | 
				
			||||
use App\Http\Controllers\Controller;
 | 
				
			||||
 | 
				
			||||
class UserController extends Controller
 | 
				
			||||
{
 | 
				
			||||
    public function register(Request $request)
 | 
				
			||||
    {
 | 
				
			||||
        $username = $request->input('username') ?? '';
 | 
				
			||||
        $matrix_username = $request->input('matrix_username') ?? '';
 | 
				
			||||
        $phone = $request->input('phone') ?? '';
 | 
				
			||||
 | 
				
			||||
        if($username == '') {
 | 
				
			||||
            return response()->json([
 | 
				
			||||
                'error' => 'The username is empty, please, write username!!!'
 | 
				
			||||
            ]);
 | 
				
			||||
        }
 | 
				
			||||
 | 
				
			||||
        $user = User::firstOrCreate(
 | 
				
			||||
            ['username' => $username],
 | 
				
			||||
            // if username is free then the new user will be created
 | 
				
			||||
            // with addition arguments:
 | 
				
			||||
            [
 | 
				
			||||
                'matrix_username' => $matrix_username,
 | 
				
			||||
                'phone' => $phone
 | 
				
			||||
            ]
 | 
				
			||||
        );
 | 
				
			||||
 | 
				
			||||
        if(!$user->wasRecentlyCreated) {
 | 
				
			||||
            return response()->json([
 | 
				
			||||
                'error' => 'A user with the username already exists!!!'
 | 
				
			||||
            ]);
 | 
				
			||||
        }
 | 
				
			||||
 | 
				
			||||
        return response()->json([
 | 
				
			||||
            'ok' => 'A user with the username registered successfully.'
 | 
				
			||||
        ]);
 | 
				
			||||
    }
 | 
				
			||||
 | 
				
			||||
    public function login(Request $request)
 | 
				
			||||
    {
 | 
				
			||||
        $username = $request->input('username') ?? '';
 | 
				
			||||
        $matrix_username = $request->input('matrix_username') ?? '';
 | 
				
			||||
 | 
				
			||||
        if($username == '') {
 | 
				
			||||
            return response()->json([
 | 
				
			||||
                'error' => 'The username is empty, please, write username!!!'
 | 
				
			||||
            ]);
 | 
				
			||||
        }
 | 
				
			||||
 | 
				
			||||
        $user = User::where('username', $username)->first();
 | 
				
			||||
 | 
				
			||||
        if($user) {
 | 
				
			||||
            return response()->json([
 | 
				
			||||
                'error' => 'A user with the username does not exist!!!'
 | 
				
			||||
            ]);
 | 
				
			||||
        }
 | 
				
			||||
 | 
				
			||||
        // Update matrix user if needed
 | 
				
			||||
        // TODO add verification check
 | 
				
			||||
        if($user->matrix_username != $matrix_username) {
 | 
				
			||||
            $user->matrix_username = $matrix_username;
 | 
				
			||||
            $user->save();
 | 
				
			||||
        }
 | 
				
			||||
 | 
				
			||||
        return response()->json([
 | 
				
			||||
            'ok' => 'Login was successful.'
 | 
				
			||||
        ]);
 | 
				
			||||
    }
 | 
				
			||||
}
 | 
				
			||||
@ -0,0 +1,88 @@
 | 
				
			||||
<?php
 | 
				
			||||
 | 
				
			||||
namespace Tests\Feature;
 | 
				
			||||
 | 
				
			||||
use Illuminate\Foundation\Testing\RefreshDatabase;
 | 
				
			||||
use Illuminate\Foundation\Testing\WithFaker;
 | 
				
			||||
use Tests\TestCase;
 | 
				
			||||
 | 
				
			||||
use App\Models\User;
 | 
				
			||||
 | 
				
			||||
class UserTest extends TestCase
 | 
				
			||||
{
 | 
				
			||||
    public function test_register_new_user(): void
 | 
				
			||||
    {
 | 
				
			||||
        $username = 'Oleg';
 | 
				
			||||
        $matrix_username = 'Oleg@oleg.com';
 | 
				
			||||
        $phone = '380671231212';
 | 
				
			||||
 | 
				
			||||
        $response = $this->post('/api/v2/register', [
 | 
				
			||||
            'username' => $username,
 | 
				
			||||
            'matrix_username' => $matrix_username,
 | 
				
			||||
            'phone' => $phone,
 | 
				
			||||
        ]);
 | 
				
			||||
 | 
				
			||||
        $response->assertStatus(200);
 | 
				
			||||
 | 
				
			||||
        $response->assertJson([
 | 
				
			||||
            'ok' => 'A user with the username registered successfully.'
 | 
				
			||||
        ]);
 | 
				
			||||
 | 
				
			||||
        $this->assertDatabaseHas('users', [
 | 
				
			||||
            'username' => $username
 | 
				
			||||
        ]);
 | 
				
			||||
 | 
				
			||||
        $user = User::where('username', $username)->first();
 | 
				
			||||
        $user->delete();
 | 
				
			||||
 | 
				
			||||
        $this->assertDatabaseMissing('users', [
 | 
				
			||||
            'username' => $username
 | 
				
			||||
        ]);
 | 
				
			||||
    }
 | 
				
			||||
 | 
				
			||||
    public function test_register_existing_user(): void
 | 
				
			||||
    {
 | 
				
			||||
        $username = 'Oleg';
 | 
				
			||||
        $matrix_username = 'Oleg@oleg.com';
 | 
				
			||||
        $phone = '380671231212';
 | 
				
			||||
 | 
				
			||||
        $response = $this->post('/api/v2/register', [
 | 
				
			||||
            'username' => $username,
 | 
				
			||||
            'matrix_username' => $matrix_username,
 | 
				
			||||
            'phone' => $phone,
 | 
				
			||||
        ]);
 | 
				
			||||
 | 
				
			||||
        $response->assertStatus(200);
 | 
				
			||||
 | 
				
			||||
        $response->assertJson([
 | 
				
			||||
            'ok' => 'A user with the username registered successfully.'
 | 
				
			||||
        ]);
 | 
				
			||||
 | 
				
			||||
        $this->assertDatabaseHas('users', [
 | 
				
			||||
            'username' => $username
 | 
				
			||||
        ]);
 | 
				
			||||
 | 
				
			||||
        // trying create again
 | 
				
			||||
 | 
				
			||||
        $response = $this->post('/api/v2/register', [
 | 
				
			||||
            'username' => $username,
 | 
				
			||||
            'matrix_username' => $matrix_username,
 | 
				
			||||
            'phone' => $phone,
 | 
				
			||||
        ]);
 | 
				
			||||
 | 
				
			||||
        $response->assertStatus(200);
 | 
				
			||||
 | 
				
			||||
        $response->assertJson([
 | 
				
			||||
            'error' => 'A user with the username already exists!!!'
 | 
				
			||||
        ]);
 | 
				
			||||
 | 
				
			||||
        // removing new user
 | 
				
			||||
 | 
				
			||||
        $user = User::where('username', $username)->first();
 | 
				
			||||
        $user->delete();
 | 
				
			||||
 | 
				
			||||
        $this->assertDatabaseMissing('users', [
 | 
				
			||||
            'username' => $username
 | 
				
			||||
        ]);
 | 
				
			||||
    }
 | 
				
			||||
}
 | 
				
			||||
					Loading…
					
					
				
		Reference in new issue