RadrootsExternalActionsTests.swift (3792B)
1 import Foundation 2 import Testing 3 @testable import RadrootsKit 4 5 @Test func webDestinationAcceptsOnlyHttpsUrlsWithHosts() throws { 6 let destination = try RadrootsExternalActionDestination.web(" https://radroots.org/field ") 7 8 #expect(destination.kind == .web) 9 #expect(destination.url?.absoluteString == "https://radroots.org/field") 10 11 #expect(throws: RadrootsExternalActionError.blockedByPolicy("external web urls must use https with a host")) { 12 _ = try RadrootsExternalActionDestination.web("http://radroots.org") 13 } 14 #expect(throws: RadrootsExternalActionError.blockedByPolicy("external web urls must use https with a host")) { 15 _ = try RadrootsExternalActionDestination.web("wss://radroots.org") 16 } 17 #expect(throws: RadrootsExternalActionError.blockedByPolicy("external web urls must use https with a host")) { 18 _ = try RadrootsExternalActionDestination.web("https:///missing-host") 19 } 20 #expect(throws: RadrootsExternalActionError.invalidRequest("web url cannot contain whitespace or control characters")) { 21 _ = try RadrootsExternalActionDestination.web("https://radroots.org/a b") 22 } 23 } 24 25 @Test func nostrDestinationAllowsPublicIdentifiersAndRejectsSecrets() throws { 26 let destination = try RadrootsExternalActionDestination.nostr("nostr:NPUB1qqqqqq") 27 28 #expect(destination.kind == .nostr) 29 #expect(destination.url?.absoluteString == "nostr:npub1qqqqqq") 30 31 _ = try RadrootsExternalActionDestination.nostr("nostr:nprofile1qqqqqq") 32 _ = try RadrootsExternalActionDestination.nostr("nostr:note1qqqqqq") 33 _ = try RadrootsExternalActionDestination.nostr("nostr:nevent1qqqqqq") 34 _ = try RadrootsExternalActionDestination.nostr("nostr:naddr1qqqqqq") 35 _ = try RadrootsExternalActionDestination.nostr("nostr:nrelay1qqqqqq") 36 37 #expect(throws: RadrootsExternalActionError.blockedByPolicy("nostr secret payloads cannot be opened externally")) { 38 _ = try RadrootsExternalActionDestination.nostr("nostr:nsec1qqqqqq") 39 } 40 #expect(throws: RadrootsExternalActionError.blockedByPolicy("nostr uri payload must be a public Nostr identifier")) { 41 _ = try RadrootsExternalActionDestination.nostr("nostr:relay1qqqqqq") 42 } 43 #expect(throws: RadrootsExternalActionError.invalidRequest("nostr uri cannot contain whitespace or control characters")) { 44 _ = try RadrootsExternalActionDestination.nostr("nostr:npub1qq q") 45 } 46 } 47 48 @Test func appleMapsDestinationBuildsSafeMapsUrls() throws { 49 let coordinate = try RadrootsLocationCoordinate(latitude: 49.2827, longitude: -123.1207) 50 let destination = try RadrootsExternalActionDestination.appleMaps( 51 coordinate: coordinate, 52 label: "Field Site" 53 ) 54 55 #expect(destination.kind == .appleMaps) 56 #expect(destination.url?.scheme == "https") 57 #expect(destination.url?.host == "maps.apple.com") 58 #expect(destination.url?.absoluteString.contains("ll=49.2827,-123.1207") == true) 59 #expect(destination.url?.absoluteString.contains("q=Field%20Site") == true) 60 61 _ = try RadrootsExternalActionDestination.appleMaps("https://maps.apple.com/?q=Field") 62 63 #expect(throws: RadrootsExternalActionError.blockedByPolicy("apple maps urls must use https://maps.apple.com")) { 64 _ = try RadrootsExternalActionDestination.appleMaps("https://example.com/maps") 65 } 66 } 67 68 @Test func externalActionRequestPreservesDestination() throws { 69 let destination = try RadrootsExternalActionDestination.web("https://radroots.org") 70 let request = RadrootsExternalActionRequest(destination: destination) 71 let capability = RadrootsExternalActionCapability(destination: destination, canOpen: true) 72 73 #expect(request.destination == destination) 74 #expect(capability.destination == destination) 75 #expect(capability.canOpen) 76 }