Skip to content

Commit 6840555

Browse files
committed
Share test helper code
1 parent 889ad19 commit 6840555

13 files changed

+275
-274
lines changed

Package.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,21 @@ let package = Package(
6464
path: "Source/AppAuthTV",
6565
publicHeadersPath: ""
6666
),
67+
.target(
68+
name: "TestHelpers",
69+
dependencies: ["AppAuthCore"],
70+
path: "UnitTests/Helpers",
71+
publicHeadersPath: ""
72+
),
6773
.testTarget(
6874
name: "AppAuthCoreTests",
69-
dependencies: ["AppAuthCore"],
75+
dependencies: ["AppAuthCore", "TestHelpers"],
7076
path: "UnitTests/AppAuthCore",
71-
exclude: ["OIDSwiftTests.swift", "AppAuthTV"]
77+
exclude: ["OIDSwiftTests.swift"]
7278
),
7379
.testTarget(
7480
name: "AppAuthTests",
75-
dependencies: ["AppAuth", "AppAuthCore"],
81+
dependencies: ["AppAuth", "TestHelpers"],
7682
path: "UnitTests/AppAuth"
7783
),
7884
.testTarget(
@@ -85,6 +91,6 @@ let package = Package(
8591
name: "AppAuthTVTests",
8692
dependencies: ["AppAuthTV"],
8793
path: "UnitTests/AppAuthTV"
88-
),
94+
)
8995
]
9096
)
Lines changed: 25 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,31 @@
1-
//
2-
// OIDExternalUserAgentIOSTests.m
3-
//
4-
//
5-
// Created by Matt Mathias on 1/11/23.
6-
//
1+
/*! @file OIDExternalUserAgentIOSTests.m
2+
@brief AppAuth iOS SDK
3+
@copyright
4+
Copyright 2023 The AppAuth Authors. All Rights Reserved.
5+
@copydetails
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
718

819
#import <XCTest/XCTest.h>
920

1021
#if SWIFT_PACKAGE
1122
@import AppAuth;
12-
@import AppAuthCore;
23+
@import TestHelpers;
1324
#else
1425
#import "Source/AppAuth/iOS/OIDExternalUserAgentIOS.h"
15-
#import "Source/AppAuthCore/OIDAuthorizationRequest.h"
16-
#import "Source/AppAuthCore/OIDAuthorizationService.h"
26+
#import "UnitTests/TestHelpers/OIDAuthorizationRequest+TestHelper.h"
1727
#endif
1828

19-
/*! @brief Test value for the @c clientID property.
20-
*/
21-
static NSString *const kTestClientID = @"ClientID";
22-
23-
/*! @brief Test value for the @c clientID property.
24-
*/
25-
static NSString *const kTestClientSecret = @"ClientSecret";
26-
27-
/*! @brief Test key for the @c additionalParameters property.
28-
*/
29-
static NSString *const kTestAdditionalParameterKey = @"A";
30-
31-
/*! @brief Test value for the @c additionalParameters property.
32-
*/
33-
static NSString *const kTestAdditionalParameterValue = @"1";
34-
35-
/*! @brief Test value for the @c scope property.
36-
*/
37-
static NSString *const kTestScope = @"Scope";
38-
39-
/*! @brief Test value for the @c scope property.
40-
*/
41-
static NSString *const kTestScopeA = @"ScopeA";
42-
43-
/*! @brief Test value for the @c redirectURL property.
44-
*/
45-
static NSString *const kTestRedirectURL = @"http://www.google.com/";
46-
47-
/*! @brief Test value for the @c state property.
48-
*/
49-
static NSString *const kTestState = @"State";
50-
51-
/*! @brief Test value for the @c responseType property.
52-
*/
53-
static NSString *const kTestResponseType = @"code";
54-
55-
/*! @brief Test value for the @c nonce property.
56-
*/
57-
static NSString *const kTestNonce = @"Nonce";
58-
59-
/*! @brief Test value for the @c codeVerifier property.
60-
*/
61-
static NSString *const kTestCodeVerifier = @"code verifier";
62-
63-
/*! @brief Test value for the @c authorizationEndpoint property.
64-
*/
65-
static NSString *const kInitializerTestAuthEndpoint = @"https://www.example.com/auth";
66-
67-
/*! @brief Test value for the @c tokenEndpoint property.
68-
*/
69-
static NSString *const kInitializerTestTokenEndpoint = @"https://www.example.com/token";
70-
71-
/*! @brief Test value for the @c tokenEndpoint property.
72-
*/
73-
static NSString *const kInitializerTestRegistrationEndpoint =
74-
@"https://www.example.com/registration";
75-
7629
@interface OIDExternalUserAgentIOSTests : XCTestCase
7730

