Create your own from a phone: the TV's own page, by QR code, on home Wi-Fi only
Ported from release 0.108.47; screenshots of the phone page and the TV's start screen in design/create-your-own/shots. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package tv.orbit.classic
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
import org.junit.After
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import java.net.HttpURLConnection
|
||||
import java.net.InetAddress
|
||||
import java.net.URL
|
||||
|
||||
/**
|
||||
* The phone page's server (Create your own from a phone, Paul, 6 November 2026), run on this computer only: loopback stands
|
||||
* in for the home network here, and nothing listens beyond this computer.
|
||||
*/
|
||||
class PhoneLinkTest {
|
||||
private val phones = mutableMapOf<String, String>()
|
||||
private val watched = mutableListOf<Int>()
|
||||
private var made: Pair<CustomChannel, List<Suggestion>>? = null
|
||||
private val port = 18_099
|
||||
private val base = "http://127.0.0.1:$port"
|
||||
|
||||
@Before fun up() {
|
||||
PhoneLink.initForTest(object : PhoneStore {
|
||||
override fun all() = phones.toMap(); override fun get(id: String) = phones[id]
|
||||
override fun put(id: String, value: String) { phones[id] = value }; override fun remove(id: String) { phones.remove(id) } })
|
||||
PhoneLink.bindTo = InetAddress.getLoopbackAddress(); PhoneLink.port = port
|
||||
PhoneLink.allowed = { it.isLoopbackAddress }; PhoneLink.mainDispatcher = Dispatchers.Unconfined
|
||||
PhoneLink.pinNeeded = { false }
|
||||
PhoneLink.handler = PhoneHandler(ChannelMaker({ d, _, _ -> listOf(Suggestion("x1", "The X-Files", 1993, "Series", "", "Science Fiction, 1990s")).filter { d.isNotBlank() } },
|
||||
{ emptyList() }, { c, s -> made = c to s; "${c.name} is ready" }, { emptyList() }), { false }, { watched += it })
|
||||
PhoneLink.start()
|
||||
}
|
||||
@After fun down() { PhoneLink.stop(); PhoneLink.handler = null; PhoneLink.allowed = EmbyServer::isHomeAddress; PhoneLink.bindTo = null; PhoneLink.port = PhoneLink.PORT; CustomChannels.clear() }
|
||||
|
||||
private class Reply(val status: Int, val body: String, val cookie: String?)
|
||||
private fun call(path: String, method: String = "GET", body: String? = null, cookie: String? = null, form: Boolean = false, orbit: Boolean = true): Reply {
|
||||
val c = URL(base + path).openConnection() as HttpURLConnection
|
||||
c.instanceFollowRedirects = false; c.requestMethod = method
|
||||
cookie?.let { c.setRequestProperty("Cookie", it) }
|
||||
if (orbit && !form && method == "POST") c.setRequestProperty("X-Orbit", "1")
|
||||
if (body != null) { c.doOutput = true; c.setRequestProperty("Content-Type", if (form) "application/x-www-form-urlencoded" else "application/json"); c.outputStream.use { it.write(body.toByteArray()) } }
|
||||
val status = c.responseCode
|
||||
val text = (if (status < 400) c.inputStream else c.errorStream)?.use { it.readBytes().toString(Charsets.UTF_8) }.orEmpty()
|
||||
return Reply(status, text, c.getHeaderField("Set-Cookie")?.substringBefore(';')).also { c.disconnect() }
|
||||
}
|
||||
|
||||
@Test fun onlyTheHomeNetworkIsAnsweredAndPrivateAddressesAreTheRule() {
|
||||
assertTrue(EmbyServer.isHomeAddress(InetAddress.getByName("192.168.0.20"))); assertFalse(EmbyServer.isHomeAddress(InetAddress.getByName("8.8.8.8")))
|
||||
assertFalse(EmbyServer.isHomeAddress(InetAddress.getLoopbackAddress()))
|
||||
PhoneLink.allowed = { false }
|
||||
assertThrows(java.io.IOException::class.java) { call("/") }
|
||||
}
|
||||
|
||||
@Test fun aPhoneJoinsWithTheTvsCodeAndThenMakesAChannel() {
|
||||
assertTrue(call("/").body.contains("Enter the code")) // not joined: the join page
|
||||
assertTrue(call("/join", "POST", "code=0000", form = true).body.contains("run out")) // no code on the TV yet
|
||||
val p = PhoneLink.beginPairing()
|
||||
val wrong = if (p.code == "1111") "2222" else "1111"
|
||||
assertTrue(call("/join", "POST", "code=$wrong", form = true).body.contains("Check the TV"))
|
||||
val joined = call("/join", "POST", "code=${p.code}", form = true)
|
||||
assertEquals(302, joined.status); val cookie = joined.cookie!!
|
||||
assertEquals(1, phones.size); assertNull(PhoneLink.pairing)
|
||||
assertTrue(call("/join", "POST", "code=${p.code}", form = true).body.contains("run out")) // once only
|
||||
// The API, joined.
|
||||
assertEquals(0, JSONObject(call("/api/channels", cookie = cookie).body).getJSONArray("channels").length())
|
||||
assertEquals("The X-Files", JSONArray(call("/api/suggest?d=90s%20sci-fi", cookie = cookie).body).getJSONObject(0).getString("title"))
|
||||
val body = JSONObject().put("name", "90s Sci-Fi").put("style", "Marathon").put("hours", JSONObject().put("from", 18).put("to", 0))
|
||||
.put("shows", JSONArray().put(JSONObject().put("id", "x1").put("title", "The X-Files").put("year", 1993).put("type", "Series"))).toString()
|
||||
assertEquals(403, call("/api/channels", "POST", body, cookie = cookie, orbit = false).status) // a cross-site form can't do it
|
||||
assertEquals(200, call("/api/channels", "POST", body, cookie = cookie).status)
|
||||
assertEquals(PlayStyle.Marathon, made!!.first.style); assertEquals(701, made!!.first.number); assertEquals(listOf("x1"), made!!.second.map { it.id })
|
||||
assertEquals(200, call("/api/watch", "POST", """{"number":701}""", cookie = cookie).status); assertEquals(listOf(701), watched)
|
||||
// Removed in Settings: the phone's cookie stops working (and with no phones left, the server stops altogether).
|
||||
PhoneLink.forget(phones.keys.single())
|
||||
assertTrue(runCatching { call("/api/channels", cookie = cookie).body.contains("Enter the code") }.getOrDefault(true))
|
||||
}
|
||||
|
||||
@Test fun guessingRunsOutAndThePinIsAskedWhenThereIsOne() {
|
||||
val p = PhoneLink.beginPairing()
|
||||
val wrong = if (p.code == "1111") "2222" else "1111"
|
||||
repeat(5) { call("/join", "POST", "code=$wrong", form = true) }
|
||||
assertTrue(call("/join", "POST", "code=${p.code}", form = true).body.contains("Too many tries"))
|
||||
assertTrue(phones.isEmpty())
|
||||
PhoneLink.pinNeeded = { true }; PhoneLink.pinCheck = { it == "4321" }
|
||||
val q = PhoneLink.beginPairing()
|
||||
assertTrue(call("/join", "POST", "code=${q.code}", form = true).body.contains("Grown-ups PIN"))
|
||||
assertTrue(call("/join", "POST", "code=${q.code}&pin=1234", form = true).body.contains("didn't match"))
|
||||
assertEquals(302, call("/join", "POST", "code=${q.code}&pin=4321", form = true).status)
|
||||
PhoneLink.pinNeeded = { false }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user