@@ -104,35 +104,61 @@ describe('Private Key DAO unit tests', function() {
104104
105105 describe ( 'isSynced' , function ( ) {
106106 beforeEach ( function ( ) {
107+ sinon . stub ( privkeyDao , '_getFolder' ) ;
107108 sinon . stub ( privkeyDao , '_fetchMessage' ) ;
108109 } ) ;
109110 afterEach ( function ( ) {
111+ privkeyDao . _getFolder . restore ( ) ;
110112 privkeyDao . _fetchMessage . restore ( ) ;
111113 } ) ;
112114
113115 it ( 'should be synced' , function ( done ) {
116+
117+ privkeyDao . _getFolder . returns ( resolves ( 'foo' ) ) ;
114118 privkeyDao . _fetchMessage . returns ( resolves ( { } ) ) ;
115119
116120 privkeyDao . isSynced ( ) . then ( function ( synced ) {
117121 expect ( synced ) . to . be . true ;
122+ expect ( privkeyDao . _getFolder . calledOnce ) . to . be . true ;
123+ expect ( privkeyDao . _fetchMessage . calledOnce ) . to . be . true ;
124+
118125 done ( ) ;
119126 } ) ;
120127 } ) ;
121128
122129 it ( 'should not be synced' , function ( done ) {
130+ privkeyDao . _getFolder . returns ( resolves ( ) ) ;
123131 privkeyDao . _fetchMessage . returns ( resolves ( ) ) ;
124132
125133 privkeyDao . isSynced ( ) . then ( function ( synced ) {
126134 expect ( synced ) . to . be . false ;
135+ expect ( privkeyDao . _getFolder . calledOnce ) . to . be . true ;
136+ expect ( privkeyDao . _fetchMessage . calledOnce ) . to . be . true ;
137+
127138 done ( ) ;
128139 } ) ;
129140 } ) ;
130141
131142 it ( 'should not be synced in case of error' , function ( done ) {
143+ privkeyDao . _getFolder . returns ( rejects ( new Error ( ) ) ) ;
144+
145+ privkeyDao . isSynced ( ) . then ( function ( synced ) {
146+ expect ( synced ) . to . be . false ;
147+ expect ( privkeyDao . _getFolder . calledOnce ) . to . be . true ;
148+
149+ done ( ) ;
150+ } ) ;
151+ } ) ;
152+
153+ it ( 'should not be synced in case of error' , function ( done ) {
154+ privkeyDao . _getFolder . returns ( resolves ( 'foo' ) ) ;
132155 privkeyDao . _fetchMessage . returns ( rejects ( new Error ( ) ) ) ;
133156
134157 privkeyDao . isSynced ( ) . then ( function ( synced ) {
135158 expect ( synced ) . to . be . false ;
159+ expect ( privkeyDao . _getFolder . calledOnce ) . to . be . true ;
160+ expect ( privkeyDao . _fetchMessage . calledOnce ) . to . be . true ;
161+
136162 done ( ) ;
137163 } ) ;
138164 } ) ;
@@ -146,15 +172,18 @@ describe('Private Key DAO unit tests', function() {
146172 } ] ;
147173
148174 beforeEach ( function ( ) {
175+ sinon . stub ( privkeyDao , '_getFolder' ) ;
149176 sinon . stub ( privkeyDao , '_fetchMessage' ) ;
150177 sinon . stub ( privkeyDao , '_parse' ) ;
151178 } ) ;
152179 afterEach ( function ( ) {
180+ privkeyDao . _getFolder . restore ( ) ;
153181 privkeyDao . _fetchMessage . restore ( ) ;
154182 privkeyDao . _parse . restore ( ) ;
155183 } ) ;
156184
157185 it ( 'should fail if key not synced' , function ( done ) {
186+ privkeyDao . _getFolder . returns ( resolves ( 'foo' ) ) ;
158187 privkeyDao . _fetchMessage . returns ( resolves ( ) ) ;
159188
160189 privkeyDao . download ( {
@@ -167,6 +196,7 @@ describe('Private Key DAO unit tests', function() {
167196 } ) ;
168197
169198 it ( 'should work' , function ( done ) {
199+ privkeyDao . _getFolder . returns ( resolves ( 'foo' ) ) ;
170200 privkeyDao . _fetchMessage . returns ( resolves ( { } ) ) ;
171201 imapClientStub . getBodyParts . returns ( resolves ( ) ) ;
172202 privkeyDao . _parse . returns ( resolves ( root ) ) ;
@@ -253,14 +283,7 @@ describe('Private Key DAO unit tests', function() {
253283 } ) ;
254284 } ) ;
255285
256- describe ( '_fetchMessage' , function ( ) {
257- it ( 'should fail due to invalid args' , function ( done ) {
258- privkeyDao . _fetchMessage ( { } ) . catch ( function ( err ) {
259- expect ( err . message ) . to . match ( / I n c o m p l e t e / ) ;
260- done ( ) ;
261- } ) ;
262- } ) ;
263-
286+ describe ( '_getFolder' , function ( ) {
264287 it ( 'should fail if imap folder does not exist' , function ( done ) {
265288 imapClientStub . listWellKnownFolders . returns ( resolves ( {
266289 Inbox : [ {
@@ -270,13 +293,10 @@ describe('Private Key DAO unit tests', function() {
270293 path : 'foo'
271294 } ]
272295 } ) ) ;
273- imapClientStub . listMessages . returns ( rejects ( new Error ( ) ) ) ;
274296
275- privkeyDao . _fetchMessage ( {
276- userId : emailAddress ,
277- keyId : keyId
278- } ) . catch ( function ( err ) {
297+ privkeyDao . _getFolder ( ) . catch ( function ( err ) {
279298 expect ( err . message ) . to . match ( / I m a p f o l d e r / ) ;
299+ expect ( imapClientStub . listWellKnownFolders . calledOnce ) . to . be . true ;
280300 done ( ) ;
281301 } ) ;
282302 } ) ;
@@ -290,6 +310,25 @@ describe('Private Key DAO unit tests', function() {
290310 path : 'openpgp_keys'
291311 } ]
292312 } ) ) ;
313+
314+ privkeyDao . _getFolder ( ) . then ( function ( path ) {
315+ expect ( path ) . to . equal ( 'openpgp_keys' ) ;
316+ expect ( imapClientStub . listWellKnownFolders . calledOnce ) . to . be . true ;
317+ done ( ) ;
318+ } ) ;
319+ } ) ;
320+ } ) ;
321+
322+ describe ( '_fetchMessage' , function ( ) {
323+ it ( 'should fail due to invalid args' , function ( done ) {
324+ privkeyDao . _fetchMessage ( { } ) . catch ( function ( err ) {
325+ expect ( err . message ) . to . match ( / I n c o m p l e t e / ) ;
326+ done ( ) ;
327+ } ) ;
328+ } ) ;
329+
330+
331+ it ( 'should work' , function ( done ) {
293332 imapClientStub . listMessages . returns ( resolves ( [ {
294333 subject : keyId
295334 } ] ) ) ;
@@ -299,21 +338,12 @@ describe('Private Key DAO unit tests', function() {
299338 keyId : keyId
300339 } ) . then ( function ( msg ) {
301340 expect ( msg . subject ) . to . equal ( keyId ) ;
302- expect ( imapClientStub . listWellKnownFolders . calledOnce ) . to . be . true ;
303341 expect ( imapClientStub . listMessages . calledOnce ) . to . be . true ;
304342 done ( ) ;
305343 } ) ;
306344 } ) ;
307345
308346 it ( 'should work with path prefix' , function ( done ) {
309- imapClientStub . listWellKnownFolders . returns ( resolves ( {
310- Inbox : [ {
311- path : 'INBOX'
312- } ] ,
313- Other : [ {
314- path : 'INBOX.openpgp_keys'
315- } ]
316- } ) ) ;
317347 imapClientStub . listMessages . returns ( resolves ( [ {
318348 subject : keyId
319349 } ] ) ) ;
@@ -323,18 +353,12 @@ describe('Private Key DAO unit tests', function() {
323353 keyId : keyId
324354 } ) . then ( function ( msg ) {
325355 expect ( msg . subject ) . to . equal ( keyId ) ;
326- expect ( imapClientStub . listWellKnownFolders . calledOnce ) . to . be . true ;
327356 expect ( imapClientStub . listMessages . calledOnce ) . to . be . true ;
328357 done ( ) ;
329358 } ) ;
330359 } ) ;
331360
332361 it ( 'should work for not matching message' , function ( done ) {
333- imapClientStub . listWellKnownFolders . returns ( resolves ( {
334- Other : [ {
335- path : 'INBOX.openpgp_keys'
336- } ]
337- } ) ) ;
338362 imapClientStub . listMessages . returns ( resolves ( [ {
339363 subject : '7890'
340364 } ] ) ) ;
@@ -349,11 +373,6 @@ describe('Private Key DAO unit tests', function() {
349373 } ) ;
350374
351375 it ( 'should work for no messages' , function ( done ) {
352- imapClientStub . listWellKnownFolders . returns ( resolves ( {
353- Other : [ {
354- path : 'INBOX.openpgp_keys'
355- } ]
356- } ) ) ;
357376 imapClientStub . listMessages . returns ( resolves ( [ ] ) ) ;
358377
359378 privkeyDao . _fetchMessage ( {
0 commit comments