138 lines
4.4 KiB
PHP
138 lines
4.4 KiB
PHP
<?php
|
|
|
|
use App\Models\User;
|
|
use App\Models\Inquiry;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Queue;
|
|
use App\Jobs\ProvisionTenantJob;
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
test('public user can submit a paid subdomain contact inquiry', function () {
|
|
$payload = [
|
|
'company_name' => 'Acme Corporation',
|
|
'contact_name' => 'Wile E. Coyote',
|
|
'email' => 'coyote@acme.com',
|
|
'phone' => '1234567890',
|
|
'desired_subdomain' => 'acme-corp',
|
|
'employee_count' => 25,
|
|
'notes' => 'Looking for enterprise HR features',
|
|
'payment_reference' => 'PAY-987654321',
|
|
];
|
|
|
|
$response = $this->postJson('/api/inquiries', $payload);
|
|
|
|
$response->assertStatus(201)
|
|
->assertJson([
|
|
'success' => true,
|
|
]);
|
|
|
|
$this->assertDatabaseHas('inquiries', [
|
|
'company_name' => 'Acme Corporation',
|
|
'desired_subdomain' => 'acme-corp',
|
|
'status' => 'pending_payment_approval',
|
|
]);
|
|
});
|
|
|
|
test('non-executive user cannot view or approve inquiries', function () {
|
|
$regularUser = User::factory()->create(['type' => 'employee']);
|
|
|
|
$response = $this->actingAs($regularUser, 'sanctum')
|
|
->getJson('/api/executive/inquiries');
|
|
|
|
$response->assertStatus(403);
|
|
});
|
|
|
|
test('executive user can view inquiries and approve subdomain tenant creation', function () {
|
|
Queue::fake();
|
|
|
|
$executive = User::factory()->create(['type' => 'company admin']);
|
|
|
|
$inquiry = Inquiry::create([
|
|
'company_name' => 'Stark Industries',
|
|
'contact_name' => 'Tony Stark',
|
|
'email' => 'tony@stark.com',
|
|
'desired_subdomain' => 'stark',
|
|
'status' => 'pending_payment_approval',
|
|
]);
|
|
|
|
$response = $this->actingAs($executive, 'sanctum')
|
|
->postJson("/api/executive/inquiries/{$inquiry->id}/approve", [
|
|
'desired_subdomain' => 'stark-tech',
|
|
'payment_reference' => 'BANK-TXN-1002',
|
|
]);
|
|
|
|
$response->assertStatus(200)
|
|
->assertJson(['success' => true]);
|
|
|
|
$this->assertDatabaseHas('inquiries', [
|
|
'id' => $inquiry->id,
|
|
'desired_subdomain' => 'stark-tech',
|
|
'status' => 'approved',
|
|
'approved_by_user_id' => $executive->id,
|
|
]);
|
|
|
|
Queue::assertPushed(ProvisionTenantJob::class);
|
|
});
|
|
|
|
test('executive user can provision subdomain from contact inquiries dashboard web route', function () {
|
|
Queue::fake();
|
|
|
|
\Spatie\Permission\Models\Permission::findOrCreate('manage-contacts', 'web');
|
|
|
|
$executive = User::factory()->create(['type' => 'company admin']);
|
|
$executive->givePermissionTo('manage-contacts');
|
|
|
|
$response = $this->actingAs($executive)
|
|
->post(route('contacts.provision-subdomain'), [
|
|
'company_name' => 'Wayne Enterprises',
|
|
'contact_name' => 'Bruce Wayne',
|
|
'email' => 'bruce@wayne.com',
|
|
'desired_subdomain' => 'wayne-tech',
|
|
'payment_reference' => 'MANUAL-WEB-PROVISION',
|
|
]);
|
|
|
|
$response->assertRedirect();
|
|
|
|
$this->assertDatabaseHas('inquiries', [
|
|
'email' => 'bruce@wayne.com',
|
|
'desired_subdomain' => 'wayne-tech',
|
|
'status' => 'approved',
|
|
'approved_by_user_id' => $executive->id,
|
|
]);
|
|
|
|
Queue::assertPushed(ProvisionTenantJob::class);
|
|
});
|
|
|
|
test('deleting contact or inquiry permanently removes records from database', function () {
|
|
\Spatie\Permission\Models\Permission::findOrCreate('manage-contacts', 'web');
|
|
\Spatie\Permission\Models\Permission::findOrCreate('delete-contacts', 'web');
|
|
|
|
$executive = User::factory()->create(['type' => 'company admin']);
|
|
$executive->givePermissionTo(['manage-contacts', 'delete-contacts']);
|
|
|
|
$inquiry = Inquiry::create([
|
|
'company_name' => 'Delete Corp',
|
|
'contact_name' => 'John Delete',
|
|
'email' => 'delete@corp.com',
|
|
'desired_subdomain' => 'delete-corp',
|
|
'status' => 'pending_payment_approval',
|
|
]);
|
|
|
|
$contact = \App\Models\Contact::create([
|
|
'email' => 'delete@corp.com',
|
|
'name' => 'John Delete',
|
|
'subject' => 'Subdomain Inquiry: Delete Corp',
|
|
'message' => 'Test deletion message',
|
|
'status' => 'New',
|
|
]);
|
|
|
|
$response = $this->actingAs($executive)
|
|
->delete(route('contacts.destroy', $contact->id));
|
|
|
|
$response->assertRedirect();
|
|
|
|
$this->assertDatabaseMissing('contacts', ['id' => $contact->id]);
|
|
$this->assertDatabaseMissing('inquiries', ['id' => $inquiry->id]);
|
|
});
|