damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

AuthIntegrationTests.swift (8072B)


      1 //
      2 //  AuthIntegrationTests.swift
      3 //  damusTests
      4 //
      5 //  Created by Charlie Fish on 12/22/23.
      6 //
      7 
      8 import XCTest
      9 @testable import damus
     10 
     11 final class AuthIntegrationTests: XCTestCase {
     12     /*
     13     func testAuthIntegrationFilterNostrWine() {
     14         // Create relay pool and connect to `wss://filter.nostr.wine`
     15         let relay_url = RelayURL("wss://filter.nostr.wine")!
     16         var received_messages: [String] = []
     17         var sent_messages: [String] = []
     18         let keypair: Keypair = generate_new_keypair().to_keypair()
     19         let pool = RelayPool(ndb: Ndb.test, keypair: keypair)
     20         pool.message_received_function = { obj in
     21             let str = obj.0
     22             let descriptor = obj.1
     23 
     24             if descriptor.url.id != relay_url.id {
     25                 XCTFail("The descriptor we recieved the message from should equal the relayURL")
     26             }
     27 
     28             received_messages.append(str)
     29         }
     30         pool.message_sent_function = { obj in
     31             let str = obj.0
     32             let relay = obj.1
     33 
     34             if relay.descriptor.url.id != relay_url.id {
     35                 XCTFail("The descriptor we sent the message to should equal the relayURL")
     36             }
     37 
     38             sent_messages.append(str)
     39         }
     40         XCTAssertEqual(pool.relays.count, 0)
     41         let relay_descriptor = RelayDescriptor.init(url: relay_url, info: .rw)
     42         try! pool.add_relay(relay_descriptor)
     43         XCTAssertEqual(pool.relays.count, 1)
     44         let connection_expectation = XCTestExpectation(description: "Waiting for connection")
     45         Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
     46             if pool.num_connected == 1 {
     47                 connection_expectation.fulfill()
     48                 timer.invalidate()
     49             }
     50         }
     51         wait(for: [connection_expectation], timeout: 30.0)
     52         XCTAssertEqual(pool.num_connected, 1)
     53         // Assert that AUTH message has been received
     54         XCTAssertTrue(received_messages.count >= 1, "expected recieved_messages to be >= 1")
     55         guard let msg = received_messages[safe: 0],
     56               let dat = msg.data(using: .utf8),
     57               let json_received = try? JSONSerialization.jsonObject(with: dat, options: []) as? [Any]
     58         else {
     59             XCTAssert(false)
     60             return
     61         }
     62         XCTAssertEqual(json_received[0] as! String, "AUTH")
     63         // Assert that we've replied with the AUTH response
     64         XCTAssertEqual(sent_messages.count, 1)
     65         let json_sent = try! JSONSerialization.jsonObject(with: sent_messages[0].data(using: .utf8)!, options: []) as! [Any]
     66         XCTAssertEqual(json_sent[0] as! String, "AUTH")
     67         let sent_msg = json_sent[1] as! [String: Any]
     68         XCTAssertEqual(sent_msg["kind"] as! Int, 22242)
     69         XCTAssertEqual((sent_msg["tags"] as! [[String]]).first { $0[0] == "challenge" }![1], json_received[1] as! String)
     70     }
     71      */
     72 
     73     func testAuthIntegrationRelayDamusIo() {
     74         // Create relay pool and connect to `wss://relay.damus.io`
     75         let relay_url = RelayURL("wss://relay.damus.io")!
     76         var received_messages: [String] = []
     77         var sent_messages: [String] = []
     78         let keypair: Keypair = generate_new_keypair().to_keypair()
     79         let pool = RelayPool(ndb: Ndb.test, keypair: keypair)
     80         pool.message_received_function = { obj in
     81             let str = obj.0
     82             let descriptor = obj.1
     83 
     84             if descriptor.url.id != relay_url.id {
     85                 XCTFail("The descriptor we recieved the message from should equal the relayURL")
     86             }
     87 
     88             received_messages.append(str)
     89         }
     90         pool.message_sent_function = { obj in
     91             let str = obj.0
     92             let relay = obj.1
     93 
     94             if relay.descriptor.url.id != relay_url.id {
     95                 XCTFail("The descriptor we sent the message to should equal the relayURL")
     96             }
     97 
     98             sent_messages.append(str)
     99         }
    100         XCTAssertEqual(pool.relays.count, 0)
    101         let relay_descriptor = RelayDescriptor.init(url: relay_url, info: .rw)
    102         try! pool.add_relay(relay_descriptor)
    103         XCTAssertEqual(pool.relays.count, 1)
    104         let connection_expectation = XCTestExpectation(description: "Waiting for connection")
    105         Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
    106             if pool.num_connected == 1 {
    107                 connection_expectation.fulfill()
    108                 timer.invalidate()
    109             }
    110         }
    111         wait(for: [connection_expectation], timeout: 30.0)
    112         XCTAssertEqual(pool.num_connected, 1)
    113         // Assert that no AUTH messages have been received
    114         XCTAssertEqual(received_messages.count, 0)
    115     }
    116 
    117     func testAuthIntegrationNostrWine() {
    118         // Create relay pool and connect to `wss://nostr.wine`
    119         let relay_url = RelayURL("wss://nostr.wine")!
    120         var received_messages: [String] = []
    121         var sent_messages: [String] = []
    122         let keypair: Keypair = generate_new_keypair().to_keypair()
    123         let pool = RelayPool(ndb: Ndb.test, keypair: keypair)
    124         pool.message_received_function = { obj in
    125             let str = obj.0
    126             let descriptor = obj.1
    127 
    128             if descriptor.url.id != relay_url.id {
    129                 XCTFail("The descriptor we recieved the message from should equal the relayURL")
    130             }
    131 
    132             received_messages.append(str)
    133         }
    134         pool.message_sent_function = { obj in
    135             let str = obj.0
    136             let relay = obj.1
    137 
    138             if relay.descriptor.url.id != relay_url.id {
    139                 XCTFail("The descriptor we sent the message to should equal the relayURL")
    140             }
    141 
    142             sent_messages.append(str)
    143         }
    144         XCTAssertEqual(pool.relays.count, 0)
    145         let relay_descriptor = RelayDescriptor.init(url: relay_url, info: .rw)
    146         try! pool.add_relay(relay_descriptor)
    147         XCTAssertEqual(pool.relays.count, 1)
    148         let connection_expectation = XCTestExpectation(description: "Waiting for connection")
    149         Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
    150             if pool.num_connected == 1 {
    151                 connection_expectation.fulfill()
    152                 timer.invalidate()
    153             }
    154         }
    155         wait(for: [connection_expectation], timeout: 30.0)
    156         XCTAssertEqual(pool.num_connected, 1)
    157         // Assert that no AUTH messages have been received
    158         XCTAssertEqual(received_messages.count, 0)
    159         // Generate UUID for subscription_id
    160         let uuid = UUID().uuidString
    161         // Send `["REQ", subscription_id, {"kinds": [4]}]`
    162         let subscribe = NostrSubscribe(filters: [
    163             NostrFilter(kinds: [.dm])
    164         ], sub_id: uuid)
    165         pool.send(NostrRequest.subscribe(subscribe))
    166         // Wait for AUTH message to have been received & sent
    167         let msg_expectation = XCTestExpectation(description: "Waiting for messages")
    168         Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
    169             if received_messages.count >= 2 && sent_messages.count >= 2 {
    170                 msg_expectation.fulfill()
    171                 timer.invalidate()
    172             }
    173         }
    174         wait(for: [msg_expectation], timeout: 30.0)
    175         // Assert that AUTH message has been received
    176         XCTAssertTrue(received_messages.count >= 1, "expected recieved_messages to be >= 1")
    177         let json_received = try! JSONSerialization.jsonObject(with: received_messages[0].data(using: .utf8)!, options: []) as! [Any]
    178         XCTAssertEqual(json_received[0] as! String, "AUTH")
    179         // Assert that we've replied with the AUTH response
    180         XCTAssertEqual(sent_messages.count, 2)
    181         let json_sent = try! JSONSerialization.jsonObject(with: sent_messages[1].data(using: .utf8)!, options: []) as! [Any]
    182         XCTAssertEqual(json_sent[0] as! String, "AUTH")
    183         let sent_msg = json_sent[1] as! [String: Any]
    184         XCTAssertEqual(sent_msg["kind"] as! Int, 22242)
    185         XCTAssertEqual((sent_msg["tags"] as! [[String]]).first { $0[0] == "challenge" }![1], json_received[1] as! String)
    186     }
    187 
    188 }