APITestBase.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 FirebaseRemoteConfig
  16. #if SWIFT_PACKAGE
  17. import RemoteConfigFakeConsoleObjC
  18. #endif
  19. import XCTest
  20. class APITestBase: XCTestCase {
  21. static var useFakeConfig: Bool!
  22. static var mockedFetch: Bool!
  23. static var mockedRealtime: Bool!
  24. var app: FirebaseApp!
  25. var config: RemoteConfig!
  26. var console: RemoteConfigConsole!
  27. var fakeConsole: FakeConsole!
  28. override class func setUp() {
  29. if FirebaseApp.app() == nil {
  30. #if USE_REAL_CONSOLE
  31. useFakeConfig = false
  32. FirebaseApp.configure()
  33. // Sleep for 1 minute between test files to avoid exceeding rate limit.
  34. sleep(60)
  35. #else
  36. useFakeConfig = true
  37. let options = FirebaseOptions(googleAppID: "1:123:ios:123abc",
  38. gcmSenderID: "correct_gcm_sender_id")
  39. options.apiKey = "A23456789012345678901234567890123456789"
  40. options.projectID = "Fake_Project"
  41. FirebaseApp.configure(options: options)
  42. APITests.mockedFetch = false
  43. APITests.mockedRealtime = false
  44. #endif
  45. }
  46. }
  47. override func setUpWithError() throws {
  48. try super.setUpWithError()
  49. app = FirebaseApp.app()
  50. config = RemoteConfig.remoteConfig(app: app!)
  51. let settings = RemoteConfigSettings()
  52. settings.minimumFetchInterval = 0
  53. config.configSettings = settings
  54. let jsonData = try JSONSerialization.data(
  55. withJSONObject: Constants.jsonValue
  56. )
  57. guard let jsonValue = String(data: jsonData, encoding: .ascii) else {
  58. fatalError("Failed to make json Value from jsonData")
  59. }
  60. let arrayjsonData = try JSONSerialization.data(
  61. withJSONObject: Constants.arrayValue
  62. )
  63. guard let arrayJsonValue = String(data: arrayjsonData, encoding: .ascii) else {
  64. fatalError("Failed to make json Value from jsonData")
  65. }
  66. let dictJsonData = try JSONSerialization.data(
  67. withJSONObject: Constants.dictValue
  68. )
  69. guard let dictJsonValue = String(data: dictJsonData, encoding: .ascii) else {
  70. fatalError("Failed to make json Value from jsonData")
  71. }
  72. if APITests.useFakeConfig {
  73. if !APITests.mockedFetch {
  74. APITests.mockedFetch = true
  75. config.configFetch = FetchMocks.mockFetch(config.configFetch)
  76. }
  77. if !APITests.mockedRealtime {
  78. APITests.mockedRealtime = true
  79. config.configRealtime = RealtimeMocks.mockRealtime(config.configRealtime)
  80. }
  81. fakeConsole = FakeConsole()
  82. config.configFetch.fetchSession = URLSessionMock(with: fakeConsole)
  83. fakeConsole.config = [Constants.key1: Constants.value1,
  84. Constants.jsonKey: jsonValue,
  85. Constants.nonJsonKey: Constants.nonJsonValue,
  86. Constants.stringKey: Constants.stringValue,
  87. Constants.intKey: String(Constants.intValue),
  88. Constants.floatKey: String(Constants.floatValue),
  89. Constants.decimalKey: "\(Constants.decimalValue)",
  90. Constants.trueKey: String(true),
  91. Constants.falseKey: String(false),
  92. Constants.dataKey: String(decoding: Constants.dataValue, as: UTF8.self),
  93. Constants.arrayKey: arrayJsonValue,
  94. Constants.dictKey: dictJsonValue]
  95. } else {
  96. console = RemoteConfigConsole()
  97. console.updateRemoteConfigValue(Constants.obiwan, forKey: Constants.jedi)
  98. }
  99. // Uncomment for verbose debug logging.
  100. // FirebaseConfiguration.shared.setLoggerLevel(FirebaseLoggerLevel.debug)
  101. }
  102. override func tearDown() {
  103. if APITests.useFakeConfig {
  104. fakeConsole.empty()
  105. } else {
  106. console.removeRemoteConfigValue(forKey: Constants.sith)
  107. console.removeRemoteConfigValue(forKey: Constants.jedi)
  108. }
  109. app = nil
  110. config = nil
  111. super.tearDown()
  112. }
  113. }