parent
b80cd18db1
commit
8a8a421891
@ -0,0 +1,109 @@ |
|||||||
|
// |
||||||
|
// DataAccessSyncTests.swift |
||||||
|
// PadelClubDataTests |
||||||
|
// |
||||||
|
// Created by Laurent Morvillier on 02/05/2025. |
||||||
|
// |
||||||
|
|
||||||
|
import Testing |
||||||
|
@testable import PadelClubData |
||||||
|
@testable import LeStorage |
||||||
|
|
||||||
|
struct DataAccessSyncTests { |
||||||
|
|
||||||
|
let username1: String = "UserDataTests" |
||||||
|
let password1: String = "MyPass1234--" |
||||||
|
|
||||||
|
let username2: String = "seconduser" |
||||||
|
let password2: String = "MyPass1234--" |
||||||
|
|
||||||
|
var secondStoreCenter: StoreCenter |
||||||
|
|
||||||
|
init() async throws { |
||||||
|
|
||||||
|
self.secondStoreCenter = StoreCenter(directoryName: "storage-2") |
||||||
|
self.secondStoreCenter.configureURLs(secureScheme: false, domain: "127.0.0.1:8000", webSockets: false) |
||||||
|
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() |
||||||
|
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) |
||||||
|
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() |
||||||
|
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.synchronizedCollectionWithFileLoading() |
||||||
|
let tournamentColA: SyncedCollection<Tournament> = await StoreCenter.main.mainStore.synchronizedCollectionWithFileLoading() |
||||||
|
let eventColB: SyncedCollection<Event> = await self.secondStoreCenter.mainStore.synchronizedCollectionWithFileLoading() |
||||||
|
let tournamentColB: SyncedCollection<Tournament> = await self.secondStoreCenter.mainStore.synchronizedCollectionWithFileLoading() |
||||||
|
|
||||||
|
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 StoreCenter.main.setAuthorizedUsers(for: tournamentA, users: [userId2]) |
||||||
|
|
||||||
|
let dataB = try await self.secondStoreCenter.testSynchronizeOnceAsync() |
||||||
|
let 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 StoreCenter.main.setAuthorizedUsers(for: tournamentA, users: []) |
||||||
|
|
||||||
|
let _ = try await self.secondStoreCenter.testSynchronizeOnceAsync() |
||||||
|
#expect(eventColB.count == 0) |
||||||
|
#expect(tournamentColB.count == 0) |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue