Browse files

Add OAuth2 support, bump version to 0.5.0

  • Loading branch information...
1 parent 2678106 commit f7371ffa46ab52154d0cd8d052936519c98c94f8 @msv msv committed Jul 6, 2012
View
1 StackMob/StackMob.h
@@ -15,6 +15,7 @@
#import <Foundation/Foundation.h>
#import "StackMobSession.h"
#import "StackMobRequest.h"
+#import "StackMobAccessTokenRequest.h"
#import "StackMobQuery.h"
#import "StackMobConfiguration.h"
#import "StackMobCookieStore.h"
View
6 StackMob/StackMob.m
@@ -191,10 +191,11 @@ - (StackMobRequest *)loginWithArguments:(NSDictionary *)arguments andCallback:(S
{
request = [StackMobRequest requestForMethod:[NSString stringWithFormat:@"%@/login", [self.session userObjectName]]
withArguments:arguments
- withHttpVerb:GET];
+ withHttpVerb:GET];
+ request.isSecure = YES;
}
- request.isSecure = YES;
+
_session.lastUserLoginName = [arguments valueForKey:@"username"];
[self queueRequest:request andCallback:callback];
@@ -469,6 +470,7 @@ - (StackMobRequest *)post:(NSString *)path withArguments:(NSDictionary *)argumen
StackMobRequest *request = [StackMobRequest requestForMethod:[self escapePath:path]
withArguments:arguments
withHttpVerb:POST];
+
[self queueRequest:request andCallback:callback];
return request;
}
View
8 StackMob/StackMobAccessTokenRequest.m
@@ -13,7 +13,10 @@ @implementation StackMobAccessTokenRequest
+ (id)requestForMethod:(NSString *)method withArguments:(NSDictionary *)arguments {
StackMobAccessTokenRequest *request = [[[StackMobAccessTokenRequest alloc] init] autorelease];
request.method = method;
- [request setArguments: arguments];
+ request.isSecure = YES;
+ NSMutableDictionary *dict = [arguments mutableCopy];
+ [dict setObject:@"mac" forKey:@"token_type"];
+ [request setArguments: dict];

do you need to do a [dict release] before dict goes out of scope since request has memory ownership now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
request.httpMethod = [StackMobRequest stringFromHttpVerb:POST];
return request;
@@ -24,7 +27,8 @@ - (id) resultFromSuccessString:(NSString *)textResult
NSDictionary * result = [textResult objectFromJSONString];
NSString *accessToken = [result valueForKey:@"access_token"];
NSNumber *expiration = [result valueForKey:@"expires_in"];
- [session saveOAuth2AccessToken:accessToken withExpiration:[NSDate dateWithTimeIntervalSinceNow:expiration.intValue]];
+ NSString *key = [result valueForKey:@"mac_key"];
+ [session saveOAuth2AccessToken:accessToken withExpiration:[NSDate dateWithTimeIntervalSinceNow:expiration.intValue] andKey:key];
return [[result valueForKey:@"stackmob"] valueForKey:@"user"];
}
View
2 StackMob/StackMobRequest.h
@@ -141,6 +141,8 @@ typedef enum {
- (id) resultFromSuccessString:(NSString *)textResult;
+- (NSString *)createMACHeaderForOAuth2;
+
@end
@protocol SMRequestDelegate <NSObject>
View
62 StackMob/StackMobRequest.m
@@ -97,6 +97,10 @@ + (id)requestForMethod:(NSString*)method withArguments:(NSDictionary*)arguments
if (arguments != nil) {
[request setArguments:arguments];
}
+ if ([[[StackMob stackmob] session] oauthVersion] == 2 && [[StackMob stackmob] isLoggedOut]) {
+ // any call using oauth2 to a schema that is open will be sent over https
@drapp
drapp added a note Jul 7, 2012

Why?

+1. would also like to know the answer

@jrwest
jrwest added a note Jul 12, 2012

alternatively, i ask: why not :)?

sure. would like some insight either way

@drapp
drapp added a note Jul 12, 2012

He removed this line later

k

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ request.isSecure = YES;
+ }
return request;
}
@@ -298,7 +302,9 @@ - (void)sendRequest
[request addValue:session.apiKey forHTTPHeaderField:@"X-StackMob-API-Key"];
if(session.oauth2TokenValid)
{
- [request addValue:session.oauth2Token forHTTPHeaderField:@"Authorization"];
+ NSString *oauth2MAC = [self createMACHeaderForOAuth2];
+ [request addValue:oauth2MAC forHTTPHeaderField:@"Authorization"];
+ SMLog(@"request headers are: %@", [request allHTTPHeaderFields]);
}
}
else
@@ -514,7 +520,20 @@ - (id) sendSynchronousRequest {
[request addValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
[request addValue:@"deflate" forHTTPHeaderField:@"Accept-Encoding"];
- [request prepare];
+ if(session.oauthVersion == OAuth2)
+ {
+ [request addValue:session.apiKey forHTTPHeaderField:@"X-StackMob-API-Key"];
+ if(session.oauth2TokenValid)
+ {
+ NSString *oauth2MAC = [self createMACHeaderForOAuth2];
+ [request addValue:oauth2MAC forHTTPHeaderField:@"Authorization"];
+ SMLog(@"request headers are: %@", [request allHTTPHeaderFields]);
+ }
+ }
+ else
+ {
+ [request prepare];
+ }
if (![[self httpMethod] isEqualToString: @"GET"]) {
[request setHTTPBody:[[mArguments JSONString] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *contentType = [NSString stringWithFormat:@"application/json"];
@@ -524,7 +543,7 @@ - (id) sendSynchronousRequest {
[mConnectionData setLength:0];
- SMLog(@"StackMobRequest %p: sending synchronous oauth request: %@", self, request);
+ SMLog(@"StackMobRequest %p: sending synchronous oauth request: %@ with headers %@", self, request, [request allHTTPHeaderFields]);
_requestFinished = NO;
self.connectionError = nil;
@@ -543,5 +562,42 @@ - (NSString*) description {
return [NSString stringWithFormat:@"%@: %@", [super description], self.url];
}
+- (NSString *)createMACHeaderForOAuth2
+{
+ // get the id
+ NSString *access_token = [session oauth2Token];
+ double timestamp = [[NSDate date] timeIntervalSince1970];
+ // create the nonce
+ NSString *nonce = [NSString stringWithFormat:@"n%d", arc4random() % 10000];
+ // create the mac
+ NSString *key = [session oauth2Key];
+ NSArray *hostAndPort = [[NSString stringWithFormat:@"api.%@.%@", [[[StackMob stackmob] session] subDomain], [[[StackMob stackmob] session] domain]] componentsSeparatedByString:@":"];
+ NSString *host = [hostAndPort objectAtIndex:0];
+ NSString *port = [hostAndPort count] > 1 ? [hostAndPort objectAtIndex:1] : @"80";
+ NSString *httpVerb = self.httpMethod;
+ NSString *uri = [NSString stringWithFormat:@"/%@", self.method];
+
+ if (([[self httpMethod] isEqualToString:@"GET"] || [[self httpMethod] isEqualToString:@"DELETE"]) &&
+ [mArguments count] > 0) {
+ uri = [uri stringByAppendingFormat:@"?%@", [mArguments queryString]];
+ }
+
+ // create base
+ NSArray *baseArray = [NSArray arrayWithObjects:[NSString stringWithFormat:@"%.f", timestamp], nonce, httpVerb, uri, host, port, nil];
+ unichar newline = 0x0A;
+ NSString *baseString = [baseArray componentsJoinedByString:[NSString stringWithFormat:@"%C", newline]];
+ baseString = [baseString stringByAppendingString:[NSString stringWithFormat:@"%C", newline]];
+ baseString = [baseString stringByAppendingString:[NSString stringWithFormat:@"%C", newline]];
+
+ //bstring through bin to string using crypto
+ OAHMAC_SHA1SignatureProvider *provider = [[OAHMAC_SHA1SignatureProvider alloc] init];
+ NSString *mac = [provider signClearText:baseString withSecret:key];
+
+ //return 'MAC id="' + id + '",ts="' + ts + '",nonce="' + nonce + '",mac="' + mac + '"'
+ unichar quotes = 0x22;
+ NSString *returnString = [NSString stringWithFormat:@"MAC id=%C%@%C,ts=%C%.f%C,nonce=%C%@%C,mac=%C%@%C", quotes, access_token, quotes, quotes, timestamp, quotes, quotes, nonce, quotes, quotes, mac, quotes];
+ return returnString;
+}
+
@end
View
5 StackMob/StackMobSession.h
@@ -45,6 +45,7 @@ static NSString *const SMSubdomainDefault = @"mob1";
int _oauthVersion;
NSString *_oauth2Token;
NSDate *_oauth2TokenExpiration;
+ NSString *_oauth2Key;
}
/**
@@ -123,6 +124,8 @@ static NSString *const SMSubdomainDefault = @"mob1";
@property(nonatomic,retain) NSDate* oauth2TokenExpiration;
+@property(nonatomic,retain) NSString* oauth2Key;
+
/**
* The globally shared session instance.
@@ -238,6 +241,6 @@ static NSString *const SMSubdomainDefault = @"mob1";
*/
-(void)recordServerTimeDiffFromHeader:(NSString*)header;
--(void)saveOAuth2AccessToken:(NSString *)token withExpiration:(NSDate *)date;
+-(void)saveOAuth2AccessToken:(NSString *)token withExpiration:(NSDate *)date andKey:(NSString *)key;
@end
View
5 StackMob/StackMobSession.m
@@ -19,6 +19,7 @@ - (void)setup;
- (NSString *)oauth2TokenKey;
- (NSString *)oauth2TokenExpirationKey;
+- (NSString *)oauth2Key;
@end
@@ -50,6 +51,7 @@ @implementation StackMobSession
@synthesize oauthVersion = _oauthVersion;
@synthesize oauth2Token = _oauth2Token;
@synthesize oauth2TokenExpiration = _oauth2TokenExpiration;
+@synthesize oauth2Key = _oauth2Key;
@synthesize pushURL;
+ (StackMobSession*)session {
@@ -272,8 +274,9 @@ - (NSString *)oauth2TokenExpirationKey
return [NSString stringWithFormat:@"%@.token.expiration", _apiKey];
}
--(void)saveOAuth2AccessToken:(NSString *)token withExpiration:(NSDate *)date
+-(void)saveOAuth2AccessToken:(NSString *)token withExpiration:(NSDate *)date andKey:(NSString *)key
{
+ self.oauth2Key = key;
self.oauth2Token = token;
self.oauth2TokenExpiration = date;
[[NSUserDefaults standardUserDefaults] setObject:token forKey:[self oauth2TokenKey]];
View
2 StackMob/StackMobVersion.h
@@ -13,4 +13,4 @@
// limitations under the License.
-#define STACKMOB_SDK_VERSION @"0.4.12"
+#define STACKMOB_SDK_VERSION @"0.5.0"
View
30 StackMobiOS.xcodeproj/project.pbxproj
@@ -108,8 +108,6 @@
82CE43E41288F16700A73E9C /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AACBBE490F95108600F1A2B1 /* Foundation.framework */; };
82CE443E1288F17700A73E9C /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 82CE433A1288F02E00A73E9C /* SystemConfiguration.framework */; };
82CE45151289025B00A73E9C /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 82CE432C1288EFB900A73E9C /* UIKit.framework */; };
- 82CE45F412892FFA00A73E9C /* APIRequestTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 82CE45F212892FFA00A73E9C /* APIRequestTests.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
- 93694862147228B400575176 /* StackMobPushTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9369485F147228B400575176 /* StackMobPushTests.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
93694863147228B400575176 /* StackMobTestUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 93694861147228B400575176 /* StackMobTestUtils.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
9369486514722C5C00575176 /* StackMobTestCommon.m in Sources */ = {isa = PBXBuildFile; fileRef = 9369485A1472286900575176 /* StackMobTestCommon.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
9369486614722C6500575176 /* StackMobTestCommon.h in Headers */ = {isa = PBXBuildFile; fileRef = 936948591472286900575176 /* StackMobTestCommon.h */; settings = {ATTRIBUTES = (); }; };
@@ -123,17 +121,20 @@
C63FB3511497EA4E007961C1 /* SMFile.h in Headers */ = {isa = PBXBuildFile; fileRef = C63FB34F1497EA4E007961C1 /* SMFile.h */; };
C63FB3521497EA4E007961C1 /* SMFile.m in Sources */ = {isa = PBXBuildFile; fileRef = C63FB3501497EA4E007961C1 /* SMFile.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
C63FB3561497EDF4007961C1 /* SMFileTest.h in Headers */ = {isa = PBXBuildFile; fileRef = C63FB3541497EDF4007961C1 /* SMFileTest.h */; };
- C63FB3581497EF38007961C1 /* SMFileTest.m in Sources */ = {isa = PBXBuildFile; fileRef = C63FB3551497EDF4007961C1 /* SMFileTest.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
C63FB3591497F22A007961C1 /* SMFile.m in Sources */ = {isa = PBXBuildFile; fileRef = C63FB3501497EA4E007961C1 /* SMFile.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
- C6A831E314981267001FB62F /* StackMobLoginTest.m in Sources */ = {isa = PBXBuildFile; fileRef = C6A831E214981267001FB62F /* StackMobLoginTest.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
- DE1AEA2215705E7C000EC712 /* AtomicCounterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DE1AEA2115705E7C000EC712 /* AtomicCounterTests.m */; };
+ DE63FE9715A7781E003E9283 /* StackMobLoginTest.m in Sources */ = {isa = PBXBuildFile; fileRef = C6A831E214981267001FB62F /* StackMobLoginTest.m */; };
+ DE63FE9915A7785C003E9283 /* APIRequestTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 82CE45F212892FFA00A73E9C /* APIRequestTests.m */; };
+ DE63FE9B15A7843C003E9283 /* StackMobPushTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9369485F147228B400575176 /* StackMobPushTests.m */; };
+ DE63FE9C15A78440003E9283 /* StackMobQueryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E3C7523D14871A18007CFEF1 /* StackMobQueryTests.m */; };
+ DE63FE9D15A78447003E9283 /* SMFileTest.m in Sources */ = {isa = PBXBuildFile; fileRef = C63FB3551497EDF4007961C1 /* SMFileTest.m */; };
+ DE63FE9E15A7844B003E9283 /* AtomicCounterTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DE1AEA2115705E7C000EC712 /* AtomicCounterTests.m */; };
+ DEDE6A4E15A3A89D00703089 /* StackMobOAuth2Test.m in Sources */ = {isa = PBXBuildFile; fileRef = DEDE6A4D15A3A89D00703089 /* StackMobOAuth2Test.m */; };
E33D87DB144616CF00FBEB96 /* StackMobVersion.h in Headers */ = {isa = PBXBuildFile; fileRef = E33D87DA144616CF00FBEB96 /* StackMobVersion.h */; };
E3897ECA14885AC000E45FB7 /* StackMobBulkRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = E3897EC814885AC000E45FB7 /* StackMobBulkRequest.h */; };
E3897ECB14885AC000E45FB7 /* StackMobBulkRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = E3897EC914885AC000E45FB7 /* StackMobBulkRequest.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
E3897ECC14885AC000E45FB7 /* StackMobBulkRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = E3897EC914885AC000E45FB7 /* StackMobBulkRequest.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
E3C752391486FBA3007CFEF1 /* SMGeoPoint.h in Headers */ = {isa = PBXBuildFile; fileRef = E3C752371486FBA3007CFEF1 /* SMGeoPoint.h */; };
E3C7523A1486FBA3007CFEF1 /* SMGeoPoint.m in Sources */ = {isa = PBXBuildFile; fileRef = E3C752381486FBA3007CFEF1 /* SMGeoPoint.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
- E3C7523E14871A19007CFEF1 /* StackMobQueryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E3C7523D14871A18007CFEF1 /* StackMobQueryTests.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
E3C7523F14873321007CFEF1 /* SMGeoPoint.m in Sources */ = {isa = PBXBuildFile; fileRef = E3C752381486FBA3007CFEF1 /* SMGeoPoint.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
E3D728D71448BE5700804149 /* StackMobQuery.h in Headers */ = {isa = PBXBuildFile; fileRef = E3D728D51448BE5700804149 /* StackMobQuery.h */; };
E3D728D81448BE5700804149 /* StackMobQuery.m in Sources */ = {isa = PBXBuildFile; fileRef = E3D728D61448BE5700804149 /* StackMobQuery.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
@@ -226,6 +227,8 @@
D2AAC07E0554694100DB518D /* libStackMobiOS.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libStackMobiOS.a; sourceTree = BUILT_PRODUCTS_DIR; };
DE1AEA2015705E7C000EC712 /* AtomicCounterTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AtomicCounterTests.h; path = tests/AtomicCounterTests.h; sourceTree = "<group>"; };
DE1AEA2115705E7C000EC712 /* AtomicCounterTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AtomicCounterTests.m; path = tests/AtomicCounterTests.m; sourceTree = "<group>"; };
+ DEDE6A4C15A3A89D00703089 /* StackMobOAuth2Test.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = StackMobOAuth2Test.h; path = tests/StackMobOAuth2Test.h; sourceTree = "<group>"; };
+ DEDE6A4D15A3A89D00703089 /* StackMobOAuth2Test.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = StackMobOAuth2Test.m; path = tests/StackMobOAuth2Test.m; sourceTree = "<group>"; };
E33D87DA144616CF00FBEB96 /* StackMobVersion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StackMobVersion.h; sourceTree = "<group>"; };
E3897EC814885AC000E45FB7 /* StackMobBulkRequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StackMobBulkRequest.h; sourceTree = "<group>"; };
E3897EC914885AC000E45FB7 /* StackMobBulkRequest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = StackMobBulkRequest.m; sourceTree = "<group>"; };
@@ -490,6 +493,8 @@
C63FB3551497EDF4007961C1 /* SMFileTest.m */,
DE1AEA2015705E7C000EC712 /* AtomicCounterTests.h */,
DE1AEA2115705E7C000EC712 /* AtomicCounterTests.m */,
+ DEDE6A4C15A3A89D00703089 /* StackMobOAuth2Test.h */,
+ DEDE6A4D15A3A89D00703089 /* StackMobOAuth2Test.m */,
);
name = Tests;
sourceTree = "<group>";
@@ -682,19 +687,20 @@
038CB29614284F04001DEAC9 /* StackMobSession.m in Sources */,
038CB29314284C97001DEAC9 /* StackMob.m in Sources */,
038CB29414284C97001DEAC9 /* StackMobRequest.m in Sources */,
- 82CE45F412892FFA00A73E9C /* APIRequestTests.m in Sources */,
E3C7523F14873321007CFEF1 /* SMGeoPoint.m in Sources */,
E3D728D91448BE5700804149 /* StackMobQuery.m in Sources */,
- 93694862147228B400575176 /* StackMobPushTests.m in Sources */,
93694863147228B400575176 /* StackMobTestUtils.m in Sources */,
9369486514722C5C00575176 /* StackMobTestCommon.m in Sources */,
E3897ECC14885AC000E45FB7 /* StackMobBulkRequest.m in Sources */,
- E3C7523E14871A19007CFEF1 /* StackMobQueryTests.m in Sources */,
- C63FB3581497EF38007961C1 /* SMFileTest.m in Sources */,
C63FB3591497F22A007961C1 /* SMFile.m in Sources */,
- C6A831E314981267001FB62F /* StackMobLoginTest.m in Sources */,
560BB28315224413004BB615 /* SecureUDID.m in Sources */,
- DE1AEA2215705E7C000EC712 /* AtomicCounterTests.m in Sources */,
+ DEDE6A4E15A3A89D00703089 /* StackMobOAuth2Test.m in Sources */,
+ DE63FE9715A7781E003E9283 /* StackMobLoginTest.m in Sources */,
+ DE63FE9915A7785C003E9283 /* APIRequestTests.m in Sources */,
+ DE63FE9B15A7843C003E9283 /* StackMobPushTests.m in Sources */,
+ DE63FE9C15A78440003E9283 /* StackMobQueryTests.m in Sources */,
+ DE63FE9D15A78447003E9283 /* SMFileTest.m in Sources */,
+ DE63FE9E15A7844B003E9283 /* AtomicCounterTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
17 tests/APIRequestTests.m
@@ -179,15 +179,28 @@ - (void)testRelatedPost {
- (void)testRelatedPut {
NSArray *putArry = [NSArray arrayWithObjects:@"one", @"two", nil];
- StackMobRequest *r = [[StackMob stackmob] put:@"primary_schema" withId:@"primary_key1" andField:@"array" andArguments:putArry andCallback:^(BOOL success, id result) {}];
+ StackMobRequest *r = [[StackMob stackmob] put:@"primary_schema" withId:@"primary_key2" andField:@"related_many" andArguments:putArry andCallback:^(BOOL success, id result) {}];
[self assertNotNSError:[StackMobTestUtils runDefaultRunLoopAndGetDictionaryResultFromRequest:r]];
}
- (void)testRelatedDelete {
- StackMobRequest *r = [[StackMob stackmob] removeIds:[NSArray arrayWithObjects:@"one", @"two", nil] forSchema:@"primary_schema" andId:@"primary_key2" andField:@"related_many" shouldCascade:YES withCallback:^(BOOL success, id result) {}];
+ NSDictionary *one = [NSDictionary dictionaryWithObjectsAndKeys:@"abc", @"name", nil];
+ NSArray *argsArray = [NSArray arrayWithObjects:one, one, nil];
+ __block NSString *id1;
+ __block NSString *id2;
+ StackMobRequest *r = [[StackMob stackmob] post:@"primary_schema" withId:@"primary_key2" andField:@"related_many" andBulkArguments:argsArray andCallback:^(BOOL success, id result) {
+ STAssertTrue(success, @"unsuccessful post to create related objects");
+ NSArray *resultSuccessDict = [(NSDictionary *)result objectForKey:@"success"];
+ id1 = [resultSuccessDict objectAtIndex:0];
+ id2 = [resultSuccessDict objectAtIndex:1];
+ }];
[self assertNotNSError:[StackMobTestUtils runDefaultRunLoopAndGetDictionaryResultFromRequest:r]];
+
+ StackMobRequest *r2 = [[StackMob stackmob] removeIds:[NSArray arrayWithObjects:id1, id2, nil] forSchema:@"primary_schema" andId:@"primary_key2" andField:@"related_many" shouldCascade:YES withCallback:^(BOOL success, id result) {}];
+
+ [self assertNotNSError:[StackMobTestUtils runDefaultRunLoopAndGetDictionaryResultFromRequest:r2]];
}
- (void) testDoubleFieldSet {
View
2 tests/StackMobLoginTest.h
@@ -26,6 +26,8 @@
@interface StackMobLoginTest : StackMobTestCommon
- (void)testSetCookieLoginLogout;
+- (void)createUser;
+- (void)deleteUser;
@end
View
32 tests/StackMobLoginTest.m
@@ -15,17 +15,43 @@
#import <UIKit/UIKit.h>
#import "NSData+Base64.h"
#import "StackMobLoginTest.h"
-
-#define USER_NAME @"USER_NAME_HERE"
-#define USER_PASSWORD @"USER_PASSWORD_HERE"
+#import "StackMobTestUtils.h"
//#import "application_headers" as required
@implementation StackMobLoginTest
- (void)setUp {
[super setUp];
+ [self createUser];
+}
+
+- (void)tearDown
+{
+ [self deleteUser];
+}
+
+- (void)createUser
+{
+ NSDictionary *createArgs = [NSDictionary dictionaryWithObjectsAndKeys:USER_NAME, @"username", USER_PASSWORD, @"password", nil];
+ StackMobRequest *createRequest = [[StackMob stackmob] post:@"user" withArguments:createArgs andCallback:^(BOOL success, id result) {
+ STAssertTrue(success, @"user not created");
+
+ }];
+
+ [StackMobTestUtils runRunLoop:[NSRunLoop currentRunLoop] untilRequestFinished:createRequest];
+}
+
+- (void)deleteUser
+{
+ NSDictionary *destroyArgs = [NSDictionary dictionaryWithObjectsAndKeys:USER_NAME, @"username", nil];
+ StackMobRequest *deleteRequest = [[StackMob stackmob] destroy:@"user" withArguments:destroyArgs andCallback:^(BOOL success, id result) {
+ STAssertTrue(success, @"object not deleted");
+ }];
+
+ [StackMobTestUtils runRunLoop:[NSRunLoop currentRunLoop] untilRequestFinished:deleteRequest];
}
+
- (void)testSetCookieLoginLogout {
NSMutableDictionary *loginRequest = [[NSMutableDictionary alloc] init];
[loginRequest setValue:USER_NAME forKey:@"username"];
View
27 tests/StackMobOAuth2Test.h
@@ -0,0 +1,27 @@
+//
+// StackMobOAuth2Test.h
+// StackMobiOS
+//
+// Created by Matt Vaznaian on 7/3/12.
+// Copyright (c) 2012 StackMob, Inc. All rights reserved.
+//
+
+// TO TEST: create a schema oauth2test with create permissions set to any logged in user with OAuth2 and read, update and delete permissions set to
+// Object owner logged in with OAuth2.
+// Also make sure your user schema is open to create and delete from.
+
+#import <SenTestingKit/SenTestingKit.h>
+#import "StackMob.h"
+#import "StackMobTestUtils.h"
+#import "StackMobTestCommon.h"
+
+@interface StackMobOAuth2Test : SenTestCase
+
+- (void)setUp;
+- (void)tearDown;
+- (void)assertNotNSError:(id)obj;
+- (void)createUser;
+- (void)deleteUser;
+- (void)testLoginAndLogoutWithOAuth2;
+- (void)changeOAuthVersion:(int)oAuthVersion;
+@end
View
123 tests/StackMobOAuth2Test.m
@@ -0,0 +1,123 @@
+//
+// StackMobOAuth2Test.m
+// StackMobiOS
+//
+// Created by Matt Vaznaian on 7/3/12.
+// Copyright (c) 2012 StackMob, Inc. All rights reserved.
+//
+
+
+#import "StackMobOAuth2Test.h"
+
+@implementation StackMobOAuth2Test
+
+- (void)setUp
+{
+ [StackMob setApplication:OAuth2 key:kAPIKey secret:kAPISecret appName:kAppName subDomain:@"mob1" userObjectName:@"user" apiVersionNumber:[NSNumber numberWithInt:kVersion]];
+ [self createUser];
+}
+
+- (void)tearDown
+{
+ [self deleteUser];
+}
+
+- (void)assertNotNSError:(id)obj
+{
+ if([obj class] == [NSError class]) STFail(@"object %@ is an error", obj);
+}
+
+- (void)changeOAuthVersion:(int)oAuthVersion
+{
+ [[[StackMob stackmob] session] setOauthVersion:oAuthVersion];
+}
+
+- (void)createUser
+{
+ NSDictionary *createArgs = [NSDictionary dictionaryWithObjectsAndKeys:USER_NAME, @"username", USER_PASSWORD, @"password", nil];
+ StackMobRequest *createRequest = [[StackMob stackmob] post:@"user" withArguments:createArgs andCallback:^(BOOL success, id result) {
+ STAssertTrue(success, @"user not created");
+
+ }];
+
+ [StackMobTestUtils runRunLoop:[NSRunLoop currentRunLoop] untilRequestFinished:createRequest];
+}
+
+- (void)deleteUser
+{
+ NSDictionary *destroyArgs = [NSDictionary dictionaryWithObjectsAndKeys:USER_NAME, @"username", nil];
+ StackMobRequest *deleteRequest = [[StackMob stackmob] destroy:@"user" withArguments:destroyArgs andCallback:^(BOOL success, id result) {
+ STAssertTrue(success, @"object not deleted");
+ }];
+
+ [StackMobTestUtils runRunLoop:[NSRunLoop currentRunLoop] untilRequestFinished:deleteRequest];
+}
+
+- (void)testLoginWithOAuth2
+{
+ [[[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];
+
+ NSDictionary *createArgs = [NSDictionary dictionaryWithObjectsAndKeys:@"cool", @"name", @"cool", @"oauth2test_id", nil];
+ StackMobRequest *createRequest = [[StackMob stackmob] post:@"oauth2test" withArguments:createArgs andCallback:^(BOOL success, id result) {
+ SMLog(@"result from create is %@", result);
+ STAssertTrue(success, @"object not created");
+
+ }];
+
+ [StackMobTestUtils runRunLoop:[NSRunLoop currentRunLoop] untilRequestFinished:createRequest];
+
+ StackMobRequest *readRequest = [[StackMob stackmob] get:@"oauth2test/cool" withCallback:^(BOOL success, id result) {
+ SMLog(@"result from read is %@", result);
+ STAssertTrue(success, @"object not read");
+ }];
+
+ [StackMobTestUtils runRunLoop:[NSRunLoop currentRunLoop] untilRequestFinished:readRequest];
+
+
+ NSDictionary *destroyArgs = [NSDictionary dictionaryWithObjectsAndKeys:@"cool", @"oauth2test_id", nil];
+ StackMobRequest *deleteRequest = [[StackMob stackmob] destroy:@"oauth2test" withArguments:destroyArgs andCallback:^(BOOL success, id result) {
+ SMLog(@"deleted object with result %@", result);
+ STAssertTrue(success, @"object not deleted");
+ }];
+
+ [StackMobTestUtils runRunLoop:[NSRunLoop currentRunLoop] untilRequestFinished:deleteRequest];
+
+ //test logout
+ StackMobRequest *logoutRequest = [[StackMob stackmob] logoutWithCallback:^(BOOL success, id result) {
+ STAssertTrue(success, @"did not logout successfully");
+ STAssertTrue([[StackMob stackmob] isLoggedOut], @"You are still logged in");
+ }];
+
+ [StackMobTestUtils runRunLoop:[NSRunLoop currentRunLoop] untilRequestFinished:logoutRequest];
+
+ //attempt to create, should fail
+ NSDictionary *createArgs2 = [NSDictionary dictionaryWithObjectsAndKeys:@"cool", @"name", @"cool", @"oauth2test_id", nil];
+ StackMobRequest *createRequest2 = [[StackMob stackmob] post:@"oauth2test" withArguments:createArgs2 andCallback:^(BOOL success, id result) {
+ SMLog(@"result from create is %@", result);
+ STAssertTrue(!success, @"should not have been able to create");
+
+ }];
+
+ [StackMobTestUtils runRunLoop:[NSRunLoop currentRunLoop] untilRequestFinished:createRequest2];
+
+}
+
+@end
View
2 tests/StackMobTestCommon.m
@@ -15,10 +15,12 @@
#import "StackMobTestCommon.h"
#import <UIKit/UIKit.h>
+
@implementation StackMobTestCommon
- (void)setUp {
[StackMob setApplication:OAuth1 key:kAPIKey secret:kAPISecret appName:kAppName subDomain:@"mob1" userObjectName:@"user" apiVersionNumber:[NSNumber numberWithInt:kVersion]];
+ [[[StackMob stackmob] session] setOauthVersion:OAuth1];
emptyCallback = ^(BOOL success, id result) {};
}
View
3 tests/StackMobTestUtils.h
@@ -15,6 +15,9 @@
#import <Foundation/Foundation.h>
#import "StackMob.h"
+#define USER_NAME @"bob"
+#define USER_PASSWORD @"bob"
+
extern NSString * const kAPIKey;
extern NSString * const kAPISecret;
extern NSString * const kSubDomain;

0 comments on commit f7371ff

Please sign in to comment.