GoogleAuthTests.swift 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2019 Google
  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. */
  16. import XCTest
  17. class GoogleAuthTestsSwift: FIRAuthApiTestsBase {
  18. let kGoogleCliendId = KGOOGLE_CLIENT_ID
  19. let kGoogleTestAccountName = KGOOGLE_USER_NAME
  20. let kGoogleTestAccountRefreshToken = KGOOGLE_TEST_ACCOUNT_REFRESH_TOKEN
  21. func testSignInWithGoogle() {
  22. let auth = Auth.auth()
  23. let userInfoDictOptional = getGoogleAccessToken()
  24. if userInfoDictOptional == nil {
  25. XCTFail("Could not obtain Google access token.")
  26. }
  27. let userInfoDict = userInfoDictOptional!
  28. let googleAccessToken = userInfoDict["access_token"] as! String
  29. let googleIdToken = userInfoDict["id_token"] as! String
  30. let credential = GoogleAuthProvider.credential(withIDToken: googleIdToken, accessToken: googleAccessToken)
  31. let expectation = self.expectation(description: "Signing in with Google finished.")
  32. auth.signIn(with: credential) { _, error in
  33. if error != nil {
  34. print("Signing in with Google had error: %@", error!)
  35. }
  36. expectation.fulfill()
  37. }
  38. waitForExpectations(timeout: kExpectationsTimeout) { error in
  39. if error != nil {
  40. XCTFail(String(format: "Failed to wait for expectations in Signing in with Google. Error: %@", error!.localizedDescription))
  41. }
  42. }
  43. XCTAssertEqual(auth.currentUser?.displayName, kGoogleTestAccountName)
  44. deleteCurrentUser()
  45. }
  46. func getGoogleAccessToken() -> [String: Any]? {
  47. let googleOauth2TokenServerUrl = "https://www.googleapis.com/oauth2/v4/token"
  48. let bodyString = String(format: "client_id=%@&grant_type=refresh_token&refresh_token=%@", kGoogleCliendId, kGoogleTestAccountRefreshToken)
  49. let postData = bodyString.data(using: .utf8)
  50. let service = GTMSessionFetcherService()
  51. let fetcher = service.fetcher(withURLString: googleOauth2TokenServerUrl)
  52. fetcher.bodyData = postData
  53. fetcher.setRequestValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
  54. let expectation = self.expectation(description: "Exchanging Google account tokens finished.")
  55. var data: Data?
  56. fetcher.beginFetch { receivedData, error in
  57. if error != nil {
  58. print("Exchanging Google account tokens finished with error: %@", error!)
  59. return
  60. }
  61. data = receivedData
  62. expectation.fulfill()
  63. }
  64. waitForExpectations(timeout: kExpectationsTimeout) { error in
  65. if error != nil {
  66. XCTFail(String(format: "Failed to wait for expectations in exchanging Google account tokens. Error: %@", error!.localizedDescription))
  67. }
  68. }
  69. let userInfo = String(data: data!, encoding: .utf8)
  70. print("The info of exchanged result is: \(userInfo ?? "<userInfo>")")
  71. let rawJsonObject = try? JSONSerialization.jsonObject(with: data!, options: [])
  72. if let userInfoDict = rawJsonObject as? [String: Any] {
  73. return userInfoDict
  74. } else {
  75. return nil
  76. }
  77. }
  78. }