FIRAuthApiTestsBase.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/LICENSE2.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 "FIRAuthApiTestsBase.h"
  17. @implementation FIRAuthApiTestsBase
  18. - (void)setUp {
  19. [super setUp];
  20. [self signOut];
  21. }
  22. - (void)tearDown {
  23. [super tearDown];
  24. }
  25. - (void)signInAnonymously {
  26. FIRAuth *auth = [FIRAuth auth];
  27. if (!auth) {
  28. XCTFail(@"Could not obtain auth object.");
  29. }
  30. XCTestExpectation *expectation =
  31. [self expectationWithDescription:@"Anonymous sign-in finished."];
  32. [auth signInAnonymouslyWithCompletion:^(FIRAuthDataResult *result, NSError *error) {
  33. if (error) {
  34. NSLog(@"Anonymous sign in error: %@", error);
  35. }
  36. [expectation fulfill];
  37. }];
  38. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  39. handler:^(NSError *error) {
  40. if (error != nil) {
  41. XCTFail(@"Failed to wait for expectations "
  42. @"in anonymousy sign in. Error: %@",
  43. error.localizedDescription);
  44. }
  45. }];
  46. }
  47. - (void)signOut {
  48. NSError *signOutError;
  49. BOOL status = [[FIRAuth auth] signOut:&signOutError];
  50. // Just log the error because we don't want to fail the test if signing out
  51. // fails.
  52. if (!status) {
  53. NSLog(@"Error signing out: %@", signOutError);
  54. }
  55. }
  56. - (void)deleteCurrentUser {
  57. FIRAuth *auth = [FIRAuth auth];
  58. if (!auth) {
  59. NSLog(@"Could not obtain auth object.");
  60. }
  61. XCTestExpectation *expectation =
  62. [self expectationWithDescription:@"Delete current user finished."];
  63. [auth.currentUser deleteWithCompletion:^(NSError *_Nullable error) {
  64. if (error) {
  65. XCTFail(@"Failed to delete user. Error: %@.", error);
  66. }
  67. [expectation fulfill];
  68. }];
  69. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  70. handler:^(NSError *error) {
  71. if (error != nil) {
  72. XCTFail(@"Failed to wait for expectations "
  73. @"in deleting user. Error: %@",
  74. error.localizedDescription);
  75. }
  76. }];
  77. }
  78. - (NSString *)fakeRandomEmail {
  79. NSMutableString *fakeEmail = [[NSMutableString alloc] init];
  80. for (int i = 0; i < 10; i++) {
  81. [fakeEmail
  82. appendString:[NSString stringWithFormat:@"%c", 'a' + arc4random_uniform('z' - 'a' + 1)]];
  83. }
  84. [fakeEmail appendString:@"@gmail.com"];
  85. return fakeEmail;
  86. }
  87. @end