Skip to content

Commit

Permalink
Merge pull request #710 from TheRestartProject/RES-1965_updated_at
Browse files Browse the repository at this point in the history
RES-1965 Test that updating group network data updates the updated_at timestamp
  • Loading branch information
edwh committed Dec 5, 2023
2 parents 3e6e30a + 6201ff3 commit 8ef4cdb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
21 changes: 13 additions & 8 deletions tests/Feature/Events/APIv2EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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());
}

/**
Expand Down
61 changes: 61 additions & 0 deletions tests/Feature/Groups/APIv2GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Group;
use App\GroupTags;
use App\Helpers\RepairNetworkService;
use App\Network;
use App\User;
use Carbon\Carbon;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 8ef4cdb

Please sign in to comment.