You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
5.0 KiB
119 lines
5.0 KiB
//
|
|
// DataAccessSyncTests.swift
|
|
// PadelClubDataTests
|
|
//
|
|
// Created by Laurent Morvillier on 02/05/2025.
|
|
//
|
|
|
|
import Testing
|
|
@testable import PadelClubData
|
|
@testable import LeStorage
|
|
|
|
struct SyncDataAccessTests {
|
|
|
|
let username1: String = "UserDataTests"
|
|
let password1: String = "MyPass1234--"
|
|
|
|
let username2: String = "seconduser"
|
|
let password2: String = "MyPass1234--"
|
|
|
|
var secondStoreCenter: StoreCenter
|
|
|
|
init() async throws {
|
|
FileManager.default.deleteDirectoryInDocuments(directoryName: "storage")
|
|
FileManager.default.deleteDirectoryInDocuments(directoryName: "storage-2")
|
|
|
|
self.secondStoreCenter = StoreCenter(directoryName: "storage-2")
|
|
self.secondStoreCenter.configureURLs(secureScheme: false, domain: "127.0.0.1:8000", webSockets: false, useSynchronization: true)
|
|
self.secondStoreCenter.tokenKeychain = MockKeychainStore(fileName: "storage-2/token.json")
|
|
self.secondStoreCenter.deviceKeychain = MockKeychainStore(fileName: "storage-2/device.json")
|
|
try self.secondStoreCenter.deviceKeychain.add(value: UUID().uuidString)
|
|
|
|
self.secondStoreCenter.classProject = "PadelClubData"
|
|
|
|
let token2 = try? self.secondStoreCenter.rawTokenShouldNotBeUsed()
|
|
if token2 == nil {
|
|
try await self.login(storeCenter: self.secondStoreCenter, username: self.username2, password: self.password2)
|
|
}
|
|
|
|
StoreCenter.main.configureURLs(secureScheme: false, domain: "127.0.0.1:8000", webSockets: false, useSynchronization: true)
|
|
StoreCenter.main.tokenKeychain = MockKeychainStore(fileName: "storage/token.json")
|
|
StoreCenter.main.deviceKeychain = MockKeychainStore(fileName: "storage/device.json")
|
|
try StoreCenter.main.deviceKeychain.add(value: UUID().uuidString)
|
|
StoreCenter.main.classProject = "PadelClubData"
|
|
|
|
let token = try? StoreCenter.main.rawTokenShouldNotBeUsed()
|
|
if token == nil {
|
|
try await self.login(storeCenter: StoreCenter.main, username: self.username1, password: self.password1)
|
|
}
|
|
}
|
|
|
|
mutating func login(storeCenter: StoreCenter, username: String, password: String) async throws {
|
|
let _: CustomUser = try await storeCenter.service().login(username: username, password: password)
|
|
}
|
|
|
|
@Test func testSetup() async throws {
|
|
#expect(StoreCenter.main.isAuthenticated)
|
|
#expect(self.secondStoreCenter.isAuthenticated)
|
|
|
|
guard let userId1 = StoreCenter.main.userId else {
|
|
throw TestError.notAuthenticated
|
|
}
|
|
guard let userId2 = self.secondStoreCenter.userId else {
|
|
throw TestError.notAuthenticated
|
|
}
|
|
#expect(userId1 != userId2)
|
|
}
|
|
|
|
@Test func testDataAccess() async throws {
|
|
|
|
guard let userId1 = StoreCenter.main.userId else {
|
|
throw TestError.notAuthenticated
|
|
}
|
|
guard let userId2 = self.secondStoreCenter.userId else {
|
|
throw TestError.notAuthenticated
|
|
}
|
|
|
|
// Setup
|
|
let eventColA: SyncedCollection<Event> = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection()
|
|
let tournamentColA: SyncedCollection<Tournament> = await StoreCenter.main.mainStore.asyncLoadingSynchronizedCollection()
|
|
let eventColB: SyncedCollection<Event> = await self.secondStoreCenter.mainStore.asyncLoadingSynchronizedCollection()
|
|
let tournamentColB: SyncedCollection<Tournament> = await self.secondStoreCenter.mainStore.asyncLoadingSynchronizedCollection()
|
|
|
|
await eventColA.deleteAsync(contentOfs: Array(eventColA))
|
|
await tournamentColA.deleteAsync(contentOfs: Array(tournamentColA))
|
|
let _ = try await self.secondStoreCenter.testSynchronizeOnceAsync()
|
|
|
|
#expect(eventColB.count == 0)
|
|
#expect(tournamentColB.count == 0)
|
|
|
|
// Create
|
|
let eventA = Event(creator: userId1)
|
|
await eventColA.addOrUpdateAsync(instance: eventA)
|
|
let tournamentA = Tournament(event: eventA.id, name: "P100")
|
|
await tournamentColA.addOrUpdateAsync(instance: tournamentA)
|
|
|
|
// Share with user2
|
|
try await StoreCenter.main.setAuthorizedUsersAsync(for: tournamentA, users: [userId2])
|
|
|
|
var dataB = try await self.secondStoreCenter.testSynchronizeOnceAsync()
|
|
var syncDataB = try SyncData(data: dataB, storeCenter: self.secondStoreCenter)
|
|
#expect(syncDataB.grants.count == 2)
|
|
|
|
#expect(eventColB.count == 1)
|
|
#expect(tournamentColB.count == 1)
|
|
|
|
// Remove sharing from user2
|
|
try await StoreCenter.main.setAuthorizedUsersAsync(for: tournamentA, users: [])
|
|
|
|
dataB = try await self.secondStoreCenter.testSynchronizeOnceAsync()
|
|
syncDataB = try SyncData(data: dataB, storeCenter: self.secondStoreCenter)
|
|
#expect(syncDataB.revocations.count == 1)
|
|
#expect(syncDataB.revocationParents.count == 1)
|
|
|
|
#expect(eventColB.count == 0)
|
|
#expect(tournamentColB.count == 0)
|
|
|
|
}
|
|
|
|
}
|
|
|