GACDeviceCheckAPIServiceE2ETests.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright 2020 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <TargetConditionals.h>
  17. // Tests that use the Keychain require a host app and Swift Package Manager
  18. // does not support adding a host app to test targets.
  19. #if !SWIFT_PACKAGE
  20. // Skip keychain tests on Catalyst and macOS. Tests are skipped because they
  21. // involve interactions with the keychain that require a provisioning profile.
  22. // See go/firebase-macos-keychain-popups for more details.
  23. #if !TARGET_OS_MACCATALYST && !TARGET_OS_OSX
  24. #import <XCTest/XCTest.h>
  25. #import "FBLPromise+Testing.h"
  26. #import <FirebaseCore/FirebaseCore.h>
  27. #import <FirebaseCoreExtension/FirebaseCoreInternal.h>
  28. #import "AppCheckCore/Sources/Core/APIService/GACAppCheckAPIService.h"
  29. #import "AppCheckCore/Sources/DeviceCheckProvider/API/GACDeviceCheckAPIService.h"
  30. #import "AppCheckCore/Sources/Public/AppCheckCore/GACAppCheckToken.h"
  31. // TODO(andrewheard): Remove from generic App Check SDK.
  32. // FIREBASE_APP_CHECK_ONLY_BEGIN
  33. static NSString *const kHeartbeatKey = @"X-firebase-client";
  34. // FIREBASE_APP_CHECK_ONLY_END
  35. @interface GACDeviceCheckAPIServiceE2ETests : XCTestCase
  36. @property(nonatomic) GACDeviceCheckAPIService *deviceCheckAPIService;
  37. @property(nonatomic) GACAppCheckAPIService *APIService;
  38. @property(nonatomic) NSURLSession *URLSession;
  39. @end
  40. // TODO(ncooke3): Fix these tests up and get them running on CI.
  41. @implementation GACDeviceCheckAPIServiceE2ETests
  42. - (void)setUp {
  43. self.URLSession = [NSURLSession
  44. sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  45. FIROptions *options = [self firebaseTestOptions];
  46. FIRHeartbeatLogger *heartbeatLogger =
  47. [[FIRHeartbeatLogger alloc] initWithAppID:options.googleAppID];
  48. GACAppCheckAPIRequestHook heartbeatLoggerHook = ^(NSMutableURLRequest *request) {
  49. [request setValue:FIRHeaderValueFromHeartbeatsPayload(
  50. [heartbeatLogger flushHeartbeatsIntoPayload])
  51. forHTTPHeaderField:kHeartbeatKey];
  52. };
  53. self.APIService = [[GACAppCheckAPIService alloc] initWithURLSession:self.URLSession
  54. baseURL:nil
  55. APIKey:options.APIKey
  56. requestHooks:@[ heartbeatLoggerHook ]];
  57. self.deviceCheckAPIService = [[GACDeviceCheckAPIService alloc]
  58. initWithAPIService:self.APIService
  59. resourceName:[GACDeviceCheckAPIServiceE2ETests resourceNameFromOptions:options]];
  60. }
  61. - (void)tearDown {
  62. self.deviceCheckAPIService = nil;
  63. self.APIService = nil;
  64. self.URLSession = nil;
  65. }
  66. // TODO: Re-enable the test once secret with "GoogleService-Info.plist" is configured.
  67. - (void)temporaryDisabled_testAppCheckTokenSuccess {
  68. __auto_type appCheckPromise =
  69. [self.deviceCheckAPIService appCheckTokenWithDeviceToken:[NSData data]];
  70. XCTAssert(FBLWaitForPromisesWithTimeout(20));
  71. XCTAssertNil(appCheckPromise.error);
  72. XCTAssertNotNil(appCheckPromise.value);
  73. XCTAssertNotNil(appCheckPromise.value.token);
  74. XCTAssertNotNil(appCheckPromise.value.expirationDate);
  75. }
  76. #pragma mark - Helpers
  77. - (FIROptions *)firebaseTestOptions {
  78. NSString *plistPath =
  79. [[NSBundle bundleForClass:[self class]] pathForResource:@"GoogleService-Info"
  80. ofType:@"plist"];
  81. FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:plistPath];
  82. return options;
  83. }
  84. // TODO(andrewheard): Remove from generic App Check SDK.
  85. // FIREBASE_APP_CHECK_ONLY_BEGIN
  86. + (NSString *)resourceNameFromOptions:(FIROptions *)options {
  87. return [NSString stringWithFormat:@"projects/%@/apps/%@", options.projectID, options.googleAppID];
  88. }
  89. // FIREBASE_APP_CHECK_ONLY_END
  90. @end
  91. #endif // !TARGET_OS_MACCATALYST && !TARGET_OS_OSX
  92. #endif // !SWIFT_PACKAGE