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 */, diff --git a/CHANGELOG.md b/CHANGELOG.md index b250ebbc5..fec6a202c 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. `OIDAuthorizationServiceProtocol` declares them, and `OIDAuthorizationServiceIOSProtocol` / `OIDAuthorizationServiceMacProtocol` declare the platform convenience methods. - 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/Package.swift b/Package.swift index a04ffee9e..bd1a6765b 100644 --- a/Package.swift +++ b/Package.swift @@ -72,7 +72,7 @@ let package = Package( ), .target( name: "AppAuthTestHelpers", - dependencies: ["AppAuthCore"], + dependencies: ["AppAuthCore", "AppAuth"], path: "Sources/AppAuthTestHelpers", publicHeadersPath: "" ), 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 new file mode 100644 index 000000000..e8452c973 --- /dev/null +++ b/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.h @@ -0,0 +1,150 @@ +/*! @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 + +#import "OIDAuthorizationService.h" +#import "OIDFakeExternalUserAgentSession.h" + +// Apps that use the Swift package don't define SWIFT_PACKAGE. Check for the header instead. +// AppAuth.xcodeproj finds these headers by name, and the package finds them by directory. +#if __has_include("OIDAuthorizationService+IOS.h") +#import "OIDAuthorizationService+IOS.h" +#import "OIDAuthorizationService+Mac.h" +#else +#import "iOS/OIDAuthorizationService+IOS.h" +#import "macOS/OIDAuthorizationService+Mac.h" +#endif + +NS_ASSUME_NONNULL_BEGIN + +/*! @brief An @c OIDAuthorizationServiceProtocol whose 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 takes an + @c id. + 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. + The requests, external user agents and sessions are kept until the next request of that + kind. An agent built by a platform convenience method holds its view controller or window, + so the fake holds it too. Use a new fake for each test. + */ +@interface OIDFakeAuthorizationService : NSObject + +/*! @brief How many authorization requests have been presented. + */ +@property(nonatomic, readonly) NSUInteger authorizationRequestCount; + +/*! @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 would build for the caller's view + controller or window. + */ +@property(nonatomic, readonly, nullable) + id lastAuthorizationExternalUserAgent; + +/*! @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; + +/*! @brief The session returned from the most recent authorization request. + */ +@property(nonatomic, readonly, nullable) OIDFakeExternalUserAgentSession *lastAuthorizationSession; + +/*! @brief How many end-session requests have been presented. + */ +@property(nonatomic, readonly) NSUInteger endSessionRequestCount; + +/*! @brief The request passed to the most recent + @c -presentEndSessionRequest:externalUserAgent:callback:. + */ +@property(nonatomic, readonly, nullable) OIDEndSessionRequest *lastEndSessionRequest; + +/*! @brief The external user agent passed to the most recent end-session request. + */ +@property(nonatomic, readonly, nullable) id lastEndSessionExternalUserAgent; + +/*! @brief The callback of the most recent end-session request. Becomes nil once invoked. + */ +@property(nonatomic, readonly, copy, nullable) OIDEndSessionCallback lastEndSessionCallback; + +/*! @brief The session returned from the most recent end-session request. + */ +@property(nonatomic, readonly, nullable) OIDFakeExternalUserAgentSession *lastEndSessionSession; + +/*! @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. + Becomes nil once invoked. + */ +@property(nonatomic, readonly, copy, nullable) OIDTokenCallback lastTokenCallback; + +/*! @brief How many registration requests have been performed. + */ +@property(nonatomic, readonly) NSUInteger registrationRequestCount; + +/*! @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. Becomes nil once invoked. + */ +@property(nonatomic, readonly, copy, nullable) OIDRegistrationCompletion lastRegistrationCompletion; + +@end + +#if TARGET_OS_IOS || TARGET_OS_MACCATALYST + +/*! @brief The iOS convenience methods. Each builds the external user agent AppAuth would build + for the view controller, then records the request like + @c -presentAuthorizationRequest:externalUserAgent:callback:. + */ +@interface OIDFakeAuthorizationService (IOS) +@end + +#endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST + +#if TARGET_OS_OSX + +/*! @brief The macOS convenience methods. Each builds the external user agent AppAuth would build + for the window, then records the request like + @c -presentAuthorizationRequest:externalUserAgent:callback:. + */ +@interface OIDFakeAuthorizationService (Mac) +@end + +#endif // TARGET_OS_OSX + +NS_ASSUME_NONNULL_END diff --git a/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.m b/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.m new file mode 100644 index 000000000..b991f8cad --- /dev/null +++ b/Sources/AppAuthTestHelpers/OIDFakeAuthorizationService.m @@ -0,0 +1,195 @@ +/*! @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 "OIDEndSessionResponse.h" +#import "OIDExternalUserAgent.h" +#import "OIDRegistrationRequest.h" +#import "OIDRegistrationResponse.h" +#import "OIDTokenRequest.h" +#import "OIDTokenResponse.h" + +#if SWIFT_PACKAGE +@import AppAuth; +#else +#import "OIDExternalUserAgentCatalyst.h" +#import "OIDExternalUserAgentIOS.h" +#import "OIDExternalUserAgentMac.h" +#endif + +NS_ASSUME_NONNULL_BEGIN + +@implementation OIDFakeAuthorizationService + +- (id) + presentAuthorizationRequest:(OIDAuthorizationRequest *)request + externalUserAgent:(id)externalUserAgent + callback:(OIDAuthorizationCallback)callback { + NSUInteger requestNumber = ++_authorizationRequestCount; + __weak OIDFakeAuthorizationService *weakSelf = self; + _lastAuthorizationRequest = request; + _lastAuthorizationExternalUserAgent = externalUserAgent; + _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; +} + +- (id) + presentEndSessionRequest:(OIDEndSessionRequest *)request + externalUserAgent:(id)externalUserAgent + callback:(OIDEndSessionCallback)callback { + NSUInteger requestNumber = ++_endSessionRequestCount; + __weak OIDFakeAuthorizationService *weakSelf = self; + _lastEndSessionRequest = request; + _lastEndSessionExternalUserAgent = externalUserAgent; + _lastEndSessionCallback = ^(OIDEndSessionResponse *_Nullable endSessionResponse, + NSError *_Nullable error) { + callback(endSessionResponse, error); + OIDFakeAuthorizationService *strongSelf = weakSelf; + if (strongSelf && strongSelf->_endSessionRequestCount == requestNumber) { + strongSelf->_lastEndSessionCallback = nil; + } + }; + _lastEndSessionSession = [[OIDFakeExternalUserAgentSession alloc] init]; + return _lastEndSessionSession; +} + +- (void)performTokenRequest:(OIDTokenRequest *)request callback:(OIDTokenCallback)callback { + [self performTokenRequest:request originalAuthorizationResponse:nil callback:callback]; +} + +- (void)performTokenRequest:(OIDTokenRequest *)request + originalAuthorizationResponse:(nullable OIDAuthorizationResponse *)authorizationResponse + callback:(OIDTokenCallback)callback { + NSUInteger requestNumber = ++_tokenRequestCount; + __weak OIDFakeAuthorizationService *weakSelf = self; + _lastTokenRequest = request; + _lastOriginalAuthorizationResponse = authorizationResponse; + _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 = ^(OIDRegistrationResponse *_Nullable registrationResponse, + NSError *_Nullable error) { + completion(registrationResponse, error); + OIDFakeAuthorizationService *strongSelf = weakSelf; + if (strongSelf && strongSelf->_registrationRequestCount == requestNumber) { + strongSelf->_lastRegistrationCompletion = nil; + } + }; +} + +@end + +#if TARGET_OS_IOS || TARGET_OS_MACCATALYST + +@implementation OIDFakeAuthorizationService (IOS) + +- (id) + presentAuthorizationRequest:(OIDAuthorizationRequest *)request + presentingViewController:(UIViewController *)presentingViewController + callback:(OIDAuthorizationCallback)callback { +#if TARGET_OS_MACCATALYST + id externalUserAgent = [[OIDExternalUserAgentCatalyst alloc] + initWithPresentingViewController:presentingViewController]; +#else // TARGET_OS_MACCATALYST + id externalUserAgent = [[OIDExternalUserAgentIOS alloc] + initWithPresentingViewController:presentingViewController]; +#endif // TARGET_OS_MACCATALYST + return [self presentAuthorizationRequest:request + externalUserAgent:externalUserAgent + callback:callback]; +} + +- (id) + presentAuthorizationRequest:(OIDAuthorizationRequest *)request + presentingViewController:(UIViewController *)presentingViewController + prefersEphemeralSession:(BOOL)prefersEphemeralSession + callback:(OIDAuthorizationCallback)callback { +#if TARGET_OS_MACCATALYST + id externalUserAgent = [[OIDExternalUserAgentCatalyst alloc] + initWithPresentingViewController:presentingViewController + prefersEphemeralSession:prefersEphemeralSession]; +#else // TARGET_OS_MACCATALYST + id externalUserAgent = [[OIDExternalUserAgentIOS alloc] + initWithPresentingViewController:presentingViewController + prefersEphemeralSession:prefersEphemeralSession]; +#endif // TARGET_OS_MACCATALYST + return [self presentAuthorizationRequest:request + externalUserAgent:externalUserAgent + callback:callback]; +} + +@end + +#endif // TARGET_OS_IOS || TARGET_OS_MACCATALYST + +#if TARGET_OS_OSX + +@implementation OIDFakeAuthorizationService (Mac) + +- (id) + presentAuthorizationRequest:(OIDAuthorizationRequest *)request + presentingWindow:(NSWindow *)presentingWindow + callback:(OIDAuthorizationCallback)callback { + id externalUserAgent = + [[OIDExternalUserAgentMac alloc] initWithPresentingWindow:presentingWindow]; + return [self presentAuthorizationRequest:request + externalUserAgent:externalUserAgent + callback:callback]; +} + +- (id) + presentAuthorizationRequest:(OIDAuthorizationRequest *)request + presentingWindow:(NSWindow *)presentingWindow + prefersEphemeralSession:(BOOL)prefersEphemeralSession + callback:(OIDAuthorizationCallback)callback { + id externalUserAgent = + [[OIDExternalUserAgentMac alloc] initWithPresentingWindow:presentingWindow + prefersEphemeralSession:prefersEphemeralSession]; + return [self presentAuthorizationRequest:request + externalUserAgent:externalUserAgent + callback:callback]; +} + +@end + +#endif // TARGET_OS_OSX + +NS_ASSUME_NONNULL_END diff --git a/UnitTests/OIDFakeAuthorizationServiceTests.m b/UnitTests/OIDFakeAuthorizationServiceTests.m new file mode 100644 index 000000000..28fd23980 --- /dev/null +++ b/UnitTests/OIDFakeAuthorizationServiceTests.m @@ -0,0 +1,456 @@ +/*! @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/AppAuth/iOS/OIDExternalUserAgentIOS.h" +#import "Sources/AppAuth/macOS/OIDExternalUserAgentMac.h" +#import "Sources/AppAuthCore.h" +#import "Sources/AppAuthTestHelpers/AppAuthTestHelpers.h" +#endif +#import "OIDAuthorizationRequestTests.h" +#import "OIDAuthorizationResponseTests.h" +#import "OIDEndSessionRequestTests.h" +#import "OIDRegistrationRequestTests.h" +#import "OIDTokenRequestTests.h" + +/*! @brief An external user agent that never presents anything. + */ +@interface OIDNoOpExternalUserAgent : NSObject +@end + +@implementation OIDNoOpExternalUserAgent + +- (BOOL)presentExternalUserAgentRequest:(id)request + session:(id)session { + return NO; +} + +- (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]; + 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); +} + +/*! @brief The two-argument token request is recorded with no authorization response. */ +- (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); +} + +/*! @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]; + 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); + XCTAssertEqual(service.authorizationRequestCount, 1u); + XCTAssertTrue([session isKindOfClass:[OIDFakeExternalUserAgentSession class]]); + XCTAssertEqual(service.lastAuthorizationRequest, request); + XCTAssertEqual(service.lastAuthorizationExternalUserAgent, userAgent); + + [session cancel]; + XCTAssertEqual(service.lastAuthorizationSession.cancelCount, 1u); + 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 authorization callback leaves the newer one in place. + */ +- (void)testInvokingOlderAuthorizationCallbackKeepsNewerOne { + OIDFakeAuthorizationService *service = [[OIDFakeAuthorizationService alloc] init]; + OIDAuthorizationRequest *request = [OIDAuthorizationRequestTests testInstance]; + OIDNoOpExternalUserAgent *userAgent = [[OIDNoOpExternalUserAgent alloc] init]; + [service presentAuthorizationRequest:request + externalUserAgent:userAgent + callback:^(OIDAuthorizationResponse *_Nullable response, + NSError *_Nullable error) { + }]; + OIDAuthorizationCallback olderCallback = service.lastAuthorizationCallback; + [service presentAuthorizationRequest:request + externalUserAgent:userAgent + callback:^(OIDAuthorizationResponse *_Nullable response, + NSError *_Nullable error) { + }]; + OIDAuthorizationCallback newerCallback = service.lastAuthorizationCallback; + + olderCallback(nil, nil); + XCTAssertEqual(service.lastAuthorizationCallback, newerCallback); +} + +/*! @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); +} + +/*! @brief An end-session request is recorded and returns a fake session, and invoking the kept + callback reaches the caller. + */ +- (void)testPresentEndSessionRequestIsRecordedAndCallbackIsReturned { + OIDFakeAuthorizationService *service = [[OIDFakeAuthorizationService alloc] init]; + OIDEndSessionRequest *request = [OIDEndSessionRequestTests testInstance]; + OIDNoOpExternalUserAgent *userAgent = [[OIDNoOpExternalUserAgent alloc] init]; + __block OIDEndSessionResponse *receivedResponse = nil; + __block NSError *receivedError = nil; + + id session = + [service presentEndSessionRequest:request + externalUserAgent:userAgent + callback:^(OIDEndSessionResponse *_Nullable response, + NSError *_Nullable error) { + receivedResponse = response; + receivedError = error; + }]; + + XCTAssertEqual(session, service.lastEndSessionSession); + XCTAssertEqual(service.endSessionRequestCount, 1u); + XCTAssertTrue([session isKindOfClass:[OIDFakeExternalUserAgentSession class]]); + XCTAssertEqual(service.lastEndSessionRequest, request); + XCTAssertEqual(service.lastEndSessionExternalUserAgent, userAgent); + XCTAssertNil(service.lastAuthorizationSession); + XCTAssertNil(service.lastAuthorizationExternalUserAgent); + XCTAssertNotNil(service.lastEndSessionCallback); + + NSError *someError = [NSError errorWithDomain:@"com.example" code:42 userInfo:nil]; + service.lastEndSessionCallback(nil, someError); + XCTAssertNil(receivedResponse); + XCTAssertEqual(receivedError, someError); +} + +/*! @brief Invoking the kept end-session callback clears it, and the fake stops holding what it + captured. + */ +- (void)testInvokingEndSessionCallbackClearsIt { + OIDFakeAuthorizationService *service = [[OIDFakeAuthorizationService alloc] init]; + OIDNoOpExternalUserAgent *userAgent = [[OIDNoOpExternalUserAgent alloc] init]; + __weak NSObject *weakCaptured = nil; + @autoreleasepool { + NSObject *captured = [[NSObject alloc] init]; + weakCaptured = captured; + [service presentEndSessionRequest:[OIDEndSessionRequestTests testInstance] + externalUserAgent:userAgent + callback:^(OIDEndSessionResponse *_Nullable response, + NSError *_Nullable error) { + (void)captured; + }]; + service.lastEndSessionCallback(nil, nil); + } + XCTAssertNil(service.lastEndSessionCallback); + XCTAssertNil(weakCaptured); +} + +/*! @brief Invoking an older kept end-session callback leaves the newer one in place. + */ +- (void)testInvokingOlderEndSessionCallbackKeepsNewerOne { + OIDFakeAuthorizationService *service = [[OIDFakeAuthorizationService alloc] init]; + OIDEndSessionRequest *request = [OIDEndSessionRequestTests testInstance]; + OIDNoOpExternalUserAgent *userAgent = [[OIDNoOpExternalUserAgent alloc] init]; + [service presentEndSessionRequest:request + externalUserAgent:userAgent + callback:^(OIDEndSessionResponse *_Nullable response, + NSError *_Nullable error) { + }]; + OIDEndSessionCallback olderCallback = service.lastEndSessionCallback; + [service presentEndSessionRequest:request + externalUserAgent:userAgent + callback:^(OIDEndSessionResponse *_Nullable response, + NSError *_Nullable error) { + }]; + OIDEndSessionCallback newerCallback = service.lastEndSessionCallback; + + olderCallback(nil, nil); + XCTAssertEqual(service.lastEndSessionCallback, newerCallback); +} + +/*! @brief A registration request is recorded, and invoking the kept completion reaches the caller. + */ +- (void)testPerformRegistrationRequestIsRecordedAndCompletionIsReturned { + OIDFakeAuthorizationService *service = [[OIDFakeAuthorizationService alloc] init]; + OIDRegistrationRequest *request = [OIDRegistrationRequestTests testInstance]; + __block OIDRegistrationResponse *receivedResponse = nil; + __block NSError *receivedError = nil; + + [service performRegistrationRequest:request + completion:^(OIDRegistrationResponse *_Nullable response, + NSError *_Nullable error) { + receivedResponse = response; + receivedError = error; + }]; + + XCTAssertEqual(service.registrationRequestCount, 1u); + XCTAssertEqual(service.lastRegistrationRequest, request); + XCTAssertNotNil(service.lastRegistrationCompletion); + + NSError *someError = [NSError errorWithDomain:@"com.example" code:42 userInfo:nil]; + service.lastRegistrationCompletion(nil, someError); + XCTAssertNil(receivedResponse); + XCTAssertEqual(receivedError, someError); +} + +/*! @brief Invoking the kept registration completion clears it, and the fake stops holding what it + captured. + */ +- (void)testInvokingRegistrationCompletionClearsIt { + OIDFakeAuthorizationService *service = [[OIDFakeAuthorizationService alloc] init]; + __weak NSObject *weakCaptured = nil; + @autoreleasepool { + NSObject *captured = [[NSObject alloc] init]; + weakCaptured = captured; + [service performRegistrationRequest:[OIDRegistrationRequestTests testInstance] + completion:^(OIDRegistrationResponse *_Nullable response, + NSError *_Nullable error) { + (void)captured; + }]; + service.lastRegistrationCompletion(nil, nil); + } + XCTAssertNil(service.lastRegistrationCompletion); + XCTAssertNil(weakCaptured); +} + +/*! @brief Invoking an older kept registration completion leaves the newer one in place. + */ +- (void)testInvokingOlderRegistrationCompletionKeepsNewerOne { + OIDFakeAuthorizationService *service = [[OIDFakeAuthorizationService alloc] init]; + OIDRegistrationRequest *request = [OIDRegistrationRequestTests testInstance]; + [service performRegistrationRequest:request + completion:^(OIDRegistrationResponse *_Nullable response, + NSError *_Nullable error) { + }]; + OIDRegistrationCompletion olderCompletion = service.lastRegistrationCompletion; + [service performRegistrationRequest:request + completion:^(OIDRegistrationResponse *_Nullable response, + NSError *_Nullable error) { + }]; + OIDRegistrationCompletion newerCompletion = service.lastRegistrationCompletion; + + olderCompletion(nil, nil); + XCTAssertEqual(service.lastRegistrationCompletion, newerCompletion); +} + +/*! @brief The fake conforms to @c OIDAuthorizationServiceProtocol, and is not an + @c OIDAuthorizationService. + */ +- (void)testFakeConformsToServiceProtocol { + id service = [[OIDFakeAuthorizationService alloc] init]; + XCTAssertTrue([service conformsToProtocol:@protocol(OIDAuthorizationServiceProtocol)]); + XCTAssertFalse([service isKindOfClass:[OIDAuthorizationService class]]); +} + +#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST && !SWIFT_PACKAGE + +/*! @brief The view controller methods record the request with an @c OIDExternalUserAgentIOS for + that view controller. + */ +- (void)testPresentingViewControllerRecordsIOSAgent { + OIDFakeAuthorizationService *service = [[OIDFakeAuthorizationService alloc] init]; + OIDAuthorizationRequest *request = [OIDAuthorizationRequestTests testInstance]; + UIViewController *viewController = [[UIViewController alloc] init]; + + id session = + [service presentAuthorizationRequest:request + presentingViewController:viewController + callback:^(OIDAuthorizationResponse *_Nullable response, + NSError *_Nullable error) { + }]; + + XCTAssertEqual(service.lastAuthorizationRequest, request); + XCTAssertTrue( + [service.lastAuthorizationExternalUserAgent isKindOfClass:[OIDExternalUserAgentIOS class]]); + OIDExternalUserAgentIOS *agent = + (OIDExternalUserAgentIOS *)service.lastAuthorizationExternalUserAgent; + XCTAssertEqual(agent.presentingViewController, viewController); + XCTAssertNotNil(service.lastAuthorizationCallback); + XCTAssertEqual(session, service.lastAuthorizationSession); + + UIViewController *secondViewController = [[UIViewController alloc] init]; + [service presentAuthorizationRequest:request + presentingViewController:secondViewController + prefersEphemeralSession:YES + callback:^(OIDAuthorizationResponse *_Nullable response, + NSError *_Nullable error) { + }]; + + XCTAssertTrue( + [service.lastAuthorizationExternalUserAgent isKindOfClass:[OIDExternalUserAgentIOS class]]); + OIDExternalUserAgentIOS *secondAgent = + (OIDExternalUserAgentIOS *)service.lastAuthorizationExternalUserAgent; + XCTAssertEqual(secondAgent.presentingViewController, secondViewController); +} + +#endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST && !SWIFT_PACKAGE + +#if TARGET_OS_OSX && !SWIFT_PACKAGE + +/*! @brief The window methods record the request with an @c OIDExternalUserAgentMac for that + window. + */ +- (void)testPresentingWindowRecordsMacAgent { + OIDFakeAuthorizationService *service = [[OIDFakeAuthorizationService alloc] init]; + OIDAuthorizationRequest *request = [OIDAuthorizationRequestTests testInstance]; + NSWindow *window = [[NSWindow alloc] init]; + + id session = + [service presentAuthorizationRequest:request + presentingWindow:window + callback:^(OIDAuthorizationResponse *_Nullable response, + NSError *_Nullable error) { + }]; + + XCTAssertEqual(service.lastAuthorizationRequest, request); + XCTAssertTrue( + [service.lastAuthorizationExternalUserAgent isKindOfClass:[OIDExternalUserAgentMac class]]); + OIDExternalUserAgentMac *agent = + (OIDExternalUserAgentMac *)service.lastAuthorizationExternalUserAgent; + XCTAssertEqual(agent.presentingWindow, window); + XCTAssertNotNil(service.lastAuthorizationCallback); + XCTAssertEqual(session, service.lastAuthorizationSession); + + NSWindow *secondWindow = [[NSWindow alloc] init]; + [service presentAuthorizationRequest:request + presentingWindow:secondWindow + prefersEphemeralSession:YES + callback:^(OIDAuthorizationResponse *_Nullable response, + NSError *_Nullable error) { + }]; + + XCTAssertTrue( + [service.lastAuthorizationExternalUserAgent isKindOfClass:[OIDExternalUserAgentMac class]]); + OIDExternalUserAgentMac *secondAgent = + (OIDExternalUserAgentMac *)service.lastAuthorizationExternalUserAgent; + XCTAssertEqual(secondAgent.presentingWindow, secondWindow); +} + +#endif // TARGET_OS_OSX && !SWIFT_PACKAGE + +@end