You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.2 KiB

<?php
namespace Tests\Feature;
use App\Http\Controllers\API\v2\UserController;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use Illuminate\Http\Request;
use App\Models\User;
class UserTest extends TestCase
{
protected $username = 'Oleg';
protected $matrix_username = '@oleg:oleg.com';
protected $phone = '380671231212';
protected $not_existing_user = [
'username' => 'Kostia',
'matrix_username' => '@kostia:kostia.com',
];
public function test_register_new_user(): void
{
$response = $this->post('/api/v2/register', [
'username' => $this->username,
'matrix_username' => $this->matrix_username,
'phone' => $this->phone,
]);
$response->assertStatus(200);
$response->assertJson([
'ok' => 'A user with the username registered successfully.'
]);
$this->assertDatabaseHas('users', [
'username' => $this->username
]);
}
public function test_register_existing_user(): void
{
$response = $this->post('/api/v2/register', [
'username' => $this->username,
'matrix_username' => $this->matrix_username,
'phone' => $this->phone,
]);
$response->assertStatus(200);
$response->assertJson([
'error' => 'A user with the username already exists!!!'
]);
}
public function test_login_not_existing_user(): void
{
$response = $this->post('/api/v2/login', $this->not_existing_user);
$response->assertStatus(200);
$response->assertJson([
'error' => 'A user with the username does not exist!!!'
]);
}
public function test_login_existing_user(): void
{
$response = $this->post('/api/v2/login', [
'username' => $this->username,
'matrix_username' => $this->matrix_username,
]);
$response->assertStatus(200);
$response->assertJson([
'ok' => 'Login was successful.'
]);
}
public function test_removing_user(): void
{
$user = User::where('username', $this->username)->first();
$user->delete();
$this->assertDatabaseMissing('users', [
'username' => $this->username
]);
}
/* User validation */
public function test_empty_username_with_matrix_username(): void
{
$json = User::validate_with_matrix_username('');
$this->assertEquals($json['error'], 'The username is empty, please, write username!!!');
}
public function test_not_existing_user_with_matrix_username(): void
{
$matrix_username = '@kostia:test.com';
$json = User::validate_with_matrix_username($matrix_username);
$this->assertEquals($json['error'], 'A user with the username does not exist!!!');
}
public function test_valid_user_with_matrix_username(): void
{
$matrix_username = '@test:test.com';
$json = User::validate_with_matrix_username($matrix_username);
$this->assertEquals($json['ok'], 'A user with the username is valid.');
}
}