2013-11-19 10:14:48 -05:00
define ( function ( require ) {
'use strict' ;
var expect = chai . expect ,
2013-11-20 13:13:39 -05:00
_ = require ( 'underscore' ) ,
2013-11-19 10:14:48 -05:00
OutboxBO = require ( 'js/bo/outbox' ) ,
2013-11-20 13:13:39 -05:00
KeychainDAO = require ( 'js/dao/keychain-dao' ) ,
2013-11-19 10:14:48 -05:00
EmailDAO = require ( 'js/dao/email-dao' ) ,
DeviceStorageDAO = require ( 'js/dao/devicestorage-dao' ) ,
InvitationDAO = require ( 'js/dao/invitation-dao' ) ;
describe ( 'Outbox Business Object unit test' , function ( ) {
2013-11-20 13:13:39 -05:00
var outbox , emailDaoStub , devicestorageStub , invitationDaoStub , keychainStub ,
dummyUser = 'spiderpig@springfield.com' ;
2013-11-19 10:14:48 -05:00
beforeEach ( function ( ) {
emailDaoStub = sinon . createStubInstance ( EmailDAO ) ;
2013-11-20 13:13:39 -05:00
emailDaoStub . _account = {
emailAddress : dummyUser
} ;
2013-11-19 10:14:48 -05:00
emailDaoStub . _devicestorage = devicestorageStub = sinon . createStubInstance ( DeviceStorageDAO ) ;
2013-11-20 13:13:39 -05:00
emailDaoStub . _keychain = keychainStub = sinon . createStubInstance ( KeychainDAO ) ;
2013-11-19 10:14:48 -05:00
invitationDaoStub = sinon . createStubInstance ( InvitationDAO ) ;
outbox = new OutboxBO ( emailDaoStub , invitationDaoStub ) ;
} ) ;
afterEach ( function ( ) { } ) ;
describe ( 'init' , function ( ) {
it ( 'should work' , function ( ) {
expect ( outbox ) . to . exist ;
expect ( outbox . _emailDao ) . to . equal ( emailDaoStub ) ;
expect ( outbox . _invitationDao ) . to . equal ( invitationDaoStub ) ;
expect ( outbox . _outboxBusy ) . to . be . false ;
} ) ;
} ) ;
describe ( 'start/stop checking' , function ( ) {
it ( 'should work' , function ( ) {
function onOutboxUpdate ( err ) {
expect ( err ) . to . not . exist ;
}
outbox . startChecking ( onOutboxUpdate ) ;
expect ( outbox . _intervalId ) . to . exist ;
outbox . stopChecking ( ) ;
expect ( outbox . _intervalId ) . to . not . exist ;
} ) ;
} ) ;
2013-11-20 13:13:39 -05:00
describe ( 'process outbox' , function ( ) {
2013-11-19 10:14:48 -05:00
it ( 'should work' , function ( done ) {
2013-11-20 13:13:39 -05:00
var dummyMails = [ {
id : '123' ,
to : [ {
name : 'member' ,
address : 'member@whiteout.io'
} ]
} , {
id : '456' ,
to : [ {
name : 'invited' ,
address : 'invited@whiteout.io'
} ]
} , {
id : '789' ,
to : [ {
name : 'notinvited' ,
address : 'notinvited@whiteout.io'
} ]
} ] ;
devicestorageStub . listItems . yieldsAsync ( null , dummyMails ) ;
emailDaoStub . encryptedSend . yieldsAsync ( ) ;
emailDaoStub . send . yieldsAsync ( ) ;
devicestorageStub . removeList . yieldsAsync ( ) ;
invitationDaoStub . check . withArgs ( sinon . match ( function ( o ) { return o . recipient === 'invited@whiteout.io' ; } ) ) . yieldsAsync ( null , InvitationDAO . INVITE _PENDING ) ;
invitationDaoStub . check . withArgs ( sinon . match ( function ( o ) { return o . recipient === 'notinvited@whiteout.io' ; } ) ) . yieldsAsync ( null , InvitationDAO . INVITE _MISSING ) ;
invitationDaoStub . invite . withArgs ( sinon . match ( function ( o ) { return o . recipient === 'notinvited@whiteout.io' ; } ) ) . yieldsAsync ( null , InvitationDAO . INVITE _SUCCESS ) ;
keychainStub . getReceiverPublicKey . withArgs ( sinon . match ( function ( o ) { return o === 'member@whiteout.io' ; } ) ) . yieldsAsync ( null , 'this is not the key you are looking for...' ) ;
keychainStub . getReceiverPublicKey . withArgs ( sinon . match ( function ( o ) { return o === 'invited@whiteout.io' || o === 'notinvited@whiteout.io' ; } ) ) . yieldsAsync ( ) ;
var check = _ . after ( dummyMails . length + 1 , function ( ) {
expect ( devicestorageStub . listItems . callCount ) . to . equal ( 1 ) ;
expect ( emailDaoStub . encryptedSend . callCount ) . to . equal ( 1 ) ;
expect ( emailDaoStub . send . callCount ) . to . equal ( 1 ) ;
expect ( devicestorageStub . removeList . callCount ) . to . equal ( 1 ) ;
expect ( invitationDaoStub . check . callCount ) . to . equal ( 2 ) ;
expect ( invitationDaoStub . invite . callCount ) . to . equal ( 1 ) ;
done ( ) ;
} ) ;
2013-11-19 10:14:48 -05:00
function onOutboxUpdate ( err , count ) {
expect ( err ) . to . not . exist ;
2013-11-20 13:13:39 -05:00
expect ( count ) . to . exist ;
check ( ) ;
2013-11-19 10:14:48 -05:00
}
2013-11-20 13:13:39 -05:00
outbox . _processOutbox ( onOutboxUpdate ) ;
2013-11-19 10:14:48 -05:00
} ) ;
} ) ;
} ) ;
} ) ;