Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Test identifyUser (many ways)
Browse files Browse the repository at this point in the history
  • Loading branch information
bfollington committed Oct 11, 2023
1 parent 541d13b commit 80cbd87
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions xcode/Subconscious/SubconsciousTests/TestUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct TestUtilities {
path: "database.sqlite",
directoryHint: .notDirectory
)
print("Test SQLite Path: \(databaseURL.path(percentEncoded: false))")
let db = SQLite3Database(
path: databaseURL.path(percentEncoded: false),
mode: .readwrite
Expand Down
121 changes: 121 additions & 0 deletions xcode/Subconscious/SubconsciousTests/Tests_UserProfileService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,127 @@ final class Tests_UserProfileService: XCTestCase {
XCTAssertTrue(profile.following[1].user.aliases.contains(where: { name in name == Petname("sphere-b")! }))
}

func testIdentifyUserContext() async throws {
let tmp = try TestUtilities.createTmpDir()
let environment = try await TestUtilities.createDataServiceEnvironment(tmp: tmp)

let firstPass = {
let receipt = try await environment.noosphere.createSphere(ownerKeyName: "test")
let did = Did(receipt.identity)!

await environment.noosphere.resetSphere(receipt.identity)

try await environment.userProfile.writeOurProfile(
profile: UserProfileEntry(
nickname: "Finn",
bio: "Mathematical!"
)
)

return did
}
// In a closure to avoid polluting lexical scope
let did = try await firstPass()

let receipt = try await environment.noosphere.createSphere(ownerKeyName: "test")
let ourDid = Did(receipt.identity)!

await environment.noosphere.resetSphere(receipt.identity)

try await environment.userProfile.writeOurProfile(
profile: UserProfileEntry(
nickname: "Jake",
bio: "Bacon pancakes!"
)
)

let _ = try await environment.data.indexOurSphere()

let ourProfile = try await environment.userProfile.identifyUser(
did: ourDid,
address: Slashlink(petname: Petname("more.path")!),
context: .petname(Petname("some.long.path")!)
)

// Our profile should not be rebased using context
XCTAssertEqual(ourProfile.did, ourDid)
XCTAssertEqual(ourProfile.address, Slashlink.ourProfile)

// Someone we know SHOULD be rebased
let profile = try await environment.userProfile.identifyUser(
did: did,
address: Slashlink(petname: Petname("more.path")!),
context: .petname(Petname("some.long.path")!)
)

XCTAssertEqual(profile.did, did)
XCTAssertEqual(profile.address, Slashlink(petname: Petname("more.path.some.long.path")!))

// Do not rebase on a DID path
let profileB = try await environment.userProfile.identifyUser(
did: did,
address: Slashlink(petname: Petname("more.path")!),
context: .did(ourDid)
)

XCTAssertEqual(profileB.did, did)
XCTAssertEqual(profileB.address, Slashlink(petname: Petname("more.path")!))
}

func testIdentifyUserSignatures() async throws {
let tmp = try TestUtilities.createTmpDir()
let environment = try await TestUtilities.createDataServiceEnvironment(tmp: tmp)

let receipt = try await environment.noosphere.createSphere(ownerKeyName: "test")
let did = Did(receipt.identity)!

await environment.noosphere.resetSphere(receipt.identity)

try await environment.userProfile.writeOurProfile(
profile: UserProfileEntry(
nickname: "Finn",
bio: "Mathematical!"
)
)

let _ = try await environment.data.indexOurSphere()

let profileA = try await environment.userProfile.identifyUser(
did: did,
address: Slashlink.ourProfile,
context: .none
)

let profileB = try await environment.userProfile.identifyUser(
did: did,
petname: nil,
context: .none
)

let profileC = try await environment.userProfile.identifyUser(
entry: AddressBookEntry(
petname: Petname("ourselves")!,
did: did,
status: .resolved(Cid("ok")),
version: Cid("ok")
),
context: .none
)

XCTAssertEqual(profileB.did, did)
XCTAssertEqual(profileC.did, did)

let profiles = [profileA, profileB, profileC]
for profile in profiles {
XCTAssertEqual(profile.did, did)
XCTAssertTrue(profile.address.isOurProfile)
XCTAssertEqual(profile.ourFollowStatus, .notFollowing)
XCTAssertEqual(profile.category, .ourself)
XCTAssertEqual(profile.bio?.text.description, "Mathematical!")
XCTAssertEqual(profile.nickname?.description, "finn")
}
}

func testReadUserProfile() async throws {
let tmp = try TestUtilities.createTmpDir()
let environment = try await TestUtilities.createDataServiceEnvironment(tmp: tmp)
Expand Down

0 comments on commit 80cbd87

Please sign in to comment.