@Test
fun `missing post ID results in invalid id response`() {
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-post-id"))
+ assertThatJsonFailed("invalid-post-id")
}
@Test
fun `empty post ID results in invalid id response`() {
addRequestParameter("post", "")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-post-id"))
+ assertThatJsonFailed("invalid-post-id")
}
@Test
fun `invalid post ID results in success but does not bookmark anything`() {
addRequestParameter("post", "missing")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
verify(core, never()).bookmarkPost(any<Post>())
}
fun `valid post ID results in success and bookmarks the post`() {
addRequestParameter("post", "valid-post-id")
val post = addNewPost("valid-post-id", "1", 2)
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
verify(core).bookmarkPost(post)
}
@Test
fun `missing text parameter returns error`() {
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("text-required"))
+ assertThatJsonFailed("text-required")
}
@Test
fun `empty text returns error`() {
addRequestParameter("text", "")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("text-required"))
+ assertThatJsonFailed("text-required")
}
@Test
fun `whitespace-only text returns error`() {
addRequestParameter("text", " ")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("text-required"))
+ assertThatJsonFailed("text-required")
}
@Test
addRequestParameter("text", "test")
val post = createPost()
whenever(core.createPost(currentSone, Optional.absent(), "test")).thenReturn(post)
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["postId"]?.asText(), equalTo("id"))
assertThat(json["sone"]?.asText(), equalTo(currentSone.id))
assertThat(json["recipient"], nullValue())
addRequestParameter("recipient", "invalid")
val post = createPost()
whenever(core.createPost(currentSone, Optional.absent(), "test")).thenReturn(post)
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["postId"]?.asText(), equalTo("id"))
assertThat(json["sone"]?.asText(), equalTo(currentSone.id))
assertThat(json["recipient"], nullValue())
addSone(recipient)
val post = createPost("valid")
whenever(core.createPost(currentSone, Optional.of(recipient), "test")).thenReturn(post)
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["postId"]?.asText(), equalTo("id"))
assertThat(json["sone"]?.asText(), equalTo(currentSone.id))
assertThat(json["recipient"]?.asText(), equalTo("valid"))
addRequestHeader("Host", "freenet.test:8888")
val post = createPost()
whenever(core.createPost(currentSone, Optional.absent(), "Link KSK@foo is filtered")).thenReturn(post)
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["postId"]?.asText(), equalTo("id"))
assertThat(json["sone"]?.asText(), equalTo(currentSone.id))
assertThat(json["recipient"], nullValue())
@Test
fun `invalid post ID results in error message`() {
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-post-id"))
+ assertThatJsonFailed("invalid-post-id")
}
@Test
val reply = mock<PostReply>().apply { whenever(id).thenReturn("reply-id") }
whenever(core.createReply(currentSone, post, "")).thenReturn(reply)
addRequestParameter("post", "post-id")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["reply"]?.asText(), equalTo("reply-id"))
assertThat(json["sone"]?.asText(), equalTo("soneId"))
}
addRequestParameter("post", "post-id")
addRequestParameter("text", "Text with http://127.0.0.1:8888/KSK@foo.bar link")
addRequestHeader("Host", "127.0.0.1:8888")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["reply"]?.asText(), equalTo("reply-id"))
assertThat(json["sone"]?.asText(), equalTo("soneId"))
}
addRequestParameter("post", "post-id")
addRequestParameter("text", "Text")
addRequestParameter("sender", "local-sone")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["reply"]?.asText(), equalTo("reply-id"))
assertThat(json["sone"]?.asText(), equalTo("local-sone"))
}
@Test
fun `missing post ID results in invalid id response`() {
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-post-id"))
+ assertThatJsonFailed("invalid-post-id")
}
@Test
whenever(post.sone).thenReturn(sone)
addPost(post, "post-id")
addRequestParameter("post", "post-id")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("not-authorized"))
+ assertThatJsonFailed("not-authorized")
}
@Test
whenever(post.sone).thenReturn(sone)
addPost(post, "post-id")
addRequestParameter("post", "post-id")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
verify(core).deletePost(post)
}
@Test
fun `request without field id results in invalid field id error`() {
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-field-id"))
+ assertThatJsonFailed("invalid-field-id")
}
@Test
profile.addField("foo")
val fieldId = profile.getFieldByName("foo")!!.id
addRequestParameter("field", fieldId)
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(profile.getFieldByName("foo"), nullValue())
verify(currentSone).profile = profile
verify(core).touchConfiguration()
@Test
fun `request with missing reply results in invalid id`() {
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-reply-id"))
+ assertThatJsonFailed("invalid-reply-id")
}
@Test
whenever(reply.sone).thenReturn(sone)
addReply(reply, "reply-id")
addRequestParameter("reply", "reply-id")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("not-authorized"))
+ assertThatJsonFailed("not-authorized")
}
@Test
whenever(reply.sone).thenReturn(sone)
addReply(reply, "reply-id")
addRequestParameter("reply", "reply-id")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
verify(core).deleteReply(reply)
}
@Test
fun `request without notification returns invalid-notification-id`() {
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-notification-id"))
+ assertThatJsonFailed("invalid-notification-id")
}
@Test
val notification = mock<Notification>()
addNotification(notification, "foo")
addRequestParameter("notification", "foo")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("not-dismissable"))
+ assertThatJsonFailed("not-dismissable")
}
@Test
val notification = mock<Notification>().apply { whenever(isDismissable).thenReturn(true) }
addNotification(notification, "foo")
addRequestParameter("notification", "foo")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
verify(notification).dismiss()
}
@Test
fun `request with missing sone results in invalid-sone-id`() {
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-sone-id"))
+ assertThatJsonFailed("invalid-sone-id")
}
@Test
fun `request with invalid sone results in invalid-sone-id`() {
addRequestParameter("sone", "invalid-sone")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-sone-id"))
+ assertThatJsonFailed("invalid-sone-id")
}
@Test
val sone = mock<Sone>()
addSone(sone, "sone-id")
addRequestParameter("sone", "sone-id")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
verify(core).distrustSone(currentSone, sone)
}
val sone = mock<Sone>()
addSone(sone, "sone-id")
addRequestParameter("sone", "sone-id")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["trustValue"]?.asInt(), equalTo(-33))
}
@Test
fun `request without album results in invalid-album-id`() {
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-album-id"))
+ assertThatJsonFailed("invalid-album-id")
}
@Test
whenever(album.sone).thenReturn(sone)
addAlbum(album)
addRequestParameter("album", "album-id")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("not-authorized"))
+ assertThatJsonFailed("not-authorized")
}
@Test
addAlbum(album)
addRequestParameter("album", "album-id")
addRequestParameter("moveLeft", "true")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["sourceAlbumId"]?.asText(), equalTo("album-id"))
assertThat(json["destinationAlbumId"]?.asText(), equalTo("swapped"))
}
addAlbum(album)
addRequestParameter("album", "album-id")
addRequestParameter("moveRight", "true")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["sourceAlbumId"]?.asText(), equalTo("album-id"))
assertThat(json["destinationAlbumId"]?.asText(), equalTo("swapped"))
}
whenever(album.modify().setTitle("")).thenThrow(AlbumTitleMustNotBeEmpty::class.java)
addAlbum(album)
addRequestParameter("album", "album-id")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-album-title"))
+ assertThatJsonFailed("invalid-album-title")
}
@Test
addRequestParameter("title", "new title")
addRequestParameter("description", "foo http://127.0.0.1:8888/KSK@foo.html link")
addRequestHeader("Host", "127.0.0.1:8888")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["albumId"]?.asText(), equalTo("album-id"))
assertThat(json["title"]?.asText(), equalTo("new title"))
assertThat(json["description"]?.asText(), equalTo("foo KSK@foo.html link"))
@Test
fun `request without image results in invalid-image-id`() {
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-image-id"))
+ assertThatJsonFailed("invalid-image-id")
}
@Test
whenever(image.sone).thenReturn(sone)
addImage(image, "image-id")
addRequestParameter("image", "image-id")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("not-authorized"))
+ assertThatJsonFailed("not-authorized")
}
@Test
addImage(image)
addRequestParameter("image", "image-id")
addRequestParameter("moveLeft", "true")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["sourceImageId"]?.asText(), equalTo("image-id"))
assertThat(json["destinationImageId"]?.asText(), equalTo("swapped"))
verify(core).touchConfiguration()
addImage(image)
addRequestParameter("image", "image-id")
addRequestParameter("moveRight", "true")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["sourceImageId"]?.asText(), equalTo("image-id"))
assertThat(json["destinationImageId"]?.asText(), equalTo("swapped"))
verify(core).touchConfiguration()
whenever(image.sone).thenReturn(sone)
addImage(image)
addRequestParameter("image", "image-id")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-image-title"))
+ assertThatJsonFailed("invalid-image-title")
}
@Test
addRequestParameter("title", "some title")
addRequestParameter("description", "some http://127.0.0.1:8888/KSK@foo link")
addRequestHeader("Host", "127.0.0.1:8888")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["title"]?.asText(), equalTo("some title"))
assertThat(json["description"]?.asText(), equalTo("some KSK@foo link"))
assertThat(json["parsedDescription"]?.asText(), equalTo("rendered description"))
@Test
fun `request without field id results in invalid-field-id`() {
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-field-id"))
+ assertThatJsonFailed("invalid-field-id")
}
@Test
val field = currentSone.profile.addField("test-field")
addRequestParameter("field", field.id)
addRequestParameter("name", " \t ")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-parameter-name"))
+ assertThatJsonFailed("invalid-parameter-name")
}
@Test
val field = currentSone.profile.addField("test-field")
addRequestParameter("field", field.id)
addRequestParameter("name", "other-field")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("duplicate-field-name"))
+ assertThatJsonFailed("duplicate-field-name")
}
@Test
val field = profile.addField("test-field")
addRequestParameter("field", field.id)
addRequestParameter("name", " new name ")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(field.name, equalTo("new name"))
verify(currentSone).profile = profile
}
@Test
fun `request without sone id results in invalid-sone-id`() {
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-sone-id"))
+ assertThatJsonFailed("invalid-sone-id")
}
@Test
fun `request with sone follows sone`() {
addSone(mock<Sone>().apply { whenever(id).thenReturn("sone-id") })
addRequestParameter("sone", "sone-id")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
verify(core).followSone(currentSone, "sone-id")
}
val sone = mock<Sone>()
addSone(sone, "sone-id")
addRequestParameter("sone", "sone-id")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
verify(core).markSoneKnown(sone)
}
fun `request with invalid post id results in invalid-post-id`() {
addRequestParameter("type", "post")
addRequestParameter("post", "invalid")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-post-id"))
+ assertThatJsonFailed("invalid-post-id")
}
@Test
fun `request with missing post id results in invalid-post-id`() {
addRequestParameter("type", "post")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-post-id"))
+ assertThatJsonFailed("invalid-post-id")
}
@Test
addLikes(post, createSone(2), createSone(1), createSone(3))
addRequestParameter("type", "post")
addRequestParameter("post", "post-id")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["likes"]?.asInt(), equalTo(3))
assertThat(json["sones"]!!.toList().map { it["id"].asText() to it["name"].asText() }, contains(
"S1" to "F1 M1 L1",
fun `request with invalid reply id results in invalid-reply-id`() {
addRequestParameter("type", "reply")
addRequestParameter("reply", "invalid")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-reply-id"))
+ assertThatJsonFailed("invalid-reply-id")
}
@Test
fun `request with missing reply id results in invalid-reply-id`() {
addRequestParameter("type", "reply")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-reply-id"))
+ assertThatJsonFailed("invalid-reply-id")
}
@Test
addLikes(reply, createSone(2), createSone(1), createSone(3))
addRequestParameter("type", "reply")
addRequestParameter("reply", "reply-id")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["likes"]?.asInt(), equalTo(3))
assertThat(json["sones"]!!.toList().map { it["id"].asText() to it["name"].asText() }, contains(
"S1" to "F1 M1 L1",
fun `request with invalid type results in invalid-type`() {
addRequestParameter("type", "invalid")
addRequestParameter("invalid", "foo")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-type"))
+ assertThatJsonFailed("invalid-type")
}
}
@Test
fun `notification hash is calculated correctly`() {
testNotifications.forEach { addNotification(it) }
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["notificationHash"]?.asInt(), equalTo(listOf(1, 0, 2).map(testNotifications::get).hashCode()))
}
@Test
fun `options are included correctly`() {
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["options"]!!["ShowNotification/NewSones"].asBoolean(), equalTo(true))
assertThat(json["options"]!!["ShowNotification/NewPosts"].asBoolean(), equalTo(true))
assertThat(json["options"]!!["ShowNotification/NewReplies"].asBoolean(), equalTo(true))
currentSone.options.isShowNewSoneNotifications = false
currentSone.options.isShowNewPostNotifications = false
currentSone.options.isShowNewReplyNotifications = false
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["options"]!!["ShowNotification/NewSones"].asBoolean(), equalTo(false))
assertThat(json["options"]!!["ShowNotification/NewPosts"].asBoolean(), equalTo(false))
assertThat(json["options"]!!["ShowNotification/NewReplies"].asBoolean(), equalTo(false))
@Test
fun `options are not included if user is not logged in`() {
unsetCurrentSone()
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["options"]?.toList(), empty())
}
@Test
fun `notifications are rendered correctly`() {
testNotifications.forEach { addNotification(it) }
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["notifications"]!!.toList().map { node -> listOf("id", "text", "createdTime", "lastUpdatedTime", "dismissable").map { it to node.get(it).asText() }.toMap() }, containsInAnyOrder(
mapOf("id" to "n1", "createdTime" to "2000", "lastUpdatedTime" to "5000", "dismissable" to "true", "text" to "t1"),
mapOf("id" to "n2", "createdTime" to "1000", "lastUpdatedTime" to "6000", "dismissable" to "false", "text" to "t2"),
}
testNotifications.forEach { addNotification(it) }
addNotification(templateNotification)
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["notifications"]!!.last()["text"].asText(), equalTo("t4"))
val templateContext = argumentCaptor<TemplateContext>()
verify(templateNotification).render(templateContext.capture(), any())
@Test
fun `request with missing post results in invalid-post-id`() {
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-post-id"))
+ assertThatJsonFailed("invalid-post-id")
}
@Test
webInterface.templateContextFactory.addAccessor(Any::class.java, ReflectionAccessor())
addPost(post)
addRequestParameter("post", "post-id")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["post"]!!["id"].asText(), equalTo("post-id"))
assertThat(json["post"]!!["time"].asLong(), equalTo(1000L))
assertThat(json["post"]!!["sone"].asText(), equalTo("sone-id"))
@Test
fun `request without reply id results in invalid-reply-id`() {
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-reply-id"))
+ assertThatJsonFailed("invalid-reply-id")
}
@Test
webInterface.templateContextFactory.addAccessor(Any::class.java, ReflectionAccessor())
addReply(reply)
addRequestParameter("reply", "reply-id")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["reply"]!!["id"].asText(), equalTo("reply-id"))
assertThat(json["reply"]!!["soneId"].asText(), equalTo("sone-id"))
assertThat(json["reply"]!!["postId"].asText(), equalTo("post-id"))
@Test
fun `request without any parameters responds with empty post and reply times`() {
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["postTimes"]?.toList(), emptyIterable())
assertThat(json["replyTimes"]?.toList(), emptyIterable())
}
fun `request with single post parameter responds with post times and empty reply times`() {
addPost(testPosts[0])
addRequestParameter("posts", "post1")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["postTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
"post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01")
))
fun `request with single reply parameter responds with reply times and empty post times`() {
addReply(testReplies[0])
addRequestParameter("replies", "reply1")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["postTimes"]?.toList(), emptyIterable())
assertThat(json["replyTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
"reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01")
addPost(testPosts[0])
addPost(testPosts[1])
addRequestParameter("posts", "post1,post2,post3")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["postTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
"post1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01"),
"post2" to jsonObject("timeText" to "2000", "refreshTime" to 4L, "tooltip" to "Jan 1, 1970, 00:00:02")
addReply(testReplies[0])
addReply(testReplies[1])
addRequestParameter("replies", "reply1,reply2,reply3")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["postTimes"]?.toList(), emptyIterable())
assertThat(json["replyTimes"]!!.fields().asSequence().map { it.key to it.value }.toList(), containsInAnyOrder<Pair<String, JsonNode>>(
"reply1" to jsonObject("timeText" to "1000", "refreshTime" to 2L, "tooltip" to "Jan 1, 1970, 00:00:01"),
fun `translation is returned correctly`() {
addTranslation("foo", "bar")
addRequestParameter("key", "foo")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["value"]?.asText(), equalTo("bar"))
}
private val expectedPath: String,
private val requiresLogin: Boolean = true,
private val needsFormPassword: Boolean = true,
- pageSupplier: (WebInterface) -> JsonPage = { mock() }): TestObjects() {
+ pageSupplier: (WebInterface) -> JsonPage = { mock() }) : TestObjects() {
protected open val page: JsonPage by lazy { pageSupplier(webInterface) }
protected val json by lazy {
page.createJsonObject(freenetRequest)
}
- protected val JsonReturnObject.error get() = if (this is JsonErrorReturnObject) this.error else null
+ private val JsonReturnObject.error get() = if (this is JsonErrorReturnObject) this.error else null
+
+ protected fun assertThatJsonIsSuccessful() {
+ assertThat(json.isSuccess, equalTo(true))
+ }
+
+ protected fun assertThatJsonFailed(error: String? = null) {
+ assertThat(json.isSuccess, equalTo(false))
+ error?.run { assertThat(json.error, equalTo(this)) }
+ }
@Test
fun `page returns correct path`() {
fun `request with invalid type results in invalid-type error`() {
addRequestParameter("type", "invalid")
addRequestParameter("invalid", "invalid-id")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-type"))
+ assertThatJsonFailed("invalid-type")
}
@Test
addRequestParameter("type", "post")
addRequestParameter("post", "post-id")
addPost(mock<Post>().apply { whenever(id).thenReturn("post-id") })
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
verify(currentSone).addLikedPostId("post-id")
verify(core).touchConfiguration()
}
addRequestParameter("type", "reply")
addRequestParameter("reply", "reply-id")
addReply(mock<PostReply>().apply { whenever(id).thenReturn("reply-id") })
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
verify(currentSone).addLikedReplyId("reply-id")
verify(core).touchConfiguration()
}
@Test
fun `request without valid sone results in invalid-sone-id`() {
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-sone-id"))
+ assertThatJsonFailed("invalid-sone-id")
}
@Test
val sone = mock<Sone>()
addLocalSone(sone, "sone-id")
addRequestParameter("sone", "sone-id")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
verify(core).lockSone(sone)
}
@Test
fun `request without type results in invalid-type`() {
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-type"))
+ assertThatJsonFailed("invalid-type")
}
@Test
fun `request with unknown sone returns successfully`() {
addRequestParameter("type", "sone")
addRequestParameter("id", "invalid")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
verify(core, never()).markSoneKnown(any())
}
val sone2 = mock<Sone>().apply { whenever(id).thenReturn("sone-id2") }
addSone(sone1)
addSone(sone2)
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
verify(core).markSoneKnown(sone1)
verify(core).markSoneKnown(sone2)
}
val post2 = mock<Post>()
addPost(post1, "post1")
addPost(post2, "post2")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
verify(core).markPostKnown(post1)
verify(core).markPostKnown(post2)
}
val reply2 = mock<PostReply>()
addReply(reply1, "reply1")
addReply(reply2, "reply2")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
verify(core).markReplyKnown(reply1)
verify(core).markReplyKnown(reply2)
}
@Test
fun `request without field id results in invalid-field-id`() {
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-field-id"))
+ assertThatJsonFailed("invalid-field-id")
}
@Test
fun `request with invalid direction results in invalid-direction`() {
val fieldId = profile.addField("someField").id
addRequestParameter("field", fieldId)
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-direction"))
+ assertThatJsonFailed("invalid-direction")
}
@Test
val fieldId = profile.addField("someField").id
addRequestParameter("field", fieldId)
addRequestParameter("direction", "up")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("not-possible"))
+ assertThatJsonFailed("not-possible")
}
@Test
val fieldId = profile.addField("someField").id
addRequestParameter("field", fieldId)
addRequestParameter("direction", "down")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("not-possible"))
+ assertThatJsonFailed("not-possible")
}
@Test
val fieldId = profile.addField("someField").id
addRequestParameter("field", fieldId)
addRequestParameter("direction", "up")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(profile.fields[0].id, equalTo(fieldId))
verify(core).touchConfiguration()
verify(currentSone).profile = profile
profile.addField("firstField")
addRequestParameter("field", fieldId)
addRequestParameter("direction", "down")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(profile.fields.last().id, equalTo(fieldId))
verify(core).touchConfiguration()
verify(currentSone).profile = profile
@Test
fun `request with invalid sone results in invalid-sone-id`() {
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-sone-id"))
+ assertThatJsonFailed("invalid-sone-id")
}
@Test
fun `request with valid sone trust sone`() {
addSone(sone, "sone-id")
addRequestParameter("sone", "sone-id")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
verify(core).trustSone(currentSone, sone)
}
addSone(sone, "sone-id")
addRequestParameter("sone", "sone-id")
core.preferences.positiveTrust = 31
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["trustValue"]?.asInt(), equalTo(31))
}
@Test
fun `request without post id results in invalid-post-id`() {
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-post-id"))
+ assertThatJsonFailed("invalid-post-id")
}
@Test
fun `request with empty post id results in invalid-post-id`() {
addRequestParameter("post", "")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-post-id"))
+ assertThatJsonFailed("invalid-post-id")
}
@Test
val post = mock<Post>()
addPost(post, "post-id")
addRequestParameter("post", "post-id")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
verify(core).unbookmarkPost(eq(post))
}
@Test
fun `request without sone returns invalid-sone-id`() {
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-sone-id"))
+ assertThatJsonFailed("invalid-sone-id")
}
@Test
fun `request with invalid sone returns invalid-sone-id`() {
addRequestParameter("sone", "invalid")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-sone-id"))
+ assertThatJsonFailed("invalid-sone-id")
}
@Test
fun `request with valid sone unfollows sone`() {
addSone(mock(), "sone-id")
addRequestParameter("sone", "sone-id")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
verify(core).unfollowSone(currentSone, "sone-id")
}
@Test
fun `request for post without id results in invalid-post-id`() {
addRequestParameter("type", "post")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-post-id"))
+ assertThatJsonFailed("invalid-post-id")
}
@Test
fun `request for invalid type results in invalid-type`() {
addRequestParameter("type", "invalid")
addRequestParameter("invalid", "invalid")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-type"))
+ assertThatJsonFailed("invalid-type")
}
@Test
fun `request for post with id removes id from liked posts`() {
addRequestParameter("type", "post")
addRequestParameter("post", "post-id")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
verify(currentSone).removeLikedPostId("post-id")
verify(core).touchConfiguration()
}
@Test
fun `request for reply without id results in invalid-reply-id`() {
addRequestParameter("type", "reply")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-reply-id"))
+ assertThatJsonFailed("invalid-reply-id")
}
@Test
fun `request for reply with id removes id from liked replys`() {
addRequestParameter("type", "reply")
addRequestParameter("reply", "reply-id")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
verify(currentSone).removeLikedReplyId("reply-id")
verify(core).touchConfiguration()
}
@Test
fun `request without sone results in invalid-sone-id`() {
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-sone-id"))
+ assertThatJsonFailed("invalid-sone-id")
}
@Test
fun `request with invalid sone results in invalid-sone-id`() {
addRequestParameter("sone", "invalid")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-sone-id"))
+ assertThatJsonFailed("invalid-sone-id")
}
@Test
val sone = mock<Sone>()
addLocalSone(sone, "sone-id")
addRequestParameter("sone", "sone-id")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
verify(core).unlockSone(sone)
}
@Test
fun `request without sone results in invalid-sone-id`() {
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-sone-id"))
+ assertThatJsonFailed("invalid-sone-id")
}
@Test
fun `request with invalid sone results in invalid-sone-id`() {
addRequestParameter("sone", "invalid")
- assertThat(json.isSuccess, equalTo(false))
- assertThat(json.error, equalTo("invalid-sone-id"))
+ assertThatJsonFailed("invalid-sone-id")
}
@Test
val sone = mock<Sone>()
addSone(sone, "sone-id")
addRequestParameter("sone", "sone-id")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
verify(core).untrustSone(currentSone, sone)
}
val sone = mock<Sone>()
addSone(sone, "sone-id")
addRequestParameter("sone", "sone-id")
- assertThat(json.isSuccess, equalTo(true))
+ assertThatJsonIsSuccessful()
assertThat(json["trustValue"], nullValue())
}