Skip to content

Commit c53abab

Browse files
authored
fix: Only encode email on ParseUser if different from Keychain (#256)
* Failing test cases * Compare email update to Keychain on Apple devices * Check Keychain for container instead of User
1 parent 1385abf commit c53abab

File tree

6 files changed

+67
-8
lines changed

6 files changed

+67
-8
lines changed

.codecov.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ coverage:
44
status:
55
patch:
66
default:
7-
target: 0
7+
target: auto
88
changes: false
99
project:
1010
default:

CHANGELOG.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
### main
44

5-
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/2.0.0...main)
5+
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/2.0.1...main)
66
* _Contributing to this repo? Add info about your change here to be included in the next release_
77

8+
### 2.0.1
9+
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/2.0.0...2.0.1)
10+
11+
__Fixes__
12+
- ParseUser should only encode email when User.current?.email is different from current user email ([#256](https://github.com/parse-community/Parse-Swift/pull/256)), thanks to [Corey Baker](https://github.com/cbaker6).
13+
814
### 2.0.0
915
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.11.0...2.0.0)
1016

Sources/ParseSwift/Objects/ParseInstallation.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ extension ParseInstallation {
323323
// MARK: Fetchable
324324
extension ParseInstallation {
325325
internal static func updateKeychainIfNeeded(_ results: [Self], deleting: Bool = false) throws {
326-
guard let currentInstallation = BaseParseInstallation.current else {
326+
guard let currentInstallation = Self.current else {
327327
return
328328
}
329329

Sources/ParseSwift/Objects/ParseUser.swift

+14-5
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ extension ParseUser {
128128
}
129129
try? KeychainStore.shared.delete(valueFor: ParseStorage.Keys.currentUser)
130130
#endif
131-
BaseParseUser.currentContainer = nil
131+
Self.currentContainer = nil
132132
}
133133

134134
/**
@@ -685,7 +685,7 @@ extension ParseUser {
685685
// MARK: Fetchable
686686
extension ParseUser {
687687
internal static func updateKeychainIfNeeded(_ results: [Self], deleting: Bool = false) throws {
688-
guard let currentUser = BaseParseUser.current else {
688+
guard let currentUser = Self.current else {
689689
return
690690
}
691691

@@ -946,9 +946,18 @@ extension ParseUser {
946946
private func updateCommand() -> API.Command<Self, Self> {
947947
var mutableSelf = self
948948
if let currentUser = Self.current,
949-
currentUser.hasSameObjectId(as: mutableSelf) == true,
950-
currentUser.email == mutableSelf.email {
951-
mutableSelf.email = nil
949+
currentUser.hasSameObjectId(as: mutableSelf) == true {
950+
#if !os(Linux) && !os(Android)
951+
// swiftlint:disable:next line_length
952+
if let currentUserContainerInKeychain: CurrentUserContainer<BaseParseUser> = try? KeychainStore.shared.get(valueFor: ParseStorage.Keys.currentUser),
953+
currentUserContainerInKeychain.currentUser?.email == mutableSelf.email {
954+
mutableSelf.email = nil
955+
}
956+
#else
957+
if currentUser.email == mutableSelf.email {
958+
mutableSelf.email = nil
959+
}
960+
#endif
952961
}
953962
let mapper = { (data) -> Self in
954963
try ParseCoding.jsonDecoder().decode(UpdateResponse.self, from: data).apply(to: self)

Sources/ParseSwift/Parse.swift

+4
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ public struct ParseConfiguration {
8484
- parameter httpAdditionalHeaders: A dictionary of additional headers to send with requests. See Apple's
8585
[documentation](https://developer.apple.com/documentation/foundation/urlsessionconfiguration/1411532-httpadditionalheaders)
8686
for more info.
87+
- parameter migrateFromObjcSDK: If your app previously used the iOS Objective-C SDK, setting this value
88+
to `true` will attempt to migrate relevant data stored in the Keychain to ParseSwift. Defaults to `false`.
89+
- parameter deleteKeychainIfNeeded: Deletes the Parse Keychain when the app is running for the first time.
90+
Defaults to `false`.
8791
- parameter authentication: A callback block that will be used to receive/accept/decline network challenges.
8892
Defaults to `nil` in which the SDK will use the default OS authentication methods for challenges.
8993
It should have the following argument signature: `(challenge: URLAuthenticationChallenge,

Tests/ParseSwiftTests/ParseUserTests.swift

+40
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,46 @@ class ParseUserTests: XCTestCase { // swiftlint:disable:this type_body_length
542542
XCTAssertEqual(command.body?.email, email)
543543
}
544544

545+
#if !os(Linux) && !os(Android)
546+
func testUpdateCommandCurrentUserModifiedEmail() throws {
547+
try userSignUp()
548+
guard let user = User.current,
549+
let objectId = user.objectId else {
550+
XCTFail("Should have current user.")
551+
return
552+
}
553+
let email = "[email protected]"
554+
User.current?.email = email
555+
XCTAssertNotNil(User.current?.email)
556+
let command = try User.current?.saveCommand()
557+
XCTAssertNotNil(command)
558+
XCTAssertEqual(command?.path.urlComponent, "/users/\(objectId)")
559+
XCTAssertEqual(command?.method, API.Method.PUT)
560+
XCTAssertNil(command?.params)
561+
XCTAssertNotNil(command?.body)
562+
XCTAssertEqual(command?.body?.email, email)
563+
}
564+
565+
func testUpdateCommandCurrentUserNotCurrentModifiedEmail() throws {
566+
try userSignUp()
567+
guard let user = User.current,
568+
let objectId = user.objectId else {
569+
XCTFail("Should have current user.")
570+
return
571+
}
572+
let email = "[email protected]"
573+
User.current?.email = email
574+
XCTAssertNotNil(User.current?.email)
575+
let command = try User.current?.saveCommand()
576+
XCTAssertNotNil(command)
577+
XCTAssertEqual(command?.path.urlComponent, "/users/\(objectId)")
578+
XCTAssertEqual(command?.method, API.Method.PUT)
579+
XCTAssertNil(command?.params)
580+
XCTAssertNotNil(command?.body)
581+
XCTAssertEqual(command?.body?.email, email)
582+
}
583+
#endif
584+
545585
func testSaveAndUpdateCurrentUser() throws { // swiftlint:disable:this function_body_length
546586
XCTAssertNil(User.current?.objectId)
547587
try userSignUp()

0 commit comments

Comments
 (0)