ExchangeServiceModelTests.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 XCTest
  15. @testable import FirebaseAuthExchange
  16. final class ModelTests: XCTestCase {
  17. let jsonEncoder: JSONEncoder = {
  18. let encoder = JSONEncoder()
  19. encoder.keyEncodingStrategy = .convertToSnakeCase
  20. encoder.outputFormatting = [.prettyPrinted,
  21. // Ensure ordering is consistent for JSON string comparison
  22. .sortedKeys]
  23. return encoder
  24. }()
  25. let jsonDecoder = JSONDecoder()
  26. // MARK: - Request Model Encoding Tests
  27. func testEncodeExchangeInstallationAuthTokenRequest() throws {
  28. let installationAuthToken = "test-installation-token"
  29. let request = ExchangeInstallationAuthTokenRequest(
  30. installationAuthToken: installationAuthToken
  31. )
  32. let expectedJSON = """
  33. {
  34. "installation_auth_token" : "\(installationAuthToken)"
  35. }
  36. """
  37. let json = try encodeAsJSON(request)
  38. XCTAssertEqual(json, expectedJSON)
  39. }
  40. func testEncodeExchangeCustomTokenRequest() throws {
  41. let customToken = "test-custom-jwt"
  42. let request = ExchangeCustomTokenRequest(
  43. customToken: customToken
  44. )
  45. let expectedJSON = """
  46. {
  47. "custom_token" : "\(customToken)"
  48. }
  49. """
  50. let json = try encodeAsJSON(request)
  51. XCTAssertEqual(json, expectedJSON)
  52. }
  53. func testEncodeExchangeOidcTokenRequestWithImplicitCredentials() throws {
  54. let providerID = "test-provider-id"
  55. let idToken = "test-id-token"
  56. let credentials = ExchangeOIDCTokenRequest.ImplicitCredentials(idToken: idToken)
  57. let request = ExchangeOIDCTokenRequest(providerID: providerID, implicitCredentials: credentials)
  58. let expectedJSON = """
  59. {
  60. "implicit_credentials" : {
  61. "id_token" : "\(idToken)"
  62. },
  63. "provider_id" : "\(providerID)"
  64. }
  65. """
  66. let json = try encodeAsJSON(request)
  67. XCTAssertEqual(json, expectedJSON)
  68. }
  69. func testEncodeExchangeOidcTokenRequestWithAuthCodeCredentials() throws {
  70. let providerID = "test-provider-id"
  71. let sessionID = "test-session-id"
  72. let credentialURI = "test-credential-uri"
  73. let credentials = ExchangeOIDCTokenRequest.AuthCodeCredentials(
  74. sessionID: sessionID,
  75. credentialURI: credentialURI
  76. )
  77. let request = ExchangeOIDCTokenRequest(providerID: providerID, authCodeCredentials: credentials)
  78. let expectedJSON = """
  79. {
  80. "auth_code_credentials" : {
  81. "credential_uri" : "\(credentialURI)",
  82. "session_id" : "\(sessionID)"
  83. },
  84. "provider_id" : "\(providerID)"
  85. }
  86. """
  87. let json = try encodeAsJSON(request)
  88. XCTAssertEqual(json, expectedJSON)
  89. }
  90. private func encodeAsJSON(_ entity: Encodable) throws -> String {
  91. let data = try jsonEncoder.encode(entity)
  92. return String(data: data, encoding: .utf8)!
  93. }
  94. // MARK: - Response Model Decoding Tests
  95. func testDecodeExchangeTokenResponse() throws {
  96. let accessToken = "test-access-token"
  97. let timeToLiveSeconds: Int64 = 3
  98. let timeToLiveNanos: Int32 = 141_592_653
  99. let timeToLive = "\(timeToLiveSeconds).\(timeToLiveNanos)s"
  100. let responseJSON = """
  101. {
  102. "token": {
  103. "accessToken": "\(accessToken)",
  104. "ttl": "\(timeToLive)"
  105. }
  106. }
  107. """
  108. let expectedResponse = ExchangeTokenResponse(token: ExchangeTokenResponse.AuthExchangeToken(
  109. accessToken: accessToken,
  110. timeToLive: try ProtobufDuration(json: timeToLive)
  111. ))
  112. let response = try jsonDecoder.decode(ExchangeTokenResponse.self, from: responseJSON)
  113. XCTAssertEqual(response.token.timeToLive.seconds, timeToLiveSeconds)
  114. XCTAssertEqual(response.token.timeToLive.nanoseconds, timeToLiveNanos)
  115. XCTAssertEqual(response, expectedResponse)
  116. }
  117. }
  118. private extension JSONDecoder {
  119. func decode<T>(_ type: T.Type, from string: String) throws -> T where T: Decodable {
  120. let data: Data = string.data(using: .utf8)!
  121. return try decode(T.self, from: data)
  122. }
  123. }