Refactored test_service and module code so that the weave service constructor is called, and auth is done more accurately.

This commit is contained in:
Atul Varma 2008-06-17 19:54:09 -07:00
Родитель d27f37c0a2
Коммит 8181559fa4
5 изменённых файлов: 68 добавлений и 68 удалений

Просмотреть файл

@ -58,7 +58,7 @@ IDManager.prototype = {
if (this._aliases[name])
return this._ids[this._aliases[name]];
else
return this._ids[name]
return this._ids[name];
},
set: function IDMgr_set(name, id) {
this._ids[name] = id;
@ -112,49 +112,14 @@ Identity.prototype = {
_password: null,
get password() {
if (!this._password)
return findPassword(this.realm, this.username);
return Utils.findPassword(this.realm, this.username);
return this._password;
},
set password(value) {
setPassword(this.realm, this.username, value);
Utils.setPassword(this.realm, this.username, value);
},
setTempPassword: function Id_setTempPassword(value) {
this._password = value;
}
};
// fixme: move these to util.js?
function findPassword(realm, username) {
// fixme: make a request and get the realm ?
let password;
let lm = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
let logins = lm.findLogins({}, 'chrome://sync', null, realm);
for (let i = 0; i < logins.length; i++) {
if (logins[i].username == username) {
password = logins[i].password;
break;
}
}
return password;
}
function setPassword(realm, username, password) {
// cleanup any existing passwords
let lm = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
let logins = lm.findLogins({}, 'chrome://sync', null, realm);
for(let i = 0; i < logins.length; i++) {
lm.removeLogin(logins[i]);
}
if (!password)
return;
// save the new one
let nsLoginInfo = new Components.Constructor(
"@mozilla.org/login-manager/loginInfo;1", Ci.nsILoginInfo, "init");
let login = new nsLoginInfo('chrome://sync', null, realm,
username, password, "", "");
lm.addLogin(login);
}

Просмотреть файл

@ -102,7 +102,7 @@ Utils.lazy(Weave, 'Service', WeaveSvc);
* Main entry point into Weave's sync framework
*/
function WeaveSvc() {
function WeaveSvc(engines) {
this._startupFinished = false;
this._initLogs();
this._log.info("Weave Sync Service Initializing");
@ -116,13 +116,19 @@ function WeaveSvc() {
ID.setAlias('WeaveID', 'DAV:default');
ID.setAlias('WeaveCryptoID', 'Engine:PBE:default');
// Register built-in engines
Engines.register(new BookmarksEngine());
Engines.register(new HistoryEngine());
Engines.register(new CookieEngine());
Engines.register(new PasswordEngine());
Engines.register(new FormEngine());
Engines.register(new TabEngine());
if (typeof engines == "undefined")
engines = [
new BookmarksEngine(),
new HistoryEngine(),
new CookieEngine(),
new PasswordEngine(),
new FormEngine(),
new TabEngine()
];
// Register engines
for (let i = 0; i < engines.length; i++)
Engines.register(engines[i]);
// Other misc startup
Utils.prefs.addObserver("", this, false);

Просмотреть файл

@ -51,6 +51,41 @@ Cu.import("resource://weave/log4moz.js");
let Utils = {
findPassword: function findPassword(realm, username) {
// fixme: make a request and get the realm ?
let password;
let lm = Cc["@mozilla.org/login-manager;1"]
.getService(Ci.nsILoginManager);
let logins = lm.findLogins({}, 'chrome://sync', null, realm);
for (let i = 0; i < logins.length; i++) {
if (logins[i].username == username) {
password = logins[i].password;
break;
}
}
return password;
},
setPassword: function setPassword(realm, username, password) {
// cleanup any existing passwords
let lm = Cc["@mozilla.org/login-manager;1"]
.getService(Ci.nsILoginManager);
let logins = lm.findLogins({}, 'chrome://sync', null, realm);
for (let i = 0; i < logins.length; i++)
lm.removeLogin(logins[i]);
if (!password)
return;
// save the new one
let nsLoginInfo = new Components.Constructor(
"@mozilla.org/login-manager/loginInfo;1", Ci.nsILoginInfo, "init");
let login = new nsLoginInfo('chrome://sync', null, realm,
username, password, "", "");
lm.addLogin(login);
},
// lazy load objects from a constructor on first access. It will
// work with the global object ('this' in the global context).
lazy: function Weave_lazy(dest, prop, ctr) {

Просмотреть файл

@ -42,11 +42,20 @@ FakePrefs.prototype = {
},
getIntPref: function fake_getIntPref(pref) {
return this._getPref(pref);
}
},
addObserver: function fake_addObserver() {}
};
Utils.__prefs = new FakePrefs();
Utils.findPassword = function fake_findPassword(realm, username) {
let contents = {
'Mozilla Services Password': {foo: "bar"},
'Mozilla Services Encryption Passphrase': {foo: "passphrase"}
};
return contents[realm][username];
};
Crypto.__proto__ = {
RSAkeydecrypt: function fake_RSAkeydecrypt(identity) {
let self = yield;
@ -75,33 +84,17 @@ DAV.__proto__ = {
}
};
function FakeID(realm, username, password) {
this.realm = realm;
this.username = username;
this.password = password;
this.setTempPassword = function FID_setTempPassword(value) {
if (typeof value != "undefined")
this.password = value;
};
}
ID.__proto__ = {
__contents: {WeaveID: new FakeID("", "foo", "bar"),
WeaveCryptoID: new FakeID("", "", "passphrase")},
get: function fake_ID_get(name) {
return this.__contents[name];
}
};
let Service = loadInSandbox("resource://weave/service.js");
function TestService() {
this._startupFinished = false;
this._log = Log4Moz.Service.getLogger("Service.Main");
this.__superclassConstructor = Service.WeaveSvc;
this.__superclassConstructor([]);
}
TestService.prototype = {
_initLogs: function TS__initLogs() {
this._log = Log4Moz.Service.getLogger("Service.Main");
}
};
TestService.prototype.__proto__ = Service.WeaveSvc.prototype;

Просмотреть файл

@ -1,5 +1,6 @@
*** test pending
Running test: test_login_works
Service.Main INFO Weave Sync Service Initializing
Service.Main DEBUG Logging in
Service.Main INFO Using server URL: https://example.com/user/foo
root INFO Retrieving meta/version