This commit is contained in:
Stefanescu Mihai
2026-03-19 18:54:50 +02:00
parent 82ebb697f3
commit 49b1d3581a
19 changed files with 1496 additions and 11 deletions
+182
View File
@@ -0,0 +1,182 @@
<?php
namespace Tests\Feature;
use App\Jobs\ProcessTrackingPayload;
use App\Models\Website;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Bus;
use Tests\TestCase;
class CollectControllerTest extends TestCase
{
use RefreshDatabase;
private Website $website;
protected function setUp(): void
{
parent::setUp();
$this->website = Website::factory()->create(['domain' => 'example.com']);
}
private function validPayload(array $overrides = []): array
{
return array_merge([
'tid' => $this->website->tracking_id,
'vid' => 'visitor-abc-123',
'sid' => 'session-abc-123',
'type' => 'pageview',
'url' => 'https://example.com/hello',
'path' => '/hello',
'title' => 'Hello Page',
], $overrides);
}
// -- Store endpoint --
public function test_valid_pageview_payload_dispatches_job(): void
{
Bus::fake();
$response = $this->postJson('/api/collect', $this->validPayload());
$response->assertOk()->assertJson(['ok' => true]);
Bus::assertDispatched(ProcessTrackingPayload::class);
}
public function test_valid_event_payload_dispatches_job(): void
{
Bus::fake();
$response = $this->postJson('/api/collect', $this->validPayload([
'type' => 'event',
'event_name' => 'signup',
'event_category' => 'conversion',
]));
$response->assertOk()->assertJson(['ok' => true]);
Bus::assertDispatched(ProcessTrackingPayload::class);
}
public function test_missing_required_fields_returns_validation_error(): void
{
$response = $this->postJson('/api/collect', []);
$response->assertUnprocessable();
$response->assertJsonValidationErrors(['tid', 'vid', 'sid', 'type']);
}
public function test_invalid_type_returns_validation_error(): void
{
$response = $this->postJson('/api/collect', $this->validPayload([
'type' => 'invalid_type',
]));
$response->assertUnprocessable();
$response->assertJsonValidationErrors(['type']);
}
public function test_domain_mismatch_returns_error(): void
{
Bus::fake();
$response = $this->postJson('/api/collect', $this->validPayload([
'url' => 'https://evil-site.com/phishing',
]));
$response->assertStatus(400);
$response->assertJson(['ok' => false, 'reason' => 'domain_mismatch']);
Bus::assertNotDispatched(ProcessTrackingPayload::class);
}
public function test_subdomain_is_allowed(): void
{
Bus::fake();
$response = $this->postJson('/api/collect', $this->validPayload([
'url' => 'https://www.example.com/page',
]));
$response->assertOk()->assertJson(['ok' => true]);
Bus::assertDispatched(ProcessTrackingPayload::class);
}
public function test_unknown_tracking_id_still_succeeds(): void
{
Bus::fake();
$response = $this->postJson('/api/collect', $this->validPayload([
'tid' => 'TA-NONEXISTENT',
'url' => null,
]));
$response->assertOk()->assertJson(['ok' => true]);
Bus::assertDispatched(ProcessTrackingPayload::class);
}
public function test_bot_requests_are_rejected(): void
{
Bus::fake();
$response = $this->postJson('/api/collect', $this->validPayload(), [
'User-Agent' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
]);
$response->assertOk()->assertJson(['ok' => false, 'reason' => 'bot']);
Bus::assertNotDispatched(ProcessTrackingPayload::class);
}
public function test_payload_with_utm_parameters(): void
{
Bus::fake();
$response = $this->postJson('/api/collect', $this->validPayload([
'utm_source' => 'google',
'utm_medium' => 'cpc',
'utm_campaign' => 'spring_sale',
'utm_term' => 'shoes',
'utm_content' => 'ad_v1',
]));
$response->assertOk()->assertJson(['ok' => true]);
Bus::assertDispatched(ProcessTrackingPayload::class);
}
// -- Pixel endpoint --
public function test_pixel_returns_gif(): void
{
Bus::fake();
$response = $this->get('/api/pixel?tid='.$this->website->tracking_id);
$response->assertOk();
$response->assertHeader('Content-Type', 'image/gif');
Bus::assertDispatched(ProcessTrackingPayload::class);
}
public function test_pixel_without_tid_returns_gif_without_dispatching(): void
{
Bus::fake();
$response = $this->get('/api/pixel');
$response->assertOk();
$response->assertHeader('Content-Type', 'image/gif');
Bus::assertNotDispatched(ProcessTrackingPayload::class);
}
public function test_pixel_rejects_bots(): void
{
Bus::fake();
$response = $this->get('/api/pixel?tid='.$this->website->tracking_id, [
'User-Agent' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
]);
$response->assertOk();
$response->assertHeader('Content-Type', 'image/gif');
Bus::assertNotDispatched(ProcessTrackingPayload::class);
}
}
+204
View File
@@ -0,0 +1,204 @@
<?php
namespace Tests\Feature;
use App\Models\User;
use App\Models\Website;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class WebsiteControllerTest extends TestCase
{
use RefreshDatabase;
private User $user;
protected function setUp(): void
{
parent::setUp();
$this->user = User::factory()->create();
}
// -- Index --
public function test_guests_cannot_access_website_list(): void
{
$this->get(route('websites.index'))->assertRedirect(route('login'));
}
public function test_authenticated_user_can_see_their_websites(): void
{
$website = Website::factory()->for($this->user)->create();
Website::factory()->create(); // another user's website
$response = $this->actingAs($this->user)->get(route('websites.index'));
$response->assertOk();
$response->assertSeeText($website->name);
}
// -- Create / Store --
public function test_user_can_view_create_form(): void
{
$this->actingAs($this->user)
->get(route('websites.create'))
->assertOk();
}
public function test_user_can_create_website(): void
{
$response = $this->actingAs($this->user)->post(route('websites.store'), [
'name' => 'My Site',
'domain' => 'mysite.com',
]);
$response->assertRedirect(route('websites.index'));
$this->assertDatabaseHas('websites', [
'user_id' => $this->user->id,
'name' => 'My Site',
'domain' => 'mysite.com',
]);
}
public function test_create_website_generates_tracking_id(): void
{
$this->actingAs($this->user)->post(route('websites.store'), [
'name' => 'My Site',
'domain' => 'mysite.com',
]);
$website = Website::where('domain', 'mysite.com')->first();
$this->assertNotNull($website->tracking_id);
$this->assertStringStartsWith('TA-', $website->tracking_id);
}
public function test_create_website_validates_domain_format(): void
{
$response = $this->actingAs($this->user)->post(route('websites.store'), [
'name' => 'My Site',
'domain' => 'not a domain!!',
]);
$response->assertSessionHasErrors('domain');
}
public function test_create_website_requires_name(): void
{
$response = $this->actingAs($this->user)->post(route('websites.store'), [
'name' => '',
'domain' => 'mysite.com',
]);
$response->assertSessionHasErrors('name');
}
// -- Edit / Update --
public function test_owner_can_edit_website(): void
{
$website = Website::factory()->for($this->user)->create();
$this->actingAs($this->user)
->get(route('websites.edit', $website))
->assertOk();
}
public function test_non_owner_cannot_edit_website(): void
{
$otherUser = User::factory()->create();
$website = Website::factory()->for($otherUser)->create();
$this->actingAs($this->user)
->get(route('websites.edit', $website))
->assertForbidden();
}
public function test_owner_can_update_website(): void
{
$website = Website::factory()->for($this->user)->create();
$response = $this->actingAs($this->user)->put(route('websites.update', $website), [
'name' => 'Updated Name',
'domain' => 'updated.com',
]);
$response->assertRedirect(route('dashboard.show', $website));
$this->assertDatabaseHas('websites', [
'id' => $website->id,
'name' => 'Updated Name',
'domain' => 'updated.com',
]);
}
public function test_non_owner_cannot_update_website(): void
{
$otherUser = User::factory()->create();
$website = Website::factory()->for($otherUser)->create();
$this->actingAs($this->user)
->put(route('websites.update', $website), [
'name' => 'Hacked',
'domain' => 'hacked.com',
])
->assertForbidden();
}
// -- Delete --
public function test_owner_can_delete_website(): void
{
$website = Website::factory()->for($this->user)->create();
$response = $this->actingAs($this->user)
->delete(route('websites.destroy', $website));
$response->assertRedirect(route('websites.index'));
$this->assertDatabaseMissing('websites', ['id' => $website->id]);
}
public function test_non_owner_cannot_delete_website(): void
{
$otherUser = User::factory()->create();
$website = Website::factory()->for($otherUser)->create();
$this->actingAs($this->user)
->delete(route('websites.destroy', $website))
->assertForbidden();
$this->assertDatabaseHas('websites', ['id' => $website->id]);
}
// -- Show redirects to dashboard --
public function test_show_redirects_to_dashboard(): void
{
$website = Website::factory()->for($this->user)->create();
$this->actingAs($this->user)
->get(route('websites.show', $website))
->assertRedirect(route('dashboard.show', $website));
}
// -- Snippet --
public function test_owner_can_view_snippet(): void
{
$website = Website::factory()->for($this->user)->create();
$this->actingAs($this->user)
->get(route('websites.snippet', $website))
->assertOk()
->assertSeeText($website->tracking_id);
}
public function test_non_owner_cannot_view_snippet(): void
{
$otherUser = User::factory()->create();
$website = Website::factory()->for($otherUser)->create();
$this->actingAs($this->user)
->get(route('websites.snippet', $website))
->assertForbidden();
}
}