diff --git a/tests/Feature/Events/APIv2EventTest.php b/tests/Feature/Events/APIv2EventTest.php index e20366f73..d88b2dbb6 100644 --- a/tests/Feature/Events/APIv2EventTest.php +++ b/tests/Feature/Events/APIv2EventTest.php @@ -126,8 +126,8 @@ public function testMaxUpdatedAt() { 'event' => $idevents, ]); - // Sleep for a second so that the updated_at time on the event and group should not be the current time. - sleep(1); + // Sleep for two seconds so that the updated_at time on the event and group should not be the current time. + sleep(2); $now = Carbon::now()->toIso8601String(); $response = $this->get("/api/v2/events/$idevents"); @@ -136,18 +136,23 @@ public function testMaxUpdatedAt() { $eventUpdated = $json['data']['updated_at']; $groupUpdated = $json['data']['group']['updated_at']; - // API v2 dates are ISO strings so we can just string compare. - self::assertTrue($eventUpdated == $groupUpdated); - self::assertFalse($eventUpdated == $now); + // Both times should not be the time we started at. They might be 1s different if we happened to hit + // the second boundary (which we have seen on CircleCI). + $this->assertNotEquals($now, $eventUpdated); + $this->assertNotEquals($now, $groupUpdated); + + $eventUpdatedEpoch = (new Carbon($eventUpdated))->getTimestamp(); + $groupUpdatedEpoch = (new Carbon($groupUpdated))->getTimestamp(); + $this->assertTrue(abs($eventUpdatedEpoch - $groupUpdatedEpoch) <= 1); $response = $this->get("/api/events/network?api_token=1234"); $response->assertSuccessful(); $json = json_decode($response->getContent(), true); // API v1 dates aren't as clear so format them for comparison. - $eventUpdated = (new Carbon($json[0]['updated_at']))->format('Y-m-d H:i:s'); - $nowF = (new Carbon($now))->format('Y-m-d H:i:s'); - self::assertFalse($eventUpdated == $nowF); + $eventUpdatedEpoch = (new Carbon($json[0]['updated_at']))->getTimestamp(); + $this->assertTrue(abs($eventUpdatedEpoch - $groupUpdatedEpoch) <= 1); + $this->assertNotEquals($now, (new Carbon($json[0]['updated_at']))->toIso8601String()); } /** diff --git a/tests/Feature/Groups/APIv2GroupTest.php b/tests/Feature/Groups/APIv2GroupTest.php index c69e75daa..d1a962de6 100644 --- a/tests/Feature/Groups/APIv2GroupTest.php +++ b/tests/Feature/Groups/APIv2GroupTest.php @@ -4,6 +4,7 @@ use App\Group; use App\GroupTags; +use App\Helpers\RepairNetworkService; use App\Network; use App\User; use Carbon\Carbon; @@ -402,4 +403,64 @@ public function testEmptyNetworkData() { $json = json_decode($response->getContent(), true); assertEquals(null, $json['data']['network_data']); } + + public function testNetworkDataUpdatedAt() { + $user = User::factory()->administrator()->create([ + 'api_token' => '1234', + ]); + $this->actingAs($user); + + $this->get('/'); + $user = Auth::user(); + + $this->lastResponse = $this->post('/api/v2/groups?api_token=' . $user->api_token, [ + 'name' => 'Test Group Updated', + 'website' => 'https://therestartproject.org', + 'location' => 'Brussels, Belgium', + 'description' => 'Some text.', + 'timezone' => 'Europe/London', + 'network_data' => [], + 'email' => null, + ]); + + $this->assertTrue($this->lastResponse->isSuccessful()); + $json = json_decode($this->lastResponse->getContent(), true); + $this->assertTrue(array_key_exists('id', $json)); + $idgroups = $json['id']; + + $network = Network::factory()->create(); + + $group = Group::find($idgroups); + $this->networkService = new RepairNetworkService(); + $this->networkService->addGroupToNetwork($user, $group, $network); + + $now = Carbon::now()->toIso8601String(); + sleep(1); + + $response = $this->patch('/api/v2/groups/' . $idgroups, [ + 'network_data' => [ + 'foo' => 'bar', + ], + ]); + + $response->assertSuccessful(); + + // Check the updated_at has been, well, updated. + $response = $this->get("/api/v2/groups/$idgroups"); + $response->assertSuccessful(); + $json = json_decode($response->getContent(), true); + $this->assertEquals($idgroups, $json['data']['id']); + $updated_at = $json['data']['updated_at']; + $this->assertNotEquals($now, $updated_at); + + // Test the v1 API. + $network->addCoordinator($user); + $this->actingAs($user); + + $response = $this->get('/api/groups/network?api_token=1234'); + $groups = json_decode($response->getContent(), true); + $this->assertEquals(1, count($groups)); + $this->assertEquals($idgroups, $groups[0]['id']); + $this->assertEquals((new Carbon($updated_at))->getTimestamp(), (new Carbon($groups[0]['updated_at']))->getTimestamp()); + } }