~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
57 lines · 2.3 KB Raw History
import Foundation
import UIKit
import XCTest

@testable import OldFella

final class SecureSessionUploadFixture {
    let suiteName: String
    let defaults: UserDefaults
    let session: URLSession
    let token: String
    var uploadedImageSizes: [[String: Int]] = []
    lazy var service = ATProtoBlueskyService(defaults: defaults, session: session)

    init() throws {
        suiteName = "OldFellaServiceTests.\(UUID().uuidString)"
        defaults = try XCTUnwrap(UserDefaults(suiteName: suiteName))
        let configuration = URLSessionConfiguration.ephemeral
        configuration.protocolClasses = [StubURLProtocol.self]
        session = URLSession(configuration: configuration)
        let futureExpiry = Int(Date().addingTimeInterval(3600).timeIntervalSince1970)
        let payload = Data("{\"exp\":\(futureExpiry)}".utf8).base64EncodedString()
            .replacingOccurrences(of: "+", with: "-")
            .replacingOccurrences(of: "/", with: "_")
            .replacingOccurrences(of: "=", with: "")
        token = "header.\(payload).signature"
    }

    func install() {
        StubURLProtocol.clearRequests()
        StubURLProtocol.handler = response
    }

    func cleanUp() {
        StubURLProtocol.handler = nil
        session.invalidateAndCancel()
        defaults.removePersistentDomain(forName: suiteName)
    }

    func response(_ request: URLRequest) throws -> (Int, Data) {
        let routes: [String: (URLRequest) throws -> (Int, Data)] = [
            "/xrpc/com.atproto.identity.resolveHandle": resolveHandle,
            "/did:plc:reader": resolveDID,
            "/xrpc/com.atproto.server.createSession": createSession,
            "/xrpc/app.bsky.actor.getProfile": profile,
            "/xrpc/app.bsky.feed.getTimeline": timeline,
            "/xrpc/app.bsky.notification.updateSeen": updateSeen,
            "/xrpc/app.bsky.notification.listNotifications": notifications,
            "/xrpc/app.bsky.feed.getPosts": posts,
            "/xrpc/app.bsky.feed.getPostThread": thread,
            "/xrpc/com.atproto.repo.uploadBlob": uploadBlob,
            "/xrpc/com.atproto.repo.createRecord": createRecord
        ]
        return try routes[request.url?.path ?? ""]?(request)
            ?? (404, Data("{\"error\":\"NotFound\",\"message\":\"Unexpected test request\"}".utf8))
    }
}