APITests.swift 11 KB

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