7831
@end
@@ -81,51 +34,16 @@ @implementation OIDExternalUserAgentIOSTests
8134

8235
- (void)testThatPresentExternalUserAgentRequestReturnsNoWhenMissingPresentingViewController {
8336
OIDExternalUserAgentIOS *userAgent = [[OIDExternalUserAgentIOS alloc] init];
84-
OIDAuthorizationRequest *authRequest = [[self class] authorizationRequestTestInstance];
85-
[OIDAuthorizationService presentAuthorizationRequest:authRequest externalUserAgent:userAgent callback:^(OIDAuthorizationResponse * _Nullable authorizationResponse, NSError * _Nullable error) {
37+
OIDAuthorizationRequest *authRequest = [OIDAuthorizationRequest testInstance];
38+
[OIDAuthorizationService
39+
presentAuthorizationRequest:authRequest
40+
externalUserAgent:userAgent
41+
callback:^(OIDAuthorizationResponse * _Nullable authorizationResponse,
42+
NSError * _Nullable error) {
8643
XCTAssertNotNil(error);
8744
XCTAssertEqual(error.code, OIDErrorCodeSafariOpenError);
8845
XCTAssertEqualObjects(error.localizedDescription, @"Unable to open Safari.");
8946
}];
9047
}
9148

92-
+ (OIDAuthorizationRequest *)authorizationRequestTestInstance {
93-
NSDictionary *additionalParameters =
94-
@{ kTestAdditionalParameterKey : kTestAdditionalParameterValue };
95-
OIDServiceConfiguration *configuration = [[self class] serviceConfigurationTestInstance];
96-
OIDAuthorizationRequest *request =
97-
[[OIDAuthorizationRequest alloc] initWithConfiguration:configuration
98-
clientId:kTestClientID
99-
clientSecret:kTestClientSecret
100-
scope:[OIDScopeUtilities scopesWithArray:@[ kTestScope, kTestScopeA ]]
101-
redirectURL:[NSURL URLWithString:kTestRedirectURL]
102-
responseType:kTestResponseType
103-
state:kTestState
104-
nonce:kTestNonce
105-
codeVerifier:kTestCodeVerifier
106-
codeChallenge:[[self class] codeChallenge]
107-
codeChallengeMethod:[[self class] codeChallengeMethod]
108-
additionalParameters:additionalParameters];
109-
return request;
110-
}
111-
112-
+ (OIDServiceConfiguration *)serviceConfigurationTestInstance {
113-
NSURL *authEndpoint = [NSURL URLWithString:kInitializerTestAuthEndpoint];
114-
NSURL *tokenEndpoint = [NSURL URLWithString:kInitializerTestTokenEndpoint];
115-
NSURL *registrationEndpoint = [NSURL URLWithString:kInitializerTestRegistrationEndpoint];
116-
OIDServiceConfiguration *configuration =
117-
[[OIDServiceConfiguration alloc] initWithAuthorizationEndpoint:authEndpoint
118-
tokenEndpoint:tokenEndpoint
119-
registrationEndpoint:registrationEndpoint];
120-
return configuration;
121-
}
122-
123-
+ (NSString *)codeChallenge {
124-
return [OIDAuthorizationRequest codeChallengeS256ForVerifier:kTestCodeVerifier];
125-
}
126-
127-
+ (NSString *)codeChallengeMethod {
128-
return OIDOAuthorizationRequestCodeChallengeMethodS256;
129-
}
130-
13149
@end

UnitTests/AppAuthCore/OIDAuthorizationRequestTests.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ NS_ASSUME_NONNULL_BEGIN
2626
*/
2727
@interface OIDAuthorizationRequestTests : XCTestCase
2828

29-
/*! @brief Creates a new @c OIDAuthorizationRequest for testing.
30-
*/
31-
+ (OIDAuthorizationRequest *)testInstance;
32-
3329
/*! @brief Creates a new @c OIDAuthorizationRequest testing a code flow request.
3430
*/
3531
+ (OIDAuthorizationRequest *)testInstanceCodeFlow;

UnitTests/AppAuthCore/OIDAuthorizationRequestTests.m

Lines changed: 12 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -18,69 +18,25 @@
1818

1919
#import "OIDAuthorizationRequestTests.h"
2020

21-
#import "OIDServiceConfigurationTests.h"
22-
2321
#if SWIFT_PACKAGE
2422
@import AppAuthCore;
23+
@import TestHelpers;
2524
#else
2625
#import "Source/AppAuthCore/OIDAuthorizationRequest.h"
2726
#import "Source/AppAuthCore/OIDScopeUtilities.h"
28-
#import "Source/AppAuthCore/OIDServiceConfiguration.h"
27+
#import "UnitTests/TestHelpers/OIDServiceConfiguration+TestHelper.h"
28+
#import "UnitTests/TestHelpers/OIDAuthorizationRequest+TestHelper.h"
2929
#endif
3030

3131
// Ignore warnings about "Use of GNU statement expression extension" which is raised by our use of
3232
// the XCTAssert___ macros.
3333
#pragma GCC diagnostic push
3434
#pragma GCC diagnostic ignored "-Wgnu"
3535

36-
/*! @brief Test value for the @c responseType property.
37-
*/
38-
static NSString *const kTestResponseType = @"code";
39-
40-
/*! @brief Test value for the @c clientID property.
41-
*/
42-
static NSString *const kTestClientID = @"ClientID";
43-
44-
/*! @brief Test value for the @c clientID property.
45-
*/
46-
static NSString *const kTestClientSecret = @"ClientSecret";
47-
48-
/*! @brief Test value for the @c scope property.
49-
*/
50-
static NSString *const kTestScope = @"Scope";
51-
52-
/*! @brief Test value for the @c scope property.
53-
*/
54-
static NSString *const kTestScopeA = @"ScopeA";
55-
5636
/*! @brief Test value for the @c scope property.
5737
*/
5838
static NSString *const kTestScopesMerged = @"Scope ScopeA";
5939

60-
/*! @brief Test value for the @c redirectURL property.
61-
*/
62-
static NSString *const kTestRedirectURL = @"http://www.google.com/";
63-
64-
/*! @brief Test key for the @c additionalParameters property.
65-
*/
66-
static NSString *const kTestAdditionalParameterKey = @"A";
67-
68-
/*! @brief Test value for the @c additionalParameters property.
69-
*/
70-
static NSString *const kTestAdditionalParameterValue = @"1";
71-
72-
/*! @brief Test value for the @c state property.
73-
*/
74-
static NSString *const kTestState = @"State";
75-
76-
/*! @brief Test value for the @c nonce property.
77-
*/
78-
static NSString *const kTestNonce = @"Nonce";
79-
80-
/*! @brief Test value for the @c codeVerifier property.
81-
*/
82-
static NSString *const kTestCodeVerifier = @"code verifier";
83-
8440
/*! @brief This test scope contains a character which is one character below the allowed character
8541
range.
8642
*/
@@ -144,28 +100,8 @@ + (NSString *)codeChallengeMethod {
144100
return OIDOAuthorizationRequestCodeChallengeMethodS256;
145101
}
146102

147-
+ (OIDAuthorizationRequest *)testInstance {
148-
NSDictionary *additionalParameters =
149-
@{ kTestAdditionalParameterKey : kTestAdditionalParameterValue };
150-
OIDServiceConfiguration *configuration = [OIDServiceConfigurationTests testInstance];
151-
OIDAuthorizationRequest *request =
152-
[[OIDAuthorizationRequest alloc] initWithConfiguration:configuration
153-
clientId:kTestClientID
154-
clientSecret:kTestClientSecret
155-
scope:[OIDScopeUtilities scopesWithArray:@[ kTestScope, kTestScopeA ]]
156-
redirectURL:[NSURL URLWithString:kTestRedirectURL]
157-
responseType:kTestResponseType
158-
state:kTestState
159-
nonce:kTestNonce
160-
codeVerifier:kTestCodeVerifier
161-
codeChallenge:[[self class] codeChallenge]
162-
codeChallengeMethod:[[self class] codeChallengeMethod]
163-
additionalParameters:additionalParameters];
164-
return request;
165-
}
166-
167103
+ (OIDAuthorizationRequest *)testInstanceCodeFlow {
168-
OIDServiceConfiguration *configuration = [OIDServiceConfigurationTests testInstance];
104+
OIDServiceConfiguration *configuration = [OIDServiceConfiguration testInstance];
169105
OIDAuthorizationRequest *request =
170106
[[OIDAuthorizationRequest alloc] initWithConfiguration:configuration
171107
clientId:kTestClientID
@@ -183,7 +119,7 @@ + (OIDAuthorizationRequest *)testInstanceCodeFlow {
183119
}
184120

185121
+ (OIDAuthorizationRequest *)testInstanceCodeFlowClientAuth {
186-
OIDServiceConfiguration *configuration = [OIDServiceConfigurationTests testInstance];
122+
OIDServiceConfiguration *configuration = [OIDServiceConfiguration testInstance];
187123
OIDAuthorizationRequest *request =
188124
[[OIDAuthorizationRequest alloc] initWithConfiguration:configuration
189125
clientId:kTestClientID
@@ -205,7 +141,7 @@ + (OIDAuthorizationRequest *)testInstanceCodeFlowClientAuth {
205141
- (void)testScopeInitializerWithManyScopesAndNoClientSecret {
206142
NSDictionary *additionalParameters =
207143
@{ kTestAdditionalParameterKey : kTestAdditionalParameterValue };
208-
OIDServiceConfiguration *configuration = [OIDServiceConfigurationTests testInstance];
144+
OIDServiceConfiguration *configuration = [OIDServiceConfiguration testInstance];
209145
OIDAuthorizationRequest *request =
210146
[[OIDAuthorizationRequest alloc] initWithConfiguration:configuration
211147
clientId:kTestClientID
@@ -226,7 +162,7 @@ - (void)testScopeInitializerWithManyScopesAndNoClientSecret {
226162
- (void)testScopeInitializerWithManyScopesAndClientSecret {
227163
NSDictionary *additionalParameters =
228164
@{ kTestAdditionalParameterKey : kTestAdditionalParameterValue };
229-
OIDServiceConfiguration *configuration = [OIDServiceConfigurationTests testInstance];
165+
OIDServiceConfiguration *configuration = [OIDServiceConfiguration testInstance];
230166
OIDAuthorizationRequest *request =
231167
[[OIDAuthorizationRequest alloc] initWithConfiguration:configuration
232168
clientId:kTestClientID
@@ -249,7 +185,7 @@ - (void)testScopeInitializerWithManyScopesAndClientSecret {
249185
process and checking to make sure the source and destination instances are equivalent.
250186
*/
251187
- (void)testCopying {
252-
OIDAuthorizationRequest *request = [[self class] testInstance];
188+
OIDAuthorizationRequest *request = [OIDAuthorizationRequest testInstance];
253189

254190
XCTAssertEqualObjects(request.responseType, kTestResponseType, @"");
255191
XCTAssertEqualObjects(request.scope, kTestScopesMerged, @"");
@@ -285,7 +221,7 @@ - (void)testCopying {
285221
checking to make sure the source and destination instances are equivalent.
286222
*/
287223
- (void)testSecureCoding {
288-
OIDAuthorizationRequest *request = [[self class] testInstance];
224+
OIDAuthorizationRequest *request = [OIDAuthorizationRequest testInstance];
289225

290226
XCTAssertEqualObjects(request.responseType, kTestResponseType, @"");
291227
XCTAssertEqualObjects(request.scope, kTestScopesMerged, @"");
@@ -327,7 +263,7 @@ - (void)testSecureCoding {
327263
*/
328264
- (void)testDisallowedCharactersInScopes {
329265
NSURL *redirectURL = [NSURL URLWithString:kTestRedirectURL];
330-
OIDServiceConfiguration *configuration = [OIDServiceConfigurationTests testInstance];
266+
OIDServiceConfiguration *configuration = [OIDServiceConfiguration testInstance];
331267
XCTAssertThrows(
332268
[[OIDAuthorizationRequest alloc] initWithConfiguration:configuration
333269
clientId:kTestClientID
@@ -441,7 +377,7 @@ - (void)testPKCEVerifierRecommendations {
441377
- (void)testSupportedResponseTypes {
442378
NSDictionary *additionalParameters =
443379
@{ kTestAdditionalParameterKey : kTestAdditionalParameterValue };
444-
OIDServiceConfiguration *configuration = [OIDServiceConfigurationTests testInstance];
380+
OIDServiceConfiguration *configuration = [OIDServiceConfiguration testInstance];
445381

446382
NSString *scope = [OIDScopeUtilities scopesWithArray:@[ kTestScope, kTestScopeA ]];
447383

@@ -524,7 +460,7 @@ - (void)testSupportedResponseTypes {
524460
}
525461

526462
- (void)testExternalUserAgentMethods {
527-
OIDAuthorizationRequest *request = [[self class] testInstance];
463+
OIDAuthorizationRequest *request = [OIDAuthorizationRequest testInstance];
528464
XCTAssertEqualObjects([request externalUserAgentRequestURL], [request authorizationRequestURL]);
529465
XCTAssert([[request redirectScheme] isEqualToString:request.redirectURL.scheme]);
530466
}

0 commit comments

Comments
 (0)