APITestBase.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. let arrayjsonData = try JSONSerialization.data(
  57. withJSONObject: Constants.arrayValue
  58. )
  59. guard let arrayJsonValue = String(data: arrayjsonData, encoding: .ascii) else {
  60. fatalError("Failed to make json Value from jsonData")
  61. }
  62. let dictJsonData = try JSONSerialization.data(
  63. withJSONObject: Constants.dictValue
  64. )
  65. guard let dictJsonValue = String(data: dictJsonData, encoding: .ascii) else {
  66. fatalError("Failed to make json Value from jsonData")
  67. }
  68. if APITests.useFakeConfig {
  69. if !APITests.mockedFetch {
  70. APITests.mockedFetch = true
  71. config.configFetch = FetchMocks.mockFetch(config.configFetch)
  72. }
  73. fakeConsole = FakeConsole()
  74. config.configFetch.fetchSession = URLSessionMock(with: fakeConsole)
  75. fakeConsole.config = [Constants.key1: Constants.value1,
  76. Constants.jsonKey: jsonValue,
  77. Constants.nonJsonKey: Constants.nonJsonValue,
  78. Constants.stringKey: Constants.stringValue,
  79. Constants.intKey: String(Constants.intValue),
  80. Constants.floatKey: String(Constants.floatValue),
  81. Constants.decimalKey: "\(Constants.decimalValue)",
  82. Constants.trueKey: String(true),
  83. Constants.falseKey: String(false),
  84. Constants.dataKey: String(decoding: Constants.dataValue, as: UTF8.self),
  85. Constants.arrayKey: arrayJsonValue,
  86. Constants.dictKey: dictJsonValue]
  87. } else {
  88. console = RemoteConfigConsole()
  89. console.updateRemoteConfigValue(Constants.obiwan, forKey: Constants.jedi)
  90. }
  91. // Uncomment for verbose debug logging.
  92. // FirebaseConfiguration.shared.setLoggerLevel(FirebaseLoggerLevel.debug)
  93. }
  94. override func tearDown() {
  95. if APITests.useFakeConfig {
  96. fakeConsole.empty()
  97. } else {
  98. console.removeRemoteConfigValue(forKey: Constants.sith)
  99. console.removeRemoteConfigValue(forKey: Constants.jedi)
  100. }
  101. app = nil
  102. config = nil
  103. super.tearDown()
  104. }
  105. }