@@ -7,6 +7,13 @@ const user = {
77 username : 'SOME_USERNAME' ,
88 passHash : 'SOME_PASSHASH'
99} ;
10+ const cookie = {
11+ img : 'SOME COOKIE IMG' ,
12+ text : 'SOME COOKIE TEXT' ,
13+ likes : 0 ,
14+ dislikes : 0 ,
15+ id : 42
16+ } ;
1017
1118describe ( 'Tests' , function ( ) {
1219 describe ( 'Get cookies tests' , function ( ) {
@@ -225,6 +232,250 @@ describe('Tests', function() {
225232 . then ( done , done ) ;
226233 } ) ;
227234 } ) ;
235+
236+ describe ( 'Logout tests' , function ( ) {
237+ beforeEach ( function ( ) {
238+ sinon . stub ( requester , 'putJSON' )
239+ . returns ( new Promise ( ( resolve , reject ) => {
240+ resolve ( {
241+ result : {
242+ username : user . username ,
243+ authKey : AUTH_KEY
244+ }
245+ } ) ;
246+ } ) ) ;
247+ localStorage . clear ( ) ;
248+ } ) ;
249+ afterEach ( function ( ) {
250+ requester . putJSON . restore ( ) ;
251+ localStorage . clear ( ) ;
252+ } ) ;
253+
254+ it ( 'expect localStorage to have no username after logout' , function ( done ) {
255+ dataService . login ( )
256+ . then ( ( ) => {
257+ return dataService . logout ( ) ;
258+ } )
259+ . then ( ( ) => {
260+ expect ( localStorage . getItem ( 'username' ) ) . to . be . null ;
261+ } )
262+ . then ( done , done ) ;
263+ } ) ;
264+
265+ it ( 'expect localStorage to have no authKey after logout' , function ( done ) {
266+ dataService . login ( )
267+ . then ( ( ) => {
268+ return dataService . logout ( ) ;
269+ } )
270+ . then ( ( ) => {
271+ expect ( localStorage . getItem ( 'authKey' ) ) . to . be . null ;
272+ } )
273+ . then ( done , done ) ;
274+ } ) ;
275+ } ) ;
276+
277+ describe ( 'Add cookie tests' , function ( ) {
278+ let cookiesDB = [ ] ;
279+
280+ beforeEach ( function ( ) {
281+ sinon . stub ( requester , 'postJSON' , ( route , cookie , options ) => {
282+ return new Promise ( ( resolve , reject ) => {
283+ cookiesDB . push ( cookie ) ;
284+ // add userId to cookie?
285+ resolve ( cookie ) ;
286+ } ) ;
287+ } ) ;
288+ sinon . stub ( requester , 'putJSON' )
289+ . returns ( new Promise ( ( resolve , reject ) => {
290+ resolve ( {
291+ result : {
292+ username : user . username ,
293+ authKey : AUTH_KEY
294+ }
295+ } ) ;
296+ } ) ) ;
297+ localStorage . clear ( ) ;
298+ cookiesDB = [ ] ;
299+ } ) ;
300+ afterEach ( function ( ) {
301+ requester . postJSON . restore ( ) ;
302+ requester . putJSON . restore ( ) ;
303+ localStorage . clear ( ) ;
304+ } ) ;
305+
306+ it ( 'expect postJSON to be called for cookie' , function ( done ) {
307+ dataService . login ( user )
308+ . then ( ( ) => {
309+ return dataService . addCookie ( cookie ) ;
310+ } )
311+ . then ( ( ) => {
312+ expect ( requester . postJSON . calledOnce ) . to . be . true ;
313+ } )
314+ . then ( done , done ) ;
315+ } ) ;
316+ it ( 'expect postJSON to be called with correct route' , function ( done ) {
317+ dataService . login ( user )
318+ . then ( ( ) => {
319+ return dataService . addCookie ( cookie ) ;
320+ } )
321+ . then ( ( ) => {
322+ const actual = requester . postJSON
323+ . firstCall
324+ . args [ 0 ] ;
325+ expect ( actual ) . to . equal ( '/api/cookies' ) ;
326+ } )
327+ . then ( done , done ) ;
328+ } ) ;
329+ it ( 'expect postJSON to be called with the cookie' , function ( done ) {
330+ dataService . login ( user )
331+ . then ( ( ) => {
332+ return dataService . addCookie ( cookie ) ;
333+ } )
334+ . then ( ( ) => {
335+ const actual = requester . postJSON
336+ . firstCall
337+ . args [ 1 ] ;
338+ expect ( actual ) . to . eql ( cookie ) ;
339+ } )
340+ . then ( done , done ) ;
341+ } ) ;
342+ it ( 'expect postJSON to be called with authorization headers' , function ( done ) {
343+ dataService . login ( user )
344+ . then ( ( ) => {
345+ return dataService . addCookie ( cookie ) ;
346+ } )
347+ . then ( ( ) => {
348+ const actual = requester . postJSON
349+ . firstCall
350+ . args [ 2 ] ;
351+ expect ( actual . headers [ HTTP_HEADER_KEY ] ) . to . equal ( AUTH_KEY ) ;
352+ } )
353+ . then ( done , done ) ;
354+ } ) ;
355+ it ( 'expect cookie to be added to cookieDB' , function ( done ) {
356+ dataService . login ( user )
357+ . then ( ( ) => {
358+ return dataService . addCookie ( cookie ) ;
359+ } )
360+ . then ( ( ) => {
361+ expect ( cookiesDB ) . to . eql ( [ cookie ] ) ;
362+ } )
363+ . then ( done , done ) ;
364+ } ) ;
365+ } ) ;
366+
367+ describe ( 'Rate cookie tests' , function ( ) {
368+ let cookiesDB = [ ] ;
369+
370+ beforeEach ( function ( ) {
371+ sinon . stub ( requester , 'putJSON' , ( route , type , options ) => {
372+ return new Promise ( ( resolve , reject ) => {
373+ if ( route === '/api/auth' ) {
374+ resolve ( {
375+ result : {
376+ username : user . username ,
377+ authKey : AUTH_KEY
378+ }
379+ } ) ;
380+ return ;
381+ } ;
382+
383+ const cookieId = + route . split ( '/' ) [ 3 ] ;
384+ const cookieToRate = cookiesDB . find ( ( c ) => c . id === cookieId ) ;
385+
386+ if ( type . type . type === 'like' ) {
387+ cookieToRate . likes += 1 ;
388+ }
389+ else if ( type . type . type === 'dislike' ) {
390+ cookieToRate . dislikes += 1 ;
391+ }
392+
393+ resolve ( cookieToRate ) ;
394+ } ) ;
395+ } ) ;
396+ localStorage . clear ( ) ;
397+ cookiesDB = [ cookie ] ;
398+ } ) ;
399+ afterEach ( function ( ) {
400+ requester . putJSON . restore ( ) ;
401+ localStorage . clear ( ) ;
402+ } ) ;
403+
404+ it ( 'expect putJSON to be called for rating' , function ( done ) {
405+ dataService . login ( user )
406+ . then ( ( ) => {
407+ return dataService . rateCookie ( cookie . id , { type :'like' } ) ;
408+ } )
409+ . then ( ( ) => {
410+ expect ( requester . putJSON . calledTwice ) . to . be . true ;
411+ } )
412+ . then ( done , done ) ;
413+ } ) ;
414+ it ( 'expect cookie rating to be done with correct route' , function ( done ) {
415+ dataService . login ( user )
416+ . then ( ( ) => {
417+ return dataService . rateCookie ( cookie . id , { type :'like' } ) ;
418+ } )
419+ . then ( ( ) => {
420+ const actual = requester . putJSON
421+ . secondCall
422+ . args [ 0 ] ;
423+ expect ( actual ) . to . equal ( `/api/cookies/${ cookie . id } ` ) ;
424+ } )
425+ . then ( done , done ) ;
426+ } ) ;
427+ it ( 'expect cookie rating type to be the used type of rating' , function ( done ) {
428+ dataService . login ( user )
429+ . then ( ( ) => {
430+ return dataService . rateCookie ( cookie . id , { type :'like' } ) ;
431+ } )
432+ . then ( ( ) => {
433+ const actual = requester . putJSON
434+ . secondCall
435+ . args [ 1 ] ;
436+ expect ( actual . type . type ) . to . equal ( 'like' ) ;
437+ } )
438+ . then ( done , done ) ;
439+ } ) ;
440+ it ( 'expect cookie rating to be called for the given cookie id' , function ( done ) {
441+ dataService . login ( user )
442+ . then ( ( ) => {
443+ return dataService . rateCookie ( cookie . id , { type :'like' } ) ;
444+ } )
445+ . then ( ( ) => {
446+ const actual = requester . putJSON
447+ . secondCall
448+ . args [ 2 ] ;
449+ expect ( actual . headers [ HTTP_HEADER_KEY ] ) . to . equal ( AUTH_KEY ) ;
450+ } )
451+ . then ( done , done ) ;
452+ } ) ;
453+
454+ it ( 'expect liking a cookie to increment likes value of cookie' , function ( done ) {
455+ let previousLikes = cookiesDB [ 0 ] . likes ;
456+
457+ dataService . login ( user )
458+ . then ( ( ) => {
459+ return dataService . rateCookie ( cookie . id , { type :'like' } ) ;
460+ } )
461+ . then ( ( ) => {
462+ expect ( cookiesDB [ 0 ] . likes ) . to . equal ( previousLikes + 1 ) ;
463+ } )
464+ . then ( done , done ) ;
465+ } ) ;
466+ it ( 'expect disliking a cookie to increment dislikes value of cookie' , function ( done ) {
467+ let previousDislikes = cookiesDB [ 0 ] . dislikes ;
468+
469+ dataService . login ( user )
470+ . then ( ( ) => {
471+ return dataService . rateCookie ( cookie . id , { type :'dislike' } ) ;
472+ } )
473+ . then ( ( ) => {
474+ expect ( cookiesDB [ 0 ] . dislikes ) . to . equal ( previousDislikes + 1 ) ;
475+ } )
476+ . then ( done , done ) ;
477+ } ) ;
478+ } ) ;
228479} ) ;
229480
230481mocha . run ( ) ;
0 commit comments