From a10ae58773820ddccca364258778111e8fd98eb6 Mon Sep 17 00:00:00 2001 From: David Jardin Date: Tue, 19 Aug 2025 08:30:23 +0200 Subject: [PATCH 1/4] Remove duplicate site rows on a successful registration --- app/Http/Controllers/Api/V1/SiteController.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Api/V1/SiteController.php b/app/Http/Controllers/Api/V1/SiteController.php index b653542..e9bbc60 100644 --- a/app/Http/Controllers/Api/V1/SiteController.php +++ b/app/Http/Controllers/Api/V1/SiteController.php @@ -48,9 +48,11 @@ public function register(SiteRequest $request): JsonResponse return $this->error($e->getMessage(), 500); } - // If successful create or update site - $site = Site::where('url', $url)->where('key', $key)->first() ?? new Site(); + // Remove older duplicates of the site if registered + Site::query()->where('url', $url)->delete(); + // Create new row + $site = new Site(); $site->key = $key; $site->url = $url; $site->last_seen = Carbon::now(); From 3b7e1f25a1a976f1c07a3ecec9df9569a58e07d3 Mon Sep 17 00:00:00 2001 From: David Jardin Date: Tue, 19 Aug 2025 08:32:24 +0200 Subject: [PATCH 2/4] add test case --- tests/Feature/Api/SiteControllerTest.php | 33 ++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/Feature/Api/SiteControllerTest.php b/tests/Feature/Api/SiteControllerTest.php index 342f77d..e8f9208 100644 --- a/tests/Feature/Api/SiteControllerTest.php +++ b/tests/Feature/Api/SiteControllerTest.php @@ -58,6 +58,39 @@ public function testRegisteringASiteWithUrlAndKeyCreatesRow(): void Queue::assertPushed(CheckSiteHealth::class); } + public function testRegisteringASiteTwiceCreatesOnlyOneRow(): void + { + Queue::fake(); + + $mock = $this->getMockBuilder(Connection::class) + ->disableOriginalConstructor() + ->getMock(); + + $mock->method('__call')->willReturn(HealthCheck::from([ + "php_version" => "1.0.0", + "db_type" => "mysqli", + "db_version" => "1.0.0", + "cms_version" => "1.0.0", + "server_os" => "Joomla OS 1.0.0", + "update_requirement_state" => true + ])); + + $this->app->bind(Connection::class, fn () => $mock); + + $this->postJson( + '/api/v1/register', + ["url" => "https://www.joomla.org", "key" => "foobar123foobar123foobar123foobar123"] + ); + + $this->postJson( + '/api/v1/register', + ["url" => "https://www.joomla.org", "key" => "foobar123foobar123foobar123foobar123"] + ); + + $count = Site::where('url', 'https://www.joomla.org')->count(); + $this->assertEquals(1, $count); + } + public function testRegisteringASiteFailsWhenHealthCheckFails(): void { $mock = $this->getMockBuilder(Connection::class) From 5cfff261f94dbcba589c04a107e52a28de016627 Mon Sep 17 00:00:00 2001 From: David Jardin Date: Tue, 19 Aug 2025 08:34:01 +0200 Subject: [PATCH 3/4] imrpove test --- tests/Feature/Api/SiteControllerTest.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/Feature/Api/SiteControllerTest.php b/tests/Feature/Api/SiteControllerTest.php index e8f9208..02357d4 100644 --- a/tests/Feature/Api/SiteControllerTest.php +++ b/tests/Feature/Api/SiteControllerTest.php @@ -84,11 +84,12 @@ public function testRegisteringASiteTwiceCreatesOnlyOneRow(): void $this->postJson( '/api/v1/register', - ["url" => "https://www.joomla.org", "key" => "foobar123foobar123foobar123foobar123"] + ["url" => "https://www.joomla.org", "key" => "abcdef"] ); - $count = Site::where('url', 'https://www.joomla.org')->count(); - $this->assertEquals(1, $count); + $rows = Site::where('url', 'https://www.joomla.org')->get(); + $this->assertEquals(1, $rows->count()); + $this->assertEquals('abcdef', $rows->first()->key); } public function testRegisteringASiteFailsWhenHealthCheckFails(): void From 95da31756a81168dadd06b9c9de5f51af9315e5c Mon Sep 17 00:00:00 2001 From: David Jardin Date: Tue, 19 Aug 2025 09:35:45 +0200 Subject: [PATCH 4/4] fix test --- tests/Feature/Api/SiteControllerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Feature/Api/SiteControllerTest.php b/tests/Feature/Api/SiteControllerTest.php index 02357d4..f81f910 100644 --- a/tests/Feature/Api/SiteControllerTest.php +++ b/tests/Feature/Api/SiteControllerTest.php @@ -84,12 +84,12 @@ public function testRegisteringASiteTwiceCreatesOnlyOneRow(): void $this->postJson( '/api/v1/register', - ["url" => "https://www.joomla.org", "key" => "abcdef"] + ["url" => "https://www.joomla.org", "key" => "abcdefabcdefabcdefabcdefabcdefabcdef"] ); $rows = Site::where('url', 'https://www.joomla.org')->get(); $this->assertEquals(1, $rows->count()); - $this->assertEquals('abcdef', $rows->first()->key); + $this->assertEquals('abcdefabcdefabcdefabcdefabcdefabcdef', $rows->first()->key); } public function testRegisteringASiteFailsWhenHealthCheckFails(): void