Skip to content
Browse files

Fix OAuth2 bug, make error status code available.

  • Loading branch information...
1 parent 86a47e4 commit 346f1a844a0e65049f8f092e02dfdb3fbc3729e3 @msv msv committed Sep 17, 2012
View
4 RELEASE_NOTES.md
@@ -1,5 +1,9 @@
# Release Notes
+## 0.5.4
+* Status code of an error is now available - use 'code' property of NSError.
+* Fix OAuth2 nil info issue.
+
## 0.5.3
* Fix bugs for user based requests
View
12 StackMob/External/JSONKit/JSONKit.m
@@ -742,14 +742,14 @@ - (NSUInteger)count
- (void)getObjects:(id *)objectsPtr range:(NSRange)range
{
NSParameterAssert((objects != NULL) && (count <= capacity));
- if((objectsPtr == NULL) && (NSMaxRange(range) > 0UL)) { [NSException raise:NSRangeException format:@"*** -[%@ %@]: pointer to objects array is NULL but range length is %lu", NSStringFromClass([self class]), NSStringFromSelector(_cmd), NSMaxRange(range)]; }
- if((range.location > count) || (NSMaxRange(range) > count)) { [NSException raise:NSRangeException format:@"*** -[%@ %@]: index (%lu) beyond bounds (%lu)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), NSMaxRange(range), count]; }
+ if((objectsPtr == NULL) && (NSMaxRange(range) > 0UL)) { [NSException raise:NSRangeException format:@"*** -[%@ %@]: pointer to objects array is NULL but range length is %u", NSStringFromClass([self class]), NSStringFromSelector(_cmd), NSMaxRange(range)]; }
+ if((range.location > count) || (NSMaxRange(range) > count)) { [NSException raise:NSRangeException format:@"*** -[%@ %@]: index (%u) beyond bounds (%u)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), NSMaxRange(range), count]; }
memcpy(objectsPtr, objects + range.location, range.length * sizeof(id));
}
- (id)objectAtIndex:(NSUInteger)objectIndex
{
- if(objectIndex >= count) { [NSException raise:NSRangeException format:@"*** -[%@ %@]: index (%lu) beyond bounds (%lu)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), objectIndex, count]; }
+ if(objectIndex >= count) { [NSException raise:NSRangeException format:@"*** -[%@ %@]: index (%u) beyond bounds (%u)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), objectIndex, count]; }
NSParameterAssert((objects != NULL) && (count <= capacity) && (objects[objectIndex] != NULL));
return(objects[objectIndex]);
}
@@ -770,7 +770,7 @@ - (void)insertObject:(id)anObject atIndex:(NSUInteger)objectIndex
{
if(mutations == 0UL) { [NSException raise:NSInternalInconsistencyException format:@"*** -[%@ %@]: mutating method sent to immutable object", NSStringFromClass([self class]), NSStringFromSelector(_cmd)]; }
if(anObject == NULL) { [NSException raise:NSInvalidArgumentException format:@"*** -[%@ %@]: attempt to insert nil", NSStringFromClass([self class]), NSStringFromSelector(_cmd)]; }
- if(objectIndex > count) { [NSException raise:NSRangeException format:@"*** -[%@ %@]: index (%lu) beyond bounds (%lu)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), objectIndex, count + 1UL]; }
+ if(objectIndex > count) { [NSException raise:NSRangeException format:@"*** -[%@ %@]: index (%u) beyond bounds (%lu)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), objectIndex, count + 1UL]; }
#ifdef __clang_analyzer__
[anObject retain]; // Stupid clang analyzer... Issue #19.
#else
@@ -783,7 +783,7 @@ - (void)insertObject:(id)anObject atIndex:(NSUInteger)objectIndex
- (void)removeObjectAtIndex:(NSUInteger)objectIndex
{
if(mutations == 0UL) { [NSException raise:NSInternalInconsistencyException format:@"*** -[%@ %@]: mutating method sent to immutable object", NSStringFromClass([self class]), NSStringFromSelector(_cmd)]; }
- if(objectIndex >= count) { [NSException raise:NSRangeException format:@"*** -[%@ %@]: index (%lu) beyond bounds (%lu)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), objectIndex, count]; }
+ if(objectIndex >= count) { [NSException raise:NSRangeException format:@"*** -[%@ %@]: index (%u) beyond bounds (%u)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), objectIndex, count]; }
_JKArrayRemoveObjectAtIndex(self, objectIndex);
mutations = (mutations == NSUIntegerMax) ? 1UL : mutations + 1UL;
}
@@ -792,7 +792,7 @@ - (void)replaceObjectAtIndex:(NSUInteger)objectIndex withObject:(id)anObject
{
if(mutations == 0UL) { [NSException raise:NSInternalInconsistencyException format:@"*** -[%@ %@]: mutating method sent to immutable object", NSStringFromClass([self class]), NSStringFromSelector(_cmd)]; }
if(anObject == NULL) { [NSException raise:NSInvalidArgumentException format:@"*** -[%@ %@]: attempt to insert nil", NSStringFromClass([self class]), NSStringFromSelector(_cmd)]; }
- if(objectIndex >= count) { [NSException raise:NSRangeException format:@"*** -[%@ %@]: index (%lu) beyond bounds (%lu)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), objectIndex, count]; }
+ if(objectIndex >= count) { [NSException raise:NSRangeException format:@"*** -[%@ %@]: index (%u) beyond bounds (%u)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), objectIndex, count]; }
#ifdef __clang_analyzer__
[anObject retain]; // Stupid clang analyzer... Issue #19.
#else
View
2 StackMob/External/OAuthConsumer/OAMutableURLRequest.m
@@ -186,7 +186,7 @@ - (void)prepare
- (void)_generateTimestamp
{
- timestamp = [[NSString stringWithFormat:@"%d", time(NULL)] retain];
+ timestamp = [[NSString stringWithFormat:@"%ld", time(NULL)] retain];
}
- (void)_generateNonce
View
2 StackMob/StackMob.m
@@ -211,7 +211,7 @@ - (StackMobRequest *)logoutWithCallback:(StackMobCallback)callback
if (self.session.oauthVersion == OAuth2) {
[self queueRequest:request andCallback:^(BOOL success, id result) {
- self.session.oauth2TokenExpiration = [NSDate date];
+ [self.session saveOAuth2AccessToken:nil withExpiration:[NSDate date] andKey:nil];
callback(success, result);
}];
View
10 StackMob/StackMobRequest.m
@@ -279,7 +279,7 @@ - (void)sendRequest
realm:nil
signatureProvider:nil // use the default method, HMAC-SHA1
nonce:nil
- timestamp:[NSString stringWithFormat:@"%d", (long) [session.serverTime timeIntervalSince1970]]];
+ timestamp:[NSString stringWithFormat:@"%ld", (long) [session.serverTime timeIntervalSince1970]]];
SMLog(@"httpMethod %@", [self httpMethod]);
if([self.method isEqualToString:@"startsession"]){
[mArguments setValue:[StackMobClientData sharedClientData].clientDataString forKey:@"cd"];
@@ -466,14 +466,14 @@ - (void)connectionDidFinishLoading:(NSURLConnection*)connection
failMsg = [errResult objectForKey:@"error"];
}
result = [NSError errorWithDomain:@"StackMob"
- code:1
- userInfo:[NSDictionary dictionaryWithObjectsAndKeys:failMsg, NSLocalizedDescriptionKey, nil]];
+ code:statusCode
+ userInfo:[NSDictionary dictionaryWithObjectsAndKeys:failMsg, NSLocalizedDescriptionKey, nil]];
}
}
@catch (NSException *e) { // catch parsing errors
NSString *failMsg = [NSString stringWithFormat:@"Response failed with code: %d", statusCode];
result = [NSError errorWithDomain:@"StackMob"
- code:1
+ code:statusCode
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:failMsg, NSLocalizedDescriptionKey, nil]];
SMLog(@"Unable to parse json '%@'", textResult);
}
@@ -515,7 +515,7 @@ - (id) sendSynchronousRequest {
realm:nil // should we set a realm?
signatureProvider:nil
nonce:nil
- timestamp:[NSString stringWithFormat:@"%d", (long) [session.serverTime timeIntervalSince1970]]] autorelease]; // use the default method, HMAC-SHA1
+ timestamp:[NSString stringWithFormat:@"%ld", (long) [session.serverTime timeIntervalSince1970]]] autorelease]; // use the default method, HMAC-SHA1
[consumer release];
[request setHTTPMethod:[self httpMethod]];
View
2 StackMob/StackMobVersion.h
@@ -13,4 +13,4 @@
// limitations under the License.
-#define STACKMOB_SDK_VERSION @"0.5.3"
+#define STACKMOB_SDK_VERSION @"0.5.4"
View
4 StackMobiOS.xcodeproj/project.pbxproj
@@ -764,7 +764,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = StackMobiOS_Prefix.pch;
GCC_PREPROCESSOR_DEFINITIONS = "DEBUG=0";
- GCC_TREAT_WARNINGS_AS_ERRORS = YES;
+ GCC_TREAT_WARNINGS_AS_ERRORS = NO;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_MISSING_FIELD_INITIALIZERS = YES;
GCC_WARN_ABOUT_MISSING_NEWLINE = YES;
@@ -801,7 +801,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = StackMobiOS_Prefix.pch;
GCC_PREPROCESSOR_DEFINITIONS = "";
- GCC_TREAT_WARNINGS_AS_ERRORS = YES;
+ GCC_TREAT_WARNINGS_AS_ERRORS = NO;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_MISSING_FIELD_INITIALIZERS = YES;
GCC_WARN_ABOUT_MISSING_NEWLINE = YES;
View
1 tests/APIRequestTests.h
@@ -46,5 +46,6 @@
- (void) testCount;
- (void) testCountQuery;
- (void) testLoginLogout;
+- (void) testProperErrorCode;
@end
View
14 tests/APIRequestTests.m
@@ -509,4 +509,18 @@ - (void) testCountZero {
[StackMobTestUtils runRunLoop:[NSRunLoop currentRunLoop] untilRequestFinished:request];
}
+- (void) testProperErrorCode
+{
+ StackMobRequest *request = [[StackMob stackmob] get:@"nonexistentschema" withCallback:^(BOOL success, id result) {
+ if (!success) {
+ NSError *theError = (NSError *)result;
+ STAssertTrue([theError code] == 400, @"There should be a proper failure code");
+ }
+ else {
+ STFail(@"Should not succeed");
+ }
+ }];
+ [StackMobTestUtils runRunLoop:[NSRunLoop currentRunLoop] untilRequestFinished:request];
+}
+
@end
View
1 tests/StackMobOAuth2Test.h
@@ -24,4 +24,5 @@
- (void)deleteUser;
- (void)testLoginAndLogoutWithOAuth2;
- (void)changeOAuthVersion:(int)oAuthVersion;
+- (void)testErrorStatusCodeAndDescription;
@end
View
67 tests/StackMobOAuth2Test.m
@@ -52,7 +52,7 @@ - (void)deleteUser
[StackMobTestUtils runRunLoop:[NSRunLoop currentRunLoop] untilRequestFinished:deleteRequest];
}
-- (void)testLoginWithOAuth2
+- (void)testLoginAndLogoutWithOAuth2
{
[[[StackMob stackmob] session] setOauthVersion:OAuth2];
@@ -70,6 +70,21 @@ - (void)testLoginWithOAuth2
STAssertTrue([[StackMob stackmob] isLoggedIn], @"You are not logged in");
STAssertTrue([[[StackMob stackmob] loggedInUser] isEqualToString:USER_NAME], @"user is not logged in");
STAssertTrue([[[StackMob stackmob] session] oauthVersion] == 2, @"oauth version not correct");
+
+ // make sure all oauth2 keys have been set
+ STAssertTrue([[[StackMob stackmob] session] oauth2Key] != nil, @"OAuth2 key should be not be nil");
+ STAssertTrue([[[StackMob stackmob] session] oauth2Token] != nil, @"OAuth2 token should not be be nil");
+
+ NSDate *expDate = [[[StackMob stackmob] session] oauth2TokenExpiration];
+
+ STAssertTrue([[expDate laterDate:[NSDate date]] isEqualToDate:expDate], @"OAuth2 expiration should be a later date than now");
+ NSString *apiKey = [[[StackMob stackmob] session] apiKey];
+
+ NSString *tokenFromDefaults = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@.token", apiKey]];
+ STAssertTrue(tokenFromDefaults != nil, @"OAuth2 token in defaults should be not be nil");
+
+ NSDate *dateFromDefaults = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@.token.expiration", apiKey]];
+ STAssertTrue([[dateFromDefaults laterDate:[NSDate date]] isEqualToDate:dateFromDefaults], @"OAuth2 token in defaults should not be be nil");
}];
[StackMobTestUtils runRunLoop:[NSRunLoop currentRunLoop] untilRequestFinished:loginRequest];
@@ -103,6 +118,21 @@ - (void)testLoginWithOAuth2
StackMobRequest *logoutRequest = [[StackMob stackmob] logoutWithCallback:^(BOOL success, id result) {
STAssertTrue(success, @"did not logout successfully");
STAssertTrue([[StackMob stackmob] isLoggedOut], @"You are still logged in");
+
+ // make sure all oauth2 associated keys have been cleared
+ STAssertTrue([[[StackMob stackmob] session] oauth2Key] == nil, @"OAuth2 key should be nil");
+ STAssertTrue([[[StackMob stackmob] session] oauth2Token] == nil, @"OAuth2 token should be nil");
+
+ NSDate *expDate = [[[StackMob stackmob] session] oauth2TokenExpiration];
+
+ STAssertTrue(![[expDate laterDate:[NSDate date]] isEqualToDate:expDate], @"OAuth2 expiration should be an earlier date than now");
+ NSString *apiKey = [[[StackMob stackmob] session] apiKey];
+
+ NSString *tokenFromDefaults = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@.token", apiKey]];
+ STAssertTrue(tokenFromDefaults == nil, @"OAuth2 token in defaults should be nil");
+
+ NSDate *dateFromDefaults = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@.token.expiration", apiKey]];
+ STAssertTrue(![[dateFromDefaults laterDate:[NSDate date]] isEqualToDate:dateFromDefaults], @"OAuth2 token in defaults should be nil");
}];
[StackMobTestUtils runRunLoop:[NSRunLoop currentRunLoop] untilRequestFinished:logoutRequest];
@@ -119,4 +149,39 @@ - (void)testLoginWithOAuth2
}
+- (void)testErrorStatusCodeAndDescription
+{
+ [[[StackMob stackmob] session] setOauthVersion:OAuth2];
+
+ SMLog(@"oauthversion is %d", [[[StackMob stackmob] session] oauthVersion]);
+ STAssertTrue([[[StackMob stackmob] session] oauthVersion] == 2, @"oauth version not correct");
+
+ NSDictionary *userArgs = [NSDictionary dictionaryWithObjectsAndKeys:USER_NAME, @"username", USER_PASSWORD, @"password", nil];
+
+
+ StackMobRequest *loginRequest = [[StackMob stackmob] loginWithArguments:userArgs andCallback:^(BOOL success, id result) {
+ if (!success) {
+ SMLog(@"you did not login correctly");
+ }
+ SMLog(@"result from login is %@", result);
+ STAssertTrue([[StackMob stackmob] isLoggedIn], @"You are not logged in");
+ STAssertTrue([[[StackMob stackmob] loggedInUser] isEqualToString:USER_NAME], @"user is not logged in");
+ STAssertTrue([[[StackMob stackmob] session] oauthVersion] == 2, @"oauth version not correct");
+ }];
+
+ [StackMobTestUtils runRunLoop:[NSRunLoop currentRunLoop] untilRequestFinished:loginRequest];
+
+ StackMobRequest *request = [[StackMob stackmob] get:@"nonexistentschema" withCallback:^(BOOL success, id result) {
+ if (!success) {
+ NSError *theError = (NSError *)result;
+ STAssertTrue([theError code] == 400, @"There should be a proper failure code");
+ STAssertTrue([[theError userInfo] objectForKey:NSLocalizedDescriptionKey] != nil, @"There should be a failure reason when using oauth2");
+ }
+ else {
+ STFail(@"Should not succeed");
+ }
+ }];
+ [StackMobTestUtils runRunLoop:[NSRunLoop currentRunLoop] untilRequestFinished:request];
+}
+
@end

0 comments on commit 346f1a8

Please sign in to comment.
Something went wrong with that request. Please try again.