Update UserTest to add login tests and a better structure.

This commit is contained in:
2023-06-17 14:24:13 +03:00
parent 17b76707b3
commit 35c3e318c7
+45 -43
View File
@@ -10,16 +10,21 @@ 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
{
$username = 'Oleg';
$matrix_username = 'Oleg@oleg.com';
$phone = '380671231212';
$response = $this->post('/api/v2/register', [
'username' => $username,
'matrix_username' => $matrix_username,
'phone' => $phone,
'username' => $this->username,
'matrix_username' => $this->matrix_username,
'phone' => $this->phone,
]);
$response->assertStatus(200);
@@ -29,45 +34,16 @@ class UserTest extends TestCase
]);
$this->assertDatabaseHas('users', [
'username' => $username
]);
$user = User::where('username', $username)->first();
$user->delete();
$this->assertDatabaseMissing('users', [
'username' => $username
'username' => $this->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,
'username' => $this->username,
'matrix_username' => $this->matrix_username,
'phone' => $this->phone,
]);
$response->assertStatus(200);
@@ -75,14 +51,40 @@ class UserTest extends TestCase
$response->assertJson([
'error' => 'A user with the username already exists!!!'
]);
}
// removing new user
public function test_login_not_existing_user(): void
{
$response = $this->post('/api/v2/login', $this->not_existing_user);
$user = User::where('username', $username)->first();
$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' => $username
'username' => $this->username
]);
}
}