APITests.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. @testable import FirebaseRemoteConfig
  16. import XCTest
  17. /// String constants used for testing.
  18. private enum Constants {
  19. static let key1 = "Key1"
  20. static let jedi = "Jedi"
  21. static let sith = "Sith_Lord"
  22. static let value1 = "Value1"
  23. static let obiwan = "Obi-Wan"
  24. static let yoda = "Yoda"
  25. static let darthSidious = "Darth Sidious"
  26. }
  27. class APITests: APITestBase {
  28. var console: RemoteConfigConsole!
  29. override func setUp() {
  30. super.setUp()
  31. if APITests.useFakeConfig {
  32. fakeConsole.config = [Constants.key1: Constants.value1]
  33. } else {
  34. console = RemoteConfigConsole()
  35. console.updateRemoteConfigValue(Constants.obiwan, forKey: Constants.jedi)
  36. }
  37. }
  38. override func tearDown() {
  39. super.tearDown()
  40. // If using RemoteConfigConsole, reset remote config values.
  41. if !APITests.useFakeConfig {
  42. console.removeRemoteConfigValue(forKey: Constants.sith)
  43. console.removeRemoteConfigValue(forKey: Constants.jedi)
  44. }
  45. }
  46. func testFetchThenActivate() {
  47. let expectation = self.expectation(description: #function)
  48. config.fetch { status, error in
  49. if let error = error {
  50. XCTFail("Fetch Error \(error)")
  51. }
  52. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  53. self.config.activate { _, error in
  54. XCTAssertNil(error)
  55. XCTAssertEqual(self.config[Constants.key1].stringValue, Constants.value1)
  56. expectation.fulfill()
  57. }
  58. }
  59. waitForExpectations()
  60. }
  61. func testFetchWithExpirationThenActivate() {
  62. let expectation = self.expectation(description: #function)
  63. config.fetch(withExpirationDuration: 0) { status, error in
  64. if let error = error {
  65. XCTFail("Fetch Error \(error)")
  66. }
  67. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  68. self.config.activate { _, error in
  69. XCTAssertNil(error)
  70. XCTAssertEqual(self.config[Constants.key1].stringValue, Constants.value1)
  71. expectation.fulfill()
  72. }
  73. }
  74. waitForExpectations()
  75. }
  76. func testFetchAndActivate() {
  77. let expectation = self.expectation(description: #function)
  78. config.fetchAndActivate { status, error in
  79. XCTAssertEqual(status, .successFetchedFromRemote)
  80. if let error = error {
  81. XCTFail("Fetch and Activate Error \(error)")
  82. }
  83. XCTAssertEqual(self.config[Constants.key1].stringValue, Constants.value1)
  84. expectation.fulfill()
  85. }
  86. waitForExpectations()
  87. }
  88. // Test New API.
  89. // Contrast with testChangedActivateWillNotFlag in FakeConsole.swift.
  90. func testUnchangedActivateWillFlag() {
  91. let expectation = self.expectation(description: #function)
  92. config.fetch { status, error in
  93. if let error = error {
  94. XCTFail("Fetch Error \(error)")
  95. }
  96. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  97. self.config.activate { changed, error in
  98. XCTAssertTrue(!APITests.useFakeConfig || changed)
  99. XCTAssertNil(error)
  100. XCTAssertEqual(self.config[Constants.key1].stringValue, Constants.value1)
  101. expectation.fulfill()
  102. }
  103. }
  104. waitForExpectations()
  105. let expectation2 = self.expectation(description: #function + "2")
  106. config.fetch { status, error in
  107. if let error = error {
  108. XCTFail("Fetch Error \(error)")
  109. }
  110. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  111. self.config.activate { changed, error in
  112. XCTAssertFalse(changed)
  113. XCTAssertNil(error)
  114. XCTAssertEqual(self.config[Constants.key1].stringValue, Constants.value1)
  115. expectation2.fulfill()
  116. }
  117. }
  118. waitForExpectations()
  119. }
  120. func testFetchAndActivateUnchangedConfig() throws {
  121. guard APITests.useFakeConfig == false else { return }
  122. let expectation = self.expectation(description: #function)
  123. XCTAssertEqual(config.settings.minimumFetchInterval, 0)
  124. let serialQueue = DispatchQueue(label: "\(#function)Queue")
  125. let group = DispatchGroup()
  126. group.enter()
  127. serialQueue.async {
  128. // Represents pre-fetch occurring sometime in past.
  129. self.config.fetch { status, error in
  130. XCTAssertNil(error, "Fetch Error \(error!)")
  131. XCTAssertEqual(status, .success)
  132. group.leave()
  133. }
  134. }
  135. serialQueue.async {
  136. group.wait()
  137. group.enter()
  138. // Represents a `fetchAndActivate` being made to pull latest changes from Remote Config.
  139. self.config.fetchAndActivate { status, error in
  140. XCTAssertNil(error, "Fetch & Activate Error \(error!)")
  141. // Since no updates to remote config have occurred we use the `.successUsingPreFetchedData`.
  142. // The behavior of the next test changed in Firebase 7.0.0.
  143. // It's an open question which is correct, but it should only
  144. // be changed in a major release.
  145. // See https://github.com/firebase/firebase-ios-sdk/pull/8788
  146. // XCTAssertEqual(status, .successUsingPreFetchedData)
  147. XCTAssertEqual(status, .successFetchedFromRemote)
  148. // The `lastETagUpdateTime` should either be older or the same time as `lastFetchTime`.
  149. if let lastFetchTime = try? XCTUnwrap(self.config.lastFetchTime) {
  150. XCTAssertLessThanOrEqual(Double(self.config.settings.lastETagUpdateTime),
  151. Double(lastFetchTime.timeIntervalSince1970))
  152. } else {
  153. XCTFail("Could not unwrap lastFetchTime.")
  154. }
  155. expectation.fulfill()
  156. }
  157. }
  158. waitForExpectations()
  159. }
  160. // MARK: - RemoteConfigConsole Tests
  161. func testFetchConfigThenUpdateConsoleThenFetchAgain() {
  162. guard APITests.useFakeConfig == false else { return }
  163. let expectation = self.expectation(description: #function)
  164. config.fetchAndActivate { status, error in
  165. XCTAssertNil(error, "Fetch & Activate Error \(error!)")
  166. if let configValue = self.config.configValue(forKey: Constants.jedi).stringValue {
  167. XCTAssertEqual(configValue, Constants.obiwan)
  168. } else {
  169. XCTFail("Could not unwrap config value for key: \(Constants.jedi)")
  170. }
  171. expectation.fulfill()
  172. }
  173. waitForExpectations()
  174. // Synchronously update the console.
  175. console.updateRemoteConfigValue(Constants.yoda, forKey: Constants.jedi)
  176. let expectation2 = self.expectation(description: #function + "2")
  177. config.fetchAndActivate { status, error in
  178. XCTAssertNil(error, "Fetch & Activate Error \(error!)")
  179. if let configValue = self.config.configValue(forKey: Constants.jedi).stringValue {
  180. XCTAssertEqual(configValue, Constants.yoda)
  181. } else {
  182. XCTFail("Could not unwrap config value for key: \(Constants.jedi)")
  183. }
  184. expectation2.fulfill()
  185. }
  186. waitForExpectations()
  187. }
  188. func testFetchConfigThenAddValueOnConsoleThenFetchAgain() {
  189. guard APITests.useFakeConfig == false else { return }
  190. // Ensure no Sith Lord has been written to Remote Config yet.
  191. let expectation = self.expectation(description: #function)
  192. config.fetchAndActivate { status, error in
  193. XCTAssertNil(error, "Fetch & Activate Error \(error!)")
  194. XCTAssertTrue(self.config.configValue(forKey: Constants.sith).dataValue.isEmpty)
  195. expectation.fulfill()
  196. }
  197. waitForExpectations()
  198. // Synchronously update the console
  199. console.updateRemoteConfigValue(Constants.darthSidious, forKey: Constants.sith)
  200. // Verify the Sith Lord can now be fetched from Remote Config.
  201. let expectation2 = self.expectation(description: #function + "2")
  202. config.fetchAndActivate { status, error in
  203. XCTAssertNil(error, "Fetch & Activate Error \(error!)")
  204. if let configValue = self.config.configValue(forKey: Constants.sith).stringValue {
  205. XCTAssertEqual(configValue, Constants.darthSidious)
  206. } else {
  207. XCTFail("Could not unwrap config value for key: \(Constants.sith)")
  208. }
  209. expectation2.fulfill()
  210. }
  211. waitForExpectations()
  212. }
  213. func testFetchConfigThenDeleteValueOnConsoleThenFetchAgain() {
  214. guard APITests.useFakeConfig == false else { return }
  215. let expectation = self.expectation(description: #function)
  216. config.fetchAndActivate { status, error in
  217. XCTAssertNil(error, "Fetch & Activate Error \(error!)")
  218. if let configValue = self.config.configValue(forKey: Constants.jedi).stringValue {
  219. XCTAssertEqual(configValue, Constants.obiwan)
  220. } else {
  221. XCTFail("Could not unwrap config value for key: \(Constants.jedi)")
  222. }
  223. expectation.fulfill()
  224. }
  225. waitForExpectations()
  226. // Synchronously delete value on the console.
  227. console.removeRemoteConfigValue(forKey: Constants.jedi)
  228. let expectation2 = self.expectation(description: #function + "2")
  229. config.fetchAndActivate { status, error in
  230. XCTAssertNil(error, "Fetch & Activate Error \(error!)")
  231. XCTAssertTrue(self.config.configValue(forKey: Constants.jedi).dataValue.isEmpty,
  232. "Remote config should have been deleted.")
  233. expectation2.fulfill()
  234. }
  235. waitForExpectations()
  236. }
  237. // MARK: - Private Helpers
  238. private func waitForExpectations() {
  239. let kTestTimeout = 10.0
  240. waitForExpectations(timeout: kTestTimeout,
  241. handler: { (error) -> Void in
  242. if let error = error {
  243. print(error)
  244. }
  245. })
  246. }
  247. }