APITestBase.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import FirebaseCore
  15. import FirebaseInstallations
  16. @testable import FirebaseRemoteConfig
  17. #if SWIFT_PACKAGE
  18. import RemoteConfigFakeConsoleObjC
  19. #endif
  20. import XCTest
  21. class InstallationsFake: InstallationsProtocol {
  22. func authToken(completion: @escaping (InstallationsAuthTokenResult?, (any Error)?) -> Void) {
  23. completion(nil, nil)
  24. }
  25. func installationID(completion: @escaping (String?, (any Error)?) -> Void) {
  26. completion("fake_installation_id", nil)
  27. }
  28. }
  29. class APITestBase: XCTestCase {
  30. static var useFakeConfig: Bool!
  31. static var mockedFetch: Bool!
  32. static var mockedRealtime: Bool!
  33. var app: FirebaseApp!
  34. var config: RemoteConfig!
  35. var console: RemoteConfigConsole!
  36. var fakeConsole: FakeConsole!
  37. override class func setUp() {
  38. if FirebaseApp.app() == nil {
  39. #if USE_REAL_CONSOLE
  40. useFakeConfig = false
  41. FirebaseApp.configure()
  42. // Sleep for 1 minute between test files to avoid exceeding rate limit.
  43. sleep(60)
  44. #else
  45. useFakeConfig = true
  46. let options = FirebaseOptions(googleAppID: "1:123:ios:123abc",
  47. gcmSenderID: "correct_gcm_sender_id")
  48. options.apiKey = "A23456789012345678901234567890123456789"
  49. options.projectID = "Fake_Project"
  50. FirebaseApp.configure(options: options)
  51. APITests.mockedFetch = false
  52. #endif
  53. }
  54. }
  55. override func setUpWithError() throws {
  56. try super.setUpWithError()
  57. app = FirebaseApp.app()
  58. config = RemoteConfig.remoteConfig(app: app!)
  59. let settings = RemoteConfigSettings()
  60. settings.minimumFetchInterval = 0
  61. config.configSettings = settings
  62. config.settings.customSignals = [:]
  63. let jsonData = try JSONSerialization.data(
  64. withJSONObject: Constants.jsonValue
  65. )
  66. guard let jsonValue = String(data: jsonData, encoding: .ascii) else {
  67. fatalError("Failed to make json Value from jsonData")
  68. }
  69. let arrayjsonData = try JSONSerialization.data(
  70. withJSONObject: Constants.arrayValue
  71. )
  72. guard let arrayJsonValue = String(data: arrayjsonData, encoding: .ascii) else {
  73. fatalError("Failed to make json Value from jsonData")
  74. }
  75. let dictJsonData = try JSONSerialization.data(
  76. withJSONObject: Constants.dictValue
  77. )
  78. guard let dictJsonValue = String(data: dictJsonData, encoding: .ascii) else {
  79. fatalError("Failed to make json Value from jsonData")
  80. }
  81. if APITests.useFakeConfig {
  82. if !APITests.mockedFetch {
  83. APITests.mockedFetch = true
  84. config.configFetch.installations = InstallationsFake()
  85. }
  86. fakeConsole = FakeConsole()
  87. config.configFetch.fetchSession = URLSessionMock(with: fakeConsole)
  88. config.configFetch.disableNetworkSessionRecreation = true
  89. fakeConsole.config = [Constants.key1: Constants.value1,
  90. Constants.jsonKey: jsonValue,
  91. Constants.nonJsonKey: Constants.nonJsonValue,
  92. Constants.stringKey: Constants.stringValue,
  93. Constants.intKey: String(Constants.intValue),
  94. Constants.floatKey: String(Constants.floatValue),
  95. Constants.decimalKey: "\(Constants.decimalValue)",
  96. Constants.trueKey: String(true),
  97. Constants.falseKey: String(false),
  98. Constants.dataKey: String(decoding: Constants.dataValue, as: UTF8.self),
  99. Constants.arrayKey: arrayJsonValue,
  100. Constants.dictKey: dictJsonValue]
  101. } else {
  102. console = RemoteConfigConsole()
  103. console.updateRemoteConfigValue(Constants.obiwan, forKey: Constants.jedi)
  104. }
  105. // Uncomment for verbose debug logging.
  106. FirebaseConfiguration.shared.setLoggerLevel(FirebaseLoggerLevel.debug)
  107. }
  108. override func tearDown() {
  109. if APITests.useFakeConfig {
  110. fakeConsole.empty()
  111. } else {
  112. console.removeRemoteConfigValue(forKey: Constants.sith)
  113. console.removeRemoteConfigValue(forKey: Constants.jedi)
  114. }
  115. app = nil
  116. config = nil
  117. super.tearDown()
  118. }
  119. }