Skip to content

Commit

Permalink
Merge pull request #51 from IT-Academy-BCN/feature/register
Browse files Browse the repository at this point in the history
Add tests to store user feature
  • Loading branch information
CloudSalander authored Jun 1, 2023
2 parents 8f08d50 + 7992190 commit 46b843e
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 5 deletions.
Binary file added .rnd
Binary file not shown.
4 changes: 3 additions & 1 deletion database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

/**
Expand All @@ -20,8 +21,9 @@ public function definition(): array
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'dni' => $this->faker->regexify('[0-9]{8}[A-Z]'),
'password' => Hash::make('password'),
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
}
Expand Down
5 changes: 1 addition & 4 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ public function run(): void
{
// \App\Models\User::factory(10)->create();

\App\Models\User::factory()->create([
'name' => 'Test User',
'email' => '[email protected]',
]);
\App\Models\User::factory(3)->create();
}
}
249 changes: 249 additions & 0 deletions tests/Feature/RegisterTest.php
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 protected]',
'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 protected]',
'dni' => '57591829J',
'password' => bcrypt('password'),
'status' => 'ACTIVE',
'role' => 'ADMIN',
]);

$response = $this->post('/api/register', [
'email' => '[email protected]',
'dni' => $userData['dni'],
'password' => 'password',
'password_confirmation' => 'password',
]);

$response->assertStatus(200)
->assertJson([
'status' => false,
]);
}
}

0 comments on commit 46b843e

Please sign in to comment.