Skip to content

Commit 68f0a5d

Browse files
authored
Remove duplicate site rows on a successful registration (#41)
* Remove duplicate site rows on a successful registration * add test case * imrpove test * fix test
1 parent f183158 commit 68f0a5d

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

app/Http/Controllers/Api/V1/SiteController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ public function register(SiteRequest $request): JsonResponse
4848
return $this->error($e->getMessage(), 500);
4949
}
5050

51-
// If successful create or update site
52-
$site = Site::where('url', $url)->where('key', $key)->first() ?? new Site();
51+
// Remove older duplicates of the site if registered
52+
Site::query()->where('url', $url)->delete();
5353

54+
// Create new row
55+
$site = new Site();
5456
$site->key = $key;
5557
$site->url = $url;
5658
$site->last_seen = Carbon::now();

tests/Feature/Api/SiteControllerTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,40 @@ public function testRegisteringASiteWithUrlAndKeyCreatesRow(): void
5858
Queue::assertPushed(CheckSiteHealth::class);
5959
}
6060

61+
public function testRegisteringASiteTwiceCreatesOnlyOneRow(): void
62+
{
63+
Queue::fake();
64+
65+
$mock = $this->getMockBuilder(Connection::class)
66+
->disableOriginalConstructor()
67+
->getMock();
68+
69+
$mock->method('__call')->willReturn(HealthCheck::from([
70+
"php_version" => "1.0.0",
71+
"db_type" => "mysqli",
72+
"db_version" => "1.0.0",
73+
"cms_version" => "1.0.0",
74+
"server_os" => "Joomla OS 1.0.0",
75+
"update_requirement_state" => true
76+
]));
77+
78+
$this->app->bind(Connection::class, fn () => $mock);
79+
80+
$this->postJson(
81+
'/api/v1/register',
82+
["url" => "https://www.joomla.org", "key" => "foobar123foobar123foobar123foobar123"]
83+
);
84+
85+
$this->postJson(
86+
'/api/v1/register',
87+
["url" => "https://www.joomla.org", "key" => "abcdefabcdefabcdefabcdefabcdefabcdef"]
88+
);
89+
90+
$rows = Site::where('url', 'https://www.joomla.org')->get();
91+
$this->assertEquals(1, $rows->count());
92+
$this->assertEquals('abcdefabcdefabcdefabcdefabcdefabcdef', $rows->first()->key);
93+
}
94+
6195
public function testRegisteringASiteFailsWhenHealthCheckFails(): void
6296
{
6397
$mock = $this->getMockBuilder(Connection::class)

0 commit comments

Comments
 (0)