FIRAppDistributionAuthPersistenceTests.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // Copyright 2020 Google LLC
  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. #import <OCMock/OCMock.h>
  16. #import <XCTest/XCTest.h>
  17. #import <AppAuth/AppAuth.h>
  18. #import <FirebaseAppDistribution/FIRAppDistributionAuthPersistence+Private.h>
  19. #import <GoogleUtilities/GULKeychainUtils.h>
  20. @interface FIRAppDistributionAuthPersistenceTests : XCTestCase
  21. @end
  22. @implementation FIRAppDistributionAuthPersistenceTests {
  23. NSMutableDictionary *_mockKeychainQuery;
  24. FIRAppDistributionAuthPersistence *_authPersistence;
  25. id _mockAuthorizationData;
  26. id _mockOIDAuthState;
  27. id _mockKeychainUtility;
  28. id _mockGoogleKeychainUtilities;
  29. id _partialMockAuthPersitence;
  30. }
  31. - (void)setUp {
  32. [super setUp];
  33. _mockKeychainQuery = [NSMutableDictionary
  34. dictionaryWithObjectsAndKeys:(id) @"thing one", (id) @"another thing", nil];
  35. _mockGoogleKeychainUtilities = OCMClassMock([GULKeychainUtils class]);
  36. _mockAuthorizationData = [@"this is some password stuff" dataUsingEncoding:NSUTF8StringEncoding];
  37. _mockOIDAuthState = OCMClassMock([OIDAuthState class]);
  38. _partialMockAuthPersitence = OCMClassMock([FIRAppDistributionAuthPersistence class]);
  39. _authPersistence = [[FIRAppDistributionAuthPersistence alloc] initWithAppId:@"test-app-id"];
  40. OCMStub(ClassMethod([_partialMockAuthPersitence unarchiveKeychainResult:[OCMArg any]]))
  41. .andReturn(_mockOIDAuthState);
  42. OCMStub(ClassMethod([_partialMockAuthPersitence archiveDataForKeychain:[OCMArg any]]))
  43. .andReturn(_mockAuthorizationData);
  44. }
  45. - (void)tearDown {
  46. [super tearDown];
  47. }
  48. - (void)testPersistAuthStateSuccess {
  49. OCMStub(ClassMethod([_mockGoogleKeychainUtilities setItem:[OCMArg any]
  50. withQuery:[OCMArg any]
  51. error:[OCMArg setTo:nil]]))
  52. .andReturn(YES);
  53. NSError *error;
  54. XCTAssertTrue([_authPersistence persistAuthState:_mockOIDAuthState error:&error]);
  55. XCTAssertNil(error);
  56. }
  57. - (void)testPersistAuthStateFailure {
  58. OCMStub(ClassMethod([_mockGoogleKeychainUtilities setItem:[OCMArg any]
  59. withQuery:[OCMArg any]
  60. error:[OCMArg setTo:nil]]))
  61. .andReturn(NO);
  62. NSError *error;
  63. XCTAssertFalse([_authPersistence persistAuthState:_mockOIDAuthState error:&error]);
  64. XCTAssertNotNil(error);
  65. XCTAssertEqual([error domain], kFIRAppDistributionAuthPersistenceErrorDomain);
  66. XCTAssertEqual([error code], FIRAppDistributionErrorTokenPersistenceFailure);
  67. }
  68. - (void)testRetrieveAuthStateSuccess {
  69. OCMStub(ClassMethod([_mockGoogleKeychainUtilities getItemWithQuery:[OCMArg any]
  70. error:[OCMArg setTo:nil]]))
  71. .andReturn(_mockAuthorizationData);
  72. NSError *error;
  73. XCTAssertTrue([[_authPersistence retrieveAuthState:&error] isKindOfClass:[OIDAuthState class]]);
  74. XCTAssertNil(error);
  75. }
  76. - (void)testRetrieveAuthStateFailure {
  77. OCMStub(ClassMethod([_mockGoogleKeychainUtilities getItemWithQuery:[OCMArg any]
  78. error:[OCMArg setTo:nil]]))
  79. .andReturn(nil);
  80. NSError *error;
  81. XCTAssertFalse([_authPersistence retrieveAuthState:&error]);
  82. XCTAssertNotNil(error);
  83. XCTAssertEqual([error domain], kFIRAppDistributionAuthPersistenceErrorDomain);
  84. XCTAssertEqual([error code], FIRAppDistributionErrorTokenRetrievalFailure);
  85. }
  86. - (void)testClearAuthStateSuccess {
  87. OCMStub(ClassMethod([_mockGoogleKeychainUtilities removeItemWithQuery:[OCMArg any]
  88. error:[OCMArg setTo:nil]]))
  89. .andReturn(YES);
  90. NSError *error;
  91. XCTAssertTrue([_authPersistence clearAuthState:&error]);
  92. XCTAssertNil(error);
  93. }
  94. - (void)testClearAuthStateFailure {
  95. OCMStub(ClassMethod([_mockGoogleKeychainUtilities removeItemWithQuery:[OCMArg any]
  96. error:[OCMArg setTo:nil]]))
  97. .andReturn(NO);
  98. NSError *error;
  99. XCTAssertFalse([_authPersistence clearAuthState:&error]);
  100. XCTAssertNotNil(error);
  101. XCTAssertEqual([error domain], kFIRAppDistributionAuthPersistenceErrorDomain);
  102. XCTAssertEqual([error code], FIRAppDistributionErrorTokenDeletionFailure);
  103. }
  104. @end