1 package net.pterodactylus.sone.template
3 import net.pterodactylus.sone.core.Core
4 import net.pterodactylus.sone.data.Image
5 import net.pterodactylus.sone.data.Profile
6 import net.pterodactylus.sone.data.Sone
7 import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions
8 import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.ALWAYS
9 import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.FOLLOWED
10 import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.MANUALLY_TRUSTED
11 import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.NEVER
12 import net.pterodactylus.sone.data.SoneOptions.LoadExternalContent.TRUSTED
13 import net.pterodactylus.sone.freenet.wot.Identity
14 import net.pterodactylus.sone.freenet.wot.OwnIdentity
15 import net.pterodactylus.sone.freenet.wot.Trust
16 import net.pterodactylus.sone.test.mock
17 import net.pterodactylus.sone.test.whenever
18 import net.pterodactylus.util.template.TemplateContext
19 import org.hamcrest.MatcherAssert.assertThat
20 import org.hamcrest.Matchers.equalTo
21 import org.hamcrest.Matchers.nullValue
22 import org.junit.Before
24 import org.mockito.ArgumentMatchers.anyBoolean
25 import org.mockito.ArgumentMatchers.eq
28 * Unit test for [ProfileAccessor].
30 class ProfileAccessorTest {
32 private val core = mock<Core>()
33 private val accessor = ProfileAccessor(core)
34 private val profile = mock<Profile>()
35 private val templateContext = mock<TemplateContext>()
36 private val currentSone = mock<Sone>()
37 private val remoteSone = mock<Sone>()
40 fun setupTemplateContext() {
41 whenever(templateContext.get("currentSone")).thenReturn(currentSone)
46 whenever(profile.sone).thenReturn(remoteSone)
47 whenever(profile.avatar).thenReturn("avatar-id")
52 val currentIdentity = mock<OwnIdentity>()
53 whenever(currentSone.options).thenReturn(DefaultSoneOptions())
54 whenever(currentSone.identity).thenReturn(currentIdentity)
55 whenever(remoteSone.id).thenReturn("remote-sone")
56 val identity = mock<Identity>()
57 val trust = Trust(null, null, null)
58 whenever(remoteSone.identity).thenReturn(identity)
59 whenever(identity.getTrust(currentIdentity)).thenReturn(trust)
64 whenever(core.getImage(eq("avatar-id"), anyBoolean())).thenReturn(mock())
69 fun `avatar is null if there is no current sone`() {
70 whenever(templateContext.get("currentSone")).thenReturn(null)
71 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
75 fun `avatar is null if profile has no avatar id`() {
76 whenever(profile.avatar).thenReturn(null)
77 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
81 fun `avatar is null if core has no image with avatar ID`() {
82 whenever(core.getImage(eq("avatar-id"), anyBoolean())).thenReturn(null)
83 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
87 fun `avatar ID is returned if profile belongs to local sone`() {
88 whenever(remoteSone.isLocal).thenReturn(true)
89 assertThat(accessor.get(templateContext, profile, "avatar"), equalTo<Any>("avatar-id"))
93 fun `avatar is null if sone is configure to never show avatars`() {
94 currentSone.options.showCustomAvatars = NEVER
95 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
99 fun `avatar ID is returned if sone is configure to always show avatars`() {
100 currentSone.options.showCustomAvatars = ALWAYS
101 assertThat(accessor.get(templateContext, profile, "avatar"), equalTo<Any>("avatar-id"))
105 fun `avatar ID is returned if sone is configure to show avatars of followed sones and remote sone is followed`() {
106 currentSone.options.showCustomAvatars = FOLLOWED
107 whenever(currentSone.hasFriend("remote-sone")).thenReturn(true)
108 assertThat(accessor.get(templateContext, profile, "avatar"), equalTo<Any>("avatar-id"))
112 fun `avatar is null if sone is configure to show avatars of followed sones but remote sone is not followed`() {
113 currentSone.options.showCustomAvatars = FOLLOWED
114 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
118 fun `avatar is null if sone is configure to show avatars based on trust but there is no trust`() {
120 currentSone.options.showCustomAvatars = MANUALLY_TRUSTED
121 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
124 private fun setTrust(trust: Trust?) {
125 whenever(remoteSone.identity.getTrust(currentSone.identity as OwnIdentity)).thenReturn(trust)
129 fun `avatar is null if sone is configure to show avatars based on manual trust but there is no explicit trust`() {
130 currentSone.options.showCustomAvatars = MANUALLY_TRUSTED
131 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
135 fun `avatar is null if sone is configure to show avatars based on manual trust but explicit trust is zero`() {
136 currentSone.options.showCustomAvatars = MANUALLY_TRUSTED
137 setTrust(Trust(0, null, null))
138 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
142 fun `avatar ID is returned if sone is configure to show avatars based on manual trust and explicit trust is one`() {
143 currentSone.options.showCustomAvatars = MANUALLY_TRUSTED
144 setTrust(Trust(1, null, null))
145 assertThat(accessor.get(templateContext, profile, "avatar"), equalTo<Any>("avatar-id"))
149 fun `avatar is null if sone is configure to show avatars based on trust but explicit trust is zero`() {
150 currentSone.options.showCustomAvatars = TRUSTED
151 setTrust(Trust(0, null, null))
152 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
156 fun `avatar ID is returned if sone is configure to show avatars based on trust and explicit trust is one`() {
157 currentSone.options.showCustomAvatars = TRUSTED
158 setTrust(Trust(1, null, null))
159 assertThat(accessor.get(templateContext, profile, "avatar"), equalTo<Any>("avatar-id"))
163 fun `avatar is null if sone is configure to show avatars based on trust but both explicit and implicit trust are null`() {
164 currentSone.options.showCustomAvatars = TRUSTED
165 setTrust(Trust(null, null, null))
166 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
170 fun `avatar is null if sone is configure to show avatars based on trust but both implicit trust is zero`() {
171 currentSone.options.showCustomAvatars = TRUSTED
172 setTrust(Trust(null, 0, null))
173 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
177 fun `avatar ID is returned if sone is configure to show avatars based on trust and implicit trust is one`() {
178 currentSone.options.showCustomAvatars = TRUSTED
179 setTrust(Trust(0, 1, null))
180 assertThat(accessor.get(templateContext, profile, "avatar"), equalTo<Any>("avatar-id"))
184 fun `accessing other members uses reflection accessor`() {
185 assertThat(accessor.get(templateContext, profile, "hashCode"), equalTo<Any>(profile.hashCode()))