GoogleAuthTests.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/XCTest.h>
  17. #import "FIRAuthApiTestsBase.h"
  18. static NSString *kGoogleCliendId = KGOOGLE_CLIENT_ID;
  19. static NSString *const kGoogleTestAccountName = KGOOGLE_USER_NAME;
  20. /** Refresh token of Google test account to exchange for access token. Refresh token never expires
  21. * unless user revokes it. If this refresh token expires, tests in record mode will fail and this
  22. * token needs to be updated.
  23. */
  24. NSString *kGoogleTestAccountRefreshToken = KGOOGLE_TEST_ACCOUNT_REFRESH_TOKEN;
  25. @interface GoogleAuthTests : FIRAuthApiTestsBase
  26. @end
  27. @implementation GoogleAuthTests
  28. - (void)testSignInWithGoogle {
  29. FIRAuth *auth = [FIRAuth auth];
  30. if (!auth) {
  31. XCTFail(@"Could not obtain auth object.");
  32. }
  33. NSDictionary *userInfoDict = [self getGoogleAccessToken];
  34. NSString *googleAccessToken = userInfoDict[@"access_token"];
  35. NSString *googleIdToken = userInfoDict[@"id_token"];
  36. FIRAuthCredential *credential = [FIRGoogleAuthProvider credentialWithIDToken:googleIdToken
  37. accessToken:googleAccessToken];
  38. XCTestExpectation *expectation =
  39. [self expectationWithDescription:@"Signing in with Google finished."];
  40. [auth signInWithCredential:credential
  41. completion:^(FIRAuthDataResult *result, NSError *error) {
  42. if (error) {
  43. NSLog(@"Signing in with Google had error: %@", error);
  44. }
  45. [expectation fulfill];
  46. }];
  47. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  48. handler:^(NSError *error) {
  49. if (error != nil) {
  50. XCTFail(@"Failed to wait for expectations "
  51. @"in Signing in with Google. Error: %@",
  52. error.localizedDescription);
  53. }
  54. }];
  55. XCTAssertEqualObjects(auth.currentUser.displayName, kGoogleTestAccountName);
  56. // Clean up the created Firebase/Facebook user for future runs.
  57. [self deleteCurrentUser];
  58. }
  59. /** Sends http request to Google OAuth2 token server to use refresh token to exchange for Google
  60. * access token. Returns a dictionary that constains "access_token", "token_type", "expires_in" and
  61. * "id_token".
  62. */
  63. - (NSDictionary *)getGoogleAccessToken {
  64. NSString *googleOauth2TokenServerUrl = @"https://www.googleapis.com/oauth2/v4/token";
  65. NSString *bodyString =
  66. [NSString stringWithFormat:@"client_id=%@&grant_type=refresh_token&refresh_token=%@",
  67. kGoogleCliendId, kGoogleTestAccountRefreshToken];
  68. NSData *postData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
  69. GTMSessionFetcherService *service = [[GTMSessionFetcherService alloc] init];
  70. GTMSessionFetcher *fetcher = [service fetcherWithURLString:googleOauth2TokenServerUrl];
  71. fetcher.bodyData = postData;
  72. [fetcher setRequestValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  73. XCTestExpectation *expectation =
  74. [self expectationWithDescription:@"Exchanging Google account tokens finished."];
  75. __block NSData *data = nil;
  76. [fetcher beginFetchWithCompletionHandler:^(NSData *receivedData, NSError *error) {
  77. if (error) {
  78. NSLog(@"Exchanging Google account tokens finished with error: %@", error);
  79. return;
  80. }
  81. data = receivedData;
  82. [expectation fulfill];
  83. }];
  84. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  85. handler:^(NSError *error) {
  86. if (error != nil) {
  87. XCTFail(@"Failed to wait for expectations "
  88. @"in exchanging Google account tokens. Error: %@",
  89. error.localizedDescription);
  90. }
  91. }];
  92. NSString *userInfo = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  93. NSLog(@"The info of exchanged result is: %@", userInfo);
  94. NSDictionary *userInfoDict = [NSJSONSerialization JSONObjectWithData:data
  95. options:kNilOptions
  96. error:nil];
  97. return userInfoDict;
  98. }
  99. @end