From 8d53fa82ed2f13d7728554d4d4a25d6555ae63c0 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Sat, 27 Sep 2014 15:14:32 -0300 Subject: [PATCH] add Profile model --- js/models/Profile.js | 38 +++++++++++++++++++++++++++++ test/test.Profile.js | 57 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 js/models/Profile.js create mode 100644 test/test.Profile.js diff --git a/js/models/Profile.js b/js/models/Profile.js new file mode 100644 index 000000000..9665710c7 --- /dev/null +++ b/js/models/Profile.js @@ -0,0 +1,38 @@ +'use strict'; +var preconditions = require('preconditions').singleton(); +var _ = require('underscore'); +var log = require('../log'); +var bitcore = require('bitcore'); + +function Profile(opts, storage) { + preconditions.checkArgument(opts.email); + preconditions.checkArgument(opts.password); + preconditions.checkArgument(storage); + preconditions.checkArgument(storage.getItem); + + this.email = opts.email; + this.password = opts.password; + this.hash = bitcore.util.sha256ripe160(this.email + this.password); + + this.extra = opts.extra; +}; + +Profile.fromObj = function(obj, storage) { + return new Profile(obj, storage); +}; + +Profile.prototype.toObj = function() { + return JSON.parse(JSON.stringify(this)); +}; + + +Profile.prototype.store = function(cb) { +// TODO + return cb(); +// this.storage.setItem(this.hash, this.toObj()); +}; + + + +module.exports = Profile; + diff --git a/test/test.Profile.js b/test/test.Profile.js new file mode 100644 index 000000000..e7a066100 --- /dev/null +++ b/test/test.Profile.js @@ -0,0 +1,57 @@ +'use strict'; + +var chai = chai || require('chai'); +var should = chai.should(); +var bitcore = bitcore || require('bitcore'); +var buffertools = bitcore.buffertools; +var Profile = require('../js/models/Profile') +var sinon = require('sinon'); +var FakeStorage = function() {}; + +describe('Profile model', function() { + var email = 'email@pepe.com'; + var password = 'iamnotsatoshi'; + var storage = new FakeStorage(); + var opts = { + email: email, + password: password, + }; + + beforeEach(function() { + storage.getItem = sinon.stub(); + }); + + it('should fail create an instance', function() { + (function() { + new Profile({ + email: email, + }, storage) + }).should.throw('Illegal Arg'); + }); + + it('should create an instance', function() { + var p = new Profile({ + email: email, + password: password + }, storage); + should.exist(p); + }); + + it('#fromObj #toObj round trip', function() { + + var p = new Profile(opts, storage); + var p2 = Profile.fromObj(p.toObj(), storage); + p2.should.deep.equal(p); + }); + + it('#store', function(done) { + var p = new Profile(opts, storage); + p.store(function(err) { + should.not.exist(err); + done(); + }) + }); + + + +});