Skip to content

Commit

Permalink
Fix missing token bug for App Events (#1670)
Browse files Browse the repository at this point in the history
Summary:
Thanks for proposing a pull request!

To help us review the request, please complete the following:

- [x] sign [contributor license agreement](https://developers.facebook.com/opensource/cla)
- [x] I've ensured that all existing tests pass and added tests (when/where necessary)
- [x] I've updated the documentation (when/where necessary) and [Changelog](CHANGELOG.md) (when/where necessary)

## Pull Request Details

App Events utils had bug where it wouldn't provide the `app|client` token unless a token was partially specified either as input or in the settings.

Pull Request resolved: #1670

Test Plan: I've added a test to verify that `tokenStringToUseFor` returns the correct token

Reviewed By: KylinChang

Differential Revision: D26876326

Pulled By: joesus

fbshipit-source-id: 2cfd7330ad596d008f7dd82c27304c0acb719787
  • Loading branch information
ptxmac authored and joesus committed Mar 9, 2021
1 parent 39e2d5e commit 6fa2705
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Avoids call to `AppEvents` singleton when setting overriding app ID [#1647](https://github.com/facebook/facebook-ios-sdk/pull/1647)
- CocoaPods now compiles `FBSDKDynamicFrameworkLoader` with ARC.
- CocoaPods now uses static frameworks as the prebuilt libraries for the aggregate FacebookSDK podspec
- App Events use the correct token if none have been provided manually ([@ptxmac](https://github.com/ptxmac)[#1670](https://github.com/facebook/facebook-ios-sdk/pull/1670)

### Deprecated

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,20 @@ + (NSString *)tokenStringToUseFor:(FBSDKAccessToken *)token
token = [FBSDKAccessToken currentAccessToken];
}

NSString *appID = [FBSDKAppEvents loggingOverrideAppID] ?: token.appID ?: [FBSDKSettings appID];
NSString *loggingOverrideAppID = [FBSDKAppEvents loggingOverrideAppID];

NSString *appID = loggingOverrideAppID ?: token.appID ?: [FBSDKSettings appID];
NSString *tokenString = token.tokenString;
NSString *clientTokenString = [FBSDKSettings clientToken];

if (!tokenString || ![appID isEqualToString:token.appID]) {
// If there's an logging override app id present, then we don't want to use the client token since the client token
// is intended to match up with the primary app id (and AppEvents doesn't require a client token).
NSString *clientTokenString = [FBSDKSettings clientToken];
if (clientTokenString && appID && [appID isEqualToString:token.appID]) {
// If there's a logging override app id present
// then we don't want to use the client token since the client token
// is intended to match up with the primary app id
// and AppEvents doesn't require a client token.
if (clientTokenString && loggingOverrideAppID) {
tokenString = nil;
} else if (clientTokenString && appID && ([appID isEqualToString:token.appID] || token == nil)) {
tokenString = [NSString stringWithFormat:@"%@|%@", appID, clientTokenString];
} else if (appID) {
tokenString = nil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,233 @@ - (void)testGetStandardEvents
}
}

// MARK: - Token Strings

- (void)testTokenStringWithoutAccessTokenWithoutAppIdWithoutClientToken
{
[FBSDKAppEvents setLoggingOverrideAppID:nil];
[FBSDKAccessToken setCurrentAccessToken:nil];
[FBSDKSettings setAppID:nil];
[FBSDKSettings setClientToken:nil];
NSString *tokenString = [FBSDKAppEventsUtility tokenStringToUseFor:nil];
XCTAssertNil(
tokenString,
"Should not provide a token string without an app id or client token"
);
}

- (void)testTokenStringWithoutAccessTokenWithoutAppIdWithClientToken
{
[FBSDKAppEvents setLoggingOverrideAppID:nil];
[FBSDKAccessToken setCurrentAccessToken:nil];
[FBSDKSettings setAppID:nil];
[FBSDKSettings setClientToken:@"toktok"];
NSString *tokenString = [FBSDKAppEventsUtility tokenStringToUseFor:nil];
XCTAssertNil(
tokenString,
"Should not provide a token string without an app id"
);
}

- (void)testTokenStringWithoutAccessTokenWithAppIdWithoutClientToken
{
[FBSDKAppEvents setLoggingOverrideAppID:nil];
[FBSDKAccessToken setCurrentAccessToken:nil];
[FBSDKSettings setAppID:@"123"];
[FBSDKSettings setClientToken:nil];
NSString *tokenString = [FBSDKAppEventsUtility tokenStringToUseFor:nil];
XCTAssertNil(
tokenString,
"Should not provide a token string without a client token"
);
}

- (void)testTokenStringWithoutAccessTokenWithAppIdWithClientToken
{
[FBSDKAppEvents setLoggingOverrideAppID:nil];
[FBSDKAccessToken setCurrentAccessToken:nil];
[FBSDKSettings setAppID:@"123"];
[FBSDKSettings setClientToken:@"toktok"];
NSString *tokenString = [FBSDKAppEventsUtility tokenStringToUseFor:nil];
XCTAssertEqualObjects(
tokenString,
@"123|toktok",
"Should provide a token string with the app id and client token"
);
}

- (void)testTokenStringWithAccessTokenWithoutAppIdWithClientToken
{
[FBSDKAppEvents setLoggingOverrideAppID:nil];
[FBSDKAccessToken setCurrentAccessToken:SampleAccessToken.validToken];
[FBSDKSettings setAppID:nil];
[FBSDKSettings setClientToken:@"toktok"];
NSString *tokenString = [FBSDKAppEventsUtility tokenStringToUseFor:nil];
XCTAssertEqualObjects(
tokenString,
SampleAccessToken.validToken.appID,
"Should provide a token string with the access token's app id"
);
}

- (void)testTokenStringWithAccessTokenWithoutAppIdWithoutClientToken
{
[FBSDKAppEvents setLoggingOverrideAppID:nil];
[FBSDKAccessToken setCurrentAccessToken:SampleAccessToken.validToken];
[FBSDKSettings setAppID:nil];
[FBSDKSettings setClientToken:nil];
NSString *tokenString = [FBSDKAppEventsUtility tokenStringToUseFor:nil];
XCTAssertEqualObjects(
tokenString,
SampleAccessToken.validToken.appID,
"Should provide a token string with the access token's app id"
);
}

- (void)testTokenStringWithAccessTokenWithAppIdWithoutClientToken
{
[FBSDKAppEvents setLoggingOverrideAppID:nil];
[FBSDKAccessToken setCurrentAccessToken:SampleAccessToken.validToken];
[FBSDKSettings setAppID:@"456"];
[FBSDKSettings setClientToken:nil];
NSString *tokenString = [FBSDKAppEventsUtility tokenStringToUseFor:nil];
XCTAssertEqualObjects(
tokenString,
SampleAccessToken.validToken.appID,
"Should provide a token string with the access token's app id"
);
}

- (void)testTokenStringWithAccessTokenWithAppIdWithClientToken
{
[FBSDKAppEvents setLoggingOverrideAppID:nil];
[FBSDKAccessToken setCurrentAccessToken:SampleAccessToken.validToken];
[FBSDKSettings setAppID:@"456"];
[FBSDKSettings setClientToken:@"toktok"];
NSString *tokenString = [FBSDKAppEventsUtility tokenStringToUseFor:nil];
XCTAssertEqualObjects(
tokenString,
SampleAccessToken.validToken.appID,
"Should provide a token string with the access token's app id"
);
}

- (void)testTokenStringWithoutAccessTokenWithoutAppIdWithoutClientTokenWithLoggingAppID
{
[FBSDKAppEvents setLoggingOverrideAppID:@"789"];
[FBSDKAccessToken setCurrentAccessToken:nil];
[FBSDKSettings setAppID:nil];
[FBSDKSettings setClientToken:nil];
NSString *tokenString = [FBSDKAppEventsUtility tokenStringToUseFor:nil];
XCTAssertNil(
tokenString,
"Should not provide a token string without an app id or client token"
);
}

- (void)testTokenStringWithoutAccessTokenWithoutAppIdWithClientTokenWithLoggingAppID
{
[FBSDKAppEvents setLoggingOverrideAppID:@"789"];
[FBSDKAccessToken setCurrentAccessToken:nil];
[FBSDKSettings setAppID:nil];
[FBSDKSettings setClientToken:@"toktok"];
NSString *tokenString = [FBSDKAppEventsUtility tokenStringToUseFor:nil];
XCTAssertNil(
tokenString,
"Should not provide a token string with the logging app id and client token"
);
}

- (void)testTokenStringWithoutAccessTokenWithAppIdWithoutClientTokenWithLoggingAppID
{
[FBSDKAppEvents setLoggingOverrideAppID:@"789"];
[FBSDKAccessToken setCurrentAccessToken:nil];
[FBSDKSettings setAppID:@"123"];
[FBSDKSettings setClientToken:nil];
NSString *tokenString = [FBSDKAppEventsUtility tokenStringToUseFor:nil];
XCTAssertNil(
tokenString,
"Should not provide a token string without a client token"
);
}

- (void)testTokenStringWithoutAccessTokenWithAppIdWithClientTokenWithLoggingAppID
{
[FBSDKAppEvents setLoggingOverrideAppID:@"789"];
[FBSDKAccessToken setCurrentAccessToken:nil];
[FBSDKSettings setAppID:@"123"];
[FBSDKSettings setClientToken:@"toktok"];
NSString *tokenString = [FBSDKAppEventsUtility tokenStringToUseFor:nil];
XCTAssertNil(
tokenString,
"Should not provide a token string with the logging app id and client token"
);
}

- (void)testTokenStringWithAccessTokenWithoutAppIdWithClientTokenWithLoggingAppID
{
[FBSDKAppEvents setLoggingOverrideAppID:@"789"];
[FBSDKAccessToken setCurrentAccessToken:SampleAccessToken.validToken];
[FBSDKSettings setAppID:nil];
[FBSDKSettings setClientToken:@"toktok"];
NSString *tokenString = [FBSDKAppEventsUtility tokenStringToUseFor:nil];
XCTAssertNil(
tokenString,
"Should not provide a token string when the logging override and access token app ids are mismatched"
);
}

- (void)testTokenStringWithAccessTokenWithoutAppIdWithoutClientTokenWithLoggingAppID
{
[FBSDKAppEvents setLoggingOverrideAppID:@"789"];
[FBSDKAccessToken setCurrentAccessToken:SampleAccessToken.validToken];
[FBSDKSettings setAppID:nil];
[FBSDKSettings setClientToken:nil];
NSString *tokenString = [FBSDKAppEventsUtility tokenStringToUseFor:nil];
XCTAssertNil(
tokenString,
"Should not provide a token string when the logging override and access token app ids are mismatched"
);
}

- (void)testTokenStringWithAccessTokenWithAppIdWithoutClientTokenWithLoggingAppID
{
[FBSDKAppEvents setLoggingOverrideAppID:@"789"];
[FBSDKAccessToken setCurrentAccessToken:SampleAccessToken.validToken];
[FBSDKSettings setAppID:@"456"];
[FBSDKSettings setClientToken:nil];
NSString *tokenString = [FBSDKAppEventsUtility tokenStringToUseFor:nil];
XCTAssertNil(
tokenString,
"Should not provide a token string when the logging override and access token app ids are mismatched"
);
}

- (void)testTokenStringWithAccessTokenWithAppIdWithClientTokenWithLoggingAppID
{
[FBSDKAppEvents setLoggingOverrideAppID:@"789"];
[FBSDKAccessToken setCurrentAccessToken:SampleAccessToken.validToken];
[FBSDKSettings setAppID:@"456"];
[FBSDKSettings setClientToken:@"toktok"];
NSString *tokenString = [FBSDKAppEventsUtility tokenStringToUseFor:nil];
XCTAssertNil(
tokenString,
"Should not provide a token string when the logging override and access token app ids are mismatched"
);
}

- (void)testTokenStringWithAccessTokenWithAppIdWithClientTokenWithLoggingAppIDMatching
{
[FBSDKAppEvents setLoggingOverrideAppID:SampleAccessToken.validToken.appID];
[FBSDKAccessToken setCurrentAccessToken:SampleAccessToken.validToken];
[FBSDKSettings setAppID:@"456"];
[FBSDKSettings setClientToken:@"toktok"];
NSString *tokenString = [FBSDKAppEventsUtility tokenStringToUseFor:nil];
XCTAssertEqualObjects(
tokenString,
SampleAccessToken.validToken.appID,
"Should provide a token string with the access token's app id when the logging override matches it"
);
}

@end

0 comments on commit 6fa2705

Please sign in to comment.