Skip to content

Commit 3852a93

Browse files
authored
Recreate new installation after deletion from keychain (#112)
* Create new installation automatically after it's removed from keychain * Add changelog * Make sure installationId's aren't nil
1 parent 7024222 commit 3852a93

File tree

4 files changed

+100
-51
lines changed

4 files changed

+100
-51
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.2.5...1.2.6)
99

1010
__Fixes__
11-
- Crash when linking auth types due to server not sending sessionToken ([#109](https://github.com/parse-community/Parse-Swift/pull/109)), thanks to [Corey Baker](https://github.com/cbaker6).
11+
- Recreate installation automatically after deletion from Keychain ([#112](https://github.com/parse-community/Parse-Swift/pull/112)), thanks to [Corey Baker](https://github.com/cbaker6).
12+
- Error when linking auth types due to server not sending sessionToken ([#109](https://github.com/parse-community/Parse-Swift/pull/109)), thanks to [Corey Baker](https://github.com/cbaker6).
1213

1314
### 1.2.5
1415
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.2.4...1.2.5)

Sources/ParseSwift/Objects/ParseInstallation.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ extension ParseInstallation {
187187
#if !os(Linux) && !os(Android)
188188
try? KeychainStore.shared.delete(valueFor: ParseStorage.Keys.currentInstallation)
189189
#endif
190+
//Prepare new installation
191+
DispatchQueue.main.async {
192+
_ = BaseParseInstallation()
193+
}
190194
}
191195

192196
/**

Tests/ParseSwiftTests/ParseUserCombineTests.swift

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -293,32 +293,47 @@ class ParseUserCombineTests: XCTestCase { // swiftlint:disable:this type_body_le
293293
}
294294

295295
let expectation1 = XCTestExpectation(description: "Logout user1")
296-
let publisher = User.logoutPublisher()
297-
.sink(receiveCompletion: { result in
298-
299-
if case let .failure(error) = result {
300-
XCTFail(error.localizedDescription)
301-
}
296+
DispatchQueue.main.async {
297+
guard let oldInstallationId = BaseParseInstallation.current?.installationId else {
298+
XCTFail("Should have unwrapped")
302299
expectation1.fulfill()
303-
304-
}, receiveValue: { _ in
305-
if let userFromKeychain = BaseParseUser.current {
306-
XCTFail("\(userFromKeychain) wasn't deleted from Keychain during logout")
300+
return
307301
}
302+
let publisher = User.logoutPublisher()
303+
.sink(receiveCompletion: { result in
308304

309-
if let installationFromMemory: CurrentInstallationContainer<BaseParseInstallation>
310-
= try? ParseStorage.shared.get(valueFor: ParseStorage.Keys.currentInstallation) {
311-
XCTFail("\(installationFromMemory) wasn't deleted from memory during logout")
312-
}
305+
if case let .failure(error) = result {
306+
XCTFail(error.localizedDescription)
307+
}
308+
expectation1.fulfill()
313309

314-
#if !os(Linux) && !os(Android)
315-
if let installationFromKeychain: CurrentInstallationContainer<BaseParseInstallation>
316-
= try? KeychainStore.shared.get(valueFor: ParseStorage.Keys.currentInstallation) {
317-
XCTFail("\(installationFromKeychain) wasn't deleted from Keychain during logout")
318-
}
319-
#endif
320-
})
321-
publisher.store(in: &subscriptions)
310+
}, receiveValue: { _ in
311+
if let userFromKeychain = BaseParseUser.current {
312+
XCTFail("\(userFromKeychain) wasn't deleted from Keychain during logout")
313+
}
314+
DispatchQueue.main.async {
315+
if let installationFromMemory: CurrentInstallationContainer<BaseParseInstallation>
316+
= try? ParseStorage.shared.get(valueFor: ParseStorage.Keys.currentInstallation) {
317+
if installationFromMemory.installationId == oldInstallationId
318+
&& installationFromMemory.installationId != nil {
319+
XCTFail("\(installationFromMemory) wasn't deleted and recreated in memory during logout")
320+
}
321+
}
322+
323+
#if !os(Linux) && !os(Android)
324+
if let installationFromKeychain: CurrentInstallationContainer<BaseParseInstallation>
325+
= try? KeychainStore.shared.get(valueFor: ParseStorage.Keys.currentInstallation) {
326+
if installationFromKeychain.installationId == oldInstallationId
327+
&& installationFromKeychain.installationId != nil {
328+
// swiftlint:disable:next line_length
329+
XCTFail("\(installationFromKeychain) wasn't deleted and recreated in Keychain during logout")
330+
}
331+
}
332+
#endif
333+
}
334+
})
335+
publisher.store(in: &subscriptions)
336+
}
322337
wait(for: [expectation1], timeout: 20.0)
323338
}
324339

Tests/ParseSwiftTests/ParseUserTests.swift

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,48 +1094,77 @@ class ParseUserTests: XCTestCase { // swiftlint:disable:this type_body_length
10941094
return nil
10951095
}
10961096
}
1097-
do {
1098-
try User.logout()
1099-
if let userFromKeychain = BaseParseUser.current {
1100-
XCTFail("\(userFromKeychain) wasn't deleted from Keychain during logout")
1101-
}
11021097

1103-
if let installationFromKeychain = BaseParseInstallation.current {
1104-
XCTFail("\(installationFromKeychain) wasn't deleted from Keychain during logout")
1098+
DispatchQueue.main.async {
1099+
guard let oldInstallationId = BaseParseInstallation.current?.installationId else {
1100+
XCTFail("Should have unwrapped")
1101+
return
1102+
}
1103+
do {
1104+
try User.logout()
1105+
if let userFromKeychain = BaseParseUser.current {
1106+
XCTFail("\(userFromKeychain) wasn't deleted from Keychain during logout")
1107+
}
1108+
DispatchQueue.main.async {
1109+
if let installationFromKeychain = BaseParseInstallation.current {
1110+
if installationFromKeychain.installationId == oldInstallationId
1111+
&& installationFromKeychain.installationId != nil {
1112+
XCTFail("\(installationFromKeychain) wasn't deleted then created in Keychain during logout")
1113+
}
1114+
}
1115+
}
1116+
} catch {
1117+
XCTFail(error.localizedDescription)
11051118
}
1106-
} catch {
1107-
XCTFail(error.localizedDescription)
11081119
}
11091120
}
11101121

11111122
func logoutAsync(callbackQueue: DispatchQueue) {
11121123

11131124
let expectation1 = XCTestExpectation(description: "Logout user1")
1114-
User.logout(callbackQueue: callbackQueue) { result in
11151125

1116-
switch result {
1126+
DispatchQueue.main.async {
1127+
guard let oldInstallationId = BaseParseInstallation.current?.installationId else {
1128+
XCTFail("Should have unwrapped")
1129+
expectation1.fulfill()
1130+
return
1131+
}
11171132

1118-
case .success:
1119-
if let userFromKeychain = BaseParseUser.current {
1120-
XCTFail("\(userFromKeychain) wasn't deleted from Keychain during logout")
1121-
}
1133+
User.logout(callbackQueue: callbackQueue) { result in
11221134

1123-
if let installationFromMemory: CurrentInstallationContainer<BaseParseInstallation>
1124-
= try? ParseStorage.shared.get(valueFor: ParseStorage.Keys.currentInstallation) {
1125-
XCTFail("\(installationFromMemory) wasn't deleted from memory during logout")
1126-
}
1135+
switch result {
11271136

1128-
#if !os(Linux) && !os(Android)
1129-
if let installationFromKeychain: CurrentInstallationContainer<BaseParseInstallation>
1130-
= try? KeychainStore.shared.get(valueFor: ParseStorage.Keys.currentInstallation) {
1131-
XCTFail("\(installationFromKeychain) wasn't deleted from Keychain during logout")
1132-
}
1133-
#endif
1137+
case .success:
1138+
if let userFromKeychain = BaseParseUser.current {
1139+
XCTFail("\(userFromKeychain) wasn't deleted from Keychain during logout")
1140+
}
1141+
DispatchQueue.main.async {
1142+
if let installationFromMemory: CurrentInstallationContainer<BaseParseInstallation>
1143+
= try? ParseStorage.shared.get(valueFor: ParseStorage.Keys.currentInstallation) {
1144+
if installationFromMemory.installationId == oldInstallationId
1145+
&& installationFromMemory.installationId != nil {
1146+
// swiftlint:disable:next line_length
1147+
XCTFail("\(installationFromMemory) wasn't deleted and recreated in memory during logout")
1148+
}
1149+
}
11341150

1135-
case .failure(let error):
1136-
XCTFail(error.localizedDescription)
1151+
#if !os(Linux) && !os(Android)
1152+
if let installationFromKeychain: CurrentInstallationContainer<BaseParseInstallation>
1153+
= try? KeychainStore.shared.get(valueFor: ParseStorage.Keys.currentInstallation) {
1154+
if installationFromKeychain.installationId == oldInstallationId
1155+
&& installationFromKeychain.installationId != nil {
1156+
// swiftlint:disable:next line_length
1157+
XCTFail("\(installationFromKeychain) wasn't deleted and recreated in Keychain during logout")
1158+
}
1159+
}
1160+
#endif
1161+
}
1162+
1163+
case .failure(let error):
1164+
XCTFail(error.localizedDescription)
1165+
}
1166+
expectation1.fulfill()
11371167
}
1138-
expectation1.fulfill()
11391168
}
11401169
wait(for: [expectation1], timeout: 20.0)
11411170
}

0 commit comments

Comments
 (0)