Skip to content

Commit

Permalink
fix: Only encode email on ParseUser if different from Keychain (#256)
Browse files Browse the repository at this point in the history
* Failing test cases

* Compare email update to Keychain on Apple devices

* Check Keychain for container instead of User
  • Loading branch information
cbaker6 authored Oct 14, 2021
1 parent 1385abf commit c53abab
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ coverage:
status:
patch:
default:
target: 0
target: auto
changes: false
project:
default:
Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

### main

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

### 2.0.1
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/2.0.0...2.0.1)

__Fixes__
- 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).

### 2.0.0
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.11.0...2.0.0)

Expand Down
2 changes: 1 addition & 1 deletion Sources/ParseSwift/Objects/ParseInstallation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ extension ParseInstallation {
// MARK: Fetchable
extension ParseInstallation {
internal static func updateKeychainIfNeeded(_ results: [Self], deleting: Bool = false) throws {
guard let currentInstallation = BaseParseInstallation.current else {
guard let currentInstallation = Self.current else {
return
}

Expand Down
19 changes: 14 additions & 5 deletions Sources/ParseSwift/Objects/ParseUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ extension ParseUser {
}
try? KeychainStore.shared.delete(valueFor: ParseStorage.Keys.currentUser)
#endif
BaseParseUser.currentContainer = nil
Self.currentContainer = nil
}

/**
Expand Down Expand Up @@ -685,7 +685,7 @@ extension ParseUser {
// MARK: Fetchable
extension ParseUser {
internal static func updateKeychainIfNeeded(_ results: [Self], deleting: Bool = false) throws {
guard let currentUser = BaseParseUser.current else {
guard let currentUser = Self.current else {
return
}

Expand Down Expand Up @@ -946,9 +946,18 @@ extension ParseUser {
private func updateCommand() -> API.Command<Self, Self> {
var mutableSelf = self
if let currentUser = Self.current,
currentUser.hasSameObjectId(as: mutableSelf) == true,
currentUser.email == mutableSelf.email {
mutableSelf.email = nil
currentUser.hasSameObjectId(as: mutableSelf) == true {
#if !os(Linux) && !os(Android)
// swiftlint:disable:next line_length
if let currentUserContainerInKeychain: CurrentUserContainer<BaseParseUser> = try? KeychainStore.shared.get(valueFor: ParseStorage.Keys.currentUser),
currentUserContainerInKeychain.currentUser?.email == mutableSelf.email {
mutableSelf.email = nil
}
#else
if currentUser.email == mutableSelf.email {
mutableSelf.email = nil
}
#endif
}
let mapper = { (data) -> Self in
try ParseCoding.jsonDecoder().decode(UpdateResponse.self, from: data).apply(to: self)
Expand Down
4 changes: 4 additions & 0 deletions Sources/ParseSwift/Parse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public struct ParseConfiguration {
- parameter httpAdditionalHeaders: A dictionary of additional headers to send with requests. See Apple's
[documentation](https://developer.apple.com/documentation/foundation/urlsessionconfiguration/1411532-httpadditionalheaders)
for more info.
- parameter migrateFromObjcSDK: If your app previously used the iOS Objective-C SDK, setting this value
to `true` will attempt to migrate relevant data stored in the Keychain to ParseSwift. Defaults to `false`.
- parameter deleteKeychainIfNeeded: Deletes the Parse Keychain when the app is running for the first time.
Defaults to `false`.
- parameter authentication: A callback block that will be used to receive/accept/decline network challenges.
Defaults to `nil` in which the SDK will use the default OS authentication methods for challenges.
It should have the following argument signature: `(challenge: URLAuthenticationChallenge,
Expand Down
40 changes: 40 additions & 0 deletions Tests/ParseSwiftTests/ParseUserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,46 @@ class ParseUserTests: XCTestCase { // swiftlint:disable:this type_body_length
XCTAssertEqual(command.body?.email, email)
}

#if !os(Linux) && !os(Android)
func testUpdateCommandCurrentUserModifiedEmail() throws {
try userSignUp()
guard let user = User.current,
let objectId = user.objectId else {
XCTFail("Should have current user.")
return
}
let email = "[email protected]"
User.current?.email = email
XCTAssertNotNil(User.current?.email)
let command = try User.current?.saveCommand()
XCTAssertNotNil(command)
XCTAssertEqual(command?.path.urlComponent, "/users/\(objectId)")
XCTAssertEqual(command?.method, API.Method.PUT)
XCTAssertNil(command?.params)
XCTAssertNotNil(command?.body)
XCTAssertEqual(command?.body?.email, email)
}

func testUpdateCommandCurrentUserNotCurrentModifiedEmail() throws {
try userSignUp()
guard let user = User.current,
let objectId = user.objectId else {
XCTFail("Should have current user.")
return
}
let email = "[email protected]"
User.current?.email = email
XCTAssertNotNil(User.current?.email)
let command = try User.current?.saveCommand()
XCTAssertNotNil(command)
XCTAssertEqual(command?.path.urlComponent, "/users/\(objectId)")
XCTAssertEqual(command?.method, API.Method.PUT)
XCTAssertNil(command?.params)
XCTAssertNotNil(command?.body)
XCTAssertEqual(command?.body?.email, email)
}
#endif

func testSaveAndUpdateCurrentUser() throws { // swiftlint:disable:this function_body_length
XCTAssertNil(User.current?.objectId)
try userSignUp()
Expand Down

0 comments on commit c53abab

Please sign in to comment.