mirror of
https://github.com/home-assistant/iOS.git
synced 2026-06-25 17:27:04 -05:00
<!-- Thank you for submitting a Pull Request and helping to improve Home Assistant. Please complete the following sections to help the processing and review of your changes. Please do not delete anything from this template. --> ## Summary <!-- Provide a brief summary of the changes you have made and most importantly what they aim to achieve --> ## Screenshots <!-- If this is a user-facing change not in the frontend, please include screenshots in light and dark mode. --> ## Link to pull request in Documentation repository <!-- Pull requests that add, change or remove functionality must have a corresponding pull request in the Companion App Documentation repository (https://github.com/home-assistant/companion.home-assistant). Please add the number of this pull request after the "#" --> Documentation: home-assistant/companion.home-assistant# ## Any other notes <!-- If there is any other information of note, like if this Pull Request is part of a bigger change, please include it here. --> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
175 lines
5.3 KiB
Swift
175 lines
5.3 KiB
Swift
@testable import Shared
|
|
import XCTest
|
|
|
|
class ServerTests: XCTestCase {
|
|
private func waitLoop() {
|
|
let expectation = expectation(description: "run loop")
|
|
DispatchQueue.main.async {
|
|
expectation.fulfill()
|
|
}
|
|
wait(for: [expectation], timeout: 10.0)
|
|
}
|
|
|
|
func testSortOrder() {
|
|
let servers: [Server] = [
|
|
Server(
|
|
identifier: "1",
|
|
getter: {
|
|
with(.fake()) {
|
|
$0.sortOrder = 100
|
|
}
|
|
}, setter: { _ in
|
|
true
|
|
}
|
|
),
|
|
Server(
|
|
identifier: "2",
|
|
getter: {
|
|
with(.fake()) {
|
|
$0.remoteName = "2"
|
|
$0.sortOrder = 50
|
|
}
|
|
}, setter: { _ in
|
|
true
|
|
}
|
|
),
|
|
Server(
|
|
identifier: "3",
|
|
getter: {
|
|
with(.fake()) {
|
|
$0.remoteName = "1"
|
|
$0.sortOrder = 50
|
|
}
|
|
}, setter: { _ in
|
|
true
|
|
}
|
|
),
|
|
Server(
|
|
identifier: "4",
|
|
getter: {
|
|
with(.fake()) {
|
|
$0.sortOrder = 1000
|
|
}
|
|
}, setter: { _ in
|
|
true
|
|
}
|
|
),
|
|
]
|
|
|
|
let sorted = servers.sorted()
|
|
XCTAssertEqual(sorted.map(\.identifier), ["3", "2", "1", "4"])
|
|
}
|
|
|
|
func testEquality() {
|
|
let server_id1_1 = Server(identifier: "1", getter: { .fake() }, setter: { _ in true })
|
|
let server_id1_2 = Server(identifier: "1", getter: { .fake() }, setter: { _ in true })
|
|
let server_id2 = Server(identifier: "2", getter: { .fake() }, setter: { _ in true })
|
|
let server_id3 = Server(identifier: "3", getter: { .fake() }, setter: { _ in true })
|
|
|
|
XCTAssertEqual(server_id1_1, server_id1_1)
|
|
XCTAssertEqual(server_id1_1, server_id1_2)
|
|
XCTAssertNotEqual(server_id1_1, server_id3)
|
|
XCTAssertNotEqual(server_id1_2, server_id3)
|
|
XCTAssertNotEqual(server_id2, server_id3)
|
|
}
|
|
|
|
func testLocalName() {
|
|
var serverInfo = ServerInfo.fake()
|
|
serverInfo.remoteName = "remote_name1"
|
|
|
|
let server = Server(identifier: "fake1", getter: { serverInfo }, setter: { serverInfo = $0; return true })
|
|
|
|
XCTAssertEqual(server.info.name, "remote_name1")
|
|
|
|
server.info.setSetting(value: "local_name1", for: .localName)
|
|
XCTAssertEqual(server.info.name, "local_name1")
|
|
|
|
server.info.setSetting(value: nil, for: .localName)
|
|
XCTAssertEqual(server.info.name, "remote_name1")
|
|
|
|
server.info.setSetting(value: "", for: .localName)
|
|
XCTAssertEqual(server.info.name, "remote_name1")
|
|
}
|
|
|
|
func testNotifyInfoChange() {
|
|
var info: ServerInfo = .fake()
|
|
|
|
var setterResponseValue = true
|
|
|
|
let server = Server(
|
|
identifier: "test",
|
|
getter: { info },
|
|
setter: { info = $0; return setterResponseValue }
|
|
)
|
|
|
|
var notifiedInfos = [ServerInfo]()
|
|
|
|
let token = server.observe { newInfo in
|
|
notifiedInfos.append(newInfo)
|
|
}
|
|
|
|
server.info = info
|
|
XCTAssertEqual(server.info, info)
|
|
waitLoop()
|
|
XCTAssertTrue(notifiedInfos.isEmpty)
|
|
|
|
server.update { info in
|
|
info.connection.webhookSecret = "update_1"
|
|
}
|
|
|
|
XCTAssertEqual(server.info.connection.webhookSecret, "update_1")
|
|
waitLoop()
|
|
|
|
XCTAssertEqual(notifiedInfos.count, 1)
|
|
XCTAssertEqual(notifiedInfos[0].connection.webhookSecret, "update_1")
|
|
|
|
setterResponseValue = false
|
|
|
|
notifiedInfos.removeAll()
|
|
|
|
server.update { info in
|
|
info.connection.webhookSecret = "update_2"
|
|
}
|
|
|
|
waitLoop()
|
|
XCTAssertTrue(notifiedInfos.isEmpty)
|
|
|
|
setterResponseValue = true
|
|
|
|
token.cancel()
|
|
|
|
notifiedInfos.removeAll()
|
|
|
|
server.update { info in
|
|
info.connection.webhookSecret = "update_3"
|
|
}
|
|
|
|
waitLoop()
|
|
|
|
XCTAssertTrue(notifiedInfos.isEmpty)
|
|
}
|
|
|
|
func testMirroredForPersistenceRemovesSensitiveConnectionState() throws {
|
|
var info = ServerInfo.fake()
|
|
var securityExceptions = SecurityExceptions()
|
|
try securityExceptions.add(for: .unitTestDotExampleDotCom1)
|
|
|
|
info.connection.cloudhookURL = URL(string: "https://hooks.nabu.casa/webhook-id")
|
|
info.connection.webhookSecret = "webhook-secret"
|
|
info.connection.securityExceptions = securityExceptions
|
|
info.connection.clientCertificate = ClientCertificate(
|
|
keychainIdentifier: "client-cert-1",
|
|
displayName: "Client Certificate"
|
|
)
|
|
|
|
let mirrored = info.mirroredForPersistence
|
|
|
|
XCTAssertEqual(mirrored.token, ServerInfo.mirrorPlaceholderToken)
|
|
XCTAssertEqual(mirrored.connection.webhookID, ServerInfo.mirrorPlaceholderWebhookID)
|
|
XCTAssertNil(mirrored.connection.cloudhookURL)
|
|
XCTAssertNil(mirrored.connection.webhookSecret)
|
|
XCTAssertFalse(mirrored.connection.securityExceptions.hasExceptions)
|
|
XCTAssertNil(mirrored.connection.clientCertificate)
|
|
}
|
|
}
|