diff --git a/Classes/BITAuthenticator.h b/Classes/BITAuthenticator.h index fd4158d6d7..79ba306f90 100644 --- a/Classes/BITAuthenticator.h +++ b/Classes/BITAuthenticator.h @@ -87,34 +87,6 @@ typedef void(^tValidationCompletion)(BOOL validated, NSError *error); */ @property (nonatomic, readonly) NSString *installationIdentification; -#pragma mark - Authentication -/** - * Authenticate this app installation - * - * Depending on 'authenticationType', this tries to authenticate the app installation - * against the HockeyApp server. - * You should not need to call this, as it's done automatically once the manager has - * been started, depending on validationType. - * - * @param completion if nil, success/failure is reported via the delegate, if not nil, the - * delegate methods are not called. - */ -- (void) authenticateWithCompletion:(tAuthenticationCompletion) completion; - -#pragma mark - Validation -/** - * Validate the app installation - * - * Depending on @see validationType, this is called by the manager after the app becomes active - * and tries to revalidate the installation. - * You should not need to call this, as it's done automatically once the manager has - * been started, depending on validationType. - * - * @param completion if nil, success/failure is reported via the delegate, if not nil, the - * delegate methods are not called - */ -- (void) validateInstallationWithCompletion:(tValidationCompletion) completion; - @end @protocol BITAuthenticatorDelegate @@ -129,11 +101,5 @@ typedef void(^tValidationCompletion)(BOOL validated, NSError *error); * */ - (void) authenticator:(BITAuthenticator *)authenticator willShowAuthenticationController:(UIViewController*) viewController; -- (void) authenticatorDidAuthenticate:(BITAuthenticator*) authenticator; -- (void) authenticator:(BITAuthenticator*) authenticator failedToAuthenticateWithError:(NSError*) error; - -- (void) authenticatorDidValidateInstallation:(BITAuthenticator*) authenticator; -- (void) authenticator:(BITAuthenticator*) authenticator failedToValidateInstallationWithError:(NSError*) error; - @end diff --git a/Classes/BITAuthenticator.m b/Classes/BITAuthenticator.m index 363b799902..69722f93a4 100644 --- a/Classes/BITAuthenticator.m +++ b/Classes/BITAuthenticator.m @@ -87,21 +87,9 @@ static NSString* const kBITAuthenticatorLastAuthenticatedVersionKey = @"BITAuthe NSError *error = [NSError errorWithDomain:kBITAuthenticatorErrorDomain code:BITAuthenticatorNotAuthorized userInfo:nil]; - if(completion) { - completion(NO, error); - } else { - if([self.delegate respondsToSelector:@selector(authenticator:failedToValidateInstallationWithError:)]) { - [self.delegate authenticator:self failedToValidateInstallationWithError:error]; - } - } + if(completion) completion(NO, error); } else { - if(completion) { - completion(YES, nil); - } else { - if([self.delegate respondsToSelector:@selector(authenticatorDidValidateInstallation:)]) { - [self.delegate authenticatorDidValidateInstallation:self]; - } - } + if(completion) completion(YES, nil); } }]; } else { @@ -253,10 +241,6 @@ static NSString* const kBITAuthenticatorLastAuthenticatedVersionKey = @"BITAuthe if(self.authenticationCompletionBlock) { self.authenticationCompletionBlock(self.authenticationToken, error); self.authenticationCompletionBlock = nil; - } else { - if([self.delegate respondsToSelector:@selector(authenticator:failedToAuthenticateWithError:)]) { - [self.delegate authenticator:self failedToAuthenticateWithError:error]; - } } } @@ -269,10 +253,6 @@ static NSString* const kBITAuthenticatorLastAuthenticatedVersionKey = @"BITAuthe if(self.authenticationCompletionBlock) { self.authenticationCompletionBlock(self.authenticationToken, nil); self.authenticationCompletionBlock = nil; - } else { - if([self.delegate respondsToSelector:@selector(authenticatorDidAuthenticate:)]) { - [self.delegate authenticatorDidAuthenticate:self]; - } } } @@ -280,20 +260,12 @@ static NSString* const kBITAuthenticatorLastAuthenticatedVersionKey = @"BITAuthe - (void)validationFailedWithError:(NSError *)validationError completion:(tValidationCompletion) completion{ if(completion) { completion(NO, validationError); - } else { - if([self.delegate respondsToSelector:@selector(authenticator:failedToValidateInstallationWithError:)]) { - [self.delegate authenticator:self failedToValidateInstallationWithError:validationError]; - } } } - (void)validationSucceededWithCompletion:(tValidationCompletion) completion { if(completion) { completion(YES, nil); - } else { - if([self.delegate respondsToSelector:@selector(authenticatorDidValidateInstallation:)]) { - [self.delegate authenticatorDidValidateInstallation:self]; - } } } diff --git a/Classes/BITAuthenticator_Private.h b/Classes/BITAuthenticator_Private.h index 439b8d670e..20a15be7c6 100644 --- a/Classes/BITAuthenticator_Private.h +++ b/Classes/BITAuthenticator_Private.h @@ -47,6 +47,36 @@ */ - (void) applicationDidBecomeActive:(NSNotification*) note; + +#pragma mark - Authentication +/** + * Authenticate this app installation + * + * Depending on 'authenticationType', this tries to authenticate the app installation + * against the HockeyApp server. + * You should not need to call this, as it's done automatically once the manager has + * been started, depending on validationType. + * + * @param completion if nil, success/failure is reported via the delegate, if not nil, the + * delegate methods are not called. + */ +- (void) authenticateWithCompletion:(tAuthenticationCompletion) completion; + +#pragma mark - Validation +/** + * Validate the app installation + * + * Depending on @see validationType, this is called by the manager after the app becomes active + * and tries to revalidate the installation. + * You should not need to call this, as it's done automatically once the manager has + * been started, depending on validationType. + * + * @param completion if nil, success/failure is reported via the delegate, if not nil, the + * delegate methods are not called + */ +- (void) validateInstallationWithCompletion:(tValidationCompletion) completion; + + #pragma mark - Validation callbacks - (void) validationSucceededWithCompletion:(tValidationCompletion) completion; - (void) validationFailedWithError:(NSError *) validationError completion:(tValidationCompletion) completion; diff --git a/Support/HockeySDKTests/BITAuthenticatorTests.m b/Support/HockeySDKTests/BITAuthenticatorTests.m index 1486c0c619..67c78ba76f 100644 --- a/Support/HockeySDKTests/BITAuthenticatorTests.m +++ b/Support/HockeySDKTests/BITAuthenticatorTests.m @@ -155,29 +155,38 @@ static void *kInstallationIdentification = &kInstallationIdentification; assertThat(_sut.authenticationToken, equalTo(@"SuperToken")); } -- (void) testThatSuccessfulAuthenticationCallsTheDelegate { +- (void) testThatSuccessfulAuthenticationCallsTheBlock { id delegateMock = mockProtocol(@protocol(BITAuthenticatorDelegate)); _sut.delegate = delegateMock; _sut.authenticationToken = @"Test"; - - [_sut authenticateWithCompletion:nil]; + __block BOOL didAuthenticate = NO; + [_sut authenticateWithCompletion:^(NSString *authenticationToken, NSError *error) { + if(authenticationToken) didAuthenticate = YES; + }]; [_sut authenticationViewController:nil authenticatedWithToken:@"SuperToken"]; - [verify(delegateMock) authenticatorDidAuthenticate:_sut]; + assertThatBool(didAuthenticate, equalToBool(YES)); } -- (void) testThatCancelledAuthenticationCallsTheErrorDelegate { +- (void) testThatCancelledAuthenticationSetsProperError { id delegateMock = mockProtocol(@protocol(BITAuthenticatorDelegate)); _sut.delegate = delegateMock; //this will prepare everything and show the viewcontroller - [_sut authenticateWithCompletion:nil]; + __block BOOL didAuthenticateCalled = NO; + __block NSError *authenticationError = nil; + [_sut authenticateWithCompletion:^(NSString *authenticationToken, NSError *error) { + didAuthenticateCalled = YES; + authenticationError = error; + }]; + //fake delegate call from the viewcontroller [_sut authenticationViewControllerDidCancel:nil]; - - [verify(delegateMock) authenticator:_sut failedToAuthenticateWithError:[NSError errorWithDomain:kBITAuthenticatorErrorDomain - code:BITAuthenticatorAuthenticationCancelled - userInfo:nil]]; + + assertThatBool(didAuthenticateCalled, equalToBool(YES)); + assertThat(authenticationError, equalTo([NSError errorWithDomain:kBITAuthenticatorErrorDomain + code:BITAuthenticatorAuthenticationCancelled + userInfo:nil])); } - (void) testThatCancelledAuthenticationResetsTheToken { @@ -279,28 +288,41 @@ static void *kInstallationIdentification = &kInstallationIdentification; [verifyCount(delegateMock, never()) authenticator:_sut willShowAuthenticationController:(id)anything()]; } -- (void) testThatFailedValidationCallsTheDelegate { +- (void) testThatFailedValidationCallsTheCompletionBlock { id delegateMock = mockProtocol(@protocol(BITAuthenticatorDelegate)); _sut.delegate = delegateMock; _sut.validationType = BITAuthenticatorValidationTypeOnFirstLaunch; - [_sut validateInstallationWithCompletion:nil]; - [_sut validationFailedWithError:nil completion:nil]; + __block BOOL validated = YES; + __block NSError *error = nil; + tValidationCompletion completion = ^(BOOL validated_, NSError *error_) { + validated = validated_; + error = error_; + }; + [_sut validateInstallationWithCompletion:completion]; + [_sut validationFailedWithError:[NSError errorWithDomain:kBITAuthenticatorErrorDomain code:0 userInfo:nil] + completion:completion]; - [verifyCount(delegateMock, times(1)) authenticator:_sut failedToValidateInstallationWithError:(id)anything()]; - [verifyCount(delegateMock, never()) authenticatorDidValidateInstallation:_sut]; + assertThatBool(validated, equalToBool(NO)); + assertThat(error, notNilValue()); } -- (void) testThatSuccessValidationCallsTheDelegate { +- (void) testThatSuccessValidationCallsTheCompletionBlock { id delegateMock = mockProtocol(@protocol(BITAuthenticatorDelegate)); _sut.delegate = delegateMock; _sut.validationType = BITAuthenticatorValidationTypeOnFirstLaunch; - [_sut validateInstallationWithCompletion:nil]; - [_sut validationSucceededWithCompletion:nil]; + __block BOOL validated = NO; + __block NSError *error = nil; + tValidationCompletion completion = ^(BOOL validated_, NSError *error_) { + validated = validated_; + error = error_; + }; + [_sut validateInstallationWithCompletion:completion]; + [_sut validationSucceededWithCompletion:completion]; - [verifyCount(delegateMock, never()) authenticator:_sut failedToValidateInstallationWithError:(id)anything()]; - [verifyCount(delegateMock, times(1)) authenticatorDidValidateInstallation:_sut]; + assertThatBool(validated, equalToBool(YES)); + assertThat(error, nilValue()); } @end