diff --git a/pom.xml b/pom.xml index 54ce19e9..238c0eba 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.uid2 uid2-admin - 2.26.7-f999928e9e + 2.27.0-18fda5471a-ian-UID2-2010-update-site-created UTF-8 diff --git a/src/main/java/com/uid2/admin/vertx/service/SiteService.java b/src/main/java/com/uid2/admin/vertx/service/SiteService.java index e27f5625..2467f4f6 100644 --- a/src/main/java/com/uid2/admin/vertx/service/SiteService.java +++ b/src/main/java/com/uid2/admin/vertx/service/SiteService.java @@ -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) { @@ -296,6 +301,40 @@ private void handleSiteDomains(RoutingContext rc) { } } + private void handleSiteCreated(RoutingContext rc) { + 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 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 getNormalizedDomainNames(RoutingContext rc, JsonArray domainNamesJa) { List domainNames = domainNamesJa.stream().map(String::valueOf).collect(Collectors.toList()); diff --git a/src/test/java/com/uid2/admin/vertx/SiteServiceTest.java b/src/test/java/com/uid2/admin/vertx/SiteServiceTest.java index 05697826..d1681223 100644 --- a/src/test/java/com/uid2/admin/vertx/SiteServiceTest.java +++ b/src/test/java/com/uid2/admin/vertx/SiteServiceTest.java @@ -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.*; @@ -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); diff --git a/version.json b/version.json index ad1028bd..a1493847 100644 --- a/version.json +++ b/version.json @@ -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" } } } } diff --git a/webroot/adm/site.html b/webroot/adm/site.html index dc2a5e51..7cdc100e 100644 --- a/webroot/adm/site.html +++ b/webroot/adm/site.html @@ -20,6 +20,8 @@

Inputs

+ +
@@ -39,6 +41,7 @@

Operations

+
@@ -102,6 +105,13 @@

Output

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})); + }); });