ContextProviderTests.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2022 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. import FirebaseCore
  16. @testable import FirebaseFunctions
  17. import FirebaseAppCheckInterop
  18. import FirebaseAuthInterop
  19. import FirebaseMessagingInterop
  20. import SharedTestUtilities
  21. import XCTest
  22. class ContextProviderTests: XCTestCase {
  23. let appCheckFake = FIRAppCheckFake()
  24. let appCheckTokenError = FIRAppCheckTokenResultFake(token: "dummy token",
  25. error: NSError(
  26. domain: "testAppCheckError",
  27. code: -1,
  28. userInfo: nil
  29. ))
  30. let appCheckTokenSuccess = FIRAppCheckTokenResultFake(token: "valid_token", error: nil)
  31. let messagingFake = FIRMessagingInteropFake()
  32. func testContextWithAuth() {
  33. let auth = FIRAuthInteropFake(token: "token", userID: "userID", error: nil)
  34. let provider = FunctionsContextProvider(auth: auth, messaging: messagingFake, appCheck: nil)
  35. let expectation = expectation(description: "Context should have auth keys.")
  36. provider.getContext { context, error in
  37. XCTAssertNotNil(context)
  38. XCTAssertEqual(context.authToken, "token")
  39. XCTAssertEqual(context.fcmToken, self.messagingFake.fcmToken)
  40. XCTAssertNil(error)
  41. expectation.fulfill()
  42. }
  43. waitForExpectations(timeout: 0.1)
  44. }
  45. func testContextWithAuthError() {
  46. let authError = NSError(domain: "com.functions.tests", code: 4, userInfo: nil)
  47. let auth = FIRAuthInteropFake(token: nil, userID: "userID", error: authError)
  48. let provider = FunctionsContextProvider(auth: auth, messaging: messagingFake, appCheck: nil)
  49. let expectation = expectation(description: "Completion handler should fail with Auth error.")
  50. provider.getContext { context, error in
  51. XCTAssertNotNil(context)
  52. XCTAssertNil(context.authToken)
  53. XCTAssertEqual(error as NSError?, authError)
  54. expectation.fulfill()
  55. }
  56. waitForExpectations(timeout: 0.1)
  57. }
  58. func testContextWithoutAuth() {
  59. let provider = FunctionsContextProvider(auth: nil, messaging: nil, appCheck: nil)
  60. let expectation = expectation(description: "Completion handler should succeed without Auth.")
  61. provider.getContext { context, error in
  62. XCTAssertNotNil(context)
  63. XCTAssertNil(error)
  64. XCTAssertNil(context.authToken)
  65. XCTAssertNil(context.fcmToken)
  66. expectation.fulfill()
  67. }
  68. waitForExpectations(timeout: 0.1)
  69. }
  70. func testContextWithAppCheckOnlySuccess() {
  71. appCheckFake.tokenResult = appCheckTokenSuccess
  72. let provider = FunctionsContextProvider(auth: nil, messaging: nil, appCheck: appCheckFake)
  73. let expectation = expectation(description: "Verify app check.")
  74. provider.getContext { context, error in
  75. XCTAssertNotNil(context)
  76. XCTAssertNil(error)
  77. XCTAssertNil(context.authToken)
  78. XCTAssertNil(context.fcmToken)
  79. XCTAssertEqual(context.appCheckToken, self.appCheckTokenSuccess.token)
  80. expectation.fulfill()
  81. }
  82. waitForExpectations(timeout: 0.1)
  83. }
  84. func testContextWithAppCheckOnlyError() {
  85. appCheckFake.tokenResult = appCheckTokenError
  86. let provider = FunctionsContextProvider(auth: nil, messaging: nil, appCheck: appCheckFake)
  87. let expectation = expectation(description: "Verify bad app check token")
  88. provider.getContext { context, error in
  89. XCTAssertNotNil(context)
  90. XCTAssertNil(error)
  91. XCTAssertNil(context.authToken)
  92. XCTAssertNil(context.fcmToken)
  93. // Don't expect any token in the case of App Check error.
  94. XCTAssertNil(context.appCheckToken)
  95. expectation.fulfill()
  96. }
  97. waitForExpectations(timeout: 0.1)
  98. }
  99. func testAllContextsAvailableSuccess() {
  100. appCheckFake.tokenResult = appCheckTokenSuccess
  101. let auth = FIRAuthInteropFake(token: "token", userID: "userID", error: nil)
  102. let provider = FunctionsContextProvider(
  103. auth: auth,
  104. messaging: messagingFake,
  105. appCheck: appCheckFake
  106. )
  107. let expectation = expectation(description: "All contexts available")
  108. provider.getContext { context, error in
  109. XCTAssertNotNil(context)
  110. XCTAssertNil(error)
  111. XCTAssertEqual(context.authToken, "token")
  112. XCTAssertEqual(context.fcmToken, self.messagingFake.fcmToken)
  113. XCTAssertEqual(context.appCheckToken, self.appCheckTokenSuccess.token)
  114. expectation.fulfill()
  115. }
  116. waitForExpectations(timeout: 0.1)
  117. }
  118. func testAllContextsAuthAndAppCheckError() {
  119. appCheckFake.tokenResult = appCheckTokenError
  120. let authError = NSError(domain: "com.functions.tests", code: 4, userInfo: nil)
  121. let auth = FIRAuthInteropFake(token: nil, userID: "userID", error: authError)
  122. let provider = FunctionsContextProvider(
  123. auth: auth,
  124. messaging: messagingFake,
  125. appCheck: appCheckFake
  126. )
  127. let expectation = expectation(description: "All contexts with errors")
  128. provider.getContext { context, error in
  129. XCTAssertNotNil(context)
  130. XCTAssertEqual(error as NSError?, authError)
  131. XCTAssertNil(context.authToken)
  132. XCTAssertEqual(context.fcmToken, self.messagingFake.fcmToken)
  133. // Don't expect any token in the case of App Check error.
  134. XCTAssertNil(context.appCheckToken)
  135. expectation.fulfill()
  136. }
  137. waitForExpectations(timeout: 0.1)
  138. }
  139. }