import net.pterodactylus.sone.web.page.FreenetRequest
import net.pterodactylus.util.notify.Notification
import net.pterodactylus.util.web.Method.GET
+import org.hamcrest.MatcherAssert.assertThat
+import org.hamcrest.Matchers.equalTo
import org.junit.Before
+import org.junit.Test
import org.mockito.ArgumentMatchers.anyBoolean
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.ArgumentMatchers.anyString
/**
* Base class for tests for any [JsonPage] implementations.
*/
-open class JsonPageTest(pageSupplier: (WebInterface) -> JsonPage = { _ -> mock<JsonPage>() }) {
+abstract class JsonPageTest(
+ private val expectedPath: String,
+ private val requiresLogin: Boolean = true,
+ private val needsFormPassword: Boolean = true,
+ pageSupplier: (WebInterface) -> JsonPage = { _ -> mock<JsonPage>() }) {
protected val webInterface = mock<WebInterface>()
protected val core = mock<Core>()
private val requestParts = mutableMapOf<String, String>()
private val localSones = mutableMapOf<String, Sone>()
private val remoteSones = mutableMapOf<String, Sone>()
+ private val posts = mutableMapOf<String, Post>()
private val newPosts = mutableMapOf<String, Post>()
private val newReplies = mutableMapOf<String, PostReply>()
private val linkedElements = mutableMapOf<String, LinkedElement>()
@Before
fun setupCore() {
whenever(core.getSone(anyString())).thenAnswer { (localSones + remoteSones)[it.getArgument(0)].asOptional() }
- whenever(core.getPost(anyString())).thenAnswer { newPosts[it[0]].asOptional() }
+ whenever(core.getLocalSone(anyString())).thenAnswer { localSones[it[0]] }
+ whenever(core.getPost(anyString())).thenAnswer { (posts + newPosts)[it[0]].asOptional() }
}
@Before
remoteSones += sone.id to sone
}
+ protected fun addLocalSone(id: String, sone: Sone) {
+ localSones += id to sone
+ }
+
+ protected fun addPost(id: String, post: Post) {
+ posts[id] = post
+ }
+
protected fun addNewPost(id: String, soneId: String, time: Long, recipientId: String? = null) =
mock<Post>().apply {
whenever(this.id).thenReturn(id)
linkedElements[link] = LinkedElement(link, failed, loading)
}
+ @Test
+ fun `page returns correct path`() {
+ assertThat(page.path, equalTo(expectedPath))
+ }
+
+ @Test
+ fun `page needs form password`() {
+ assertThat(page.needsFormPassword(), equalTo(needsFormPassword))
+ }
+
+ @Test
+ fun `page requires login`() {
+ assertThat(page.requiresLogin(), equalTo(requiresLogin))
+ }
+
}