refactor iden #create

This commit is contained in:
Matias Alejo Garcia 2014-09-30 15:28:02 -03:00
commit 606ea0668c
10 changed files with 72 additions and 54 deletions

View file

@ -1,12 +1,12 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $location, identity, notification, controllerUtils) { angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $location, notification, controllerUtils) {
controllerUtils.redirIfLogged(); controllerUtils.redirIfLogged();
$scope.retreiving = true; //$scope.retreiving = true;
identity.getWallets(function(err,ret) { // identity.getWallets(function(err,ret) {
$scope.retreiving = false; // $scope.retreiving = false;
$scope.hasWallets = (ret && ret.length > 0) ? true : false; // $scope.hasWallets = (ret && ret.length > 0) ? true : false;
}); // });
}); });

View file

@ -57,15 +57,12 @@ function Identity(email, password, opts) {
// open wallets // open wallets
this.wallets = []; this.wallets = [];
this.profile = Identity._newProfile({
email: email,
}, password, this.storage);
}; };
/* for stubbing */ /* for stubbing */
Identity._newProfile = function(info, password, storage) { Identity._createProfile = function(email, password, storage, cb) {
return new Profile(info, password, storage); Profile.create(email, password, storage, cb);
}; };
Identity._newInsight = function(opts) { Identity._newInsight = function(opts) {
@ -93,7 +90,7 @@ Identity._walletDelete = function(id, cb) {
}; };
Identity._profileOpen = function(e, p, s, cb) { Identity._profileOpen = function(e, p, s, cb) {
return Profile.open(e, p, s, cb); Profile.create(e, p, s, cb);
}; };
@ -109,11 +106,13 @@ Identity._profileOpen = function(e, p, s, cb) {
* @return {undefined} * @return {undefined}
*/ */
Identity.create = function(email, password, opts, cb) { Identity.create = function(email, password, opts, cb) {
var iden = new Identity(email, password, opts); var iden = new Identity(email, password, opts);
iden.store({ Identity._createProfile(email, password, iden.storage, function(err, profile) {
overwrite: false, if (err) return cb(err);
}, function(err) {
return cb(err, iden); iden.profile = profile;
return cb(null, iden);
}); });
}; };
@ -144,7 +143,7 @@ Identity.prototype.validate = function(authcode, cb) {
Identity.open = function(email, password, opts, cb) { Identity.open = function(email, password, opts, cb) {
var iden = new Identity(email, password, opts); var iden = new Identity(email, password, opts);
Identity._profileOpen(email, password, iden.storage, function(err, profile){ Identity._profileOpen(email, password, iden.storage, function(err, profile) {
if (err) return cb(err); if (err) return cb(err);
iden.profile = profile; iden.profile = profile;
@ -174,6 +173,8 @@ Identity.isAvailable = function(email, opts, cb) {
* @return {undefined} * @return {undefined}
*/ */
Identity.prototype.store = function(opts, cb) { Identity.prototype.store = function(opts, cb) {
preconditions.checkState(this.profile);
var self = this; var self = this;
self.profile.store(opts, function(err) { self.profile.store(opts, function(err) {
if (err) return cb(err); if (err) return cb(err);

View file

@ -31,8 +31,8 @@ PluginManager.prototype._register = function(obj, name) {
var type = obj.type; var type = obj.type;
var kind = PluginManager.TYPE[type]; var kind = PluginManager.TYPE[type];
preconditions.checkArgument(kind, 'Plugin has unknown type' + name); preconditions.checkArgument(kind, 'Unknown plugin type:' + name);
preconditions.checkState(kind !== PluginManager.KIND_UNIQUE || !this.registered[type], 'Plugin kind already registered: ' + name); preconditions.checkState(kind !== PluginManager.KIND_UNIQUE || !this.registered[type], 'Plugin kind already registered:' + name);
if (kind === PluginManager.KIND_UNIQUE) { if (kind === PluginManager.KIND_UNIQUE) {
this.registered[type] = obj; this.registered[type] = obj;

View file

@ -24,7 +24,18 @@ Profile.hash = function(email, password) {
}; };
Profile.key = function(hash) { Profile.key = function(hash) {
return 'identity::' + hash; return 'profile::' + hash;
};
Profile.create = function(email, password, storage, cb) {
preconditions.checkArgument(cb);
var p = new Profile({
email: email,
hash: Profile.hash(email,password),
}, storage);
p.store(cb);
}; };
Profile.open = function(email, password, storage, cb) { Profile.open = function(email, password, storage, cb) {

View file

@ -10,6 +10,10 @@ angular
templateUrl: 'views/home.html', templateUrl: 'views/home.html',
validate: false validate: false
}) })
.when('/createProfile', {
templateUrl: 'views/createProfile.html',
validate: false
})
.when('/open', { .when('/open', {
templateUrl: 'views/open.html', templateUrl: 'views/open.html',
validate: false validate: false

View file

@ -1,5 +0,0 @@
'use strict';
angular.module('copayApp.services').factory('identity', function(pluginManager){
return new copay.Identity(config, copay.version, pluginManager);
});

View file

@ -12,7 +12,7 @@ function GoogleDrive(config) {
this.home = config.home || 'copay'; this.home = config.home || 'copay';
this.idCache = {}; this.idCache = {};
this.type = 'STORAGE'; this.type = 'DB';
this.scripts = [{ this.scripts = [{
then: this.initLoaded.bind(this), then: this.initLoaded.bind(this),

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
function LocalStorage() { function LocalStorage() {
this.type = 'STORAGE'; this.type = 'DB';
}; };
LocalStorage.prototype.init = function() { LocalStorage.prototype.init = function() {

View file

@ -30,7 +30,7 @@ function assertObjectEqual(a, b) {
describe('Identity model', function() { describe('Identity model', function() {
var iden, storage, wallet, profile; var iden, storage, wallet, profile;
beforeEach(function() { beforeEach(function(done) {
storage = sinon.stub(); storage = sinon.stub();
storage.getItem = sinon.stub(); storage.getItem = sinon.stub();
storage.setPassphrase = sinon.spy(); storage.setPassphrase = sinon.spy();
@ -52,11 +52,14 @@ describe('Identity model', function() {
profile.listWallets = sinon.stub().returns([]); profile.listWallets = sinon.stub().returns([]);
profile.setLastOpenedTs = sinon.stub().yields(null);; profile.setLastOpenedTs = sinon.stub().yields(null);;
profile.store = sinon.stub().yields(null);; profile.store = sinon.stub().yields(null);;
Identity._newProfile = sinon.stub().returns(profile); Identity._createProfile = sinon.stub().callsArgWith(3,null,profile);
iden = new Identity(email, password, config); Identity.create(email, password, config, function(err,i){
iden = i;
done();
});
}); });
@ -105,25 +108,18 @@ describe('Identity model', function() {
var iden = new Identity(email, password, config); var iden = new Identity(email, password, config);
should.exist(iden); should.exist(iden);
iden.walletDefaults.should.deep.equal(config.wallet); iden.walletDefaults.should.deep.equal(config.wallet);
iden.version.should.equal('0.0.1');
should.exist(iden.profile.addWallet);
Identity._newProfile.getCall(0).args[0].should.deep.equal({
email: email
});
Identity._newProfile.getCall(0).args[1].should.equal(password);
Identity._newProfile.getCall(0).args[2].should.equal(iden.storage);
}); });
}); });
describe('#create', function(done) { describe('#create', function(done) {
it('should call .store', function(done) { it('should call .store', function(done) {
Identity.create(email, password, config, function(err, iden) { Identity.create(email, password, config, function(err, iden) {
should.not.exist(err); should.not.exist(err);
should.exist(iden.profile.addWallet); should.exist(iden.profile.addWallet);
iden.profile.store.getCall(0).args[0].should.deep.equal({
overwrite: false Identity._createProfile.getCall(0).args[0].should.deep.equal(email);
}); Identity._createProfile.getCall(0).args[1].should.deep.equal(password);
done(); done();
}); });
}); });

View file

@ -1,4 +1,7 @@
<div class="home" ng-controller="HomeController"> <div class="home" ng-controller="HomeController">
<P>( TODO1: only this form if there is any profile:: key)
<p>( TODO2: if user has wallets (wallet::) show message: Copay now needs a profile to ... , you can import your wallets after creating your profile )
<div data-alert class="loading-screen" ng-show="retreiving"> <div data-alert class="loading-screen" ng-show="retreiving">
<i class="size-60 fi-bitcoin-circle icon-rotate spinner"></i> <i class="size-60 fi-bitcoin-circle icon-rotate spinner"></i>
Retreiving information from storage... Retreiving information from storage...
@ -8,25 +11,33 @@
<img src="img/logo-negative-beta.svg" alt="Copay" width="146" height="59"> <img src="img/logo-negative-beta.svg" alt="Copay" width="146" height="59">
<div ng-include="'views/includes/version.html'"></div> <div ng-include="'views/includes/version.html'"></div>
</div> </div>
<div class="large-8 columns"> <div class="large-8 columns line-dashed-setup-v">
<div class="button-setup" ng-show="hasWallets">
<a translate class="text-white" href="#!/open">Open a wallet</a>
</div>
<div class="button-setup" ng-show="!hasWallets">
<a translate class="text-secondary" href="#!/create">Create a new wallet</a>
</div>
<div class="button-setup"> <div class="button-setup">
<a translate class="text-primary" href="#!/join">Join a Wallet in Creation</a> <h1 class="text-white line-sidebar-b" translate >Login </h1>
</div> <form name="settingsForm">
<div class="button-setup" ng-show="hasWallets"> <fieldset>
<a translate class="text-secondary" href="#!/create">Create a wallet</a> <label for="insight-livenet">Email</label>
<input type="text" ng-model="profile.email" class="form-control" name="profile-email">
<label for="insight-testnet">Password</label>
<input type="text" ng-model="profile.password" class="form-control" name="profile-password">
</fieldset>
<div class="text-right">
<button translate type="submit" class="button primary m0 ng-binding" ng-disabled="setupForm.$invalid || loading" disabled="disabled" ng-click="save()">
Login
</button>
</div>
</form>
</div> </div>
<div class="button-setup">
<a translate class="text-secondary" href="#!/createProfile">Create a profile</a>
</div>
<div class="footer-setup"> <div class="footer-setup">
<a class="right size-12 text-gray" href="#!/settings"><i class="m10r <a class="right size-12 text-gray" href="#!/settings"><i class="m10r
size-14 fi-wrench"></i><span translate>Settings</span></a> size-14 fi-wrench"></i><span translate>Settings</span></a>
<a class="left size-12 text-gray" href="#!/import"><i class="m10r
size-14 fi-upload"></i><span translate>Import a backup</span></a>
</div> </div>
</div> </div>
</div> </div>