APITestBase.swift 4.3 KB

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