GoogleAuthTests.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 *kGoogleClientID = 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. if (auth.currentUser.displayName) {
  56. XCTAssertEqualObjects(auth.currentUser.displayName, kGoogleTestAccountName);
  57. }
  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. * sometimes the "id_token". (The id_token is not guaranteed to be returned during a refresh
  62. * exchange; see https://openid.net/specs/openid-connect-core-1_0.html#RefreshTokenResponse)
  63. */
  64. - (NSDictionary *)getGoogleAccessToken {
  65. NSString *googleOauth2TokenServerUrl = @"https://www.googleapis.com/oauth2/v4/token";
  66. NSString *bodyString =
  67. [NSString stringWithFormat:@"client_id=%@&grant_type=refresh_token&refresh_token=%@",
  68. kGoogleClientID, kGoogleTestAccountRefreshToken];
  69. NSData *postData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
  70. GTMSessionFetcherService *service = [[GTMSessionFetcherService alloc] init];
  71. GTMSessionFetcher *fetcher = [service fetcherWithURLString:googleOauth2TokenServerUrl];
  72. fetcher.bodyData = postData;
  73. [fetcher setRequestValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  74. XCTestExpectation *expectation =
  75. [self expectationWithDescription:@"Exchanging Google account tokens finished."];
  76. __block NSData *data = nil;
  77. [fetcher beginFetchWithCompletionHandler:^(NSData *receivedData, NSError *error) {
  78. if (error) {
  79. NSLog(@"Exchanging Google account tokens finished with error: %@", error);
  80. return;
  81. }
  82. data = receivedData;
  83. [expectation fulfill];
  84. }];
  85. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  86. handler:^(NSError *error) {
  87. if (error != nil) {
  88. XCTFail(@"Failed to wait for expectations "
  89. @"in exchanging Google account tokens. Error: %@",
  90. error.localizedDescription);
  91. }
  92. }];
  93. NSString *userInfo = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  94. NSLog(@"The info of exchanged result is: %@", userInfo);
  95. NSDictionary *userInfoDict = [NSJSONSerialization JSONObjectWithData:data
  96. options:kNilOptions
  97. error:nil];
  98. return userInfoDict;
  99. }
  100. @end