HTTPTests.swift (4447B)
1 // SPDX-License-Identifier: GPL-2.0-only 2 import XCTest 3 import GitLite 4 import GitTestSupport 5 @testable import SubsurfaceLog 6 7 /// The app's real path: URLSession → HTTP with Basic authentication → `git http-backend`. 8 final class HTTPTests: XCTestCase { 9 func testSyncOverAuthenticatedHTTP() async throws { 10 let email = Fixture.branch 11 let password = "S3cret+_-.@" 12 let (seed, _) = try Fixture.makeRemote() 13 let root = try LocalGit.temporaryDirectory("www") 14 let bare = LocalGit(directory: root.appendingPathComponent("git").appendingPathComponent(email)) 15 try FileManager.default.createDirectory(at: bare.directory, withIntermediateDirectories: true) 16 try bare.git(["init", "-q", "--bare"]) 17 try bare.git(["config", "http.receivepack", "true"]) 18 try seed.git(["push", "-q", bare.directory.path, "refs/heads/\(email):refs/heads/\(email)"]) 19 20 let server = try GitHTTPServer(root: root, user: email, password: password) 21 defer { server.stop() } 22 let base = URL(string: "http://127.0.0.1:\(server.port)/git/")!.appendingPathComponent(email) 23 let transport = HTTPTransport(baseURL: base, username: email, password: password) 24 25 // Wrong password: rejected outright. 26 do { 27 _ = try await GitClient(transport: HTTPTransport(baseURL: base, username: email, password: "wrong"), store: MemoryObjectStore()).refs() 28 XCTFail("should have been rejected") 29 } catch let error as GitError { 30 XCTAssertEqual(error, .unauthorized) 31 } 32 33 // Right password: the list of branches, then the whole logbook. 34 let refs = try await GitClient(transport: transport, store: MemoryObjectStore()).refs() 35 XCTAssertNotNil(refs.branch(email)) 36 let repo = try LogRepository(directory: try LocalGit.temporaryDirectory("local"), branch: email, authorName: "DiveLog", authorEmail: email) 37 let first = try await repo.sync(transport: transport) 38 XCTAssertTrue(first.downloaded) 39 var log = try await repo.logbook() 40 XCTAssertEqual(log.dives.map(\.number), [43, 42, 41]) 41 42 // A change is sent and ends up in the server's repository. 43 try await repo.enqueue(.editDive(when: Fixture.when(2024, 7, 13, 10, 15, 30), number: 41, fields: ["buddy": "Over HTTP"])) 44 let second = try await repo.sync(transport: transport) 45 XCTAssertEqual(second.sent.count, 1) 46 try bare.git(["fsck", "--strict"]) 47 XCTAssertTrue(try bare.text(["show", "\(email):2024/07/13-Sat-10=15=30/Dive-41"]).contains("buddy \"Over HTTP\"")) 48 49 // A second device picks up the change. 50 let other = try LogRepository(directory: try LocalGit.temporaryDirectory("other"), branch: email, authorName: "DiveLog", authorEmail: email) 51 _ = try await other.sync(transport: transport) 52 log = try await other.logbook() 53 XCTAssertEqual(log.dives.last?.buddy, "Over HTTP") 54 } 55 56 /// Real cloud, read-only: CLOUD_EMAIL and CLOUD_PASSWORD in the environment. Sends nothing. 57 func testRealCloudReadOnly() async throws { 58 let env = ProcessInfo.processInfo.environment 59 guard let email = env["CLOUD_EMAIL"], let password = env["CLOUD_PASSWORD"] else { throw XCTSkip("CLOUD_EMAIL not set") } 60 let transport = SubsurfaceCloud.transport(email: email, password: password) 61 print("CLOUD url=\(transport.baseURL.absoluteString)") 62 let refs = try await GitClient(transport: transport, store: MemoryObjectStore()).refs() 63 print("CLOUD refs=\(refs.refs.keys.sorted()) caps=\(refs.capabilities.sorted())") 64 let repo = try LogRepository(directory: try LocalGit.temporaryDirectory("cloud"), branch: SubsurfaceCloud.sanitize(email: email), 65 authorName: "DiveLog", authorEmail: email) 66 let report = try await repo.sync(transport: transport) 67 XCTAssertEqual(report.sent, []) 68 let log = try await repo.logbook() 69 print("CLOUD dives=\(log.dives.count) sites=\(log.sites.count) trips=\(log.trips.count) warnings=\(log.warnings)") 70 var samples = 0 71 for dive in log.dives { samples += (try await repo.profile(of: dive)).reduce(0) { $0 + $1.samples.count } } 72 print("CLOUD samples=\(samples) numbers=\(log.dives.first?.number ?? 0)…\(log.dives.last?.number ?? 0) models=\(Set(log.dives.compactMap(\.computerModel)).sorted())") 73 } 74 }