Update UserTest to add login tests and a better structure.

main
KKlochko 2 years ago
parent 17b76707b3
commit 35c3e318c7

@ -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,60 +34,57 @@ 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->assertStatus(200);
$response->assertJson([ $response->assertJson([
'ok' => 'A user with the username registered successfully.' 'error' => 'A user with the username already exists!!!'
]); ]);
}
$this->assertDatabaseHas('users', [ public function test_login_not_existing_user(): void
'username' => $username {
]); $response = $this->post('/api/v2/login', $this->not_existing_user);
// trying create again $response->assertStatus(200);
$response = $this->post('/api/v2/register', [ $response->assertJson([
'username' => $username, 'error' => 'A user with the username does not exist!!!'
'matrix_username' => $matrix_username, ]);
'phone' => $phone, }
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->assertStatus(200);
$response->assertJson([ $response->assertJson([
'error' => 'A user with the username already exists!!!' 'ok' => 'Login was successful.'
]); ]);
}
// removing new user public function test_removing_user(): void
{
$user = User::where('username', $username)->first(); $user = User::where('username', $this->username)->first();
$user->delete(); $user->delete();
$this->assertDatabaseMissing('users', [ $this->assertDatabaseMissing('users', [
'username' => $username 'username' => $this->username
]); ]);
} }
} }

Loading…
Cancel
Save