FIRAppDistributionAuthPersistenceTests.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 <FirebaseAppDistribution/FIRAppDistributionKeychainUtility+Private.h>
  20. @interface FIRAppDistributionAuthPersistenceTests : XCTestCase
  21. @end
  22. @implementation FIRAppDistributionAuthPersistenceTests {
  23. NSMutableDictionary *_mockKeychainQuery;
  24. id _mockAuthorizationData;
  25. id _mockOIDAuthState;
  26. id _mockKeychainUtility;
  27. id _partialMockAuthPersitence;
  28. }
  29. - (void)setUp {
  30. [super setUp];
  31. _mockKeychainQuery = [NSMutableDictionary
  32. dictionaryWithObjectsAndKeys:(id) @"thing one", (id) @"another thing", nil];
  33. _mockKeychainUtility = OCMClassMock([FIRAppDistributionKeychainUtility class]);
  34. _mockAuthorizationData = [@"this is some password stuff" dataUsingEncoding:NSUTF8StringEncoding];
  35. _mockOIDAuthState = OCMClassMock([OIDAuthState class]);
  36. OCMStub(ClassMethod([_mockKeychainUtility unarchiveKeychainResult:[OCMArg any]]))
  37. .andReturn(_mockOIDAuthState);
  38. OCMStub(ClassMethod([_mockKeychainUtility archiveDataForKeychain:[OCMArg any]]))
  39. .andReturn(_mockAuthorizationData);
  40. }
  41. - (void)tearDown {
  42. [super tearDown];
  43. }
  44. - (void)testPersistAuthStateSuccess {
  45. OCMStub(ClassMethod([_mockKeychainUtility addKeychainItem:[OCMArg any]
  46. withDataDictionary:[OCMArg any]]))
  47. .andReturn(YES);
  48. NSError *error;
  49. XCTAssertTrue([FIRAppDistributionAuthPersistence persistAuthState:_mockOIDAuthState
  50. error:&error]);
  51. XCTAssertNil(error);
  52. }
  53. - (void)testPersistAuthStateFailure {
  54. OCMStub(ClassMethod([_mockKeychainUtility addKeychainItem:[OCMArg any]
  55. withDataDictionary:[OCMArg any]]))
  56. .andReturn(NO);
  57. NSError *error;
  58. XCTAssertFalse([FIRAppDistributionAuthPersistence persistAuthState:_mockOIDAuthState
  59. error:&error]);
  60. XCTAssertNotNil(error);
  61. XCTAssertEqual([error domain], kFIRAppDistributionAuthPersistenceErrorDomain);
  62. XCTAssertEqual([error code], FIRAppDistributionErrorTokenPersistenceFailure);
  63. }
  64. - (void)testOverwriteAuthStateSuccess {
  65. OCMStub(ClassMethod([_mockKeychainUtility fetchKeychainItemMatching:[OCMArg any]
  66. error:[OCMArg setTo:nil]]))
  67. .andReturn(_mockAuthorizationData);
  68. OCMStub(ClassMethod([_mockKeychainUtility updateKeychainItem:[OCMArg any]
  69. withDataDictionary:[OCMArg any]]))
  70. .andReturn(YES);
  71. NSError *error;
  72. XCTAssertTrue([FIRAppDistributionAuthPersistence persistAuthState:_mockOIDAuthState
  73. error:&error]);
  74. XCTAssertNil(error);
  75. }
  76. - (void)testOverwriteAuthStateFailure {
  77. OCMStub(ClassMethod([_mockKeychainUtility fetchKeychainItemMatching:[OCMArg any]
  78. error:[OCMArg setTo:nil]]))
  79. .andReturn(_mockAuthorizationData);
  80. OCMStub(ClassMethod([_mockKeychainUtility updateKeychainItem:[OCMArg any]
  81. withDataDictionary:[OCMArg any]]))
  82. .andReturn(NO);
  83. NSError *error;
  84. XCTAssertFalse([FIRAppDistributionAuthPersistence persistAuthState:_mockOIDAuthState
  85. error:&error]);
  86. XCTAssertNotNil(error);
  87. XCTAssertEqual([error domain], kFIRAppDistributionAuthPersistenceErrorDomain);
  88. XCTAssertEqual([error code], FIRAppDistributionErrorTokenPersistenceFailure);
  89. }
  90. - (void)testRetrieveAuthStateSuccess {
  91. OCMStub(ClassMethod([_mockKeychainUtility fetchKeychainItemMatching:[OCMArg any]
  92. error:[OCMArg setTo:nil]]))
  93. .andReturn(_mockAuthorizationData);
  94. NSError *error;
  95. XCTAssertTrue([[FIRAppDistributionAuthPersistence retrieveAuthState:&error]
  96. isKindOfClass:[OIDAuthState class]]);
  97. XCTAssertNil(error);
  98. }
  99. - (void)testRetrieveAuthStateFailure {
  100. OCMStub(ClassMethod([_mockKeychainUtility fetchKeychainItemMatching:[OCMArg any]
  101. error:[OCMArg setTo:nil]]))
  102. .andReturn(nil);
  103. NSError *error;
  104. XCTAssertFalse([FIRAppDistributionAuthPersistence retrieveAuthState:&error]);
  105. XCTAssertNotNil(error);
  106. XCTAssertEqual([error domain], kFIRAppDistributionAuthPersistenceErrorDomain);
  107. XCTAssertEqual([error code], FIRAppDistributionErrorTokenRetrievalFailure);
  108. }
  109. - (void)testClearAuthStateSuccess {
  110. OCMStub(ClassMethod([_mockKeychainUtility deleteKeychainItem:[OCMArg any]])).andReturn(YES);
  111. NSError *error;
  112. XCTAssertTrue([FIRAppDistributionAuthPersistence clearAuthState:&error]);
  113. XCTAssertNil(error);
  114. }
  115. - (void)testClearAuthStateFailure {
  116. OCMStub(ClassMethod([_mockKeychainUtility deleteKeychainItem:[OCMArg any]])).andReturn(NO);
  117. NSError *error;
  118. XCTAssertFalse([FIRAppDistributionAuthPersistence clearAuthState:&error]);
  119. XCTAssertNotNil(error);
  120. XCTAssertEqual([error domain], kFIRAppDistributionAuthPersistenceErrorDomain);
  121. XCTAssertEqual([error code], FIRAppDistributionErrorTokenDeletionFailure);
  122. }
  123. @end