-
-
Notifications
You must be signed in to change notification settings - Fork 56
Add support for using PF_Twitter without a linking or logging into a Parse account
#27
Changes from 2 commits
16121e2
6e5e9ca
ae4a350
9f062c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,6 +92,30 @@ - (BFTask *)authorizeInBackground { | |
| return source.task; | ||
| }]; | ||
| } | ||
| - (BFTask *)deauthorizeInBackground { | ||
| if (self.consumerKey.length == 0 || self.consumerSecret.length == 0) { | ||
| //TODO: (nlutsenko) This doesn't look right, maybe we should add additional error code? | ||
| return [BFTask taskWithError:[NSError errorWithDomain:PFParseErrorDomain code:1 userInfo:nil]]; | ||
| } | ||
|
|
||
| return [[self _performDeauthAsync] pftw_continueAsyncWithBlock:^id(BFTask *task) { | ||
| BFTaskCompletionSource *source = [BFTaskCompletionSource taskCompletionSource]; | ||
| dispatch_async(dispatch_get_main_queue(), ^{ | ||
|
||
| if (task.cancelled) { | ||
| [source cancel]; | ||
| } else if (!task.error && !task.result) { | ||
| source.result = nil; | ||
| } else if (task.error) { | ||
| [source trySetError:task.error]; | ||
| } else if (task.result) { | ||
| [self setLoginResultValues:@{}]; | ||
|
||
|
|
||
| [source trySetResult:task.result]; | ||
| } | ||
| }); | ||
| return source.task; | ||
| }]; | ||
| } | ||
|
|
||
| - (void)authorizeWithSuccess:(void (^)(void))success | ||
| failure:(void (^)(NSError *error))failure | ||
|
|
@@ -428,6 +452,26 @@ - (BFTask *)_performWebViewAuthAsync { | |
| return source.task; | ||
| } | ||
|
|
||
| - (BFTask *)_performDeauthAsync { | ||
| NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; | ||
| request.URL = [NSURL URLWithString:@"https://api.twitter.com/oauth2/invalidate_token"]; | ||
| request.HTTPMethod = @"POST"; | ||
|
|
||
| [self signRequest:request]; | ||
|
|
||
| BFTaskCompletionSource *taskCompletionSource = [BFTaskCompletionSource taskCompletionSource]; | ||
|
|
||
| [[self.urlSession dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { | ||
| if (error) { | ||
| [taskCompletionSource trySetError:error]; | ||
| } else { | ||
| [taskCompletionSource trySetResult:data]; | ||
| } | ||
| }] resume]; | ||
|
|
||
| return taskCompletionSource.task; | ||
| } | ||
|
|
||
| ///-------------------------------------- | ||
| #pragma mark - Sign Request | ||
| ///-------------------------------------- | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -622,4 +622,67 @@ - (void)testAuthorizeWithoutLocalAccountAndNetworkSuccess { | |
| OCMVerifyAll(mockedURLSession); | ||
| } | ||
|
|
||
| - (void)testDeauthorizeLoggedOutAccount { | ||
| id store = PFStrictClassMock([ACAccountStore class]); | ||
| NSURLSession *session = PFStrictClassMock([NSURLSession class]); | ||
| id mockedDialog = PFStrictProtocolMock(@protocol(PFOAuth1FlowDialogInterface)); | ||
| PF_Twitter *twitter = [[PF_Twitter alloc] initWithAccountStore:store urlSession:session dialogClass:mockedDialog]; | ||
|
|
||
| XCTestExpectation *expectation = [self currentSelectorTestExpectation]; | ||
| [[twitter authorizeInBackground] continueWithBlock:^id(BFTask *task) { | ||
| NSError *error = task.error; | ||
| XCTAssertNotNil(error); | ||
| XCTAssertEqualObjects(error.domain, PFParseErrorDomain); | ||
| XCTAssertEqual(error.code, 2); | ||
|
||
| [expectation fulfill]; | ||
| return nil; | ||
| }]; | ||
| [self waitForTestExpectations]; | ||
| } | ||
|
|
||
| - (void)testDeauthorizeLoggedInAccount { | ||
| id store = PFStrictClassMock([ACAccountStore class]); | ||
| NSURLSession *session = PFStrictClassMock([NSURLSession class]); | ||
|
|
||
| id mockedStore = PFStrictClassMock([ACAccountStore class]); | ||
| id mockedURLSession = PFStrictClassMock([NSURLSession class]); | ||
| id mockedOperationQueue = PFStrictClassMock([NSOperationQueue class]); | ||
|
|
||
| id mockedDialog = PFStrictProtocolMock(@protocol(PFOAuth1FlowDialogInterface)); | ||
| PF_Twitter *twitter = [[PF_Twitter alloc] initWithAccountStore:store urlSession:session dialogClass:mockedDialog]; | ||
| twitter.consumerKey = @"consumer_key"; | ||
| twitter.consumerSecret = @"consumer_secret"; | ||
| twitter.authToken = @"auth_token"; | ||
| twitter.authTokenSecret = @"auth_token_secret"; | ||
| twitter.userId = @"user_id"; | ||
| twitter.screenName = @"screen_name"; | ||
|
|
||
| __block NSURLSessionDataTaskCompletionHandler completionHandler = nil; | ||
| [OCMStub([mockedURLSession dataTaskWithRequest:[OCMArg checkWithBlock:^BOOL(id obj) { | ||
| NSURLRequest *request = obj; | ||
| return [request.URL.lastPathComponent isEqualToString:@"invalidate_token"]; | ||
| }] completionHandler:[OCMArg checkWithBlock:^BOOL(id obj) { | ||
| completionHandler = obj; | ||
| return (obj != nil); | ||
| }]]).andDo(^(NSInvocation *invocation) { | ||
| completionHandler([NSData data], nil, nil); | ||
| }) andReturn:[OCMockObject niceMockForClass:[NSURLSessionDataTask class]]]; | ||
|
|
||
| XCTestExpectation *expectation = [self currentSelectorTestExpectation]; | ||
| [[twitter deauthorizeInBackground] continueWithBlock:^id(BFTask *task) { | ||
| NSError *error = task.error; | ||
| XCTAssertNil(error); | ||
| XCTAssertNotNil(task.result); | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The checks for |
||
| XCTAssertNil(twitter.userId); | ||
|
||
| XCTAssertNil(twitter.screenName); | ||
| XCTAssertNil(twitter.userId); | ||
| XCTAssertNil(twitter.userId); | ||
|
|
||
| [expectation fulfill]; | ||
| return nil; | ||
| }]; | ||
| [self waitForTestExpectations]; | ||
| } | ||
|
|
||
| @end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
Initializes an instance of ...