RequestTests.swift (3497B)
1 // 2 // RequestTests.swift 3 // damusTests 4 // 5 // Created by Bryan Montz on 2/17/23. 6 // 7 8 import XCTest 9 @testable import damus 10 11 final class RequestTests: XCTestCase { 12 13 func testMakeUnsubscribeRequest() { 14 let request = NostrRequest.unsubscribe("64FD064D-EB9E-4771-8255-8D16981B920B") 15 let result = make_nostr_req(request) 16 let expectedResult = "[\"CLOSE\",\"64FD064D-EB9E-4771-8255-8D16981B920B\"]" 17 XCTAssertEqual(result, expectedResult) 18 } 19 20 func testMakeAuthRequest() { 21 let challenge_string = "8bc847dd-f2f6-4b3a-9c8a-71776ad9b071" 22 let url = RelayURL("wss://example.com")! 23 let relayInfo = RelayInfo(read: true, write: true) 24 let relayDescriptor = RelayDescriptor(url: url, info: relayInfo) 25 let relayConnection = RelayConnection(url: url) { _ in 26 } processEvent: { _ in 27 } 28 29 let relay = Relay(descriptor: relayDescriptor, connection: relayConnection) 30 let event = make_auth_request(keypair: FullKeypair.init(pubkey: Pubkey.empty, privkey: Privkey.empty), challenge_string: challenge_string, relay: relay)! 31 32 let result = make_nostr_auth_event(ev: event) 33 let json = try! JSONSerialization.jsonObject(with: result!.data(using: .utf8)!, options: []) as! [Any] 34 35 XCTAssertEqual(json[0] as! String, "AUTH") 36 let dictionary = json[1] as! [String: Any] 37 XCTAssertEqual(dictionary["content"] as! String, "") 38 XCTAssertEqual(dictionary["kind"] as! Int, 22242) 39 XCTAssertEqual(dictionary["sig"] as! String, String(repeating: "0", count: 128)) 40 XCTAssertEqual(dictionary["pubkey"] as! String, String(repeating: "0", count: 64)) 41 let tags = dictionary["tags"] as! [[String]] 42 XCTAssertEqual(tags.first { $0[0] == "relay" }![1], "wss://example.com") 43 XCTAssertEqual(tags.first { $0[0] == "challenge" }![1], challenge_string) 44 XCTAssertEqual(dictionary["id"] as! String, String(repeating: "0", count: 64)) 45 } 46 47 /* FIXME: these tests depend on order of json fields which is undefined 48 func testMakePushEvent() { 49 let now = Int64(Date().timeIntervalSince1970) 50 let event = NostrEvent(id: "59c1cf11a3e9e128c6fd5402f41e8ae0c0c7fbab570203d7410518be68c3115f", 51 content: "Testing", 52 pubkey: "d9fa34214aa9d151c4f4db843e9c2af4f246bab4205137731f91bcfa44d66a62", 53 kind: 1, 54 createdAt: now) 55 let result = make_nostr_req(.event(event)) 56 let expectedResult = "[\"EVENT\",{\"pubkey\":\"d9fa34214aa9d151c4f4db843e9c2af4f246bab4205137731f91bcfa44d66a62\",\"content\":\"Testing\",\"id\":\"59c1cf11a3e9e128c6fd5402f41e8ae0c0c7fbab570203d7410518be68c3115f\",\"created_at\":\(now),\"sig\":\"\",\"kind\":1,\"tags\":[]}]" 57 XCTAssertEqual(result, expectedResult) 58 } 59 60 func testMakeSubscriptionRequest() { 61 let filter = NostrFilter(kinds: [.contacts], limit: 1, authors: ["d9fa34214aa9d151c4f4db843e9c2af4f246bab4205137731f91bcfa44d66a62"]) 62 let subscribe = NostrSubscribe(filters: [filter], sub_id: "31C737B7-C8F9-41DD-8707-325974F279A4") 63 let result = make_nostr_req(.subscribe(subscribe)) 64 let expectedResult = "[\"REQ\",\"31C737B7-C8F9-41DD-8707-325974F279A4\",{\"kinds\":[3],\"authors\":[\"d9fa34214aa9d151c4f4db843e9c2af4f246bab4205137731f91bcfa44d66a62\"],\"limit\":1}]" 65 XCTAssertEqual(result, expectedResult) 66 } 67 */ 68 }