-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from IT-Academy-BCN/feature/register
Add tests to store user feature
- Loading branch information
Showing
4 changed files
with
253 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,249 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use App\Models\User; | ||
use Tests\TestCase; | ||
|
||
class RegisterTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
/** | ||
* Test basic store user | ||
*/ | ||
public function test_store_with_valid_data(): void | ||
{ | ||
$userData = User::factory()->makeOne(['dni' => '48042812K']); | ||
|
||
$response = $this->post('/api/register', [ | ||
'name' => $userData['name'], | ||
'email' => $userData['email'], | ||
'dni' => $userData['dni'], | ||
'password' => $userData['password'], | ||
'password_confirmation' => $userData['password'], | ||
]); | ||
|
||
$response->assertStatus(200) | ||
->assertJson([ | ||
'status' => true, | ||
]); | ||
|
||
$this->assertDatabaseHas('users', [ | ||
'email' => $userData['email'], | ||
'name' => $userData['name'], | ||
'dni' => $userData['dni'], | ||
'status' => 'ACTIVE', | ||
'role' => 'ADMIN', | ||
]); | ||
} | ||
|
||
/** | ||
* Test store user without name | ||
*/ | ||
public function test_store_with_valid_data_and_without_name(): void | ||
{ | ||
$userData = User::factory()->makeOne(['dni' => '35983746Q']); | ||
|
||
$response = $this->post('/api/register', [ | ||
'email' => $userData['email'], | ||
'dni' => $userData['dni'], | ||
'password' => $userData['password'], | ||
'password_confirmation' => $userData['password'], | ||
]); | ||
|
||
$response->assertStatus(200) | ||
->assertJson([ | ||
'status' => true, | ||
]); | ||
|
||
$this->assertDatabaseHas('users', [ | ||
'email' => $userData['email'], | ||
'dni' => $userData['dni'], | ||
'status' => 'ACTIVE', | ||
'role' => 'ADMIN', | ||
]); | ||
} | ||
|
||
/** | ||
* Test store user with invalid DNI | ||
*/ | ||
public function test_store_with_invalid_dni(): void | ||
{ | ||
$userData = User::factory()->makeOne(['dni' => '35983747Q']); | ||
|
||
$response = $this->post('/api/register', [ | ||
'name' => $userData['name'], | ||
'email' => $userData['email'], | ||
'dni' => $userData['dni'], | ||
'password' => $userData['password'], | ||
'password_confirmation' => $userData['password'], | ||
]); | ||
|
||
$response->assertStatus(200) | ||
->assertJson([ | ||
'status' => false, | ||
]); | ||
} | ||
|
||
|
||
/** | ||
* Test store user with valid NIE | ||
*/ | ||
public function test_store_with_valid_nie(): void | ||
{ | ||
$userData = User::factory()->makeOne(['dni' => 'Z6383416R']); | ||
|
||
$response = $this->post('/api/register', [ | ||
'name' => $userData['name'], | ||
'email' => $userData['email'], | ||
'dni' => $userData['dni'], | ||
'password' => $userData['password'], | ||
'password_confirmation' => $userData['password'], | ||
]); | ||
|
||
$response->assertStatus(200) | ||
->assertJson([ | ||
'status' => true, | ||
]); | ||
} | ||
|
||
/** | ||
* Test store user with invalid NIE | ||
*/ | ||
public function test_store_with_invalid_nie(): void | ||
{ | ||
$userData = User::factory()->makeOne(['dni' => 'Z6383416Q']); | ||
|
||
$response = $this->post('/api/register', [ | ||
'name' => $userData['name'], | ||
'email' => $userData['email'], | ||
'dni' => $userData['dni'], | ||
'password' => $userData['password'], | ||
'password_confirmation' => $userData['password'], | ||
]); | ||
|
||
$response->assertStatus(200) | ||
->assertJson([ | ||
'status' => false, | ||
]); | ||
} | ||
|
||
/** | ||
* Test store user without obligatory fields | ||
*/ | ||
public function test_store_with_obligatory_fields(): void | ||
{ | ||
$userData = User::factory()->makeOne(['dni' => '36372839H']); | ||
|
||
$response = $this->post('/api/register', [ | ||
'dni' => $userData['dni'], | ||
'password' => $userData['password'], | ||
'password_confirmation' => $userData['password'], | ||
]); | ||
|
||
$response->assertStatus(200) | ||
->assertJson([ | ||
'status' => false, | ||
]); | ||
|
||
$response = $this->post('/api/register', [ | ||
'email' => $userData['email'], | ||
'password' => $userData['password'], | ||
'password_confirmation' => $userData['password'], | ||
]); | ||
|
||
$response->assertStatus(200) | ||
->assertJson([ | ||
'status' => false, | ||
]); | ||
|
||
|
||
$response = $this->post('/api/register', [ | ||
'email' => $userData['email'], | ||
'dni' => $userData['dni'], | ||
'password' => $userData['password'], | ||
]); | ||
|
||
$response->assertStatus(200) | ||
->assertJson([ | ||
'status' => false, | ||
]); | ||
} | ||
|
||
/** | ||
* Test store user with short password | ||
*/ | ||
public function test_store_with_short_password(): void | ||
{ | ||
$userData = User::factory()->makeOne(['dni' => '89137481S', 'password' => 'pas']); | ||
|
||
$response = $this->post('/api/register', [ | ||
'name' => $userData['name'], | ||
'email' => $userData['email'], | ||
'dni' => $userData['dni'], | ||
'password' => $userData['password'], | ||
'password_confirmation' => $userData['password'], | ||
]); | ||
|
||
$response->assertStatus(200) | ||
->assertJson([ | ||
'status' => false, | ||
]); | ||
} | ||
|
||
/** | ||
* Test store user with taken email | ||
*/ | ||
public function test_store_with_taken_email(): void | ||
{ | ||
$userData = User::create([ | ||
'name' => 'name', | ||
'email' => 'email@email.com', | ||
'dni' => '57591829J', | ||
'password' => bcrypt('password'), | ||
'status' => 'ACTIVE', | ||
'role' => 'ADMIN', | ||
]); | ||
|
||
$response = $this->post('/api/register', [ | ||
'email' => $userData['email'], | ||
'dni' => '57591829J', | ||
'password' => 'password', | ||
'password_confirmation' => 'password', | ||
]); | ||
|
||
$response->assertStatus(200) | ||
->assertJson([ | ||
'status' => false, | ||
]); | ||
} | ||
|
||
/** | ||
* Test store user with taken DNI | ||
*/ | ||
public function test_store_with_taken_dni(): void | ||
{ | ||
$userData = User::create([ | ||
'name' => 'name', | ||
'email' => 'email@email.com', | ||
'dni' => '57591829J', | ||
'password' => bcrypt('password'), | ||
'status' => 'ACTIVE', | ||
'role' => 'ADMIN', | ||
]); | ||
|
||
$response = $this->post('/api/register', [ | ||
'email' => 'email@email.com', | ||
'dni' => $userData['dni'], | ||
'password' => 'password', | ||
'password_confirmation' => 'password', | ||
]); | ||
|
||
$response->assertStatus(200) | ||
->assertJson([ | ||
'status' => false, | ||
]); | ||
} | ||
} |