FakeInstallations.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2023 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 Foundation
  15. @testable import FirebaseInstallations
  16. @testable import FirebaseAppDistributionInternal
  17. // This is needed because importing the setter from FIRInstallationsAuthTokenResultInternal.h in
  18. // the bridging error results in errors.
  19. extension InstallationsAuthTokenResult {
  20. convenience init(token: String, expirationDate: Date) {
  21. self.init()
  22. // TODO(tejasd): If needed for tests, explore a way to set the get only properties.
  23. }
  24. }
  25. class FakeInstallations: InstallationsProtocol {
  26. let fakeErrorDomain = "test.failure.domain"
  27. let fakeInstallationID = "this-id-is-fake"
  28. let fakeAuthToken = "this-is-a-fake-auth-token"
  29. enum TestCase {
  30. case success
  31. case authTokenFailure
  32. case installationIDFailure
  33. }
  34. var testCase: TestCase
  35. required init(testCase: TestCase) {
  36. self.testCase = testCase
  37. }
  38. func authToken(completion: @escaping (InstallationsAuthTokenResult?, Error?) -> Void) {
  39. switch testCase {
  40. case .success:
  41. let authToken = InstallationsAuthTokenResult(token: fakeAuthToken, expirationDate: Date())
  42. completion(authToken, nil)
  43. case .authTokenFailure:
  44. let error = NSError(domain: fakeErrorDomain, code: 1)
  45. completion(nil, error)
  46. default:
  47. completion(nil, nil)
  48. }
  49. }
  50. func installationID(completion: @escaping (String?, Error?) -> Void) {
  51. switch testCase {
  52. case .success:
  53. completion(fakeInstallationID, nil)
  54. case .installationIDFailure:
  55. let error = NSError(domain: fakeErrorDomain, code: 1)
  56. completion(nil, error)
  57. default:
  58. completion(nil, nil)
  59. }
  60. }
  61. }