From 35c3e318c73eff467d0faada9a7d6b36f6615fe4 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Sat, 17 Jun 2023 14:24:13 +0300 Subject: [PATCH] Update UserTest to add login tests and a better structure. --- tests/Feature/UserTest.php | 74 +++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/tests/Feature/UserTest.php b/tests/Feature/UserTest.php index 5a18f99..6ae3d40 100644 --- a/tests/Feature/UserTest.php +++ b/tests/Feature/UserTest.php @@ -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,60 +34,57 @@ 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, + 'username' => $this->username, + 'matrix_username' => $this->matrix_username, + 'phone' => $this->phone, ]); $response->assertStatus(200); $response->assertJson([ - 'ok' => 'A user with the username registered successfully.' + 'error' => 'A user with the username already exists!!!' ]); + } - $this->assertDatabaseHas('users', [ - 'username' => $username - ]); + public function test_login_not_existing_user(): void + { + $response = $this->post('/api/v2/login', $this->not_existing_user); - // trying create again + $response->assertStatus(200); - $response = $this->post('/api/v2/register', [ - 'username' => $username, - 'matrix_username' => $matrix_username, - 'phone' => $phone, + $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([ - 'error' => 'A user with the username already exists!!!' + 'ok' => 'Login was successful.' ]); + } - // removing new user - - $user = User::where('username', $username)->first(); + public function test_removing_user(): void + { + $user = User::where('username', $this->username)->first(); $user->delete(); $this->assertDatabaseMissing('users', [ - 'username' => $username + 'username' => $this->username ]); } }