diff --git a/services/sync/tests/unit/fake_login_manager.js b/services/sync/tests/unit/fake_login_manager.js new file mode 100644 index 00000000000..3c01d24479e --- /dev/null +++ b/services/sync/tests/unit/fake_login_manager.js @@ -0,0 +1,38 @@ +Cu.import("resource://weave/util.js"); + +// ---------------------------------------- +// Fake Sample Data +// ---------------------------------------- + +let fakeSampleLogins = [ + // Fake nsILoginInfo object. + {hostname: "www.boogle.com", + formSubmitURL: "http://www.boogle.com/search", + httpRealm: "", + username: "", + password: "", + usernameField: "test_person", + passwordField: "test_password"} +]; + +// ---------------------------------------- +// Fake Login Manager +// ---------------------------------------- + +function FakeLoginManager(fakeLogins) { + this.fakeLogins = fakeLogins; + + let self = this; + + Utils.getLoginManager = function fake_getLoginManager() { + // Return a fake nsILoginManager object. + return { + getAllLogins: function() { return self.fakeLogins; }, + addLogin: function(login) { + getTestLogger().info("nsILoginManager.addLogin() called " + + "with hostname '" + login.hostname + "'."); + self.fakeLogins.push(login); + } + }; + }; +} diff --git a/services/sync/tests/unit/test_password_syncing.js b/services/sync/tests/unit/test_password_syncing.js new file mode 100644 index 00000000000..90af22cb7fb --- /dev/null +++ b/services/sync/tests/unit/test_password_syncing.js @@ -0,0 +1,42 @@ +Cu.import("resource://weave/engines/passwords.js"); + +load("fake_login_manager.js"); + +// ---------------------------------------- +// Test Logic +// ---------------------------------------- + +function run_test() { + var syncTesting = new SyncTestingInfrastructure(); + var fakeLoginManager = new FakeLoginManager(fakeSampleLogins); + + function freshEngineSync(cb) { + let engine = new PasswordEngine(); + engine.sync(cb); + }; + + syncTesting.runAsyncFunc("initial sync", freshEngineSync); + + syncTesting.runAsyncFunc("trivial re-sync", freshEngineSync); + + fakeLoginManager.fakeLogins.push( + {hostname: "www.yoogle.com", + formSubmitURL: "http://www.yoogle.com/search", + httpRealm: "", + username: "", + password: "", + usernameField: "test_person2", + passwordField: "test_password2"} + ); + + syncTesting.runAsyncFunc("add user and re-sync", freshEngineSync); + + fakeLoginManager.fakeLogins.pop(); + + syncTesting.runAsyncFunc("remove user and re-sync", freshEngineSync); + + syncTesting.fakeFilesystem.fakeContents = {}; + fakeLoginManager.fakeLogins = []; + + syncTesting.runAsyncFunc("resync on second computer", freshEngineSync); +} diff --git a/services/sync/tests/unit/test_passwords.log.expected b/services/sync/tests/unit/test_password_syncing.log.expected similarity index 100% rename from services/sync/tests/unit/test_passwords.log.expected rename to services/sync/tests/unit/test_password_syncing.log.expected diff --git a/services/sync/tests/unit/test_passwords.js b/services/sync/tests/unit/test_passwords.js index 9b69927314d..ee9ca4fda7b 100644 --- a/services/sync/tests/unit/test_passwords.js +++ b/services/sync/tests/unit/test_passwords.js @@ -1,92 +1,19 @@ -Cu.import("resource://weave/util.js"); +load("fake_login_manager.js"); -// ---------------------------------------- -// Fake Data -// ---------------------------------------- +var loginMgr = new FakeLoginManager(fakeSampleLogins); -let __fakeLogins = [ - // Fake nsILoginInfo object. - {hostname: "www.boogle.com", - formSubmitURL: "http://www.boogle.com/search", - httpRealm: "", - username: "", - password: "", - usernameField: "test_person", - passwordField: "test_password"} -]; +// The JS module we're testing, with all members exposed. +var passwords = loadInSandbox("resource://weave/engines/passwords.js"); -// ---------------------------------------- -// Test Logic -// ---------------------------------------- - -function run_test() { - // The JS module we're testing, with all members exposed. - var passwords = loadInSandbox("resource://weave/engines/passwords.js"); - - // Ensure that _hashLoginInfo() works. - var fakeUserHash = passwords._hashLoginInfo(__fakeLogins[0]); +function test_hashLoginInfo_works() { + var fakeUserHash = passwords._hashLoginInfo(fakeSampleLogins[0]); do_check_eq(typeof fakeUserHash, 'string'); do_check_eq(fakeUserHash.length, 40); +} - // Ensure that PasswordSyncCore._itemExists() works. +function test_synccore_itemexists_works() { + var fakeUserHash = passwords._hashLoginInfo(fakeSampleLogins[0]); var psc = new passwords.PasswordSyncCore(); - psc.__loginManager = {getAllLogins: function() { return __fakeLogins; }}; do_check_false(psc._itemExists("invalid guid")); do_check_true(psc._itemExists(fakeUserHash)); - - // Make sure the engine can sync. - var syncTesting = new SyncTestingInfrastructure(); - var fakeLoginManager = new FakeLoginManager(__fakeLogins); - - function freshEngineSync(cb) { - let engine = new passwords.PasswordEngine(); - engine.sync(cb); - }; - - syncTesting.runAsyncFunc("initial sync", freshEngineSync); - - syncTesting.runAsyncFunc("trivial re-sync", freshEngineSync); - - fakeLoginManager.fakeLogins.push( - {hostname: "www.yoogle.com", - formSubmitURL: "http://www.yoogle.com/search", - httpRealm: "", - username: "", - password: "", - usernameField: "test_person2", - passwordField: "test_password2"} - ); - - syncTesting.runAsyncFunc("add user and re-sync", freshEngineSync); - - fakeLoginManager.fakeLogins.pop(); - - syncTesting.runAsyncFunc("remove user and re-sync", freshEngineSync); - - syncTesting.fakeFilesystem.fakeContents = {}; - fakeLoginManager.fakeLogins = []; - - syncTesting.runAsyncFunc("resync on second computer", freshEngineSync); -} - -// ---------------------------------------- -// Fake Infrastructure -// ---------------------------------------- - -function FakeLoginManager(fakeLogins) { - this.fakeLogins = fakeLogins; - - let self = this; - - Utils.getLoginManager = function fake_getLoginManager() { - // Return a fake nsILoginManager object. - return { - getAllLogins: function() { return self.fakeLogins; }, - addLogin: function(login) { - getTestLogger().info("nsILoginManager.addLogin() called " + - "with hostname '" + login.hostname + "'."); - self.fakeLogins.push(login); - } - }; - }; }