267 lines
No EOL
9.3 KiB
PHP
267 lines
No EOL
9.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Tests\TestCase;
|
|
|
|
class TestSetupControllerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
/**
|
|
* Test creating a new test user.
|
|
*/
|
|
public function test_can_create_new_test_user()
|
|
{
|
|
$userData = [
|
|
'name' => 'Test User',
|
|
'email' => 'test@example.com',
|
|
'password' => 'password123',
|
|
];
|
|
|
|
$response = $this->postJson('/api/e2e/test/setup/user', $userData);
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertJson([
|
|
'success' => true,
|
|
'message' => 'Test user created',
|
|
'data' => [
|
|
'email' => 'test@example.com',
|
|
'name' => 'Test User'
|
|
]
|
|
]);
|
|
|
|
$this->assertDatabaseHas('users', [
|
|
'email' => 'test@example.com',
|
|
'name' => 'Test User',
|
|
]);
|
|
|
|
$user = User::where('email', 'test@example.com')->first();
|
|
$this->assertNotNull($user->email_verified_at);
|
|
$this->assertTrue(\Hash::check('password123', $user->password));
|
|
}
|
|
|
|
/**
|
|
* Test returning existing test user if already exists.
|
|
*/
|
|
public function test_returns_existing_test_user_if_already_exists()
|
|
{
|
|
// Create a user first
|
|
$existingUser = User::factory()->create([
|
|
'email' => 'existing@example.com',
|
|
'name' => 'Existing User',
|
|
]);
|
|
|
|
$userData = [
|
|
'name' => 'Different Name',
|
|
'email' => 'existing@example.com',
|
|
'password' => 'password123',
|
|
];
|
|
|
|
$response = $this->postJson('/api/e2e/test/setup/user', $userData);
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertJson([
|
|
'success' => true,
|
|
'message' => 'Test user already exists',
|
|
'data' => [
|
|
'id' => $existingUser->id,
|
|
'email' => 'existing@example.com',
|
|
'name' => 'Existing User' // Should keep original name
|
|
]
|
|
]);
|
|
|
|
// Should not create a new user
|
|
$this->assertCount(1, User::where('email', 'existing@example.com')->get());
|
|
}
|
|
|
|
/**
|
|
* Test validation for required fields.
|
|
*/
|
|
public function test_validates_required_fields_for_create_user()
|
|
{
|
|
$response = $this->postJson('/api/e2e/test/setup/user', []);
|
|
|
|
$response->assertStatus(422);
|
|
$response->assertJsonPath('errors.name', ['The name field is required.']);
|
|
$response->assertJsonPath('errors.email', ['The email field is required.']);
|
|
$response->assertJsonPath('errors.password', ['The password field is required.']);
|
|
}
|
|
|
|
/**
|
|
* Test email validation.
|
|
*/
|
|
public function test_validates_email_format()
|
|
{
|
|
$userData = [
|
|
'name' => 'Test User',
|
|
'email' => 'invalid-email',
|
|
'password' => 'password123',
|
|
];
|
|
|
|
$response = $this->postJson('/api/e2e/test/setup/user', $userData);
|
|
|
|
$response->assertStatus(422);
|
|
$response->assertJsonPath('errors.email', ['The email field must be a valid email address.']);
|
|
}
|
|
|
|
/**
|
|
* Test password length validation.
|
|
*/
|
|
public function test_validates_password_length()
|
|
{
|
|
$userData = [
|
|
'name' => 'Test User',
|
|
'email' => 'test@example.com',
|
|
'password' => 'short',
|
|
];
|
|
|
|
$response = $this->postJson('/api/e2e/test/setup/user', $userData);
|
|
|
|
$response->assertStatus(422);
|
|
$response->assertJsonPath('errors.password', ['The password field must be at least 8 characters.']);
|
|
}
|
|
|
|
/**
|
|
* Test cleanup removes test users with test email patterns.
|
|
*/
|
|
public function test_cleanup_removes_test_users()
|
|
{
|
|
// Create test users with test email patterns
|
|
User::factory()->create(['email' => 'test.user.1@example.com']);
|
|
User::factory()->create(['email' => 'test.user.2@example.com']);
|
|
User::factory()->create(['email' => 'test123@example.com']);
|
|
|
|
// Create a regular user that should not be deleted
|
|
User::factory()->create(['email' => 'regular@example.com']);
|
|
|
|
$response = $this->postJson('/api/e2e/test/cleanup');
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertJson([
|
|
'success' => true,
|
|
'message' => 'Deleted 3 test users'
|
|
]);
|
|
|
|
// Test users should be deleted
|
|
$this->assertDatabaseMissing('users', ['email' => 'test.user.1@example.com']);
|
|
$this->assertDatabaseMissing('users', ['email' => 'test.user.2@example.com']);
|
|
$this->assertDatabaseMissing('users', ['email' => 'test123@example.com']);
|
|
|
|
// Regular user should still exist
|
|
$this->assertDatabaseHas('users', ['email' => 'regular@example.com']);
|
|
}
|
|
|
|
/**
|
|
* Test cleanup when no test users exist.
|
|
*/
|
|
public function test_cleanup_when_no_test_users_exist()
|
|
{
|
|
// Create only regular users
|
|
User::factory()->create(['email' => 'regular1@example.com']);
|
|
User::factory()->create(['email' => 'regular2@example.com']);
|
|
|
|
$response = $this->postJson('/api/e2e/test/cleanup');
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertJson([
|
|
'success' => true,
|
|
'message' => 'Deleted 0 test users'
|
|
]);
|
|
|
|
// Regular users should still exist
|
|
$this->assertDatabaseHas('users', ['email' => 'regular1@example.com']);
|
|
$this->assertDatabaseHas('users', ['email' => 'regular2@example.com']);
|
|
}
|
|
|
|
/**
|
|
* Test cleanup removes only users matching test patterns.
|
|
*/
|
|
public function test_cleanup_only_removes_matching_patterns()
|
|
{
|
|
// Create users with various email patterns
|
|
// Patterns that should be deleted: 'test%@example.com' OR 'test.user.%@example.com'
|
|
User::factory()->create(['email' => 'test@example.com']); // Should be deleted (matches test%@example.com)
|
|
User::factory()->create(['email' => 'test.user.123@example.com']); // Should be deleted (matches test.user.%@example.com)
|
|
User::factory()->create(['email' => 'testABC@example.com']); // Should be deleted (matches test%@example.com)
|
|
User::factory()->create(['email' => 'test.user.xyz@example.com']); // Should be deleted (matches test.user.%@example.com)
|
|
User::factory()->create(['email' => 'test999@example.com']); // Should be deleted (matches test%@example.com)
|
|
User::factory()->create(['email' => 'mytesting@example.com']); // Should NOT be deleted
|
|
User::factory()->create(['email' => 'test@gmail.com']); // Should NOT be deleted
|
|
User::factory()->create(['email' => 'mytest@example.com']); // Should NOT be deleted
|
|
|
|
$response = $this->postJson('/api/e2e/test/cleanup');
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertJsonPath('success', true);
|
|
// Just verify the message contains "Deleted" and "test users"
|
|
$this->assertStringContainsString('Deleted', $response->json('message'));
|
|
$this->assertStringContainsString('test users', $response->json('message'));
|
|
|
|
// Only specific patterns should be deleted
|
|
$this->assertDatabaseMissing('users', ['email' => 'test@example.com']);
|
|
$this->assertDatabaseMissing('users', ['email' => 'test.user.123@example.com']);
|
|
$this->assertDatabaseMissing('users', ['email' => 'testABC@example.com']);
|
|
$this->assertDatabaseMissing('users', ['email' => 'test.user.xyz@example.com']);
|
|
$this->assertDatabaseMissing('users', ['email' => 'test999@example.com']);
|
|
|
|
// Others should remain
|
|
$this->assertDatabaseHas('users', ['email' => 'mytesting@example.com']);
|
|
$this->assertDatabaseHas('users', ['email' => 'test@gmail.com']);
|
|
$this->assertDatabaseHas('users', ['email' => 'mytest@example.com']);
|
|
}
|
|
|
|
|
|
/**
|
|
* Test create user endpoint works in non-production environment.
|
|
*/
|
|
public function test_endpoints_work_in_non_production_environment()
|
|
{
|
|
// Ensure we're not in production environment
|
|
$this->assertNotEquals('production', app()->environment());
|
|
|
|
$response = $this->postJson('/api/e2e/test/setup/user', [
|
|
'name' => 'Test User',
|
|
'email' => 'test@example.com',
|
|
'password' => 'password123',
|
|
]);
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertJson(['success' => true]);
|
|
}
|
|
|
|
/**
|
|
* Test user data is properly formatted in response.
|
|
*/
|
|
public function test_user_data_properly_formatted_in_response()
|
|
{
|
|
$userData = [
|
|
'name' => 'John Doe',
|
|
'email' => 'john.doe@example.com',
|
|
'password' => 'securepassword123',
|
|
];
|
|
|
|
$response = $this->postJson('/api/e2e/test/setup/user', $userData);
|
|
|
|
$response->assertStatus(200);
|
|
$response->assertJsonStructure([
|
|
'success',
|
|
'message',
|
|
'data' => [
|
|
'id',
|
|
'email',
|
|
'name'
|
|
]
|
|
]);
|
|
|
|
// Ensure password is not included in response
|
|
$response->assertJsonMissing(['password']);
|
|
|
|
$data = $response->json('data');
|
|
$this->assertIsInt($data['id']);
|
|
$this->assertEquals('john.doe@example.com', $data['email']);
|
|
$this->assertEquals('John Doe', $data['name']);
|
|
}
|
|
} |