Skip to content

Commit fe67c88

Browse files
authored
refactor: linking current user (#292)
* refactor: linking current user * Remove build step for SPM
1 parent 4734212 commit fe67c88

File tree

8 files changed

+81
-61
lines changed

8 files changed

+81
-61
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
env:
1919
DEVELOPER_DIR: ${{ env.CI_XCODE_13 }}
2020
- name: Build-Test
21-
run: set -o pipefail && env NSUnbufferedIO=YES xcodebuild -workspace Parse.xcworkspace -scheme ParseSwift\ \(iOS\) -destination platform\=iOS\ Simulator,name\=iPhone\ 12\ Pro\ Max -derivedDataPath DerivedData clean test | xcpretty
21+
run: set -o pipefail && env NSUnbufferedIO=YES xcodebuild -workspace Parse.xcworkspace -scheme ParseSwift\ \(iOS\) -destination platform\=iOS\ Simulator,name\=iPhone\ 12\ Pro\ Max -derivedDataPath DerivedData -test-iterations 10 -retry-tests-on-failure clean test | xcpretty
2222
env:
2323
DEVELOPER_DIR: ${{ env.CI_XCODE_13 }}
2424
- name: Prepare codecov
@@ -49,7 +49,7 @@ jobs:
4949
security unlock-keychain -p "" temporary
5050
security set-keychain-settings -lut 7200 temporary
5151
- name: Build-Test
52-
run: set -o pipefail && env NSUnbufferedIO=YES xcodebuild -workspace Parse.xcworkspace -scheme ParseSwift\ \(macOS\) -destination platform\=macOS -derivedDataPath DerivedData clean test | xcpretty
52+
run: set -o pipefail && env NSUnbufferedIO=YES xcodebuild -workspace Parse.xcworkspace -scheme ParseSwift\ \(macOS\) -destination platform\=macOS -derivedDataPath DerivedData -test-iterations 10 -retry-tests-on-failure clean test | xcpretty
5353
env:
5454
DEVELOPER_DIR: ${{ env.CI_XCODE_13 }}
5555
- name: Prepare codecov
@@ -74,7 +74,7 @@ jobs:
7474
steps:
7575
- uses: actions/checkout@v2
7676
- name: Build
77-
run: set -o pipefail && env NSUnbufferedIO=YES xcodebuild -workspace Parse.xcworkspace -scheme ParseSwift\ \(tvOS\) -destination platform\=tvOS\ Simulator,name\=Apple\ TV -derivedDataPath DerivedData clean test | xcpretty
77+
run: set -o pipefail && env NSUnbufferedIO=YES xcodebuild -workspace Parse.xcworkspace -scheme ParseSwift\ \(tvOS\) -destination platform\=tvOS\ Simulator,name\=Apple\ TV -derivedDataPath DerivedData -test-iterations 10 -retry-tests-on-failure clean test | xcpretty
7878
env:
7979
DEVELOPER_DIR: ${{ env.CI_XCODE_13 }}
8080
- name: Prepare codecov
@@ -119,11 +119,7 @@ jobs:
119119
security default-keychain -s temporary
120120
security unlock-keychain -p "" temporary
121121
security set-keychain-settings -lut 7200 temporary
122-
- name: Build
123-
run: swift build -v
124-
env:
125-
DEVELOPER_DIR: ${{ env.CI_XCODE_13 }}
126-
- name: Test
122+
- name: Build and Test
127123
run: swift test --enable-code-coverage -v
128124
env:
129125
DEVELOPER_DIR: ${{ env.CI_XCODE_13 }}
@@ -155,11 +151,7 @@ jobs:
155151
security default-keychain -s temporary
156152
security unlock-keychain -p "" temporary
157153
security set-keychain-settings -lut 7200 temporary
158-
- name: Build
159-
run: swift build -v
160-
env:
161-
DEVELOPER_DIR: ${{ env.CI_XCODE_VER }}
162-
- name: Test
154+
- name: Build and Test
163155
run: swift test --enable-code-coverage -v
164156
env:
165157
DEVELOPER_DIR: ${{ env.CI_XCODE_VER }}
@@ -186,10 +178,8 @@ jobs:
186178
with:
187179
release-version: "5"
188180
github-token: ${{ secrets.GITHUB_TOKEN }}
189-
- name: Build
190-
run: swift build
191-
- name: Test
192-
run: swift test --enable-test-discovery --enable-code-coverage
181+
- name: Build and Test
182+
run: swift test --enable-test-discovery --enable-code-coverage -v
193183
- name: Prepare codecov
194184
run: |
195185
llvm-cov export -format="lcov" .build/x86_64-unknown-linux-gnu/debug/ParseSwiftPackageTests.xctest -instr-profile .build/x86_64-unknown-linux-gnu/debug/codecov/default.profdata > info_linux.lcov
@@ -206,7 +196,7 @@ jobs:
206196
- uses: MaxDesiatov/swift-windows-action@v1
207197
with:
208198
swift-version: "5.5.1"
209-
shell-action: swift test --enable-test-discovery --enable-code-coverage
199+
shell-action: swift test --enable-test-discovery --enable-code-coverage -v
210200
- name: Upload coverage to Codecov
211201
uses: codecov/codecov-action@v2
212202
with:

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
__New features__
1212
- Add a retry mechanism to the SDK that randomly (up to 3 seconds each) tries to reconnect up to 5 times. The developer can increase or reduce the amount of retries when configuring the SDK ([#291](https://github.com/parse-community/Parse-Swift/pull/291)), thanks to [Corey Baker](https://github.com/cbaker6).
13-
- Add toCLLocation and toCLLocationCoordinate2D methods for easy conversion from a ParseGeoPoint object. ([#287](https://github.com/parse-community/Parse-Swift/pull/287)), thanks to [Jayson Ng](https://github.com/jaysonng).
13+
- Add toCLLocation and toCLLocationCoordinate2D methods for easy conversion from a ParseGeoPoint object ([#287](https://github.com/parse-community/Parse-Swift/pull/287)), thanks to [Jayson Ng](https://github.com/jaysonng).
1414

1515
__Fixes__
1616
- Fixed an issue where an annonymous couldn't be turned into a regular user using signup ([#291](https://github.com/parse-community/Parse-Swift/pull/291)), thanks to [Corey Baker](https://github.com/cbaker6).

Sources/ParseSwift/Authentication/Protocols/ParseAuthentication.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,15 @@ public extension ParseUser {
432432
}
433433
}
434434

435-
internal func linkCommand() -> API.Command<Self, Self> {
436-
var mutableSelf = self
437-
mutableSelf = mutableSelf.anonymous.strip(mutableSelf)
435+
internal func linkCommand() throws -> API.Command<Self, Self> {
436+
var mutableSelf = self.anonymous.strip(self)
437+
if let current = Self.current {
438+
guard current.hasSameObjectId(as: self) else {
439+
let error = ParseError(code: .unknownError,
440+
message: "Can't signup a user with a different objectId than the current user")
441+
throw error
442+
}
443+
}
438444
return API.Command<Self, Self>(method: .PUT,
439445
path: endpoint,
440446
body: mutableSelf) { (data) -> Self in

Sources/ParseSwift/Objects/ParseUser.swift

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -557,12 +557,7 @@ extension ParseUser {
557557
public func signup(options: API.Options = []) throws -> Self {
558558
var options = options
559559
options.insert(.cachePolicy(.reloadIgnoringLocalCacheData))
560-
if let current = Self.current {
561-
guard current.hasSameObjectId(as: self) else {
562-
let error = ParseError(code: .unknownError,
563-
message: "Can't signup a user with a different objectId than the current user")
564-
throw error
565-
}
560+
if Self.current != nil {
566561
return try self.linkCommand()
567562
.execute(options: options)
568563
} else {
@@ -587,20 +582,24 @@ extension ParseUser {
587582
completion: @escaping (Result<Self, ParseError>) -> Void) {
588583
var options = options
589584
options.insert(.cachePolicy(.reloadIgnoringLocalCacheData))
590-
if let current = Self.current {
591-
guard current.hasSameObjectId(as: self) else {
592-
let error = ParseError(code: .unknownError,
593-
message: "Can't signup a user with a different objectId than the current user")
585+
if Self.current != nil {
586+
do {
587+
try self.linkCommand()
588+
.executeAsync(options: options,
589+
callbackQueue: callbackQueue) { result in
590+
completion(result)
591+
}
592+
} catch {
594593
callbackQueue.async {
595-
completion(.failure(error))
594+
if let parseError = error as? ParseError {
595+
completion(.failure(parseError))
596+
} else {
597+
let parseError = ParseError(code: .unknownError,
598+
message: error.localizedDescription)
599+
completion(.failure(parseError))
600+
}
596601
}
597-
return
598602
}
599-
self.linkCommand()
600-
.executeAsync(options: options,
601-
callbackQueue: callbackQueue) { result in
602-
completion(result)
603-
}
604603
} else {
605604
do {
606605
try signupCommand()

Tests/ParseSwiftTests/APICommandTests.swift

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,34 @@ class APICommandTests: XCTestCase {
261261
}
262262
}
263263

264-
//This is less common as the HTTP won't be able to produce ParseErrors directly, but used for testing
265-
func testErrorHTTPReturnsParseError1() {
264+
func testErrorHTTPReturns400NoDataFromServer() {
266265
let originalError = ParseError(code: .unknownError, message: "Couldn't decode")
267266
MockURLProtocol.mockRequests { _ in
268-
return MockURLResponse(error: originalError)
267+
return MockURLResponse(error: originalError) // Status code defaults to 400
268+
}
269+
do {
270+
_ = try API.NonParseBodyCommand<NoBody, NoBody>(method: .GET,
271+
path: .login,
272+
params: nil,
273+
mapper: { (_) -> NoBody in
274+
throw originalError
275+
}).execute(options: [])
276+
XCTFail("Should have thrown an error")
277+
} catch {
278+
guard let error = error as? ParseError else {
279+
XCTFail("should be able unwrap final error to ParseError")
280+
return
281+
}
282+
XCTAssertEqual(originalError.code, error.code)
283+
}
284+
}
285+
286+
func testErrorHTTPReturns500NoDataFromServer() {
287+
let originalError = ParseError(code: .unknownError, message: "Couldn't decode")
288+
MockURLProtocol.mockRequests { _ in
289+
var response = MockURLResponse(error: originalError)
290+
response.statusCode = 500
291+
return response
269292
}
270293
do {
271294
_ = try API.NonParseBodyCommand<NoBody, NoBody>(method: .GET,

Tests/ParseSwiftTests/NetworkMocking/MockURLProtocol.swift

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class MockURLProtocol: URLProtocol {
3737
mockRequestsPassing(Int.max, test: test, with: response)
3838
}
3939

40-
class func mockRequestsPassing(_ attempts: Int, test: @escaping (URLRequest) -> Bool,
40+
class func mockRequestsPassing(_ attempts: Int,
41+
test: @escaping (URLRequest) -> Bool,
4142
with response: @escaping (URLRequest) -> MockURLResponse?) {
4243
let mock = MockURLProtocolMock(attempts: attempts, test: test, response: response)
4344
mocks.append(mock)
@@ -77,7 +78,9 @@ class MockURLProtocol: URLProtocol {
7778
return request
7879
}
7980

80-
override required init(request: URLRequest, cachedResponse: CachedURLResponse?, client: URLProtocolClient?) {
81+
override required init(request: URLRequest,
82+
cachedResponse: CachedURLResponse?,
83+
client: URLProtocolClient?) {
8184
super.init(request: request, cachedResponse: cachedResponse, client: client)
8285
guard let mock = MockURLProtocol.firstMockForRequest(request) else {
8386
self.mock = nil
@@ -105,17 +108,17 @@ class MockURLProtocol: URLProtocol {
105108
}
106109

107110
guard let url = request.url,
108-
let urlResponse = HTTPURLResponse(url: url, statusCode: response.statusCode,
109-
httpVersion: "HTTP/2", headerFields: response.headerFields) else {
110-
return
111-
}
111+
let urlResponse = HTTPURLResponse(url: url,
112+
statusCode: response.statusCode,
113+
httpVersion: "HTTP/2",
114+
headerFields: response.headerFields) else {
115+
return
116+
}
112117

113118
DispatchQueue.global(qos: .default).asyncAfter(deadline: .now() + response.delay) {
114-
115119
if !self.loading {
116120
return
117121
}
118-
119122
self.client?.urlProtocol(self, didReceive: urlResponse, cacheStoragePolicy: .notAllowed)
120123
if let data = response.responseData {
121124
self.client?.urlProtocol(self, didLoad: data)

Tests/ParseSwiftTests/NetworkMocking/MockURLResponse.swift

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,17 @@ struct MockURLResponse {
2727
try self.init(string: string, statusCode: 200, delay: .init(0.0))
2828
}
2929

30-
init(string: String, statusCode: Int, delay: TimeInterval,
30+
init(string: String,
31+
statusCode: Int,
32+
delay: TimeInterval,
3133
headerFields: [String: String] = ["Content-Type": "application/json"]) throws {
32-
33-
do {
34-
let encoded = try JSONEncoder().encode(string)
35-
self.init(data: encoded, statusCode: statusCode, delay: delay, headerFields: headerFields)
36-
} catch {
37-
throw ParseError(code: .unknownError, message: "unable to convert string to data")
38-
}
34+
let encoded = try JSONEncoder().encode(string)
35+
self.init(data: encoded, statusCode: statusCode, delay: delay, headerFields: headerFields)
3936
}
4037

41-
init(data: Data, statusCode: Int, delay: TimeInterval,
38+
init(data: Data,
39+
statusCode: Int,
40+
delay: TimeInterval,
4241
headerFields: [String: String] = ["Content-Type": "application/json"]) {
4342
self.statusCode = statusCode
4443
self.headerFields = headerFields

Tests/ParseSwiftTests/ParseAuthenticationTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class ParseAuthenticationTests: XCTestCase {
167167
var user = User()
168168
user.username = "hello"
169169
user.password = "world"
170-
let command = user.linkCommand()
170+
let command = try user.linkCommand()
171171
XCTAssertNotNil(command)
172172
XCTAssertEqual(command.path.urlComponent, "/users")
173173
XCTAssertEqual(command.method, API.Method.PUT)
@@ -188,7 +188,7 @@ class ParseAuthenticationTests: XCTestCase {
188188

189189
func testLinkCommandNoBodyLoggedIn() throws {
190190
let user = try loginNormally()
191-
let command = user.linkCommand()
191+
let command = try user.linkCommand()
192192
XCTAssertNotNil(command)
193193
XCTAssertEqual(command.path.urlComponent, "/users/\("yarr")")
194194
XCTAssertEqual(command.method, API.Method.PUT)

0 commit comments

Comments
 (0)