OXYGENHUB
# 3f3508bd5f16 oxygenhub-android / app / src / test / java / io / oxygenhub / android / api / ModelsTest.kt
4.2 KiB · last changed The hub's API and device sign-in, the token in the Keystore (refs #2) 11 days ago source blame history raw
1
package io.oxygenhub.android.api
2
3
import io.oxygenhub.android.Fixtures
4
import org.junit.Assert.assertEquals
5
import org.junit.Assert.assertFalse
6
import org.junit.Assert.assertNull
7
import org.junit.Assert.assertTrue
8
import org.junit.Test
9
10
class ModelsTest {
11
    @Test
12
    fun user() {
13
        val u = Fixtures.user
14
        assertEquals("andymccall", u.username)
15
        assertEquals("Andy McCall", u.displayName)
16
        assertEquals("Andy McCall", u.shownName)
17
        assertTrue(u.isAdmin)
18
        assertEquals(1, u.keys.size)
19
        assertEquals(1790530000000, u.createdMs)
20
    }
21
22
    @Test
23
    fun shownNameFallsBackToUsername() {
24
        assertEquals("claude", User(username = "claude").shownName)
25
    }
26
27
    @Test
28
    fun repositoriesWithEveryVisibilityAndAnUnknownField() {
29
        val r = Fixtures.repos
30
        assertEquals(3, r.size)
31
        assertEquals("andymccall/oxygenflow", r[0].slug)
32
        assertEquals(Visibility.Public, r[0].visibility)
33
        assertEquals(Visibility.Private, r[1].visibility)
34
        assertEquals(Visibility.Internal, r[2].visibility)
35
        assertEquals("trunk", r[2].defaultBookmark)
36
        assertEquals(1792878439999, r[0].updatedMs)
37
        assertEquals("https://oxygenhub.io/andymccall/oxygenflow", r[0].cloneUrl)
38
    }
39
40
    @Test
41
    fun issues() {
42
        val i = Fixtures.issues
43
        assertEquals(listOf(3L, 2L, 1L), i.map { it.number })
44
        val two = i[1]
45
        assertTrue(two.isOpen)
46
        assertTrue(two.pinned)
47
        assertEquals("0.1", two.milestone)
48
        assertEquals(listOf("claude"), two.assignees)
49
        assertEquals(Label("feature", "#0064e0", "Something new"), two.labels.single())
50
        assertNull(two.closedMs)
51
        val one = i[2]
52
        assertFalse(one.isOpen)
53
        assertEquals("completed", one.reason)
54
        assertEquals(1792875000000, one.closedMs)
55
        assertNull(one.milestone)
56
    }
57
58
    @Test
59
    fun inbox() {
60
        val inbox = Fixtures.inbox
61
        assertEquals(2, inbox.unread)
62
        assertEquals(listOf(41L, 40L, 12L), inbox.items.map { it.id })
63
        assertEquals("issue.opened", inbox.items[0].kind)
64
        assertFalse(inbox.items[0].read)
65
        assertEquals("", inbox.items[1].actor)
66
        assertTrue(inbox.items[2].read)
67
    }
68
69
    @Test
70
    fun deviceCodeAndToken() {
71
        val code = HubJson.decodeFromString(DeviceCode.serializer(), Fixtures.text("device_code.json"))
72
        assertEquals("BCDF-GHJK", code.userCode)
73
        assertEquals("https://oxygenhub.io/device?code=BCDF-GHJK", code.pageToOpen)
74
        assertEquals(900, code.expiresIn)
75
        assertEquals(5, code.interval)
76
        val grant = HubJson.decodeFromString(TokenGrant.serializer(), Fixtures.text("device_token.json"))
77
        assertEquals(listOf("repo:read", "user:read"), grant.scopes)
78
        assertEquals("andymccall", grant.username)
79
    }
80
81
    @Test
82
    fun pageToOpenWithoutTheCompleteUri() {
83
        val code = DeviceCode("d", "BCDF-GHJK", "https://hub.example/device")
84
        assertEquals("https://hub.example/device", code.pageToOpen)
85
    }
86
87
    @Test
88
    fun secretsStayOutOfToString() {
89
        val code = HubJson.decodeFromString(DeviceCode.serializer(), Fixtures.text("device_code.json"))
90
        assertFalse(code.toString().contains("oxydev_"))
91
        val grant = HubJson.decodeFromString(TokenGrant.serializer(), Fixtures.text("device_token.json"))
92
        assertFalse(grant.toString().contains("oxy_test_not_a_real_token"))
93
    }
94
95
    @Test
96
    fun errorDetails() {
97
        assertEquals("this token lacks the user:read scope", errorDetail(Fixtures.text("problem_403.json")))
98
        assertEquals("not yet approved", errorDetail(Fixtures.text("device_authorization_pending.json")))
99
        assertEquals("plain text", errorDetail("plain text\n"))
100
    }
101
102
    @Test
103
    fun hubUrls() {
104
        assertEquals("https://oxygenhub.io", normalizeHubUrl("https://oxygenhub.io/"))
105
        assertEquals("https://oxygenhub.io", normalizeHubUrl("  oxygenhub.io "))
106
        assertEquals("https://hub.example:7343/forge", normalizeHubUrl("hub.example:7343/forge/"))
107
        assertNull(normalizeHubUrl("http://oxygenhub.io"))
108
        assertNull(normalizeHubUrl(""))
109
        assertNull(normalizeHubUrl("https://"))
110
        assertNull(normalizeHubUrl("https://oxygenhub.io/?x=1"))
111
    }
112
}