Skip to content

Commit

Permalink
Merge pull request #53 from Clubjudge/auth-update
Browse files Browse the repository at this point in the history
Implements logging in with username/password combo
  • Loading branch information
inf0rmer committed May 8, 2014
2 parents 952de40 + b43574f commit ebc2db7
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 12 deletions.
24 changes: 20 additions & 4 deletions CJAPIClient/Engine/CJEngine.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ - (void)authenticateWithFacebookToken:(NSString *)facebookToken
withSuccess:(CJLoginSuccessBlock)success
andFailure:(CJLoginFailureBlock)failure
{
[self.authSessionManager POST:@"facebook_token"
parameters:@{@"token": facebookToken}
[self.authSessionManager POST:@"tokens"
parameters:@{@"facebook_token": facebookToken, @"app_key": [CJEngine clientKey]}
success:^(id operation, id responseObject) {
NSString *token = responseObject[@"access_token"];
NSString *token = responseObject[@"token"];
[CJEngine setUserToken:token];

if (success) {
Expand All @@ -109,7 +109,23 @@ - (void)authenticateWithUsername:(NSString *)username
withSuccess:(CJLoginSuccessBlock)success
andFailure:(CJLoginFailureBlock)failure
{
NSLog(@"Authenticating with username/password is not implemented yet!");
[self.authSessionManager POST:@"tokens"
parameters:@{@"email": username, @"password": password, @"app_key": [CJEngine clientKey]}
success:^(id operation, id responseObject) {
NSString *token = responseObject[@"token"];
[CJEngine setUserToken:token];

if (success) {
success(token);
}
}
failure:^(id operation, NSError *error) {
[CJEngine setUserToken:nil];

if (failure) {
failure(error);
}
}];
}

#pragma mark - Custom
Expand Down
157 changes: 149 additions & 8 deletions CJAPIClientTests/Engine/CJEngineTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ @interface CJEngine()
KWCaptureSpy *pathSpy = [engine.authSessionManager captureArgument:@selector(POST:parameters:success:failure:) atIndex:0];
KWCaptureSpy *tokenSpy = [engine.authSessionManager captureArgument:@selector(POST:parameters:success:failure:) atIndex:1];

[engine authenticateWithFacebookToken:@"12345667abcd"];
[engine authenticateWithFacebookToken:@"12345667abcd"
withSuccess:nil
andFailure:nil];

[[expectFutureValue(pathSpy.argument) shouldEventually] equal:@"facebook_token"];
[[expectFutureValue(tokenSpy.argument) shouldEventually] equal:@{@"token": @"12345667abcd"}];
[[expectFutureValue(pathSpy.argument) shouldEventually] equal:@"tokens"];
[[expectFutureValue(tokenSpy.argument) shouldEventually] equal:@{@"facebook_token": @"12345667abcd", @"app_key": [CJEngine clientKey]}];
});

context(@"when POST succeeds", ^{
Expand All @@ -86,11 +88,11 @@ @interface CJEngine()

beforeEach(^{
stubbedResponse = @{
@"access_token": @"a_token"
@"token": @"a_token"
};

stub = [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *req) {
return ([req.URL.path isEqualToString:@"/baws/auth/facebook_token"]);
return ([req.URL.path isEqualToString:@"/baws/auth/tokens"]);
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *req) {
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:stubbedResponse
Expand All @@ -109,7 +111,7 @@ @interface CJEngine()
[OHHTTPStubs removeStub:stub];
});

it(@"sets the Engine's .userToken to the \"access_token\" key in the response", ^{
it(@"sets the Engine's .userToken to the \"token\" key in the response", ^{
CJEngine *engine = [CJEngine sharedEngine];
[engine authenticateWithFacebookToken:@"123456" withSuccess:nil andFailure:nil];
[[expectFutureValue([CJEngine userToken]) shouldEventually] equal:@"a_token"];
Expand Down Expand Up @@ -141,7 +143,7 @@ @interface CJEngine()
[CJEngine setUserToken:@"something"];

stub = [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *req) {
return ([req.URL.path isEqualToString:@"/baws/auth/facebook_token"]);
return ([req.URL.path isEqualToString:@"/baws/auth/tokens"]);
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *req) {
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:stubbedResponse
Expand All @@ -162,7 +164,7 @@ @interface CJEngine()

it(@"sets the Engine's .userToken to nil", ^{
CJEngine *engine = [CJEngine sharedEngine];
[engine authenticateWithFacebookToken:@"123456"];
[engine authenticateWithFacebookToken:@"123456" withSuccess:nil andFailure:nil];
[[expectFutureValue([CJEngine userToken]) shouldEventually] beNil];
});

Expand All @@ -189,6 +191,145 @@ @interface CJEngine()

});
});


describe(@"#authenticateWithUsername:andPassword:withSuccess:andFailure", ^{
it(@"POSTs a request to the authentication API", ^{
CJEngine *engine = [CJEngine sharedEngine];

KWCaptureSpy *pathSpy = [engine.authSessionManager captureArgument:@selector(POST:parameters:success:failure:) atIndex:0];
KWCaptureSpy *tokenSpy = [engine.authSessionManager captureArgument:@selector(POST:parameters:success:failure:) atIndex:1];

[engine authenticateWithUsername:@"a_username"
andPassword:@"a_password"
withSuccess:nil
andFailure:nil];

[[expectFutureValue(pathSpy.argument) shouldEventually] equal:@"tokens"];
[[expectFutureValue(tokenSpy.argument) shouldEventually] equal:@{@"email": @"a_username", @"password": @"a_password", @"app_key": [CJEngine clientKey]}];
});

context(@"when POST succeeds", ^{
__block NSDictionary *stubbedResponse;
__block id<OHHTTPStubsDescriptor> stub = nil;

beforeEach(^{
stubbedResponse = @{
@"token": @"a_token"
};

stub = [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *req) {
return ([req.URL.path isEqualToString:@"/baws/auth/tokens"]);
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *req) {
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:stubbedResponse
options:kNilOptions
error:&error];

return [OHHTTPStubsResponse responseWithData:data
statusCode:200
headers:@{@"Content-Type":@"text/json"}];
}];

stub.name = @"postSucceeds";
});

afterEach(^{
[OHHTTPStubs removeStub:stub];
});

it(@"sets the Engine's .userToken to the \"token\" key in the response", ^{
CJEngine *engine = [CJEngine sharedEngine];
[engine authenticateWithUsername:@"a_username"
andPassword:@"a_password"
withSuccess:nil
andFailure:nil];

[[expectFutureValue([CJEngine userToken]) shouldEventually] equal:@"a_token"];
});

it(@"invokes the success block with the token as an argument", ^{
CJEngine *engine = [CJEngine sharedEngine];
__block NSString *expectedToken = nil;

[engine authenticateWithUsername:@"a_username"
andPassword:@"a_password"
withSuccess:^(NSString *token) {
expectedToken = token;
}
andFailure:nil];

[[expectFutureValue(expectedToken) shouldEventually] equal:@"a_token"];
});
});

context(@"when POST fails", ^{
__block NSDictionary *stubbedResponse;
__block id<OHHTTPStubsDescriptor> stub = nil;

beforeEach(^{
stubbedResponse = @{
@"error": @"bla"
};

[CJEngine setUserToken:@"something"];

stub = [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *req) {
return ([req.URL.path isEqualToString:@"/baws/auth/tokens"]);
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *req) {
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:stubbedResponse
options:kNilOptions
error:&error];

return [OHHTTPStubsResponse responseWithData:data
statusCode:500
headers:@{@"Content-Type":@"text/json"}];
}];

stub.name = @"postSucceeds";
});

afterEach(^{
[OHHTTPStubs removeStub:stub];
});

it(@"sets the Engine's .userToken to nil", ^{
CJEngine *engine = [CJEngine sharedEngine];
[engine authenticateWithUsername:@"a_username"
andPassword:@"a_password"
withSuccess:nil
andFailure:nil];

[[expectFutureValue([CJEngine userToken]) shouldEventually] beNil];
});

it(@"invokes the failure block with the error as an argument", ^{
CJEngine *engine = [CJEngine sharedEngine];
__block NSError *expectedError = nil;

[engine authenticateWithUsername:@"a_username"
andPassword:@"a_password"
withSuccess:nil
andFailure:^(NSError *error) {
expectedError = error;
}];

[[expectFutureValue(expectedError) shouldEventually] beNonNil];
});

describe(@"PromiseKit support", ^{
it(@"returns a Promise", ^{
CJEngine *engine = [CJEngine sharedEngine];
id promise = [engine authenticateWithUsername:@"a_username"
andPassword:@"a_password"];

[[promise should] beKindOfClass:[Promise class]];
});
});

});
});
});

SPEC_END

0 comments on commit ebc2db7

Please sign in to comment.