EmailPasswordAuthTests.m 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright 2017 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. @import FirebaseAuth;
  19. /** The testing email address for testCreateAccountWithEmailAndPassword. */
  20. static NSString *const kNewEmailToCreateUser = @"user+email_new_user@example.com";
  21. /** The testing email address for testSignInExistingUserWithEmailAndPassword. */
  22. static NSString *const kExistingEmailToSignIn = @"user+email_existing_user@example.com";
  23. /** The testing password for testSignInExistingUserWithEmailAndPassword. */
  24. static NSString *const kExistingPasswordToSignIn = @"password";
  25. @interface EmailPasswordAuthTests : FIRAuthApiTestsBase
  26. @end
  27. @implementation EmailPasswordAuthTests
  28. - (void)testCreateAccountWithEmailAndPassword {
  29. SKIP_IF_ON_MOBILE_HARNESS
  30. FIRAuth *auth = [FIRAuth auth];
  31. if (!auth) {
  32. XCTFail(@"Could not obtain auth object.");
  33. }
  34. XCTestExpectation *expectation =
  35. [self expectationWithDescription:@"Created account with email and password."];
  36. [auth createUserWithEmail:kNewEmailToCreateUser
  37. password:@"password"
  38. completion:^(FIRAuthDataResult *result, NSError *error) {
  39. if (error) {
  40. NSLog(@"createUserWithEmail has error: %@", error);
  41. }
  42. [expectation fulfill];
  43. }];
  44. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  45. handler:^(NSError *error) {
  46. if (error != nil) {
  47. XCTFail(@"Failed to wait for expectations "
  48. @"in creating account. Error: %@",
  49. error.localizedDescription);
  50. }
  51. }];
  52. XCTAssertEqualObjects(auth.currentUser.email, kNewEmailToCreateUser);
  53. [self deleteCurrentUser];
  54. }
  55. - (void)testSignInExistingUserWithEmailAndPassword {
  56. FIRAuth *auth = [FIRAuth auth];
  57. if (!auth) {
  58. XCTFail(@"Could not obtain auth object.");
  59. }
  60. XCTestExpectation *expectation =
  61. [self expectationWithDescription:@"Signed in existing account with email and password."];
  62. [auth signInWithEmail:kExistingEmailToSignIn
  63. password:kExistingPasswordToSignIn
  64. completion:^(FIRAuthDataResult *user, NSError *error) {
  65. if (error) {
  66. NSLog(@"Signing in existing account has error: %@", error);
  67. }
  68. [expectation fulfill];
  69. }];
  70. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  71. handler:^(NSError *error) {
  72. if (error != nil) {
  73. XCTFail(@"Failed to wait for expectations "
  74. @"in signing in existing account. Error: %@",
  75. error.localizedDescription);
  76. }
  77. }];
  78. XCTAssertEqualObjects(auth.currentUser.email, kExistingEmailToSignIn);
  79. }
  80. @end