SettingsTests.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // Copyright 2022 Google LLC
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. import XCTest
  16. @testable import FirebaseSessions
  17. class SettingsTests: XCTestCase {
  18. // 2021-11-01 @ 00:00:00 (EST)
  19. let date = Date(timeIntervalSince1970: 1_635_739_200)
  20. let validSettings: [String: Any] = [
  21. "cache_duration": 10,
  22. "sessions_enabled": false,
  23. "sampling_rate": 0.5,
  24. "session_timeout": 10,
  25. ]
  26. let corruptedJSONString: String = "{{{{ non_key: non\"value {}"
  27. var cache: SettingsCacheClient!
  28. var settings: Settings!
  29. var appInfo: MockApplicationInfo!
  30. override func setUp() {
  31. appInfo = MockApplicationInfo()
  32. cache = SettingsCache()
  33. cache.removeCache()
  34. settings = Settings(cache: cache, appInfo: appInfo)
  35. }
  36. func test_noCacheSaved_returnsDefaultSettings() {
  37. XCTAssertTrue(settings.isCacheExpired(currentTime: Date.distantFuture))
  38. XCTAssertTrue(settings.sessionsEnabled)
  39. XCTAssertEqual(settings.samplingRate, 1)
  40. XCTAssertEqual(settings.sessionTimeout, 30 * 60)
  41. }
  42. func test_activatedCache_returnsCachedSettings() {
  43. appInfo.mockAllInfo()
  44. let cacheKey = CacheKey(
  45. createdAt: date,
  46. googleAppID: appInfo.appID,
  47. appVersion: appInfo.synthesizedVersion
  48. )
  49. write(settings: validSettings)
  50. write(cacheKey: cacheKey)
  51. // time passed = 5, TTL = 10, time passed < TTL
  52. let now = date.addingTimeInterval(5)
  53. XCTAssertFalse(settings.isCacheExpired(currentTime: now))
  54. // Should be same as self.validSettings
  55. XCTAssertFalse(settings.sessionsEnabled)
  56. XCTAssertEqual(settings.samplingRate, 0.5)
  57. XCTAssertEqual(settings.sessionTimeout, 10)
  58. }
  59. func test_cacheKeyExpiredFromAppVersion_marksCacheAsExpired() {
  60. appInfo.mockAllInfo()
  61. let cacheKey = CacheKey(
  62. createdAt: date,
  63. googleAppID: appInfo.appID,
  64. appVersion: appInfo.synthesizedVersion
  65. )
  66. write(settings: validSettings)
  67. write(cacheKey: cacheKey)
  68. // time passed = 5, TTL = 10, time passed < TTL
  69. let now = date.addingTimeInterval(5)
  70. appInfo.appBuildVersion = "testNewAppBuildVersion"
  71. appInfo.appDisplayVersion = "testNewAppDisplayVersion"
  72. XCTAssertTrue(settings.isCacheExpired(currentTime: now)) // requires refetch
  73. // However, still provide already cached settings
  74. XCTAssertFalse(settings.sessionsEnabled)
  75. XCTAssertEqual(settings.samplingRate, 0.5)
  76. XCTAssertEqual(settings.sessionTimeout, 10)
  77. }
  78. func test_cacheKeyExpiredFromTTL_marksCacheAsExpired() {
  79. appInfo.mockAllInfo()
  80. let cacheKey = CacheKey(
  81. createdAt: date,
  82. googleAppID: appInfo.appID,
  83. appVersion: appInfo.synthesizedVersion
  84. )
  85. write(settings: validSettings)
  86. write(cacheKey: cacheKey)
  87. // time passed = 11, TTL = 10, tim passed > TTL
  88. let now = date.addingTimeInterval(11)
  89. XCTAssertTrue(settings.isCacheExpired(currentTime: now)) // requires refetch
  90. // However, still provide already cached settings
  91. XCTAssertFalse(settings.sessionsEnabled)
  92. XCTAssertEqual(settings.samplingRate, 0.5)
  93. XCTAssertEqual(settings.sessionTimeout, 10)
  94. }
  95. func test_cacheKeyGoogleAppIDChanged_returnsDefaultSettings() {
  96. appInfo.mockAllInfo()
  97. let cacheKey = CacheKey(
  98. createdAt: date,
  99. googleAppID: appInfo.appID,
  100. appVersion: appInfo.synthesizedVersion
  101. )
  102. write(settings: validSettings)
  103. write(cacheKey: cacheKey)
  104. // change appID
  105. appInfo.appID = "testDifferentGoogleAppID"
  106. // time passed = 5, TTL = 10, time passed < TTL
  107. let now = date.addingTimeInterval(5)
  108. XCTAssertTrue(settings.isCacheExpired(currentTime: now)) // requires refetch
  109. // provide default settings
  110. XCTAssertTrue(settings.sessionsEnabled)
  111. XCTAssertEqual(settings.samplingRate, 1)
  112. XCTAssertEqual(settings.sessionTimeout, 30 * 60)
  113. }
  114. func test_corruptedCache_returnsDefaultSettings() {
  115. // First write and load a valid settings file
  116. appInfo.mockAllInfo()
  117. let cacheKey = CacheKey(
  118. createdAt: date,
  119. googleAppID: appInfo.appID,
  120. appVersion: appInfo.synthesizedVersion
  121. )
  122. write(settings: validSettings)
  123. write(cacheKey: cacheKey)
  124. let now = date.addingTimeInterval(5)
  125. XCTAssertFalse(settings.isCacheExpired(currentTime: now))
  126. XCTAssertFalse(settings.sessionsEnabled)
  127. XCTAssertEqual(settings.samplingRate, 0.5)
  128. XCTAssertEqual(settings.sessionTimeout, 10)
  129. // Then write a corrupted one and reload it
  130. write(jsonString: corruptedJSONString)
  131. XCTAssertTrue(settings.isCacheExpired(currentTime: now))
  132. // should have default values
  133. XCTAssertTrue(settings.sessionsEnabled)
  134. XCTAssertEqual(settings.samplingRate, 1)
  135. XCTAssertEqual(settings.sessionTimeout, 30 * 60)
  136. }
  137. func test_corruptedCacheKey_returnsDefaultSettings() {
  138. // First write and load a valid settings file
  139. appInfo.mockAllInfo()
  140. let cacheKey = CacheKey(
  141. createdAt: date,
  142. googleAppID: appInfo.appID,
  143. appVersion: appInfo.synthesizedVersion
  144. )
  145. write(settings: validSettings)
  146. write(cacheKey: cacheKey)
  147. let now = date.addingTimeInterval(5)
  148. XCTAssertFalse(settings.isCacheExpired(currentTime: now))
  149. XCTAssertFalse(settings.sessionsEnabled)
  150. XCTAssertEqual(settings.samplingRate, 0.5)
  151. XCTAssertEqual(settings.sessionTimeout, 10)
  152. // Then write a corrupted one and reload it
  153. write(jsonString: corruptedJSONString, isCacheKey: true)
  154. XCTAssertTrue(settings.isCacheExpired(currentTime: now))
  155. // should have default values
  156. XCTAssertTrue(settings.sessionsEnabled)
  157. XCTAssertEqual(settings.samplingRate, 1)
  158. XCTAssertEqual(settings.sessionTimeout, 30 * 60)
  159. }
  160. // TODO: make Settings.CacheKey private again after implementing download and save
  161. func write(cacheKey: CacheKey) {
  162. do {
  163. try UserDefaults.standard.set(
  164. JSONEncoder().encode(cacheKey),
  165. forKey: "firebase-sessions-cache-key"
  166. )
  167. } catch {
  168. print("SettingsTests: \(error)")
  169. }
  170. }
  171. func write(settings: [String: Any]) {
  172. UserDefaults.standard.set(settings, forKey: "firebase-sessions-settings")
  173. }
  174. func write(jsonString: String, isCacheKey: Bool = true) {
  175. let name = isCacheKey ? "firebase-sessions-cache-key" : "firebase-sessions-settings"
  176. UserDefaults.standard.set(jsonString, forKey: name)
  177. }
  178. }