APITests.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. if let error = error {
  80. XCTFail("Fetch and Activate Error \(error)")
  81. }
  82. XCTAssertEqual(self.config[Constants.key1].stringValue, Constants.value1)
  83. expectation.fulfill()
  84. }
  85. waitForExpectations()
  86. }
  87. // Test New API.
  88. // Contrast with testChangedActivateWillNotFlag in FakeConsole.swift.
  89. func testUnchangedActivateWillFlag() {
  90. let expectation = self.expectation(description: #function)
  91. config.fetch { status, error in
  92. if let error = error {
  93. XCTFail("Fetch Error \(error)")
  94. }
  95. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  96. self.config.activate { changed, error in
  97. XCTAssertTrue(!APITests.useFakeConfig || changed)
  98. XCTAssertNil(error)
  99. XCTAssertEqual(self.config[Constants.key1].stringValue, Constants.value1)
  100. expectation.fulfill()
  101. }
  102. }
  103. waitForExpectations()
  104. let expectation2 = self.expectation(description: #function + "2")
  105. config.fetch { status, error in
  106. if let error = error {
  107. XCTFail("Fetch Error \(error)")
  108. }
  109. XCTAssertEqual(status, RemoteConfigFetchStatus.success)
  110. self.config.activate { changed, error in
  111. XCTAssertFalse(changed)
  112. XCTAssertNil(error)
  113. XCTAssertEqual(self.config[Constants.key1].stringValue, Constants.value1)
  114. expectation2.fulfill()
  115. }
  116. }
  117. waitForExpectations()
  118. }
  119. func testFetchAndActivateUnchangedConfig() throws {
  120. guard APITests.useFakeConfig == false else { return }
  121. let expectation = self.expectation(description: #function)
  122. XCTAssertEqual(config.settings.minimumFetchInterval, 0)
  123. let serialQueue = DispatchQueue(label: "\(#function)Queue")
  124. let group = DispatchGroup()
  125. group.enter()
  126. serialQueue.async {
  127. // Represents pre-fetch occurring sometime in past.
  128. self.config.fetch { status, error in
  129. XCTAssertNil(error, "Fetch Error \(error!)")
  130. XCTAssertEqual(status, .success)
  131. group.leave()
  132. }
  133. }
  134. serialQueue.async {
  135. group.wait()
  136. group.enter()
  137. // Represents a `fetchAndActivate` being made to pull latest changes from Remote Config.
  138. self.config.fetchAndActivate { status, error in
  139. XCTAssertNil(error, "Fetch & Activate Error \(error!)")
  140. // Since no updates to remote config have occurred we use the `.successUsingPreFetchedData`.
  141. XCTAssertEqual(status, .successUsingPreFetchedData)
  142. // The `lastETagUpdateTime` should either be older or the same time as `lastFetchTime`.
  143. if let lastFetchTime = try? XCTUnwrap(self.config.lastFetchTime) {
  144. XCTAssertLessThanOrEqual(Double(self.config.settings.lastETagUpdateTime),
  145. Double(lastFetchTime.timeIntervalSince1970))
  146. } else {
  147. XCTFail("Could not unwrap lastFetchTime.")
  148. }
  149. expectation.fulfill()
  150. }
  151. }
  152. waitForExpectations()
  153. }
  154. // MARK: - RemoteConfigConsole Tests
  155. func testFetchConfigThenUpdateConsoleThenFetchAgain() {
  156. guard APITests.useFakeConfig == false else { return }
  157. let expectation = self.expectation(description: #function)
  158. config.fetchAndActivate { status, error in
  159. XCTAssertNil(error, "Fetch & Activate Error \(error!)")
  160. if let configValue = self.config.configValue(forKey: Constants.jedi).stringValue {
  161. XCTAssertEqual(configValue, Constants.obiwan)
  162. } else {
  163. XCTFail("Could not unwrap config value for key: \(Constants.jedi)")
  164. }
  165. expectation.fulfill()
  166. }
  167. waitForExpectations()
  168. // Synchronously update the console.
  169. console.updateRemoteConfigValue(Constants.yoda, forKey: Constants.jedi)
  170. let expectation2 = self.expectation(description: #function + "2")
  171. config.fetchAndActivate { status, error in
  172. XCTAssertNil(error, "Fetch & Activate Error \(error!)")
  173. if let configValue = self.config.configValue(forKey: Constants.jedi).stringValue {
  174. XCTAssertEqual(configValue, Constants.yoda)
  175. } else {
  176. XCTFail("Could not unwrap config value for key: \(Constants.jedi)")
  177. }
  178. expectation2.fulfill()
  179. }
  180. waitForExpectations()
  181. }
  182. func testFetchConfigThenAddValueOnConsoleThenFetchAgain() {
  183. guard APITests.useFakeConfig == false else { return }
  184. // Ensure no Sith Lord has been written to Remote Config yet.
  185. let expectation = self.expectation(description: #function)
  186. config.fetchAndActivate { status, error in
  187. XCTAssertNil(error, "Fetch & Activate Error \(error!)")
  188. XCTAssertTrue(self.config.configValue(forKey: Constants.sith).dataValue.isEmpty)
  189. expectation.fulfill()
  190. }
  191. waitForExpectations()
  192. // Synchronously update the console
  193. console.updateRemoteConfigValue(Constants.darthSidious, forKey: Constants.sith)
  194. // Verify the Sith Lord can now be fetched from Remote Config.
  195. let expectation2 = self.expectation(description: #function + "2")
  196. config.fetchAndActivate { status, error in
  197. XCTAssertNil(error, "Fetch & Activate Error \(error!)")
  198. if let configValue = self.config.configValue(forKey: Constants.sith).stringValue {
  199. XCTAssertEqual(configValue, Constants.darthSidious)
  200. } else {
  201. XCTFail("Could not unwrap config value for key: \(Constants.sith)")
  202. }
  203. expectation2.fulfill()
  204. }
  205. waitForExpectations()
  206. }
  207. func testFetchConfigThenDeleteValueOnConsoleThenFetchAgain() {
  208. guard APITests.useFakeConfig == false else { return }
  209. let expectation = self.expectation(description: #function)
  210. config.fetchAndActivate { status, error in
  211. XCTAssertNil(error, "Fetch & Activate Error \(error!)")
  212. if let configValue = self.config.configValue(forKey: Constants.jedi).stringValue {
  213. XCTAssertEqual(configValue, Constants.obiwan)
  214. } else {
  215. XCTFail("Could not unwrap config value for key: \(Constants.jedi)")
  216. }
  217. expectation.fulfill()
  218. }
  219. waitForExpectations()
  220. // Synchronously delete value on the console.
  221. console.removeRemoteConfigValue(forKey: Constants.jedi)
  222. let expectation2 = self.expectation(description: #function + "2")
  223. config.fetchAndActivate { status, error in
  224. XCTAssertNil(error, "Fetch & Activate Error \(error!)")
  225. XCTAssertTrue(self.config.configValue(forKey: Constants.jedi).dataValue.isEmpty,
  226. "Remote config should have been deleted.")
  227. expectation2.fulfill()
  228. }
  229. waitForExpectations()
  230. }
  231. // MARK: - Private Helpers
  232. private func waitForExpectations() {
  233. let kFIRStorageIntegrationTestTimeout = 10.0
  234. waitForExpectations(timeout: kFIRStorageIntegrationTestTimeout,
  235. handler: { (error) -> Void in
  236. if let error = error {
  237. print(error)
  238. }
  239. })
  240. }
  241. }