Update UserTest to add login tests and a better structure.
This commit is contained in:
+45
-43
@@ -10,16 +10,21 @@ use App\Models\User;
|
|||||||
|
|
||||||
class UserTest extends TestCase
|
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
|
public function test_register_new_user(): void
|
||||||
{
|
{
|
||||||
$username = 'Oleg';
|
|
||||||
$matrix_username = 'Oleg@oleg.com';
|
|
||||||
$phone = '380671231212';
|
|
||||||
|
|
||||||
$response = $this->post('/api/v2/register', [
|
$response = $this->post('/api/v2/register', [
|
||||||
'username' => $username,
|
'username' => $this->username,
|
||||||
'matrix_username' => $matrix_username,
|
'matrix_username' => $this->matrix_username,
|
||||||
'phone' => $phone,
|
'phone' => $this->phone,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$response->assertStatus(200);
|
$response->assertStatus(200);
|
||||||
@@ -29,45 +34,16 @@ class UserTest extends TestCase
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertDatabaseHas('users', [
|
$this->assertDatabaseHas('users', [
|
||||||
'username' => $username
|
'username' => $this->username
|
||||||
]);
|
|
||||||
|
|
||||||
$user = User::where('username', $username)->first();
|
|
||||||
$user->delete();
|
|
||||||
|
|
||||||
$this->assertDatabaseMissing('users', [
|
|
||||||
'username' => $username
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_register_existing_user(): void
|
public function test_register_existing_user(): void
|
||||||
{
|
{
|
||||||
$username = 'Oleg';
|
|
||||||
$matrix_username = 'Oleg@oleg.com';
|
|
||||||
$phone = '380671231212';
|
|
||||||
|
|
||||||
$response = $this->post('/api/v2/register', [
|
$response = $this->post('/api/v2/register', [
|
||||||
'username' => $username,
|
'username' => $this->username,
|
||||||
'matrix_username' => $matrix_username,
|
'matrix_username' => $this->matrix_username,
|
||||||
'phone' => $phone,
|
'phone' => $this->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->assertStatus(200);
|
||||||
@@ -75,14 +51,40 @@ class UserTest extends TestCase
|
|||||||
$response->assertJson([
|
$response->assertJson([
|
||||||
'error' => 'A user with the username already exists!!!'
|
'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();
|
$user->delete();
|
||||||
|
|
||||||
$this->assertDatabaseMissing('users', [
|
$this->assertDatabaseMissing('users', [
|
||||||
'username' => $username
|
'username' => $this->username
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user