Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions apple/TablePro/Core/Services/Account/AccountServices.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ internal final class AccountServices: AccountBackendListener {
// Whether subscribers know of a session, so a sign-out reaches them once, whether the core's listener or the
// call that caused it gets here first.
private var announcedSession = false
// Why the last sign-in ended, for Settings to say so until the next one.
private var signedOutBy: AccountSignOutCause?

init(
backend: any AccountBackend,
Expand Down Expand Up @@ -314,8 +316,9 @@ internal final class AccountServices: AccountBackendListener {

// A sign-out the person asked for is announced by the call that made it.
func accountStatusChanged(signedOutBy cause: AccountSignOutCause?) {
if cause != nil {
if let cause {
verifiedAt = nil
signedOutBy = cause == .user ? nil : cause
}
publish()
guard let cause, cause != .user, !isSignedIn else { return }
Expand Down Expand Up @@ -397,6 +400,7 @@ internal final class AccountServices: AccountBackendListener {
}
if snapshot.state == .signedIn {
announcedSession = true
signedOutBy = nil
}
let license = backend.license()
access.update(AccountState(
Expand All @@ -408,7 +412,8 @@ internal final class AccountServices: AccountBackendListener {
features: features,
verifiedAt: verifiedAt,
isRestored: isRestored,
sessionID: snapshot.sessionID
sessionID: snapshot.sessionID,
signedOutBy: signedOutBy
))
let current = now()
let boundaries = [entitlement?.expiresAt, license?.expiresAt, license?.graceEndsAt].compactMap { $0 }
Expand Down
2 changes: 2 additions & 0 deletions apple/TablePro/Core/Services/Account/FeatureAccess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ internal struct AccountState: Equatable, Sendable {
var isRestored = false
// The core's id of the sign-in; what the app keeps for a sign-in goes with it.
var sessionID: String?
// Why the last sign-in ended when the person did not end it; nil once signed in again.
var signedOutBy: AccountSignOutCause?
}

// Whether the person in front of the app has paid, which losing the network cannot undo.
Expand Down
4 changes: 3 additions & 1 deletion apple/TablePro/Core/Sync/MoveBetweenProviders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,9 @@ extension ProviderMoves {

nonisolated static func message(forCode code: String) -> String {
switch code {
case "sync.locked", "sync.not_allowed", "sync.device_not_approved", "sync.device_removed":
case "sync.device_removed":
return String(localized: "This Mac was removed from your account. Sign in again to use TablePro Sync here.")
case "sync.locked", "sync.not_allowed", "sync.device_not_approved":
return String(localized: "This Mac cannot read your account's keys. Approve it from another device, or use your recovery key.")
case "sync.network", "sync.server", "sync.rate_limited", "sync.restore_check_unavailable", "account.unreachable":
return String(localized: "TablePro Sync cannot be reached. Your data stays on this Mac; try again once you are online.")
Expand Down
6 changes: 6 additions & 0 deletions apple/TablePro/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -229647,6 +229647,12 @@
},
"This Mac was removed from your account" : {

},
"This Mac was removed from your account. Sign in again to use it here." : {

},
"This Mac was removed from your account. Sign in again to use TablePro Sync here." : {

},
"This Mac was removed from your account. Sign in again to use your keys." : {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ internal struct AccountSettingsContent: Equatable {
static let expiryWarning: TimeInterval = 3 * 24 * 60 * 60

let identity: Identity
// The server removed this Mac's row, which ended its sign-in (core ADR 0056, A locked device).
let removedFromAccount: Bool
let notices: [Notice]
let showsPlan: Bool
let plans: [PlanRow]
Expand Down Expand Up @@ -125,6 +127,7 @@ internal struct AccountSettingsContent: Equatable {

return AccountSettingsContent(
identity: identity(state: state, profile: profile),
removedFromAccount: !state.isSignedIn && entitlement == nil && state.signedOutBy == .deviceRevoked,
notices: notices(state: state, plans: plans, isDeviceLimit: isDeviceLimit, now: now),
showsPlan: showsPlan,
plans: plans,
Expand Down
15 changes: 14 additions & 1 deletion apple/TablePro/Views/Settings/Account/AccountSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ internal struct AccountSettingsView: View {
var body: some View {
let content = model.content
Form {
AccountIdentitySection(model: model, identity: content.identity, canRefresh: content.canRefresh)
AccountIdentitySection(
model: model,
identity: content.identity,
canRefresh: content.canRefresh,
removedFromAccount: content.removedFromAccount
)

AccountServerSection(model: model, content: content)

Expand Down Expand Up @@ -57,6 +62,7 @@ private struct AccountIdentitySection: View {
@ObservedObject var model: AccountSettingsModel
let identity: AccountSettingsContent.Identity
let canRefresh: Bool
let removedFromAccount: Bool

var body: some View {
Section {
Expand Down Expand Up @@ -142,6 +148,13 @@ private struct AccountIdentitySection: View {

signInFailure

if removedFromAccount {
AccountFailureLine(
message: String(localized: "This Mac was removed from your account. Sign in again to use it here."),
identifier: "account-removed"
)
}

if server.isProduction, server.othersAllowed {
LabeledContent {
Button(String(localized: "Use a Company Server…")) {
Expand Down
5 changes: 3 additions & 2 deletions apple/TablePro/Views/Sync/Keys/AccountRecoveryKeyFlow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ internal final class AccountRecoveryKeyFlow: ObservableObject, Identifiable {
}
}

// A device the rotation may keep: one this Mac compared words with. This Mac always stays.
// A device the rotation may keep, as the core offers it: one this Mac compared words with that a row still holds.
// This Mac always stays.
func canKeep(_ device: ReviewedDeviceInfo) -> Bool {
device.device.compared && !device.device.thisDevice
device.canKeep && !device.device.thisDevice
}

func setKeep(_ device: ReviewedDeviceInfo, _ kept: Bool) {
Expand Down
24 changes: 17 additions & 7 deletions apple/TablePro/Views/Sync/Keys/AccountRecoveryKeySheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ internal struct AccountRecoveryKeySheet: View {
Section {
ForEach(flow.devices, id: \.device.keyDevice) { device in
AccountKeepDeviceToggle(
device: device.device,
reviewed: device,
isOn: Binding(
get: { device.device.thisDevice || flow.keep.contains(device.device.keyDevice) },
set: { flow.setKeep(device, $0) }
Expand Down Expand Up @@ -230,23 +230,33 @@ internal struct AccountRecoveryKeySheet: View {

// A device a rotation can keep, ticked for those it keeps. This Mac always stays.
internal struct AccountKeepDeviceToggle: View {
let device: AccountDeviceInfo
let reviewed: ReviewedDeviceInfo
let isOn: Binding<Bool>
let isEnabled: Bool

private var subtitle: String {
private var device: AccountDeviceInfo {
reviewed.device
}

// A key device no row holds goes with the rotation, as the Devices list shows it removed.
static func subtitle(_ reviewed: ReviewedDeviceInfo) -> String {
let device = reviewed.device
if device.thisDevice {
return String(localized: "This Mac")
}
return isEnabled
? String(localized: "You compared words with it")
: String(localized: "Asks for approval again")
if reviewed.canKeep {
return String(localized: "You compared words with it")
}
if device.device == nil {
return String(localized: "Removed")
}
return String(localized: "Asks for approval again")
}

var body: some View {
Toggle(isOn: isOn) {
Text(verbatim: AccountKeysText.deviceName(device.name))
Text(verbatim: [AccountKeysText.platform(device.platform), subtitle].compactMap { $0 }.joined(separator: " · "))
Text(verbatim: [AccountKeysText.platform(device.platform), Self.subtitle(reviewed)].compactMap { $0 }.joined(separator: " · "))
}
.disabled(!isEnabled)
.accessibilityIdentifier("account-keep-device-\(device.keyDevice)")
Expand Down
5 changes: 3 additions & 2 deletions apple/TablePro/Views/Sync/Keys/AccountRemovalFlow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ internal final class AccountRemovalFlow: ObservableObject, Identifiable {
}
}

// Only a device this Mac compared words with can keep its keys; every other one joins again by approval.
// The core decides: a device this Mac compared words with that a row still holds. Every other one joins again by
// approval, and one no row holds was removed or signed out, so it goes with the lost one.
func canKeep(_ device: ReviewedDeviceInfo) -> Bool {
device.device.compared && !device.device.thisDevice && device.device.keyDevice != lost.keyDevice
device.canKeep && !device.device.thisDevice && device.device.keyDevice != lost.keyDevice
}

func setKeep(_ device: ReviewedDeviceInfo, _ kept: Bool) {
Expand Down
2 changes: 1 addition & 1 deletion apple/TablePro/Views/Sync/Keys/AccountRemovalSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ internal struct AccountRemovalSheet: View {
Section {
ForEach(review.devices, id: \.device.keyDevice) { device in
AccountKeepDeviceToggle(
device: device.device,
reviewed: device,
isOn: Binding(
get: { device.device.thisDevice || flow.keep.contains(device.device.keyDevice) },
set: { flow.setKeep(device, $0) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ struct AccountServicesTests {
}
}

// E2E F33: Settings says this Mac was removed until it signs in again.
@Test("Why the core signed the account out stays on its state until the next sign-in")
func signOutCauseStaysUntilSignIn() async throws {
let fixture = try AccountFixture()
try await fixture.signIn()
await fixture.server.set(Self.revokesDevice)

await fixture.services.refreshEntitlement()
let removed = fixture.access.state.signedOutBy
await fixture.server.set { $0.deviceRevoked = false }
try await fixture.signIn()

#expect(removed == .deviceRevoked)
#expect(fixture.access.state.isSignedIn)
#expect(fixture.access.state.signedOutBy == nil)
}

@Test("A key an older build stored is exchanged once through the core, then deleted with its payload")
func storedKeyIsExchanged() async throws {
let fixture = try AccountFixture()
Expand Down
9 changes: 9 additions & 0 deletions apple/TableProTests/Core/Sync/ProviderMovesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,15 @@ struct ProviderMovesTests {
#expect(!message.isEmpty)
#expect(!message.contains(code))
}

// E2E F33: approval can't help a removed Mac, so its words are its own.
@Test("A removed Mac is told it was removed, not to ask for approval")
func removedMacHasItsOwnWords() {
let removed = ProviderMoves.message(forCode: "sync.device_removed")

#expect(removed == String(localized: "This Mac was removed from your account. Sign in again to use TablePro Sync here."))
#expect(removed != ProviderMoves.message(forCode: "sync.device_not_approved"))
}
}

// The core engine as both the iCloud coordinator and the moves drive it, one per provider it is opened for, so the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ struct AccountSettingsContentTests {
#expect(content.machineID == Self.machineID)
}

// E2E F33: the server removed this Mac's row, which ended its sign-in. Signed out, the page says why; any other
// sign-out the core made says nothing more than signed out.
@Test("Signed out because this Mac was removed says so, and only then")
func signedOutAsRemoved() {
let removed = Self.make(AccountState(signedOutBy: .deviceRevoked))
let refused = Self.make(AccountState(signedOutBy: .refreshRefused))
let signedIn = Self.make(AccountState(isSignedIn: true, entitlement: Self.entitlement(), signedOutBy: .deviceRevoked))

#expect(removed.identity == .signedOut)
#expect(removed.removedFromAccount)
#expect(!refused.removedFromAccount)
#expect(!signedIn.removedFromAccount)
#expect(!Self.make(AccountState()).removedFromAccount)
}

@Test("A lifetime starter-tier license reads as Pro with a Lifetime status, owned personally, with the personal billing page")
func starterLifetime() throws {
let content = Self.make(AccountState(
Expand Down
12 changes: 6 additions & 6 deletions apple/TableProTests/Views/Sync/Keys/AccountKeysScreenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ struct AccountKeysScreenTests {
return sheet(.recoveryKey(flow), model)
case .replaceCurrentKey, .replaceDevices:
flows.rotationDevices = [
ReviewedDeviceInfo(device: FakeAccountKeyFlows.device("key-this", name: "MacBook Air", thisDevice: true), keepByDefault: false),
ReviewedDeviceInfo(device: FakeAccountKeyFlows.device("key-mini", name: "Mac mini", compared: true), keepByDefault: true),
ReviewedDeviceInfo(device: FakeAccountKeyFlows.device("key-phone", name: "iPhone"), keepByDefault: false),
ReviewedDeviceInfo(device: FakeAccountKeyFlows.device("key-this", name: "MacBook Air", thisDevice: true), keepByDefault: false, canKeep: false),
ReviewedDeviceInfo(device: FakeAccountKeyFlows.device("key-mini", name: "Mac mini", compared: true), keepByDefault: true, canKeep: true),
ReviewedDeviceInfo(device: FakeAccountKeyFlows.device("key-phone", name: "iPhone"), keepByDefault: false, canKeep: false),
]
let flow = AccountRecoveryKeyFlow(purpose: .replace) { flows }
if screen == .replaceDevices {
Expand All @@ -114,9 +114,9 @@ struct AccountKeysScreenTests {
flows.review = RemovalReviewInfo(
review: "review-1",
devices: [
ReviewedDeviceInfo(device: FakeAccountKeyFlows.device("key-this", name: "MacBook Air", thisDevice: true), keepByDefault: false),
ReviewedDeviceInfo(device: FakeAccountKeyFlows.device("key-mini", name: "Mac mini", compared: true), keepByDefault: true),
ReviewedDeviceInfo(device: FakeAccountKeyFlows.device("key-old", name: "Old MacBook Pro"), keepByDefault: false),
ReviewedDeviceInfo(device: FakeAccountKeyFlows.device("key-this", name: "MacBook Air", thisDevice: true), keepByDefault: false, canKeep: false),
ReviewedDeviceInfo(device: FakeAccountKeyFlows.device("key-mini", name: "Mac mini", compared: true), keepByDefault: true, canKeep: true),
ReviewedDeviceInfo(device: FakeAccountKeyFlows.device("key-old", name: "Old MacBook Pro"), keepByDefault: false, canKeep: false),
],
members: [
FakeAccountKeyFlows.member("team_a", "A", teamName: "Data Platform", name: "Ana Silva"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ struct AccountRecoveryKeyFlowTests {
let phone = FakeAccountKeyFlows.device("key-phone", compared: true)
let stranger = FakeAccountKeyFlows.device("key-stranger")
flows.rotationDevices = [
ReviewedDeviceInfo(device: this, keepByDefault: false),
ReviewedDeviceInfo(device: mini, keepByDefault: true),
ReviewedDeviceInfo(device: phone, keepByDefault: true),
ReviewedDeviceInfo(device: stranger, keepByDefault: true),
ReviewedDeviceInfo(device: this, keepByDefault: false, canKeep: false),
ReviewedDeviceInfo(device: mini, keepByDefault: true, canKeep: true),
ReviewedDeviceInfo(device: phone, keepByDefault: true, canKeep: true),
ReviewedDeviceInfo(device: stranger, keepByDefault: true, canKeep: false),
]
let flow = Self.flow(.replace, flows)
#expect(flow.step == .enterCurrentKey)
Expand All @@ -129,6 +129,29 @@ struct AccountRecoveryKeyFlowTests {
#expect(flows.calls(named: "finishReplace") == [FakeAccountKeyFlows.Call("finishReplace", ["replace-1"] + Self.groups([0, 13]) + ["key-mini"])])
}

// E2E F32: a compared device whose row signed out is on no row, so Replace never keeps it.
@Test("Replace never keeps a compared device the core can't keep")
func replaceSkipsRemovedDevice() async {
let flows = FakeAccountKeyFlows()
let signedOut = FakeAccountKeyFlows.device("key-signed-out", row: nil, compared: true)
let mini = FakeAccountKeyFlows.device("key-mini", row: "row-mini", compared: true)
flows.rotationDevices = [
ReviewedDeviceInfo(device: FakeAccountKeyFlows.device("key-this", thisDevice: true), keepByDefault: false, canKeep: false),
ReviewedDeviceInfo(device: signedOut, keepByDefault: true, canKeep: false),
ReviewedDeviceInfo(device: mini, keepByDefault: true, canKeep: true),
]
let flow = Self.flow(.replace, flows)
flow.currentKey = FakeAccountKeyFlows.recoveryKey
await flow.advance()

flow.setKeep(flows.rotationDevices[1], true)

#expect(flow.step == .chooseDevices)
#expect(flow.keep == ["key-mini"])
#expect(!flow.canKeep(flows.rotationDevices[1]))
#expect(AccountKeepDeviceToggle.subtitle(flows.rotationDevices[1]) == String(localized: "Removed"))
}

@Test("A wrong current key stays on its field with the reason")
func wrongCurrentKey() async {
let flows = FakeAccountKeyFlows()
Expand Down
Loading
Loading