🐛 Remove backlinks from keys (thx, TheSeeker!)
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / template / ProfileAccessorTest.kt
1 package net.pterodactylus.sone.template
2
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.`is`
21 import org.hamcrest.Matchers.nullValue
22 import org.junit.Before
23 import org.junit.Test
24 import org.mockito.ArgumentMatchers.anyBoolean
25 import org.mockito.ArgumentMatchers.eq
26
27 /**
28  * Unit test for [ProfileAccessor].
29  */
30 class ProfileAccessorTest {
31
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>()
38
39         @Before
40         fun setupTemplateContext() {
41                 whenever(templateContext.get("currentSone")).thenReturn(currentSone)
42         }
43
44         @Before
45         fun setupProfile() {
46                 whenever(profile.sone).thenReturn(remoteSone)
47                 whenever(profile.avatar).thenReturn("avatar-id")
48         }
49
50         @Before
51         fun setupSones() {
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)
60         }
61
62         @Before
63         fun setupCore() {
64                 whenever(core.getImage(eq("avatar-id"), anyBoolean())).thenReturn(mock())
65         }
66
67
68         @Test
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())
72         }
73
74         @Test
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())
78         }
79
80         @Test
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())
84         }
85
86         @Test
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"), `is`<Any>("avatar-id"))
90         }
91
92         @Test
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())
96         }
97
98         @Test
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"), `is`<Any>("avatar-id"))
102         }
103
104         @Test
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"), `is`<Any>("avatar-id"))
109         }
110
111         @Test
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())
115         }
116
117         @Test
118         fun `avatar is null if sone is configure to show avatars based on trust but there is no trust`() {
119                 setTrust(null)
120                 currentSone.options.showCustomAvatars = MANUALLY_TRUSTED
121                 assertThat(accessor.get(templateContext, profile, "avatar"), nullValue())
122         }
123
124         private fun setTrust(trust: Trust?) {
125                 whenever(remoteSone.identity.getTrust(currentSone.identity as OwnIdentity)).thenReturn(trust)
126         }
127
128         @Test
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())
132         }
133
134         @Test
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())
139         }
140
141         @Test
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"), `is`<Any>("avatar-id"))
146         }
147
148         @Test
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())
153         }
154
155         @Test
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"), `is`<Any>("avatar-id"))
160         }
161
162         @Test
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())
167         }
168
169         @Test
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())
174         }
175
176         @Test
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"), `is`<Any>("avatar-id"))
181         }
182
183         @Test
184         fun `accessing other members uses reflection accessor`() {
185                 assertThat(accessor.get(templateContext, profile, "hashCode"), `is`<Any>(profile.hashCode()))
186         }
187
188 }