Skip to content

Validate put request json #107

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

Merged
merged 6 commits into from
Feb 17, 2025
Merged
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 build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
id("io.micronaut.application") version "2.0.2"
}

version = "1.0.1"
version = "1.0.2"
group = "com.jsonblob"

val kotlinVersion= project.properties["kotlinVersion"]
Expand Down
26 changes: 15 additions & 11 deletions src/main/kotlin/jsonblob/api/http/ApiController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class ApiController(
return false
}

private fun delete(blobId: String) = jsonBlobStore.remove(blobId)
private fun delete(blobId: String) = jsonBlobStore.exists(blobId) && jsonBlobStore.remove(blobId)

private fun updateFirstBlobFromPath(path: String, json: String): JsonBlob? {
val ids = blobIdsFromPath(path)
Expand All @@ -169,17 +169,21 @@ class ApiController(
}

private fun update(blobId: String, json: String): JsonBlob? {
val resolver = idResolvers.firstOrNull { it.handles(blobId) }
return if (resolver != null) {
val created = resolver.resolveTimestamp(blobId)
val jsonBlob = JsonBlob(
id = blobId,
json = json,
created = created
)
jsonBlobStore.write(jsonBlob)
if (JsonCleaner.validJson(json)) {
val resolver = idResolvers.firstOrNull { it.handles(blobId) }
return if (resolver != null) {
val created = resolver.resolveTimestamp(blobId)
val jsonBlob = JsonBlob(
id = blobId,
json = json,
created = created
)
jsonBlobStore.write(jsonBlob)
} else {
null
}
} else {
null
throw HttpStatusException(HttpStatus.BAD_REQUEST, "Invalid JSON")
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/jsonblob/core/store/JsonBlobStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ abstract class JsonBlobStore(
created = created
)
}.onFailure {
log.warn { "Couldn't read JsonBlob with id=$id " }
log.debug { "Couldn't read JsonBlob with id=$id " }
}.getOrNull()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ open class FileSystemJsonBlobStore(
lock.unlock()
}
}.onFailure {
log.warn { "Couldn't retrieve JsonBlob with id=$id " }
log.debug { "Couldn't retrieve JsonBlob with id=$id " }
}.getOrNull()
}

Expand Down
44 changes: 1 addition & 43 deletions src/main/resources/views/editor.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@
<script type="text/javascript">
{{#if blobId}}
var blobId = "{{blobId}}";
ga('send', 'event', "Blobs", "load", blobId);
{{else}}
var blobId = null;
{{/if}}
Expand Down Expand Up @@ -322,11 +321,6 @@
app.codeEditor.resize();
}
$('#tour').click(function() {
if (blobId) {
ga('send', 'event', "Tour", "start", blobId);
} else {
ga('send', 'event', "Tour", "start");
}
$('#tourContent').joyride({
autoStart: true,
expose: true,
Expand All @@ -335,21 +329,7 @@
tipAnimationFadeSpeed: 300,
cookieMonster: true,
cookieName: 'jsonblobTour',
cookieDomain: true,
postRideCallback: function () {
if (blobId) {
ga('send', 'event', "Tour", "complete", blobId);
} else {
ga('send', 'event', "Tour", "complete");
}
},
'postStepCallback': function (index, tip) {
if (blobId) {
ga('send', 'event', "Tour", "step", blobId, index);
} else {
ga('send', 'event', "Tour", "step", null, index);
}
}
cookieDomain: true
});
});
$(document).ready(function() {
Expand All @@ -365,11 +345,6 @@
}
});
$("#createGist").click(function() {
if (blobId) {
ga('send', 'event', "Blobs", "gist", blobId);
} else {
ga('send', 'event', "Blobs", "gist");
}
var gist = {
"description": "JSON Blob from " + document.URL,
"public": false,
Expand All @@ -394,11 +369,6 @@
});
});
$("#clear").click(function() {
if (blobId) {
ga('send', 'event', "Blobs", "clear", blobId);
} else {
ga('send', 'event', "Blobs", "clear");
}
app.codeEditor.set({});
app.treeEditor.set({});
blobId = null;
Expand All @@ -419,11 +389,6 @@
alert("Please enter a URL for the request to be sent to!");
return false;
}
if (blobId) {
ga('send', 'event', "Blobs", "makeRequest", blobId);
} else {
ga('send', 'event', "Blobs", "makeRequest");
}
var method = $(this).text();
var headers = {};
$(".requestHeader").each(function() {
Expand Down Expand Up @@ -455,7 +420,6 @@
}
$('#makeRequestModal').modal('hide');
$('#makeRequestSuccess').modal('show');
ga('send', 'event', "Blobs", "makeRequestSuccess");
},
error: function(jqXHR, textStatus, errorThrown) {
if (errorThrown) {
Expand All @@ -470,7 +434,6 @@
$(".requestedUrl").text($("#requestUrl").val());
$("#makeRequestModal").modal('hide');
$("#makeRequestFailure").modal('show');
ga('send', 'event', "Blobs", "makeRequestFailure");
},
cache: false
};
Expand All @@ -490,11 +453,6 @@
$("#save").click(function() {
var jsonString = app.codeEditor == app.lastChanged ? app.codeEditor.getText() : app.treeEditor.getText();
var valid = !isValidJson(jsonString);
if (blobId) {
ga('send', 'event', "Blobs", "save", blobId, valid ? 1 : 0);
} else {
ga('send', 'event', "Blobs", "save", null, valid ? 1 : 0);
}
if (valid) {
$('#invalidJson').modal('show');
return;
Expand Down
14 changes: 7 additions & 7 deletions src/main/resources/views/ga.hbs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id={{gaWebPropertyID}}"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '{{gaWebPropertyID}}', 'auto');
ga('require', 'displayfeatures');
ga('send', 'pageview');
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', '{{gaWebPropertyID}}');
</script>
27 changes: 21 additions & 6 deletions src/test/kotlin/jsonblob/api/http/ApiTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,20 @@ import io.micronaut.http.HttpResponse
import io.micronaut.http.MediaType
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.http.client.exceptions.HttpClientResponseException
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import io.micronaut.test.support.TestPropertyProvider
import jsonblob.config.S3ClientBuilderListener
import jsonblob.core.compression.compressor.GZIPBlobCompressor
import jsonblob.core.id.Type1UUIDJsonBlobHandler
import jsonblob.core.store.JsonBlobStore
import mu.KotlinLogging
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.BeforeAll
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.skyscreamer.jsonassert.JSONAssert.assertEquals
import org.testcontainers.containers.localstack.LocalStackContainer
import org.testcontainers.shaded.com.google.common.io.Files
import org.testcontainers.utility.DockerImageName
import software.amazon.awssdk.services.s3.S3Client
import java.util.UUID
import javax.inject.Inject


Expand Down Expand Up @@ -129,6 +126,24 @@ class ApiTest: TestPropertyProvider {
}
}

@Test
fun `blob is created on API PUT`() {
val resp = client
.toBlocking()
.exchange(PUT("/api/jsonBlob/${type1UUIDJsonBlobHandler.generate()}", json).contentType(MediaType.APPLICATION_JSON_TYPE), String::class.java)
assertThat(resp.code()).isEqualTo(200)
}

@Test
fun `blob is not created on bad API PUT`() {
assertThatThrownBy {
client
.toBlocking()
.exchange(PUT("/api/jsonBlob/${UUID.randomUUID()}", json).contentType(MediaType.APPLICATION_JSON_TYPE), String::class.java)

}.isInstanceOf(HttpClientResponseException::class.java)
}

@Test
fun `blob is updated on custom API PUT`() {
validateUpdate {
Expand Down