Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update site created timestamp #166

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.uid2</groupId>
<artifactId>uid2-admin</artifactId>
<version>2.26.7-f999928e9e</version>
<version>2.27.0-18fda5471a-ian-UID2-2010-update-site-created</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/uid2/admin/vertx/service/SiteService.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public void setupRoutes(Router router) {
this.handleSiteDomains(ctx);
}
}, Role.CLIENTKEY_ISSUER));
router.post("/api/site/created").blockingHandler(auth.handle((ctx) -> {
synchronized (writeLock) {
this.handleSiteCreated(ctx);
}
}, Role.CLIENTKEY_ISSUER));
}

private void handleRewriteMetadata(RoutingContext rc) {
Expand Down Expand Up @@ -296,6 +301,40 @@ private void handleSiteDomains(RoutingContext rc) {
}
}

private void handleSiteCreated(RoutingContext rc) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started out thinking this was handling a new site created action - changing the name to something like "handleUpdateSiteCreated" ior "handleUpdateCreatedTimestamp" would be clearer.

try {
// refresh manually
siteProvider.loadContent();

final Site existingSite = RequestUtil.getSite(rc, "id", siteProvider);
if (existingSite == null) {
return;
}

JsonObject body = rc.body().asJsonObject();
Long created = body.getLong("created");
if(created == null) {
ResponseUtil.error(rc, 400, "required parameters: created");
return;
}

Site updatedSite = new Site(existingSite.getId(), existingSite.getName(), existingSite.isEnabled(), existingSite.getClientTypes(), existingSite.getDomainNames(), created);

final List<Site> sites = this.siteProvider.getAllSites()
.stream().sorted(Comparator.comparingInt(Site::getId))
.collect(Collectors.toList());

sites.remove(existingSite);
sites.add(updatedSite);

storeWriter.upload(sites, null);

rc.response().end(jsonWriter.writeValueAsString(updatedSite));
} catch (Exception e) {
ResponseUtil.errorInternal(rc, "set site created failed", e);
}
}

private static List<String> getNormalizedDomainNames(RoutingContext rc, JsonArray domainNamesJa) {
List<String> domainNames = domainNamesJa.stream().map(String::valueOf).collect(Collectors.toList());

Expand Down
28 changes: 24 additions & 4 deletions src/test/java/com/uid2/admin/vertx/SiteServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -611,6 +608,29 @@ void domainNameOverWrite(Vertx vertx, VertxTestContext testContext) {
});
}

@Test
void createdOverWrite(Vertx vertx, VertxTestContext testContext) {
fakeAuth(Role.CLIENTKEY_ISSUER);
Site s = new Site(123, "name", true, Set.of("qwerty.com"));
setSites(s);

JsonObject reqBody = new JsonObject();
reqBody.put("created", 123456);

post(vertx, "api/site/created?id=123", reqBody.encode(), ar -> {
HttpResponse response = ar.result();
assertEquals(200, response.statusCode());
Site updatedSite = new Site(123, "name", true, Set.of(), Set.of("qwerty.com"), 123456);
checkSiteResponse(updatedSite, response.bodyAsJsonObject());
try {
verify(storeWriter, times(1)).upload(List.of(updatedSite), null);
} catch (Exception e) {
testContext.failed();
}
testContext.completeNow();
});
}

@Test
void addSiteWithDomainNames(Vertx vertx, VertxTestContext testContext) {
fakeAuth(Role.CLIENTKEY_ISSUER);
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", "version": "2.26", "publicReleaseRefSpec": [ "^refs/heads/master$", "^refs/heads/v\\d+(?:\\.\\d+)?$" ], "cloudBuild": { "setVersionVariables": true, "buildNumber": { "enabled": true, "includeCommitId": { "when": "always" } } } }
{ "$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", "version": "2.27", "publicReleaseRefSpec": [ "^refs/heads/master$", "^refs/heads/v\\d+(?:\\.\\d+)?$" ], "cloudBuild": { "setVersionVariables": true, "buildNumber": { "enabled": true, "includeCommitId": { "when": "always" } } } }
10 changes: 10 additions & 0 deletions webroot/adm/site.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ <h3>Inputs</h3>
<input type="text" id="siteId" name="siteId">
<label for="types">Client Types:</label>
<input type="text" id="types" name="types">
<label for="created">Created:</label>
<input type="text" id="created" name="created" value="0">
</div>
<br>
<div>
Expand All @@ -39,6 +41,7 @@ <h3>Operations</h3>
<li class="ro-cki" style="display: none"><a href="#" id="doDisable">Disable Site</a></li>
<li class="ro-cki" style="display: none"><a href="#" id="doSetTypes">Set Types</a></li>
<li class="ro-cki" style="display: none"><a href="#" id="doDomainNames">Set Site Domain Names</a></li>
<li class="ro-cki" style="display: none"><a href="#" id="doCreated">Update Created</a></li>
</ul>

<br>
Expand Down Expand Up @@ -102,6 +105,13 @@ <h3>Output</h3>

doApiCall('POST', url, '#standardOutput', '#errorOutput', JSON.stringify(payload));
});

$('#doCreated').on('click', function () {
var created = parseInt($('#created').val());
var siteId = encodeURIComponent($('#siteId').val());
var url = '/api/site/created?id=' + siteId;
doApiCall('POST', url, '#standardOutput', '#errorOutput', JSON.stringify({created:created}));
});
});
</script>

Expand Down