@@ -55,6 +55,71 @@ + (SFSDKTestCredentialsData *)populateAuthCredentialsFromConfigFileForClass:(Cla
5555 NSFileManager *fm = [NSFileManager defaultManager ];
5656 NSData *tokenJson = [fm contentsAtPath: tokenPath];
5757 id jsonResponse = [SFJsonUtils objectFromJSONData: tokenJson];
58+
59+ return [TestSetupUtils authCredentialsFromJson: jsonResponse initializeSdk: YES ];
60+
61+ }
62+
63+ + (SFSDKTestCredentialsData *)populateAuthCredentialsFromString : (NSString *)testCredentialsJsonString {
64+ return [self populateAuthCredentialsFromString: testCredentialsJsonString initializeSdk: YES ];
65+ }
66+
67+ + (SFSDKTestCredentialsData *)populateAuthCredentialsFromString : (NSString *)testCredentialsJsonString initializeSdk : (BOOL )initializeSdk {
68+
69+ return [TestSetupUtils authCredentialsFromJson: [SFJsonUtils objectFromJSONString: testCredentialsJsonString] initializeSdk: initializeSdk];
70+ }
71+
72+ + (void )synchronousAuthRefresh {
73+ [self synchronousAuthRefreshWithUserDidLoginNotification: NO ];
74+ }
75+
76+ + (void )synchronousAuthRefreshWithUserDidLoginNotification : (BOOL )postUserDidLogIn
77+ {
78+ // All of the setup and validation of prerequisite auth state is done in populateAuthCredentialsFromConfigFile.
79+ // Make sure that method has run before this one.
80+ NSAssert (credentials!=nil , @" You must call populateAuthCredentialsFromConfigFileForClass before synchronousAuthRefresh" );
81+ __block SFSDKTestRequestListener *authListener = [[SFSDKTestRequestListener alloc ] init ];
82+ __block SFUserAccount *user = nil ;
83+ [[SFUserAccountManager sharedInstance ]
84+ refreshCredentials: credentials
85+ completion: ^(SFOAuthInfo *authInfo, SFUserAccount *userAccount) {
86+ authListener.returnStatus = kTestRequestStatusDidLoad ;
87+ user = userAccount;
88+ // Ensure tests don't change/corrupt the current user credentials.
89+ if (user.credentials .refreshToken == nil ) {
90+ user.credentials = credentials;
91+ }
92+ if (postUserDidLogIn) {
93+ NSDictionary *userInfo = @{kSFNotificationUserInfoAccountKey : userAccount,
94+ kSFNotificationUserInfoAuthTypeKey : authInfo};
95+ [[NSNotificationCenter defaultCenter ] postNotificationName: kSFNotificationUserDidLogIn
96+ object: [SFUserAccountManager sharedInstance ]
97+ userInfo: userInfo];
98+ [[SFUserAccountManager sharedInstance ] stopCurrentAuthentication: ^(BOOL result){}];
99+ }
100+ } failure: ^(SFOAuthInfo *authInfo, NSError *error) {
101+ authListener.lastError = error;
102+ authListener.returnStatus = kTestRequestStatusDidFail ;
103+ }];
104+ [authListener waitForCompletion ];
105+ [[SFUserAccountManager sharedInstance ] setCurrentUserInternal: user];
106+ NSAssert ([authListener.returnStatus isEqualToString: kTestRequestStatusDidLoad ], @" After auth attempt, expected status '%@ ', got '%@ '" ,
107+ kTestRequestStatusDidLoad ,
108+ authListener.returnStatus);
109+ }
110+
111+ + (SFOAuthCredentials *)newClientCredentials {
112+
113+ NSString *identifier = [[SFUserAccountManager sharedInstance ] uniqueUserAccountIdentifier: [SFUserAccountManager sharedInstance ].oauthClientId];
114+ SFOAuthCredentials *creds = [[SFOAuthCredentials alloc ] initWithIdentifier: identifier clientId: [SFUserAccountManager sharedInstance ].oauthClientId encrypted: YES ];
115+ creds.clientId = [SFUserAccountManager sharedInstance ].oauthClientId ;
116+ creds.redirectUri = [SFUserAccountManager sharedInstance ].oauthCompletionUrl ;
117+ creds.domain = [SFUserAccountManager sharedInstance ].loginHost ;
118+ creds.accessToken = nil ;
119+ return creds;
120+ }
121+
122+ + (SFSDKTestCredentialsData *)authCredentialsFromJson : (id )jsonResponse initializeSdk : (BOOL )initializeSdk {
58123 NSAssert (jsonResponse != nil , @" Error parsing JSON from config file: %@ " , [SFJsonUtils lastError ]);
59124 NSDictionary *dictResponse = (NSDictionary *)jsonResponse;
60125 SFSDKTestCredentialsData *credsData = [[SFSDKTestCredentialsData alloc ] initWithDict: dictResponse];
@@ -65,12 +130,14 @@ + (SFSDKTestCredentialsData *)populateAuthCredentialsFromConfigFileForClass:(Cla
65130 nil != credsData.identityUrl &&
66131 nil != credsData.instanceUrl, @" config credentials are missing! %@ " ,
67132 dictResponse);
68-
133+
69134 // check whether the test config file has never been edited
70135 NSAssert (![credsData.refreshToken isEqualToString: @" __INSERT_TOKEN_HERE__" ],
71136 @" You need to obtain credentials for your test org and replace test_credentials.json" );
72- [SalesforceSDKManager initializeSDK ];
73-
137+ if (initializeSdk) {
138+ [SalesforceSDKManager initializeSDK ];
139+ }
140+
74141 // Note: We need to fix this inconsistency for tests in the long run.There should be a clean way to refresh appConfigs for tests. The configs should apply across all components that need the config.
75142 SFSDKAppConfig *appconfig = [[SFSDKAppConfig alloc ] init ];
76143 appconfig.oauthRedirectURI = credsData.redirectUri ;
@@ -98,41 +165,4 @@ + (SFSDKTestCredentialsData *)populateAuthCredentialsFromConfigFileForClass:(Cla
98165 return credsData;
99166}
100167
101- + (void )synchronousAuthRefresh
102- {
103- // All of the setup and validation of prerequisite auth state is done in populateAuthCredentialsFromConfigFile.
104- // Make sure that method has run before this one.
105- NSAssert (credentials!=nil , @" You must call populateAuthCredentialsFromConfigFileForClass before synchronousAuthRefresh" );
106- __block SFSDKTestRequestListener *authListener = [[SFSDKTestRequestListener alloc ] init ];
107- __block SFUserAccount *user = nil ;
108- [[SFUserAccountManager sharedInstance ]
109- refreshCredentials: credentials
110- completion: ^(SFOAuthInfo *authInfo, SFUserAccount *userAccount) {
111- authListener.returnStatus = kTestRequestStatusDidLoad ;
112- user = userAccount;
113- // Ensure tests don't change/corrupt the current user credentials.
114- if (user.credentials .refreshToken == nil ) {
115- user.credentials = credentials;
116- }
117- } failure: ^(SFOAuthInfo *authInfo, NSError *error) {
118- authListener.lastError = error;
119- authListener.returnStatus = kTestRequestStatusDidFail ;
120- }];
121- [authListener waitForCompletion ];
122- [[SFUserAccountManager sharedInstance ] setCurrentUserInternal: user];
123- NSAssert ([authListener.returnStatus isEqualToString: kTestRequestStatusDidLoad ], @" After auth attempt, expected status '%@ ', got '%@ '" ,
124- kTestRequestStatusDidLoad ,
125- authListener.returnStatus);
126- }
127-
128- + (SFOAuthCredentials *)newClientCredentials {
129-
130- NSString *identifier = [[SFUserAccountManager sharedInstance ] uniqueUserAccountIdentifier: [SFUserAccountManager sharedInstance ].oauthClientId];
131- SFOAuthCredentials *creds = [[SFOAuthCredentials alloc ] initWithIdentifier: identifier clientId: [SFUserAccountManager sharedInstance ].oauthClientId encrypted: YES ];
132- creds.clientId = [SFUserAccountManager sharedInstance ].oauthClientId ;
133- creds.redirectUri = [SFUserAccountManager sharedInstance ].oauthCompletionUrl ;
134- creds.domain = [SFUserAccountManager sharedInstance ].loginHost ;
135- creds.accessToken = nil ;
136- return creds;
137- }
138168@end
0 commit comments