1 package net.pterodactylus.sone.web
3 import net.pterodactylus.sone.data.Post
4 import net.pterodactylus.sone.data.PostReply
5 import net.pterodactylus.sone.data.Profile
6 import net.pterodactylus.sone.data.Sone
7 import net.pterodactylus.sone.test.asOptional
8 import net.pterodactylus.sone.test.mock
9 import net.pterodactylus.sone.test.whenever
10 import org.hamcrest.MatcherAssert.assertThat
11 import org.hamcrest.Matchers.contains
12 import org.hamcrest.Matchers.equalTo
13 import org.hamcrest.Matchers.nullValue
14 import org.junit.Before
18 * Unit test for [ViewSonePage].
20 class ViewSonePageTest : WebPageTest() {
23 whenever(currentSone.id).thenReturn("sone-id")
26 private val page = ViewSonePage(template, webInterface)
27 private val post1 = createPost("post1", "First Post.", 1000, currentSone)
28 private val post2 = createPost("post2", "Second Post.", 2000, currentSone)
29 private val foreignPost1 = createPost("foreign-post1", "First Foreign Post.", 1000, mock<Sone>())
30 private val foreignPost2 = createPost("foreign-post2", "Second Foreign Post.", 2000, mock<Sone>())
31 private val directed1 = createPost("post3", "First directed.", 1500, mock<Sone>(), recipient = currentSone)
32 private val directed2 = createPost("post4", "Second directed.", 2500, mock<Sone>(), recipient = currentSone)
34 override fun getPage() = page
38 whenever(currentSone.posts).thenReturn(mutableListOf(post2, post1))
39 whenever(core.getDirectedPosts("sone-id")).thenReturn(setOf(directed1, directed2))
40 core.preferences.postsPerPage = 2
44 fun `get request without sone parameter stores null in template context`() {
45 page.handleRequest(freenetRequest, templateContext)
46 assertThat(templateContext["sone"], nullValue())
47 assertThat(templateContext["soneId"], equalTo<Any>(""))
51 fun `get request with invalid sone parameter stores null in template context`() {
52 addHttpRequestParameter("sone", "invalid-sone-id")
53 page.handleRequest(freenetRequest, templateContext)
54 assertThat(templateContext["sone"], nullValue())
55 assertThat(templateContext["soneId"], equalTo<Any>("invalid-sone-id"))
59 fun `get request with valid sone parameter stores sone in template context`() {
60 whenever(currentSone.posts).thenReturn(mutableListOf())
61 whenever(core.getDirectedPosts("sone-id")).thenReturn(emptyList())
62 addHttpRequestParameter("sone", "sone-id")
63 addSone("sone-id", currentSone)
64 page.handleRequest(freenetRequest, templateContext)
65 assertThat(templateContext["sone"], equalTo<Any>(currentSone))
66 assertThat(templateContext["soneId"], equalTo<Any>("sone-id"))
69 private fun createPost(id: String, text: String, time: Long, sender: Sone? = null, recipient: Sone? = null) = mock<Post>().apply {
70 whenever(this.id).thenReturn(id)
71 sender?.run { whenever(this@apply.sone).thenReturn(this) }
72 val recipientId = recipient?.id
73 whenever(this.recipientId).thenReturn(recipientId.asOptional())
74 whenever(this.recipient).thenReturn(recipient.asOptional())
75 whenever(this.time).thenReturn(time)
76 whenever(this.text).thenReturn(text)
80 @Suppress("UNCHECKED_CAST")
81 fun `request with valid sone stores posts and directed posts in template context`() {
82 addSone("sone-id", currentSone)
83 addHttpRequestParameter("sone", "sone-id")
84 page.handleRequest(freenetRequest, templateContext)
85 assertThat(templateContext["posts"] as Iterable<Post>, contains(directed2, post2))
89 @Suppress("UNCHECKED_CAST")
90 fun `second page of posts is shown correctly`() {
91 addSone("sone-id", currentSone)
92 addHttpRequestParameter("sone", "sone-id")
93 addHttpRequestParameter("postPage", "1")
94 page.handleRequest(freenetRequest, templateContext)
95 assertThat(templateContext["posts"] as Iterable<Post>, contains(directed1, post1))
98 private fun createReply(text: String, time: Long, post: Post?) = mock<PostReply>().apply {
99 whenever(this.text).thenReturn(text)
100 whenever(this.time).thenReturn(time)
101 whenever(this.post).thenReturn(post.asOptional())
105 @Suppress("UNCHECKED_CAST")
106 fun `replies are shown correctly`() {
107 val reply1 = createReply("First Reply", 1500, foreignPost1)
108 val reply2 = createReply("Second Reply", 2500, foreignPost2)
109 val reply3 = createReply("Third Reply", 1750, post1)
110 val reply4 = createReply("Fourth Reply", 2250, post2)
111 val reply5 = createReply("Fifth Reply", 1600, post1)
112 val reply6 = createReply("Sixth Reply", 2100, directed1)
113 val reply7 = createReply("Seventh Reply", 2200, null)
114 val reply8 = createReply("Eigth Reply", 2300, foreignPost1)
115 whenever(currentSone.replies).thenReturn(setOf(reply1, reply2, reply3, reply4, reply5, reply6, reply7, reply8))
116 whenever(core.getReplies("post1")).thenReturn(listOf(reply3, reply5))
117 whenever(core.getReplies("post2")).thenReturn(listOf(reply4))
118 whenever(core.getReplies("foreign-post1")).thenReturn(listOf(reply1))
119 whenever(core.getReplies("foreign-post2")).thenReturn(listOf(reply2))
120 whenever(core.getReplies("post3")).thenReturn(listOf(reply6))
121 addSone("sone-id", currentSone)
122 addHttpRequestParameter("sone", "sone-id")
123 page.handleRequest(freenetRequest, templateContext)
124 assertThat(templateContext["repliedPosts"] as Iterable<Post>, contains(foreignPost2, foreignPost1))
128 fun `page title is default for request without parameters`() {
129 assertThat(page.getPageTitle(freenetRequest), equalTo("Page.ViewSone.Page.TitleWithoutSone"))
133 fun `page title is default for request with invalid sone parameters`() {
134 addHttpRequestParameter("sone", "invalid-sone-id")
135 assertThat(page.getPageTitle(freenetRequest), equalTo("Page.ViewSone.Page.TitleWithoutSone"))
139 fun `page title contains sone name for request with sone parameters`() {
140 addHttpRequestParameter("sone", "sone-id")
141 addSone("sone-id", currentSone)
142 whenever(currentSone.profile).thenReturn(Profile(currentSone).apply {
147 assertThat(page.getPageTitle(freenetRequest), equalTo("First M. Last - Page.ViewSone.Title"))
151 fun `page is link-excepted`() {
152 assertThat(page.isLinkExcepted(null), equalTo(true))