From 759e40a20515e5cd4e85891b4bd4a3045215796a Mon Sep 17 00:00:00 2001 From: Worthing ~ <115107835+w-goog@users.noreply.github.com> Date: Fri, 30 Oct 2026 15:38:50 -0700 Subject: [PATCH 1/4] g-orchestrated: add OIDFakeAuthorizationService to AppAuthTestHelpers --- .../OIDFakeAuthorizationService.h | 74 ++++++++++ .../OIDFakeAuthorizationService.m | 67 +++++++++ UnitTests/OIDFakeAuthorizationServiceTests.m | 135 ++++++++++++++++++ 3 files changed, 276 insertions(+) create mode 100644 Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.h create mode 100644 Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.m create mode 100644 UnitTests/OIDFakeAuthorizationServiceTests.m diff --git a/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.h b/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.h new file mode 100644 index 000000000..5c245df75 --- /dev/null +++ b/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.h @@ -0,0 +1,74 @@ +/*! @file OIDFakeAuthorizationService.h + @brief AppAuth iOS SDK + @copyright + Copyright 2026 The AppAuth Authors. All Rights Reserved. + @copydetails + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + +#import + +#import "OIDAuthorizationService.h" +#import "OIDFakeExternalUserAgentSession.h" + +NS_ASSUME_NONNULL_BEGIN + +/*! @brief An @c OIDAuthorizationService whose instance methods record their arguments and keep the + callbacks for the test to invoke, instead of presenting UI or making network requests. + @discussion Inject an instance where production code would otherwise call the class methods of + @c OIDAuthorizationService. The class methods of this fake are inherited unchanged and still + perform real work; only the instance methods are faked. + */ +@interface OIDFakeAuthorizationService : OIDAuthorizationService + +/*! @brief The request passed to the most recent @c -presentAuthorizationRequest:externalUserAgent:callback:. */ +@property(nonatomic, readonly, nullable) OIDAuthorizationRequest *lastAuthorizationRequest; + +/*! @brief The external user agent passed to the most recent authorization request. For the + platform convenience methods this is the agent AppAuth built, e.g. an + @c OIDExternalUserAgentIOS whose @c presentingViewController is the one the caller supplied. */ +@property(nonatomic, readonly, nullable) id lastExternalUserAgent; + +/*! @brief The callback of the most recent authorization request. Invoke it to complete the flow. */ +@property(nonatomic, readonly, copy, nullable) OIDAuthorizationCallback lastAuthorizationCallback; + +/*! @brief The session returned from the most recent authorization request. */ +@property(nonatomic, readonly, nullable) OIDFakeExternalUserAgentSession *lastAuthorizationSession; + +/*! @brief The request passed to the most recent @c -presentEndSessionRequest:externalUserAgent:callback:. */ +@property(nonatomic, readonly, nullable) OIDEndSessionRequest *lastEndSessionRequest; + +/*! @brief The callback of the most recent end-session request. */ +@property(nonatomic, readonly, copy, nullable) OIDEndSessionCallback lastEndSessionCallback; + +/*! @brief How many token requests have been performed. */ +@property(nonatomic, readonly) NSUInteger tokenRequestCount; + +/*! @brief The request passed to the most recent @c -performTokenRequest:originalAuthorizationResponse:callback:. */ +@property(nonatomic, readonly, nullable) OIDTokenRequest *lastTokenRequest; + +/*! @brief The authorization response passed alongside the most recent token request, if any. */ +@property(nonatomic, readonly, nullable) OIDAuthorizationResponse *lastOriginalAuthorizationResponse; + +/*! @brief The callback of the most recent token request. Invoke it to complete the request. */ +@property(nonatomic, readonly, copy, nullable) OIDTokenCallback lastTokenCallback; + +/*! @brief The request passed to the most recent @c -performRegistrationRequest:completion:. */ +@property(nonatomic, readonly, nullable) OIDRegistrationRequest *lastRegistrationRequest; + +/*! @brief The completion of the most recent registration request. */ +@property(nonatomic, readonly, copy, nullable) OIDRegistrationCompletion lastRegistrationCompletion; + +@end + +NS_ASSUME_NONNULL_END diff --git a/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.m b/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.m new file mode 100644 index 000000000..f5f9b03a3 --- /dev/null +++ b/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.m @@ -0,0 +1,67 @@ +/*! @file OIDFakeAuthorizationService.m + @brief AppAuth iOS SDK + @copyright + Copyright 2026 The AppAuth Authors. All Rights Reserved. + @copydetails + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + +#import "OIDFakeAuthorizationService.h" + +#import "OIDAuthorizationRequest.h" +#import "OIDAuthorizationResponse.h" +#import "OIDEndSessionRequest.h" +#import "OIDExternalUserAgent.h" +#import "OIDRegistrationRequest.h" +#import "OIDTokenRequest.h" + +NS_ASSUME_NONNULL_BEGIN + +@implementation OIDFakeAuthorizationService + +- (id)presentAuthorizationRequest:(OIDAuthorizationRequest *)request + externalUserAgent:(id)externalUserAgent + callback:(OIDAuthorizationCallback)callback { + _lastAuthorizationRequest = request; + _lastExternalUserAgent = externalUserAgent; + _lastAuthorizationCallback = [callback copy]; + _lastAuthorizationSession = [[OIDFakeExternalUserAgentSession alloc] init]; + return _lastAuthorizationSession; +} + +- (id)presentEndSessionRequest:(OIDEndSessionRequest *)request + externalUserAgent:(id)externalUserAgent + callback:(OIDEndSessionCallback)callback { + _lastEndSessionRequest = request; + _lastEndSessionCallback = [callback copy]; + return [[OIDFakeExternalUserAgentSession alloc] init]; +} + +- (void)performTokenRequest:(OIDTokenRequest *)request + originalAuthorizationResponse:(nullable OIDAuthorizationResponse *)authorizationResponse + callback:(OIDTokenCallback)callback { + _tokenRequestCount++; + _lastTokenRequest = request; + _lastOriginalAuthorizationResponse = authorizationResponse; + _lastTokenCallback = [callback copy]; +} + +- (void)performRegistrationRequest:(OIDRegistrationRequest *)request + completion:(OIDRegistrationCompletion)completion { + _lastRegistrationRequest = request; + _lastRegistrationCompletion = [completion copy]; +} + +@end + +NS_ASSUME_NONNULL_END diff --git a/UnitTests/OIDFakeAuthorizationServiceTests.m b/UnitTests/OIDFakeAuthorizationServiceTests.m new file mode 100644 index 000000000..a54b92e33 --- /dev/null +++ b/UnitTests/OIDFakeAuthorizationServiceTests.m @@ -0,0 +1,135 @@ +/*! @file OIDFakeAuthorizationServiceTests.m + @brief AppAuth iOS SDK + @copyright + Copyright 2026 The AppAuth Authors. All Rights Reserved. + @copydetails + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ + +#import + +#if SWIFT_PACKAGE +@import AppAuthCore; +@import AppAuthTestHelpers; +#else +#import "Sources/AppAuthCore.h" +#import "Sources/AppAuthTestHelpers/AppAuthTestHelpers.h" +#endif +#import "OIDAuthorizationRequestTests.h" +#import "OIDAuthorizationResponseTests.h" +#import "OIDTokenRequestTests.h" + +@interface OIDNoOpExternalUserAgent : NSObject +@end + +@implementation OIDNoOpExternalUserAgent + +- (BOOL)presentExternalUserAgentRequest:(id)request + session:(id)session { + return NO; +} + +- (void)dismissExternalUserAgentAnimated:(BOOL)animated completion:(void (^)(void))completion { +} + +@end + +@interface OIDFakeAuthorizationServiceTests : XCTestCase +@end + +@implementation OIDFakeAuthorizationServiceTests + +- (void)testPerformTokenRequestIsRecordedAndCallbackIsReturned { + OIDFakeAuthorizationService *service = [[OIDFakeAuthorizationService alloc] init]; + OIDTokenRequest *request = [OIDTokenRequestTests testInstance]; + OIDAuthorizationResponse *authorizationResponse = [OIDAuthorizationResponseTests testInstance]; + __block OIDTokenResponse *receivedResponse = nil; + __block NSError *receivedError = nil; + + [service performTokenRequest:request + originalAuthorizationResponse:authorizationResponse + callback:^(OIDTokenResponse *_Nullable tokenResponse, + NSError *_Nullable error) { + receivedResponse = tokenResponse; + receivedError = error; + }]; + + XCTAssertEqual(service.tokenRequestCount, 1u); + XCTAssertEqual(service.lastTokenRequest, request); + XCTAssertEqual(service.lastOriginalAuthorizationResponse, authorizationResponse); + XCTAssertNotNil(service.lastTokenCallback); + + NSError *someError = [NSError errorWithDomain:@"com.example" code:42 userInfo:nil]; + service.lastTokenCallback(nil, someError); + XCTAssertNil(receivedResponse); + XCTAssertEqual(receivedError, someError); +} + +- (void)testTwoArgumentPerformTokenRequestRoutesToThreeArgument { + OIDFakeAuthorizationService *service = [[OIDFakeAuthorizationService alloc] init]; + OIDTokenRequest *request = [OIDTokenRequestTests testInstance]; + + [service performTokenRequest:request + callback:^(OIDTokenResponse *_Nullable tokenResponse, + NSError *_Nullable error) { + }]; + + XCTAssertEqual(service.tokenRequestCount, 1u); + XCTAssertEqual(service.lastTokenRequest, request); + XCTAssertNil(service.lastOriginalAuthorizationResponse); +} + +- (void)testPresentAuthorizationRequestReturnsFakeSession { + OIDFakeAuthorizationService *service = [[OIDFakeAuthorizationService alloc] init]; + OIDAuthorizationRequest *request = [OIDAuthorizationRequestTests testInstance]; + OIDNoOpExternalUserAgent *userAgent = [[OIDNoOpExternalUserAgent alloc] init]; + __block BOOL callbackInvoked = NO; + + id session = + [service presentAuthorizationRequest:request + externalUserAgent:userAgent + callback:^(OIDAuthorizationResponse *_Nullable response, + NSError *_Nullable error) { + callbackInvoked = YES; + }]; + + XCTAssertEqual(session, service.lastAuthorizationSession); + XCTAssertTrue([session isKindOfClass:[OIDFakeExternalUserAgentSession class]]); + XCTAssertEqual(service.lastAuthorizationRequest, request); + XCTAssertEqual(service.lastExternalUserAgent, userAgent); + + [session cancel]; + XCTAssertEqual(service.lastAuthorizationSession.cancelCount, 1u); + XCTAssertFalse(callbackInvoked); +} + +- (void)testFakeSessionResumeRecordsURLAndHonoursResumeResult { + OIDFakeExternalUserAgentSession *session = [[OIDFakeExternalUserAgentSession alloc] init]; + NSURL *firstURL = [NSURL URLWithString:@"https://example.com/callback?code=1"]; + NSURL *secondURL = [NSURL URLWithString:@"https://example.com/callback?code=2"]; + + session.resumeResult = NO; + NSError *error = nil; + XCTAssertFalse([session resumeExternalUserAgentFlowWithURL:firstURL error:&error]); + XCTAssertEqualObjects(session.lastResumedURL, firstURL); + XCTAssertEqualObjects(error.domain, OIDGeneralErrorDomain); + XCTAssertEqual(error.code, OIDErrorCodeURLMismatch); + + session.resumeResult = YES; + error = nil; + XCTAssertTrue([session resumeExternalUserAgentFlowWithURL:secondURL error:&error]); + XCTAssertEqualObjects(session.lastResumedURL, secondURL); + XCTAssertNil(error); +} + +@end From 47885d75a9a19f38f7edbe6b8db38a83c465629d Mon Sep 17 00:00:00 2001 From: Worthing ~ <115107835+w-goog@users.noreply.github.com> Date: Fri, 30 Oct 2026 15:45:32 -0700 Subject: [PATCH 2/4] g-orchestrated: tidy the fake service and its tests --- CHANGELOG.md | 2 +- .../AppAuthTestHelpers/AppAuthTestHelpers.h | 1 + .../OIDFakeAuthorizationService.h | 44 +++++++++----- .../OIDFakeAuthorizationService.m | 14 +++-- UnitTests/OIDFakeAuthorizationServiceTests.m | 59 ++++++++----------- 5 files changed, 66 insertions(+), 54 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d6a7deaad..b33281aa7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ - Added instance-method counterparts of the `OIDAuthorizationService` class methods, and made `-init` public, so the service can be injected and faked in tests. - Deprecated `OIDAuthorizationService.configuration`. It is never set and is always `nil`; use the request's `configuration` instead. It will be removed in 4.0. - Added instance-method counterparts of the iOS and macOS `OIDAuthorizationService` convenience methods, and read-only `presentingViewController` / `presentingWindow` properties on the external user agents. -- Added the `AppAuthTestHelpers` Swift package product, with `OIDFakeExternalUserAgentSession`, for testing code that depends on AppAuth. +- Added the `AppAuthTestHelpers` Swift package product, with `OIDFakeAuthorizationService` and `OIDFakeExternalUserAgentSession`, for testing code that depends on AppAuth. # 3.0.0 - BREAKING: Updates made to support Xcode 27. ([#972](https://github.com/openid/AppAuth-iOS/pull/972), [#973](https://github.com/openid/AppAuth-iOS/pull/973)) diff --git a/Sources/AppAuthTestHelpers/AppAuthTestHelpers.h b/Sources/AppAuthTestHelpers/AppAuthTestHelpers.h index ec07d2698..aa6954c63 100644 --- a/Sources/AppAuthTestHelpers/AppAuthTestHelpers.h +++ b/Sources/AppAuthTestHelpers/AppAuthTestHelpers.h @@ -16,4 +16,5 @@ limitations under the License. */ +#import "OIDFakeAuthorizationService.h" #import "OIDFakeExternalUserAgentSession.h" diff --git a/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.h b/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.h index 5c245df75..504146819 100644 --- a/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.h +++ b/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.h @@ -31,42 +31,58 @@ NS_ASSUME_NONNULL_BEGIN */ @interface OIDFakeAuthorizationService : OIDAuthorizationService -/*! @brief The request passed to the most recent @c -presentAuthorizationRequest:externalUserAgent:callback:. */ +/*! @brief The request passed to the most recent + @c -presentAuthorizationRequest:externalUserAgent:callback:. + */ @property(nonatomic, readonly, nullable) OIDAuthorizationRequest *lastAuthorizationRequest; /*! @brief The external user agent passed to the most recent authorization request. For the - platform convenience methods this is the agent AppAuth built, e.g. an - @c OIDExternalUserAgentIOS whose @c presentingViewController is the one the caller supplied. */ + platform convenience methods, this is the agent AppAuth built for the caller's view + controller or window. + */ @property(nonatomic, readonly, nullable) id lastExternalUserAgent; -/*! @brief The callback of the most recent authorization request. Invoke it to complete the flow. */ +/*! @brief The callback of the most recent authorization request. Invoke it to complete the flow. + */ @property(nonatomic, readonly, copy, nullable) OIDAuthorizationCallback lastAuthorizationCallback; -/*! @brief The session returned from the most recent authorization request. */ +/*! @brief The session returned from the most recent authorization request. + */ @property(nonatomic, readonly, nullable) OIDFakeExternalUserAgentSession *lastAuthorizationSession; -/*! @brief The request passed to the most recent @c -presentEndSessionRequest:externalUserAgent:callback:. */ +/*! @brief The request passed to the most recent + @c -presentEndSessionRequest:externalUserAgent:callback:. + */ @property(nonatomic, readonly, nullable) OIDEndSessionRequest *lastEndSessionRequest; -/*! @brief The callback of the most recent end-session request. */ +/*! @brief The callback of the most recent end-session request. + */ @property(nonatomic, readonly, copy, nullable) OIDEndSessionCallback lastEndSessionCallback; -/*! @brief How many token requests have been performed. */ +/*! @brief How many token requests have been performed. + */ @property(nonatomic, readonly) NSUInteger tokenRequestCount; -/*! @brief The request passed to the most recent @c -performTokenRequest:originalAuthorizationResponse:callback:. */ +/*! @brief The request passed to the most recent + @c -performTokenRequest:originalAuthorizationResponse:callback:. + */ @property(nonatomic, readonly, nullable) OIDTokenRequest *lastTokenRequest; -/*! @brief The authorization response passed alongside the most recent token request, if any. */ -@property(nonatomic, readonly, nullable) OIDAuthorizationResponse *lastOriginalAuthorizationResponse; +/*! @brief The authorization response passed alongside the most recent token request, if any. + */ +@property(nonatomic, readonly, nullable) + OIDAuthorizationResponse *lastOriginalAuthorizationResponse; -/*! @brief The callback of the most recent token request. Invoke it to complete the request. */ +/*! @brief The callback of the most recent token request. Invoke it to complete the request. + */ @property(nonatomic, readonly, copy, nullable) OIDTokenCallback lastTokenCallback; -/*! @brief The request passed to the most recent @c -performRegistrationRequest:completion:. */ +/*! @brief The request passed to the most recent @c -performRegistrationRequest:completion:. + */ @property(nonatomic, readonly, nullable) OIDRegistrationRequest *lastRegistrationRequest; -/*! @brief The completion of the most recent registration request. */ +/*! @brief The completion of the most recent registration request. + */ @property(nonatomic, readonly, copy, nullable) OIDRegistrationCompletion lastRegistrationCompletion; @end diff --git a/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.m b/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.m index f5f9b03a3..68a443282 100644 --- a/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.m +++ b/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.m @@ -29,9 +29,10 @@ @implementation OIDFakeAuthorizationService -- (id)presentAuthorizationRequest:(OIDAuthorizationRequest *)request - externalUserAgent:(id)externalUserAgent - callback:(OIDAuthorizationCallback)callback { +- (id) + presentAuthorizationRequest:(OIDAuthorizationRequest *)request + externalUserAgent:(id)externalUserAgent + callback:(OIDAuthorizationCallback)callback { _lastAuthorizationRequest = request; _lastExternalUserAgent = externalUserAgent; _lastAuthorizationCallback = [callback copy]; @@ -39,9 +40,10 @@ @implementation OIDFakeAuthorizationService return _lastAuthorizationSession; } -- (id)presentEndSessionRequest:(OIDEndSessionRequest *)request - externalUserAgent:(id)externalUserAgent - callback:(OIDEndSessionCallback)callback { +- (id) + presentEndSessionRequest:(OIDEndSessionRequest *)request + externalUserAgent:(id)externalUserAgent + callback:(OIDEndSessionCallback)callback { _lastEndSessionRequest = request; _lastEndSessionCallback = [callback copy]; return [[OIDFakeExternalUserAgentSession alloc] init]; diff --git a/UnitTests/OIDFakeAuthorizationServiceTests.m b/UnitTests/OIDFakeAuthorizationServiceTests.m index a54b92e33..905a5b3e9 100644 --- a/UnitTests/OIDFakeAuthorizationServiceTests.m +++ b/UnitTests/OIDFakeAuthorizationServiceTests.m @@ -1,19 +1,19 @@ /*! @file OIDFakeAuthorizationServiceTests.m - @brief AppAuth iOS SDK - @copyright + @brief AppAuth iOS SDK + @copyright Copyright 2026 The AppAuth Authors. All Rights Reserved. - @copydetails - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. + @copydetails + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. */ #import @@ -29,6 +29,8 @@ #import "OIDAuthorizationResponseTests.h" #import "OIDTokenRequestTests.h" +/*! @brief An external user agent that never presents anything. + */ @interface OIDNoOpExternalUserAgent : NSObject @end @@ -40,15 +42,21 @@ - (BOOL)presentExternalUserAgentRequest:(id)request } - (void)dismissExternalUserAgentAnimated:(BOOL)animated completion:(void (^)(void))completion { + if (completion) { + completion(); + } } @end +/*! @brief Unit tests for @c OIDFakeAuthorizationService. + */ @interface OIDFakeAuthorizationServiceTests : XCTestCase @end @implementation OIDFakeAuthorizationServiceTests +/*! @brief A token request is recorded, and invoking the kept callback reaches the caller. */ - (void)testPerformTokenRequestIsRecordedAndCallbackIsReturned { OIDFakeAuthorizationService *service = [[OIDFakeAuthorizationService alloc] init]; OIDTokenRequest *request = [OIDTokenRequestTests testInstance]; @@ -75,6 +83,7 @@ - (void)testPerformTokenRequestIsRecordedAndCallbackIsReturned { XCTAssertEqual(receivedError, someError); } +/*! @brief The two-argument token request is recorded with no authorization response. */ - (void)testTwoArgumentPerformTokenRequestRoutesToThreeArgument { OIDFakeAuthorizationService *service = [[OIDFakeAuthorizationService alloc] init]; OIDTokenRequest *request = [OIDTokenRequestTests testInstance]; @@ -89,6 +98,9 @@ - (void)testTwoArgumentPerformTokenRequestRoutesToThreeArgument { XCTAssertNil(service.lastOriginalAuthorizationResponse); } +/*! @brief An authorization request is recorded and returns a fake session, which counts a cancel + without calling back. + */ - (void)testPresentAuthorizationRequestReturnsFakeSession { OIDFakeAuthorizationService *service = [[OIDFakeAuthorizationService alloc] init]; OIDAuthorizationRequest *request = [OIDAuthorizationRequestTests testInstance]; @@ -113,23 +125,4 @@ - (void)testPresentAuthorizationRequestReturnsFakeSession { XCTAssertFalse(callbackInvoked); } -- (void)testFakeSessionResumeRecordsURLAndHonoursResumeResult { - OIDFakeExternalUserAgentSession *session = [[OIDFakeExternalUserAgentSession alloc] init]; - NSURL *firstURL = [NSURL URLWithString:@"https://example.com/callback?code=1"]; - NSURL *secondURL = [NSURL URLWithString:@"https://example.com/callback?code=2"]; - - session.resumeResult = NO; - NSError *error = nil; - XCTAssertFalse([session resumeExternalUserAgentFlowWithURL:firstURL error:&error]); - XCTAssertEqualObjects(session.lastResumedURL, firstURL); - XCTAssertEqualObjects(error.domain, OIDGeneralErrorDomain); - XCTAssertEqual(error.code, OIDErrorCodeURLMismatch); - - session.resumeResult = YES; - error = nil; - XCTAssertTrue([session resumeExternalUserAgentFlowWithURL:secondURL error:&error]); - XCTAssertEqualObjects(session.lastResumedURL, secondURL); - XCTAssertNil(error); -} - @end From 489643025ce87f36a4f56792db31b15ffc689b61 Mon Sep 17 00:00:00 2001 From: Worthing ~ <115107835+w-goog@users.noreply.github.com> Date: Wed, 11 Nov 2026 13:29:25 -0800 Subject: [PATCH 3/4] g-orchestrated: add the fake service and its tests to the Xcode test targets --- AppAuth.xcodeproj/project.pbxproj | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/AppAuth.xcodeproj/project.pbxproj b/AppAuth.xcodeproj/project.pbxproj index 0c1afb824..879b71081 100644 --- a/AppAuth.xcodeproj/project.pbxproj +++ b/AppAuth.xcodeproj/project.pbxproj @@ -551,6 +551,7 @@ 34D5EC451E6D1AD900814354 /* OIDSwiftTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34D5EC441E6D1AD900814354 /* OIDSwiftTests.swift */; }; 34FEA6AE1DB6E083005C9212 /* OIDLoopbackHTTPServer.h in Headers */ = {isa = PBXBuildFile; fileRef = 34FEA6AC1DB6E083005C9212 /* OIDLoopbackHTTPServer.h */; }; 34FEA6AF1DB6E083005C9212 /* OIDLoopbackHTTPServer.m in Sources */ = {isa = PBXBuildFile; fileRef = 34FEA6AD1DB6E083005C9212 /* OIDLoopbackHTTPServer.m */; }; + 3FCB7E7F03D56BFC508468CC /* OIDFakeAuthorizationService.m in Sources */ = {isa = PBXBuildFile; fileRef = 98F28C97D4A3EC0E6491BA78 /* OIDFakeAuthorizationService.m */; }; 4042060AFEB6D5DE8A86B483 /* OIDAuthorizationServiceInstanceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 459EB273F7CD3FCE6BEA1EA9 /* OIDAuthorizationServiceInstanceTests.m */; }; 55A094CF20DFBB10000045D1 /* OIDURLSessionProvider.h in Headers */ = {isa = PBXBuildFile; fileRef = 039697441FA8258D003D1FB2 /* OIDURLSessionProvider.h */; settings = {ATTRIBUTES = (Public, ); }; }; 55A094D020DFBB11000045D1 /* OIDURLSessionProvider.h in Headers */ = {isa = PBXBuildFile; fileRef = 039697441FA8258D003D1FB2 /* OIDURLSessionProvider.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -564,6 +565,7 @@ 60140F861DE43CC700DA0DC3 /* OIDRegistrationResponseTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 60140F851DE43CC700DA0DC3 /* OIDRegistrationResponseTests.m */; }; 6BAAE1371FA75D57506027DB /* OIDFakeExternalUserAgentSession.m in Sources */ = {isa = PBXBuildFile; fileRef = B5FB594568F5DAA341E261F5 /* OIDFakeExternalUserAgentSession.m */; }; 6C9154FEA8F907C759672BD2 /* OIDFakeExternalUserAgentSession.m in Sources */ = {isa = PBXBuildFile; fileRef = B5FB594568F5DAA341E261F5 /* OIDFakeExternalUserAgentSession.m */; }; + 6EC35A668CCAE107D5C4F983 /* OIDFakeAuthorizationService.m in Sources */ = {isa = PBXBuildFile; fileRef = 98F28C97D4A3EC0E6491BA78 /* OIDFakeAuthorizationService.m */; }; 73F574342B7C42690023FFF0 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 73F574332B7C42690023FFF0 /* PrivacyInfo.xcprivacy */; }; 73F574352B7C42690023FFF0 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 73F574332B7C42690023FFF0 /* PrivacyInfo.xcprivacy */; }; 73F574362B7C42690023FFF0 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 73F574332B7C42690023FFF0 /* PrivacyInfo.xcprivacy */; }; @@ -572,6 +574,9 @@ 73F574392B7C42690023FFF0 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 73F574332B7C42690023FFF0 /* PrivacyInfo.xcprivacy */; }; 7C8FC04BF7D3FA45950EEDC3 /* OIDFakeExternalUserAgentSessionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 006AFE509B9AA12E7203154D /* OIDFakeExternalUserAgentSessionTests.m */; }; 7F1EA0AC51A93928019E09BF /* OIDFakeExternalUserAgentSession.m in Sources */ = {isa = PBXBuildFile; fileRef = B5FB594568F5DAA341E261F5 /* OIDFakeExternalUserAgentSession.m */; }; + 7F9FD7EBFBC828904F80547A /* OIDFakeAuthorizationService.m in Sources */ = {isa = PBXBuildFile; fileRef = 98F28C97D4A3EC0E6491BA78 /* OIDFakeAuthorizationService.m */; }; + 869B3B05A87137E1D0085826 /* OIDFakeAuthorizationServiceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 606F68661B550F6F822BE4C8 /* OIDFakeAuthorizationServiceTests.m */; }; + 987F98D95DC498D36CEBA89B /* OIDFakeAuthorizationService.m in Sources */ = {isa = PBXBuildFile; fileRef = 98F28C97D4A3EC0E6491BA78 /* OIDFakeAuthorizationService.m */; }; 9BE107A4CCBF0579E2354D3B /* OIDAuthorizationServiceInstanceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 459EB273F7CD3FCE6BEA1EA9 /* OIDAuthorizationServiceInstanceTests.m */; }; A592292659F68909AE332653 /* OIDFakeExternalUserAgentSessionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 006AFE509B9AA12E7203154D /* OIDFakeExternalUserAgentSessionTests.m */; }; A5EEF29720D821120044F470 /* OIDTokenUtilitiesTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A5EEF1FD20CF07760044F470 /* OIDTokenUtilitiesTests.m */; }; @@ -620,12 +625,17 @@ A6DEABB52018ECF30022AC32 /* OIDEndSessionResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = CF6431F21F228A980075B6B5 /* OIDEndSessionResponse.h */; settings = {ATTRIBUTES = (Public, ); }; }; A6DEABB62018ECF30022AC32 /* OIDEndSessionResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = CF6431F21F228A980075B6B5 /* OIDEndSessionResponse.h */; settings = {ATTRIBUTES = (Public, ); }; }; A6DEABB72018ECF40022AC32 /* OIDEndSessionResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = CF6431F21F228A980075B6B5 /* OIDEndSessionResponse.h */; settings = {ATTRIBUTES = (Public, ); }; }; + AA05B11331E4CEE5BFCC6046 /* OIDFakeAuthorizationServiceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 606F68661B550F6F822BE4C8 /* OIDFakeAuthorizationServiceTests.m */; }; ABC445C387A0FBA5E0F2ED72 /* OIDFakeExternalUserAgentSession.m in Sources */ = {isa = PBXBuildFile; fileRef = B5FB594568F5DAA341E261F5 /* OIDFakeExternalUserAgentSession.m */; }; B794C1C6F75082B099F84438 /* OIDFakeExternalUserAgentSessionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 006AFE509B9AA12E7203154D /* OIDFakeExternalUserAgentSessionTests.m */; }; + B7DAD3185B79D5D8FE503603 /* OIDFakeAuthorizationServiceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 606F68661B550F6F822BE4C8 /* OIDFakeAuthorizationServiceTests.m */; }; + B99F22B7DECC1FEDBD38FFBC /* OIDFakeAuthorizationService.m in Sources */ = {isa = PBXBuildFile; fileRef = 98F28C97D4A3EC0E6491BA78 /* OIDFakeAuthorizationService.m */; }; + C0CC1373332B72C28D75D56E /* OIDFakeAuthorizationServiceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 606F68661B550F6F822BE4C8 /* OIDFakeAuthorizationServiceTests.m */; }; C14E3B6827E3BEFB00CF05A9 /* OIDExternalUserAgentIOSCustomBrowser.h in Headers */ = {isa = PBXBuildFile; fileRef = C14E3B6627E3BEFB00CF05A9 /* OIDExternalUserAgentIOSCustomBrowser.h */; settings = {ATTRIBUTES = (Public, ); }; }; C14E3B6927E3BEFB00CF05A9 /* OIDExternalUserAgentIOSCustomBrowser.m in Sources */ = {isa = PBXBuildFile; fileRef = C14E3B6727E3BEFB00CF05A9 /* OIDExternalUserAgentIOSCustomBrowser.m */; }; C14E3B6A27E3BFB800CF05A9 /* OIDExternalUserAgentIOSCustomBrowser.m in Sources */ = {isa = PBXBuildFile; fileRef = C14E3B6727E3BEFB00CF05A9 /* OIDExternalUserAgentIOSCustomBrowser.m */; }; C278E4687E3AD74270EF88DA /* OIDFakeExternalUserAgentSessionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 006AFE509B9AA12E7203154D /* OIDFakeExternalUserAgentSessionTests.m */; }; + CF1C5DEAA01896E9EBBEC074 /* OIDFakeAuthorizationServiceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 606F68661B550F6F822BE4C8 /* OIDFakeAuthorizationServiceTests.m */; }; CF37C06E1F1FC21A00662E41 /* OIDEndSessionRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = CF37C06C1F1FC21A00662E41 /* OIDEndSessionRequest.m */; }; CF37C06F1F1FC21A00662E41 /* OIDEndSessionRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = CF37C06C1F1FC21A00662E41 /* OIDEndSessionRequest.m */; }; CF37C0701F1FC21A00662E41 /* OIDEndSessionRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = CF37C06C1F1FC21A00662E41 /* OIDEndSessionRequest.m */; }; @@ -633,8 +643,10 @@ CF6431F41F228A980075B6B5 /* OIDEndSessionResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = CF6431F31F228A980075B6B5 /* OIDEndSessionResponse.m */; }; D07958F60AC2A47A79500914 /* OIDFakeExternalUserAgentSession.m in Sources */ = {isa = PBXBuildFile; fileRef = B5FB594568F5DAA341E261F5 /* OIDFakeExternalUserAgentSession.m */; }; DD05E93BBBA3AB6BF88CAB8D /* OIDAuthorizationServiceInstanceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 459EB273F7CD3FCE6BEA1EA9 /* OIDAuthorizationServiceInstanceTests.m */; }; + DD8D4B26F435BEF787C63052 /* OIDFakeAuthorizationService.m in Sources */ = {isa = PBXBuildFile; fileRef = 98F28C97D4A3EC0E6491BA78 /* OIDFakeAuthorizationService.m */; }; E56963F03064815800AC4DFE /* OIDExternalUserAgentIOSTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E56963EF3064815800AC4DFE /* OIDExternalUserAgentIOSTests.m */; }; E56963F13064815800AC4DFE /* OIDExternalUserAgentIOSTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E56963EF3064815800AC4DFE /* OIDExternalUserAgentIOSTests.m */; }; + EB66273FF8DFEFBB0871BDE6 /* OIDFakeAuthorizationServiceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 606F68661B550F6F822BE4C8 /* OIDFakeAuthorizationServiceTests.m */; }; F1A717E418CCA5400B300540 /* OIDAuthorizationServiceInstanceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 459EB273F7CD3FCE6BEA1EA9 /* OIDAuthorizationServiceInstanceTests.m */; }; F7DA76425730ACDA2D3107BF /* OIDFakeExternalUserAgentSession.m in Sources */ = {isa = PBXBuildFile; fileRef = B5FB594568F5DAA341E261F5 /* OIDFakeExternalUserAgentSession.m */; }; F9A7082E2355ED74004B3E6D /* OIDExternalUserAgentCatalyst.h in Headers */ = {isa = PBXBuildFile; fileRef = F9A7082C2355ED74004B3E6D /* OIDExternalUserAgentCatalyst.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -869,8 +881,11 @@ 60140F821DE43BAF00DA0DC3 /* OIDRegistrationRequestTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OIDRegistrationRequestTests.m; sourceTree = ""; }; 60140F841DE43C8C00DA0DC3 /* OIDRegistrationResponseTests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OIDRegistrationResponseTests.h; sourceTree = ""; }; 60140F851DE43CC700DA0DC3 /* OIDRegistrationResponseTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OIDRegistrationResponseTests.m; sourceTree = ""; }; + 606F68661B550F6F822BE4C8 /* OIDFakeAuthorizationServiceTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OIDFakeAuthorizationServiceTests.m; sourceTree = ""; }; + 6314127B4C217751880C08C6 /* OIDFakeAuthorizationService.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OIDFakeAuthorizationService.h; sourceTree = ""; }; 73F574332B7C42690023FFF0 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = ""; }; 988AF8BA66200AFC0A60FB14 /* AppAuthTestHelpers.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppAuthTestHelpers.h; sourceTree = ""; }; + 98F28C97D4A3EC0E6491BA78 /* OIDFakeAuthorizationService.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OIDFakeAuthorizationService.m; sourceTree = ""; }; A5EEF1FD20CF07760044F470 /* OIDTokenUtilitiesTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OIDTokenUtilitiesTests.m; sourceTree = ""; }; A6CEB1172007E384009D492A /* OIDEndSessionRequestTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OIDEndSessionRequestTests.h; sourceTree = ""; }; A6CEB1182007E384009D492A /* OIDEndSessionRequestTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OIDEndSessionRequestTests.m; sourceTree = ""; }; @@ -1172,6 +1187,7 @@ 3417420C1C5D82D3000EF209 /* OIDServiceDiscoveryTests.m */, 0A987C0100000001000A0987 /* OIDAuthorizationServiceTests.m */, 459EB273F7CD3FCE6BEA1EA9 /* OIDAuthorizationServiceInstanceTests.m */, + 606F68661B550F6F822BE4C8 /* OIDFakeAuthorizationServiceTests.m */, 006AFE509B9AA12E7203154D /* OIDFakeExternalUserAgentSessionTests.m */, 3417420D1C5D82D3000EF209 /* OIDTokenRequestTests.h */, 3417420E1C5D82D3000EF209 /* OIDTokenRequestTests.m */, @@ -1329,6 +1345,8 @@ isa = PBXGroup; children = ( 988AF8BA66200AFC0A60FB14 /* AppAuthTestHelpers.h */, + 6314127B4C217751880C08C6 /* OIDFakeAuthorizationService.h */, + 98F28C97D4A3EC0E6491BA78 /* OIDFakeAuthorizationService.m */, EAB1B3CDF2B7C7405D39FD8F /* OIDFakeExternalUserAgentSession.h */, B5FB594568F5DAA341E261F5 /* OIDFakeExternalUserAgentSession.m */, ); @@ -2272,6 +2290,8 @@ F1A717E418CCA5400B300540 /* OIDAuthorizationServiceInstanceTests.m in Sources */, 7F1EA0AC51A93928019E09BF /* OIDFakeExternalUserAgentSession.m in Sources */, B794C1C6F75082B099F84438 /* OIDFakeExternalUserAgentSessionTests.m in Sources */, + 987F98D95DC498D36CEBA89B /* OIDFakeAuthorizationService.m in Sources */, + B7DAD3185B79D5D8FE503603 /* OIDFakeAuthorizationServiceTests.m in Sources */, 3417421F1C5D82D3000EF209 /* OIDTokenRequestTests.m in Sources */, 341742181C5D82D3000EF209 /* OIDAuthorizationResponseTests.m in Sources */, 34A6638B1E8865090060B664 /* OIDRPProfileCode.m in Sources */, @@ -2303,6 +2323,8 @@ DD05E93BBBA3AB6BF88CAB8D /* OIDAuthorizationServiceInstanceTests.m in Sources */, F7DA76425730ACDA2D3107BF /* OIDFakeExternalUserAgentSession.m in Sources */, C278E4687E3AD74270EF88DA /* OIDFakeExternalUserAgentSessionTests.m in Sources */, + 7F9FD7EBFBC828904F80547A /* OIDFakeAuthorizationService.m in Sources */, + C0CC1373332B72C28D75D56E /* OIDFakeAuthorizationServiceTests.m in Sources */, 341AA5071E7F3A9B00FCA5C6 /* OIDAuthStateTests.m in Sources */, 341AA50E1E7F3A9B00FCA5C6 /* OIDTokenResponseTests.m in Sources */, 341AA50D1E7F3A9B00FCA5C6 /* OIDTokenRequestTests.m in Sources */, @@ -2330,6 +2352,8 @@ 5DEEE663EDB0D4A1C1118284 /* OIDAuthorizationServiceInstanceTests.m in Sources */, D07958F60AC2A47A79500914 /* OIDFakeExternalUserAgentSession.m in Sources */, 7C8FC04BF7D3FA45950EEDC3 /* OIDFakeExternalUserAgentSessionTests.m in Sources */, + B99F22B7DECC1FEDBD38FFBC /* OIDFakeAuthorizationService.m in Sources */, + AA05B11331E4CEE5BFCC6046 /* OIDFakeAuthorizationServiceTests.m in Sources */, 341AA4FA1E7F3A9400FCA5C6 /* OIDAuthStateTests.m in Sources */, 341AA5011E7F3A9400FCA5C6 /* OIDTokenResponseTests.m in Sources */, 341AA5001E7F3A9400FCA5C6 /* OIDTokenRequestTests.m in Sources */, @@ -2460,6 +2484,8 @@ 4042060AFEB6D5DE8A86B483 /* OIDAuthorizationServiceInstanceTests.m in Sources */, ABC445C387A0FBA5E0F2ED72 /* OIDFakeExternalUserAgentSession.m in Sources */, 1F3A45649A64B5C1937E5920 /* OIDFakeExternalUserAgentSessionTests.m in Sources */, + 6EC35A668CCAE107D5C4F983 /* OIDFakeAuthorizationService.m in Sources */, + 869B3B05A87137E1D0085826 /* OIDFakeAuthorizationServiceTests.m in Sources */, 343AAA751E8346B400F9D36E /* OIDAuthStateTests.m in Sources */, 343AAA7C1E8346B400F9D36E /* OIDTokenResponseTests.m in Sources */, 343AAA7B1E8346B400F9D36E /* OIDTokenRequestTests.m in Sources */, @@ -2551,6 +2577,8 @@ 1AB85393A09567E7CBB687AE /* OIDAuthorizationServiceInstanceTests.m in Sources */, 6BAAE1371FA75D57506027DB /* OIDFakeExternalUserAgentSession.m in Sources */, 0C2B4674EA8DD9B5FFCD7FD0 /* OIDFakeExternalUserAgentSessionTests.m in Sources */, + DD8D4B26F435BEF787C63052 /* OIDFakeAuthorizationService.m in Sources */, + CF1C5DEAA01896E9EBBEC074 /* OIDFakeAuthorizationServiceTests.m in Sources */, 343AAB811E8349CE00F9D36E /* OIDAuthStateTests.m in Sources */, 343AAB881E8349CE00F9D36E /* OIDTokenResponseTests.m in Sources */, 343AAB871E8349CE00F9D36E /* OIDTokenRequestTests.m in Sources */, @@ -2615,6 +2643,8 @@ 9BE107A4CCBF0579E2354D3B /* OIDAuthorizationServiceInstanceTests.m in Sources */, 6C9154FEA8F907C759672BD2 /* OIDFakeExternalUserAgentSession.m in Sources */, A592292659F68909AE332653 /* OIDFakeExternalUserAgentSessionTests.m in Sources */, + 3FCB7E7F03D56BFC508468CC /* OIDFakeAuthorizationService.m in Sources */, + EB66273FF8DFEFBB0871BDE6 /* OIDFakeAuthorizationServiceTests.m in Sources */, 343AAB8F1E8349CF00F9D36E /* OIDAuthStateTests.m in Sources */, 343AAB961E8349CF00F9D36E /* OIDTokenResponseTests.m in Sources */, 343AAB951E8349CF00F9D36E /* OIDTokenRequestTests.m in Sources */, From c0bef02d48dd3437f5d35b6959f888afc5834b27 Mon Sep 17 00:00:00 2001 From: Worthing ~ <115107835+w-goog@users.noreply.github.com> Date: Wed, 11 Nov 2026 13:59:57 -0800 Subject: [PATCH 4/4] g-orchestrated: clear the fake's kept callbacks once invoked --- .../OIDFakeAuthorizationService.h | 8 ++- .../OIDFakeAuthorizationService.m | 62 +++++++++++++++++-- UnitTests/OIDFakeAuthorizationServiceTests.m | 61 ++++++++++++++++++ 3 files changed, 123 insertions(+), 8 deletions(-) diff --git a/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.h b/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.h index 504146819..bdb9497b5 100644 --- a/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.h +++ b/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.h @@ -28,6 +28,8 @@ NS_ASSUME_NONNULL_BEGIN @discussion Inject an instance where production code would otherwise call the class methods of @c OIDAuthorizationService. The class methods of this fake are inherited unchanged and still perform real work; only the instance methods are faked. + A kept callback clears itself once invoked. Callbacks usually capture the caller, and the + caller often holds this fake. Keeping the callback would leak both. */ @interface OIDFakeAuthorizationService : OIDAuthorizationService @@ -43,6 +45,7 @@ NS_ASSUME_NONNULL_BEGIN @property(nonatomic, readonly, nullable) id lastExternalUserAgent; /*! @brief The callback of the most recent authorization request. Invoke it to complete the flow. + Becomes nil once invoked. */ @property(nonatomic, readonly, copy, nullable) OIDAuthorizationCallback lastAuthorizationCallback; @@ -55,7 +58,7 @@ NS_ASSUME_NONNULL_BEGIN */ @property(nonatomic, readonly, nullable) OIDEndSessionRequest *lastEndSessionRequest; -/*! @brief The callback of the most recent end-session request. +/*! @brief The callback of the most recent end-session request. Becomes nil once invoked. */ @property(nonatomic, readonly, copy, nullable) OIDEndSessionCallback lastEndSessionCallback; @@ -74,6 +77,7 @@ NS_ASSUME_NONNULL_BEGIN OIDAuthorizationResponse *lastOriginalAuthorizationResponse; /*! @brief The callback of the most recent token request. Invoke it to complete the request. + Becomes nil once invoked. */ @property(nonatomic, readonly, copy, nullable) OIDTokenCallback lastTokenCallback; @@ -81,7 +85,7 @@ NS_ASSUME_NONNULL_BEGIN */ @property(nonatomic, readonly, nullable) OIDRegistrationRequest *lastRegistrationRequest; -/*! @brief The completion of the most recent registration request. +/*! @brief The completion of the most recent registration request. Becomes nil once invoked. */ @property(nonatomic, readonly, copy, nullable) OIDRegistrationCompletion lastRegistrationCompletion; diff --git a/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.m b/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.m index 68a443282..258390edf 100644 --- a/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.m +++ b/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.m @@ -21,21 +21,46 @@ #import "OIDAuthorizationRequest.h" #import "OIDAuthorizationResponse.h" #import "OIDEndSessionRequest.h" +#import "OIDEndSessionResponse.h" #import "OIDExternalUserAgent.h" #import "OIDRegistrationRequest.h" +#import "OIDRegistrationResponse.h" #import "OIDTokenRequest.h" +#import "OIDTokenResponse.h" NS_ASSUME_NONNULL_BEGIN -@implementation OIDFakeAuthorizationService +@implementation OIDFakeAuthorizationService { + /*! @brief How many authorization requests have been made. A kept callback checks it, so an + older callback doesn't clear a newer one. + */ + NSUInteger _authorizationRequestCount; + + /*! @brief How many end-session requests have been made. Used the same way. + */ + NSUInteger _endSessionRequestCount; + + /*! @brief How many registration requests have been made. Used the same way. + */ + NSUInteger _registrationRequestCount; +} - (id) presentAuthorizationRequest:(OIDAuthorizationRequest *)request externalUserAgent:(id)externalUserAgent callback:(OIDAuthorizationCallback)callback { + NSUInteger requestNumber = ++_authorizationRequestCount; + __weak OIDFakeAuthorizationService *weakSelf = self; _lastAuthorizationRequest = request; _lastExternalUserAgent = externalUserAgent; - _lastAuthorizationCallback = [callback copy]; + _lastAuthorizationCallback = ^(OIDAuthorizationResponse *_Nullable authorizationResponse, + NSError *_Nullable error) { + callback(authorizationResponse, error); + OIDFakeAuthorizationService *strongSelf = weakSelf; + if (strongSelf && strongSelf->_authorizationRequestCount == requestNumber) { + strongSelf->_lastAuthorizationCallback = nil; + } + }; _lastAuthorizationSession = [[OIDFakeExternalUserAgentSession alloc] init]; return _lastAuthorizationSession; } @@ -44,24 +69,49 @@ @implementation OIDFakeAuthorizationService presentEndSessionRequest:(OIDEndSessionRequest *)request externalUserAgent:(id)externalUserAgent callback:(OIDEndSessionCallback)callback { + NSUInteger requestNumber = ++_endSessionRequestCount; + __weak OIDFakeAuthorizationService *weakSelf = self; _lastEndSessionRequest = request; - _lastEndSessionCallback = [callback copy]; + _lastEndSessionCallback = ^(OIDEndSessionResponse *_Nullable endSessionResponse, + NSError *_Nullable error) { + callback(endSessionResponse, error); + OIDFakeAuthorizationService *strongSelf = weakSelf; + if (strongSelf && strongSelf->_endSessionRequestCount == requestNumber) { + strongSelf->_lastEndSessionCallback = nil; + } + }; return [[OIDFakeExternalUserAgentSession alloc] init]; } - (void)performTokenRequest:(OIDTokenRequest *)request originalAuthorizationResponse:(nullable OIDAuthorizationResponse *)authorizationResponse callback:(OIDTokenCallback)callback { - _tokenRequestCount++; + NSUInteger requestNumber = ++_tokenRequestCount; + __weak OIDFakeAuthorizationService *weakSelf = self; _lastTokenRequest = request; _lastOriginalAuthorizationResponse = authorizationResponse; - _lastTokenCallback = [callback copy]; + _lastTokenCallback = ^(OIDTokenResponse *_Nullable tokenResponse, NSError *_Nullable error) { + callback(tokenResponse, error); + OIDFakeAuthorizationService *strongSelf = weakSelf; + if (strongSelf && strongSelf->_tokenRequestCount == requestNumber) { + strongSelf->_lastTokenCallback = nil; + } + }; } - (void)performRegistrationRequest:(OIDRegistrationRequest *)request completion:(OIDRegistrationCompletion)completion { + NSUInteger requestNumber = ++_registrationRequestCount; + __weak OIDFakeAuthorizationService *weakSelf = self; _lastRegistrationRequest = request; - _lastRegistrationCompletion = [completion copy]; + _lastRegistrationCompletion = ^(OIDRegistrationResponse *_Nullable registrationResponse, + NSError *_Nullable error) { + completion(registrationResponse, error); + OIDFakeAuthorizationService *strongSelf = weakSelf; + if (strongSelf && strongSelf->_registrationRequestCount == requestNumber) { + strongSelf->_lastRegistrationCompletion = nil; + } + }; } @end diff --git a/UnitTests/OIDFakeAuthorizationServiceTests.m b/UnitTests/OIDFakeAuthorizationServiceTests.m index 905a5b3e9..d69196435 100644 --- a/UnitTests/OIDFakeAuthorizationServiceTests.m +++ b/UnitTests/OIDFakeAuthorizationServiceTests.m @@ -125,4 +125,65 @@ - (void)testPresentAuthorizationRequestReturnsFakeSession { XCTAssertFalse(callbackInvoked); } +/*! @brief Invoking the kept token callback clears it, and the fake stops holding what it captured. + */ +- (void)testInvokingTokenCallbackClearsIt { + OIDFakeAuthorizationService *service = [[OIDFakeAuthorizationService alloc] init]; + __weak NSObject *weakCaptured = nil; + @autoreleasepool { + NSObject *captured = [[NSObject alloc] init]; + weakCaptured = captured; + [service performTokenRequest:[OIDTokenRequestTests testInstance] + callback:^(OIDTokenResponse *_Nullable tokenResponse, + NSError *_Nullable error) { + (void)captured; + }]; + service.lastTokenCallback(nil, nil); + } + XCTAssertNil(service.lastTokenCallback); + XCTAssertNil(weakCaptured); +} + +/*! @brief Invoking the kept authorization callback clears it, and the fake stops holding what it + captured. + */ +- (void)testInvokingAuthorizationCallbackClearsIt { + OIDFakeAuthorizationService *service = [[OIDFakeAuthorizationService alloc] init]; + OIDNoOpExternalUserAgent *userAgent = [[OIDNoOpExternalUserAgent alloc] init]; + __weak NSObject *weakCaptured = nil; + @autoreleasepool { + NSObject *captured = [[NSObject alloc] init]; + weakCaptured = captured; + [service presentAuthorizationRequest:[OIDAuthorizationRequestTests testInstance] + externalUserAgent:userAgent + callback:^(OIDAuthorizationResponse *_Nullable response, + NSError *_Nullable error) { + (void)captured; + }]; + service.lastAuthorizationCallback(nil, nil); + } + XCTAssertNil(service.lastAuthorizationCallback); + XCTAssertNil(weakCaptured); +} + +/*! @brief Invoking an older kept callback leaves the newer one in place. + */ +- (void)testInvokingOlderTokenCallbackKeepsNewerOne { + OIDFakeAuthorizationService *service = [[OIDFakeAuthorizationService alloc] init]; + OIDTokenRequest *request = [OIDTokenRequestTests testInstance]; + [service performTokenRequest:request + callback:^(OIDTokenResponse *_Nullable tokenResponse, + NSError *_Nullable error) { + }]; + OIDTokenCallback olderCallback = service.lastTokenCallback; + [service performTokenRequest:request + callback:^(OIDTokenResponse *_Nullable tokenResponse, + NSError *_Nullable error) { + }]; + OIDTokenCallback newerCallback = service.lastTokenCallback; + + olderCallback(nil, nil); + XCTAssertEqual(service.lastTokenCallback, newerCallback); +} + @end