Permalink
Browse files

Add specs for last AuthenticationViewController changes and refactor …

…a bit.
  • Loading branch information...
1 parent 4b89fff commit f85b34d4013195b2618601a0c30bd0df13f02bce @alloy alloy committed May 25, 2011
@@ -24,6 +24,7 @@
+ (BOOL)signIn;
+ (void)retrieveUsername:(NSString **)username password:(NSString **)password;
++ (void)saveAndAuthenticate:(NSString *)username password:(NSString *)password;
+ (void)authenticate:(NSString *)username password:(NSString *)password;
- (IBAction)dismissDialog:(id)sender;
@@ -24,6 +24,14 @@ + (void)retrieveUsername:(NSString **)username password:(NSString **)password {
}
}
++ (void)saveAndAuthenticate:(NSString *)username password:(NSString *)password {
+ NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
+ [defaults setValue:username forKey:@"Username"]; // TODO move to constant
+ [defaults synchronize];
+ [SSKeychain setPassword:password forService:@"iTrakt" account:username];
+ [self authenticate:username password:password];
+}
+
+ (void)authenticate:(NSString *)username password:(NSString *)password {
[[Trakt sharedInstance] setApiUser:username];
[[Trakt sharedInstance] setApiPassword:password];
@@ -111,21 +119,14 @@ - (IBAction)saveCredentials:(id)sender {
NSString *username = self.usernameField.text;
NSString *password = self.passwordField.text;
- [AuthenticationViewController authenticate:username password:password];
-
- NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
- [defaults setValue:username forKey:@"Username"]; // TODO move to constant
- [defaults synchronize];
- [SSKeychain setPassword:password forService:@"iTrakt" account:username];
+ [AuthenticationViewController saveAndAuthenticate:username password:password];
signingIn = YES;
[self.tableView reloadData];
[[Trakt sharedInstance] verifyCredentials:^(BOOL valid) {
if (valid) {
- NSLog(@"VALID!");
[self dismissDialog:self];
} else {
- NSLog(@"NOT VALID!");
self.doneButton.enabled = YES;
self.usernameField.enabled = YES;
self.passwordField.enabled = YES;
View
@@ -59,7 +59,7 @@ - (NSURL *)verifyCredentialsURL {
}
- (void)verifyCredentials:(void (^)(BOOL valid))block {
- NSString *json = [NSString stringWithFormat:@"{ \"username\": \"%@\", \"password\": \"%@\" }", self.apiUser, self.apiPasswordHash];
+ NSString *json = [NSString stringWithFormat:@"{ \"username\":\"%@\", \"password\":\"%@\" }", self.apiUser, self.apiPasswordHash];
JSONDownload *dl = [[JSONDownload alloc] initWithURL:[self verifyCredentialsURL] postBody:json username:nil password:nil block:^(id response) {
NSString *status = (NSString *)[(NSDictionary *)response objectForKey:@"status"];
if (status && [status isEqualToString:@"success"]) {
@@ -8,40 +8,88 @@
;)
(describe "AuthenticationViewController" `(
+ (before (do ()
+ (set @rootController ((UIViewController alloc) init))
+ (set @controller ((AuthenticationViewController alloc) initWithNibName:"AuthenticationViewController" bundle:nil))
+ ((UIBacon sharedWindow) setRootViewController:@rootController)
+ (@rootController presentModalViewController:@controller animated:nil)
+
+ (set @usernameField (@controller usernameField))
+ (set @passwordField (@controller passwordField))
+ ))
+
+ (after (do ()
+ (@controller dismissModalViewControllerAnimated:nil)
+ ((UIBacon sharedWindow) setRootViewController:nil)
+ ))
+
(describe "when not authenticated" `(
(before (do ()
- (set @controller ((AuthenticationViewController alloc) initWithNibName:"AuthenticationViewController" bundle:nil))
- ;(window setRootViewController:@controller)
- ((UIBacon sharedWindow) setRootViewController:@controller)
+ (@usernameField setText:nil)
+ (@passwordField setText:nil)
+ (@controller textDidChange:nil)
))
(after (do ()
- ((UIBacon sharedWindow) setRootViewController:nil)
+ (AuthenticationViewController saveAndAuthenticate:"bob" password:"secret")
))
(it "has a `Sign in' button which is disabled when the username/password fields are empty" (do ()
(set button ($ "Sign in"))
(~ button should not be:nil)
(~ button should not be enabled)
- ((@controller usernameField) setText:"bob")
+ (@usernameField setText:"bob")
(@controller textDidChange:nil) ; action send by textfield
(~ button should not be enabled)
- ((@controller passwordField) setText:"secret")
+ (@passwordField setText:"secret")
(@controller textDidChange:nil) ; action send by textfield
(~ button should be enabled)
- ((@controller usernameField) setText:"")
+ (@usernameField setText:"")
(@controller textDidChange:nil) ; action send by textfield
(~ button should not be enabled)
))
- (it "saves the credentials and dismisses the view" (do ()
- ((@controller usernameField) setText:"bob")
- ((@controller passwordField) setText:"secret")
+ (it "saves the credentials and if they're valid dismisses the view" (do ()
+ (@usernameField setText:"bob")
+ (@passwordField setText:"secret")
+ (@controller textDidChange:nil) ; action send by textfield
+ (($ "Sign in") touch)
+
+ (~ ($ "Sign in") should not be enabled)
+ (~ @usernameField should not be enabled)
+ (~ @passwordField should not be enabled)
+
+ (wait 0.8 (do ()
+ (~ (@rootController modalViewController) should be:nil)
+ ))
+ ))
+
+ (it "does not dismiss the view if the credentials are invalid" (do ()
+ (@usernameField setText:"bob")
+ (@passwordField setText:"wrong")
(@controller textDidChange:nil) ; action send by textfield
(($ "Sign in") touch)
+
+ (~ ($ "Sign in") should not be enabled)
+ (~ @usernameField should not be enabled)
+ (~ @passwordField should not be enabled)
+
+ (wait 0.1 (do ()
+ (~ (@rootController modalViewController) should be:@controller)
+ (~ ($ "Sign in") should be enabled)
+ (~ @usernameField should be enabled)
+ (~ @passwordField should be enabled)
+ ))
+ ))
+ ))
+
+ (describe "when authenticated" `(
+ (it "fills in the username and password" (do ()
+ (~ (@usernameField text) should be:"bob")
+ (~ (@passwordField text) should be:"secret")
))
))
))
@@ -77,6 +77,14 @@ mappings = lambda do
end
end
+ map('/trakt/account/test/apikey') do
+ if $env['REQUEST_METHOD'] == 'POST' && $env['rack.input'].read == "{ \"username\":\"bob\", \"password\":\"e5e9fa1ba31ecd1ae84f75caaa474f3a663f05f4\" }"
+ send_json "{ \"status\":\"success\" }"
+ else
+ send_json "{ \"status\":\"failure\" }", 401
+ end
+ end
+
end
@@ -147,8 +155,12 @@ module FixtureServe
send_text(fixture_io("#{name}.txt"))
end
+ def send_json(json, status = 200)
+ [status, { 'Content-Type' => 'application/json' }, json]
+ end
+
def serve_json_fixture(name)
- [200, { 'Content-Type' => 'application/json' }, fixture_io("#{name}.json")]
+ send_json(fixture_io("#{name}.json"))
end
def serve_jpeg_fixture(name)

0 comments on commit f85b34d

Please sign in to comment.