     func testRemoveLogin() {
         let expectation = self.expectationWithDescription("Remove login")
 
-        addLogin(login)() >>>
+        addLogin(login) >>>
             removeLogin(login) >>>
             getLoginsFor(login.protectionSpace, expected: []) >>>
-            done(expectation)
+            done(login.protectionSpace, expectation: expectation)
 
         waitForExpectationsWithTimeout(10.0, handler: nil)
     }

    }

    func testAddLogin() {
        let expectation = self.expectationWithDescription("Add login")

        addLogin(login)() >>>
            getLoginsFor(login.protectionSpace, expected: [login]) >>>
            done(expectation)

        waitForExpectationsWithTimeout(10.0, handler: nil)
    }

        let expectation = self.expectationWithDescription("Update login")
        let updated = Login.createWithHostname("hostname1", username: "username1", password: "password3")

        addLogin(login) >>>
            updateLogin(updated) >>>
            getLoginsFor(login.protectionSpace, expected: [updated]) >>>
            done(login.protectionSpace, expectation: expectation)

        waitForExpectationsWithTimeout(10.0, handler: nil)
    }

    /*
    func testAddUseOfLogin() {
        let expectation = self.expectationWithDescription("Add visit")

        if var usageData = login as? LoginUsageData {
            usageData.timeCreated = NSDate.nowMicroseconds()
        }

        addLogin(login) >>>
            addUseDelayed(login, time: 1) >>>
            getLoginDetailsFor(login, expected: login as! LoginUsageData) >>>
            done(login.protectionSpace, expectation: expectation)

        waitForExpectationsWithTimeout(10.0, handler: nil)
    }
    */

    func done(protectionSpace: NSURLProtectionSpace, expectation: XCTestExpectation)() -> Success {
        return removeAllLogins() >>>
            getLoginsFor(protectionSpace, expected: []) >>> {
                expectation.fulfill()
                return succeed()
        }
    }

    // Note: These functions are all curried so that we pass arguments, but still chain them below
    func addLogin(login: LoginData) -> Success {
        log.debug("Add \(login)")
        return logins.addLogin(login)
    }

    func addLoginCurried(login: LoginData) -> (() -> Success) {
        return { return self.addLogin(login) }
    }

    func updateLogin(login: LoginData) -> (() -> Success) {
        return {
            log.debug("Update \(login)")
            var usage = login as! LoginUsageData
            usage.timePasswordChanged = NSDate.nowMicroseconds()
            return self.logins.updateLogin(login)
        }
    }

    func addUseDelayed(login: LoginData, time: UInt32) -> (() -> Success) {
        return {
            sleep(time)
            if var usageData = login as? LoginUsageData {
                usageData.timeLastUsed = NSDate.nowMicroseconds()
            }
            let res = self.logins.addUseOf(login)
            sleep(time)
            return res
        }
    }

    func getLoginsFor(protectionSpace: NSURLProtectionSpace, expected: [LoginData]) -> (() -> Success) {
        return {
            log.debug("Get logins for \(protectionSpace)")
            return self.logins.getLoginsForProtectionSpace(protectionSpace) >>== { results in
                XCTAssertEqual(expected.count, results.count)
                for (index, login) in enumerate(expected) {
                    XCTAssertEqual(results[index]!.username!, login.username!)
                    XCTAssertEqual(results[index]!.hostname, login.hostname)
                    XCTAssertEqual(results[index]!.password, login.password)
                }
                return succeed()
            }
        }
    }

    /*
    func getLoginDetailsFor(login: LoginData, expected: LoginUsageData) -> (() -> Success) {
        return {
            log.debug("Get details for \(login)")
            let deferred = self.logins.getUsageDataForLogin(login)
            log.debug("Final result \(deferred)")
            return deferred >>== { l in
                log.debug("Got cursor")
                XCTAssertLessThan(expected.timePasswordChanged - l.timePasswordChanged, 10)
                XCTAssertLessThan(expected.timeLastUsed - l.timeLastUsed, 10)
                XCTAssertLessThan(expected.timeCreated - l.timeCreated, 10)
                return succeed()
            }
        }
    }
    */

    func removeLogin(login: LoginData) -> (() -> Success) {
        return {
            log.debug("Remove \(login)")
            return self.logins.removeLogin(login)
        }
    }

    func removeAllLogins() -> Success {
        log.debug("Remove All")
        return self.logins.removeAll()
    }
}