Skip to content

Commit

Permalink
Add tests for coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jdpgrailsdev committed Dec 23, 2024
1 parent f369691 commit 61ec8c7
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 2 deletions.
1 change: 1 addition & 0 deletions bigbone/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ dependencies {
testImplementation(libs.kotlin.reflect)
testImplementation libs.mockk
testImplementation libs.mockk.dsl
testImplementation libs.okhttp.mockwebserver
}
57 changes: 55 additions & 2 deletions bigbone/src/test/kotlin/social/bigbone/MastodonClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MastodonClientTest {
@Test
fun `Given response body without instance version, when building MastodonClient, then fail with InstanceVersionRetrievalException`() {
val serverUrl = "foo.bar"
val scheme = "https"
val scheme = "http"
val port = 443
val clientBuilder = spyk(MastodonClient.Builder(serverUrl)) {
// Mock internal NodeInfoClient so that we don't open the site in unit testing
Expand All @@ -40,6 +40,33 @@ class MastodonClientTest {
}
every { executeInstanceRequest() } answers { responseMock }
}
.withHttpsDisabled()
.withPort(port)

invoking(clientBuilder::build)
.shouldThrow(ServerInfoRetrievalException::class)
.withMessage("just for testing")
}

@Test
fun `Given response body without instance version, when building MastodonClient with defaults, then fail with InstanceVersionRetrievalException`() {
val serverUrl = "foo.bar"
val clientBuilder = spyk(MastodonClient.Builder(serverUrl)) {
// Mock internal NodeInfoClient so that we don't open the site in unit testing
mockkObject(NodeInfoClient)
every { NodeInfoClient.retrieveServerInfo(host = serverUrl) } throws ServerInfoRetrievalException(
"just for testing",
null
)

val responseMock = mockk<Response> {
val invalidResponseBody = "{ \"foo\": \"bar\" }"
every { body } answers { invalidResponseBody.toResponseBody("application/json".toMediaType()) }
every { isSuccessful } answers { true }
every { close() } returns Unit
}
every { executeInstanceRequest() } answers { responseMock }
}

invoking(clientBuilder::build)
.shouldThrow(ServerInfoRetrievalException::class)
Expand All @@ -49,7 +76,7 @@ class MastodonClientTest {
@Test
fun `Given a server that doesn't run Mastodon, when building MastodonClient, then fail with InstanceVersionRetrievalException`() {
val serverUrl = "diasp.eu"
val scheme = "https"
val scheme = "http"
val port = 443
val clientBuilder = spyk(MastodonClient.Builder(serverUrl)) {
// Mock internal NodeInfoClient so that we don't open the site in unit testing
Expand All @@ -67,6 +94,32 @@ class MastodonClientTest {
}
every { executeInstanceRequest() } answers { responseMock }
}
.withHttpsDisabled()
.withPort(port)
invoking(clientBuilder::build)
.shouldThrow(InstanceVersionRetrievalException::class)
.withMessage("Server $serverUrl doesn't appear to run Mastodon")
}

@Test
fun `Given a server that doesn't run Mastodon, when building MastodonClient with defaults, then fail with InstanceVersionRetrievalException`() {
val serverUrl = "diasp.eu"
val clientBuilder = spyk(MastodonClient.Builder(serverUrl)) {
// Mock internal NodeInfoClient so that we don't open the site in unit testing
mockkObject(NodeInfoClient)
every { NodeInfoClient.retrieveServerInfo(host = serverUrl) } returns Server(
schemaVersion = "2.0",
software = Server.Software(name = "diaspora", version = "0.7.18.2-p84e7e411")
)

val responseMock = mockk<Response> {
every { code } answers { 404 }
every { message } answers { "Not Found" }
every { isSuccessful } answers { false }
every { close() } returns Unit
}
every { executeInstanceRequest() } answers { responseMock }
}
invoking(clientBuilder::build)
.shouldThrow(InstanceVersionRetrievalException::class)
.withMessage("Server $serverUrl doesn't appear to run Mastodon")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package social.bigbone.nodeinfo.entity

import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.amshove.kluent.shouldBeEqualTo
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import social.bigbone.JSON_SERIALIZER
import social.bigbone.api.exception.ServerInfoRetrievalException
import social.bigbone.api.exception.ServerInfoUrlRetrievalException
import social.bigbone.nodeinfo.NodeInfoClient
import social.bigbone.testtool.AssetsUtil

class NodeInfoTest {
Expand All @@ -21,4 +29,79 @@ class NodeInfoTest {
link.rel shouldBeEqualTo "http://nodeinfo.diaspora.software/ns/schema/2.0"
link.href shouldBeEqualTo "https://example.org/nodeinfo/2.0"
}

@Test
fun retrieveServerInfoWithOverloads() {
val mockWebServer = MockWebServer().apply {
start()
}

try {
val schemaVersion = "1.0.0"
val softwareName = "name"
val softwareVersion = "1.0.0"
val nodeInfoResponseBody = "{\"links\":[{\"href\":\"${mockWebServer.url("")}\",\"rel\":\"http://nodeinfo.diaspora.software/ns/schema/2.0\"}]}"
val serverResponseBody = "{\"version\":\"$schemaVersion\", \"software\":{ \"name\":\"$softwareName\", \"version\":\"$softwareVersion\"}}"

mockWebServer.enqueue(MockResponse().setResponseCode(200).setBody(nodeInfoResponseBody))
mockWebServer.enqueue(MockResponse().setResponseCode(200).setBody(serverResponseBody))

val server = NodeInfoClient.retrieveServerInfo(host = mockWebServer.url("").toUrl().host, scheme = "http", port = mockWebServer.port)
assertNotNull(server)
assertNotNull(server?.software)
assertEquals(schemaVersion, server?.schemaVersion)
assertEquals(softwareName, server?.software?.name)
assertEquals(softwareVersion, server?.software?.version)
} finally {
mockWebServer.shutdown()
}
}

@Test
fun retrieveServerInfoWithOverloadsUnsuccessfulNodeInfoResponse() {
val mockWebServer = MockWebServer().apply {
start()
}

try {
val schemaVersion = "1.0.0"
val softwareName = "name"
val softwareVersion = "1.0.0"
val nodeInfoResponseBody = "{\"links\":[{\"href\":\"${mockWebServer.url("")}\",\"rel\":\"http://nodeinfo.diaspora.software/ns/schema/2.0\"}]}"
val serverResponseBody = "{\"version\":\"$schemaVersion\", \"software\":{ \"name\":\"$softwareName\", \"version\":\"$softwareVersion\"}}"

mockWebServer.enqueue(MockResponse().setResponseCode(400).setBody(nodeInfoResponseBody))
mockWebServer.enqueue(MockResponse().setResponseCode(200).setBody(serverResponseBody))

assertThrows<ServerInfoUrlRetrievalException> {
NodeInfoClient.retrieveServerInfo(host = mockWebServer.url("").toUrl().host, scheme = "http", port = mockWebServer.port)
}
} finally {
mockWebServer.shutdown()
}
}

@Test
fun retrieveServerInfoWithOverloadsUnsuccessfulServerInfoResponse() {
val mockWebServer = MockWebServer().apply {
start()
}

try {
val schemaVersion = "1.0.0"
val softwareName = "name"
val softwareVersion = "1.0.0"
val nodeInfoResponseBody = "{\"links\":[{\"href\":\"${mockWebServer.url("")}\",\"rel\":\"http://nodeinfo.diaspora.software/ns/schema/2.0\"}]}"
val serverResponseBody = "{\"version\":\"$schemaVersion\", \"software\":{ \"name\":\"$softwareName\", \"version\":\"$softwareVersion\"}}"

mockWebServer.enqueue(MockResponse().setResponseCode(200).setBody(nodeInfoResponseBody))
mockWebServer.enqueue(MockResponse().setResponseCode(400).setBody(serverResponseBody))

assertThrows<ServerInfoRetrievalException> {
NodeInfoClient.retrieveServerInfo(host = mockWebServer.url("").toUrl().host, scheme = "http", port = mockWebServer.port)
}
} finally {
mockWebServer.shutdown()
}
}
}

0 comments on commit 61ec8c7

Please sign in to comment.