APITestBase.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. var app: FirebaseApp!
  24. var config: RemoteConfig!
  25. var console: RemoteConfigConsole!
  26. var fakeConsole: FakeConsole!
  27. override class func setUp() {
  28. if FirebaseApp.app() == nil {
  29. #if USE_REAL_CONSOLE
  30. useFakeConfig = false
  31. FirebaseApp.configure()
  32. #else
  33. useFakeConfig = true
  34. let options = FirebaseOptions(googleAppID: "1:123:ios:123abc",
  35. gcmSenderID: "correct_gcm_sender_id")
  36. options.apiKey = "A23456789012345678901234567890123456789"
  37. options.projectID = "Fake Project"
  38. FirebaseApp.configure(options: options)
  39. APITests.mockedFetch = false
  40. #endif
  41. }
  42. }
  43. override func setUpWithError() throws {
  44. try super.setUpWithError()
  45. app = FirebaseApp.app()
  46. config = RemoteConfig.remoteConfig(app: app!)
  47. let settings = RemoteConfigSettings()
  48. settings.minimumFetchInterval = 0
  49. config.configSettings = settings
  50. let jsonData = try JSONSerialization.data(
  51. withJSONObject: Constants.jsonValue
  52. )
  53. guard let jsonValue = String(data: jsonData, encoding: .ascii) else {
  54. fatalError("Failed to make json Value from jsonData")
  55. }
  56. if APITests.useFakeConfig {
  57. if !APITests.mockedFetch {
  58. APITests.mockedFetch = true
  59. config.configFetch = FetchMocks.mockFetch(config.configFetch)
  60. }
  61. fakeConsole = FakeConsole()
  62. config.configFetch.fetchSession = URLSessionMock(with: fakeConsole)
  63. fakeConsole.config = [Constants.key1: Constants.value1,
  64. Constants.jsonKey: jsonValue,
  65. Constants.nonJsonKey: Constants.nonJsonValue,
  66. Constants.stringKey: Constants.stringValue,
  67. Constants.intKey: String(Constants.intValue),
  68. Constants.floatKey: String(Constants.floatValue),
  69. Constants.decimalKey: "\(Constants.decimalValue)",
  70. Constants.trueKey: String(true),
  71. Constants.falseKey: String(false),
  72. Constants.dataKey: String(decoding: Constants.dataValue, as: UTF8.self)]
  73. } else {
  74. console = RemoteConfigConsole()
  75. console.updateRemoteConfigValue(Constants.obiwan, forKey: Constants.jedi)
  76. }
  77. // Uncomment for verbose debug logging.
  78. // FirebaseConfiguration.shared.setLoggerLevel(FirebaseLoggerLevel.debug)
  79. }
  80. override func tearDown() {
  81. if APITests.useFakeConfig {
  82. fakeConsole.empty()
  83. } else {
  84. console.removeRemoteConfigValue(forKey: Constants.sith)
  85. console.removeRemoteConfigValue(forKey: Constants.jedi)
  86. }
  87. app = nil
  88. config = nil
  89. super.tearDown()
  90. }
  91. }