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