11'use strict' ;
2- const execSync = require ( 'child_process' ) . execSync ;
3- const fs = require ( 'fs' ) ;
4-
52const _ = require ( 'underscore' ) ;
63const assert = require ( 'chai' ) . assert ;
74const rewire = require ( 'rewire' ) ;
85
96const log = require ( '../../lib/log' ) ;
107const config = require ( '../../lib/config' ) ;
11-
12- const cache = rewire ( '../../lib/cache' ) ;
13- const h = rewire ( '../../lib/helper' ) ;
14- const session = rewire ( '../../lib/session' ) ;
15- const plugin = rewire ( '../../lib/plugins/cache' ) ;
16-
17- const HOME = './tmp' ;
8+ const th = require ( '../helper' ) ;
189
1910describe ( 'plugin:cache' , function ( ) {
11+ let plugin ;
12+ let next ;
13+ let cache ;
14+ let h ;
15+ let session ;
16+
2017 const PROBLEMS = [
2118 { id : 0 , fid : 0 , name : 'name0' , slug : 'slug0' , starred : false , category : 'algorithms' } ,
2219 { id : 1 , fid : 1 , name : 'name1' , slug : 'slug1' , starred : true , category : 'algorithms' }
2320 ] ;
2421 const PROBLEM = { id : 0 , fid : 0 , slug : 'slug0' , category : 'algorithms' } ;
2522
26- const NEXT = { } ;
27-
2823 before ( function ( ) {
2924 log . init ( ) ;
3025 config . init ( ) ;
31- plugin . init ( ) ;
26+ } ) ;
3227
33- h . getCacheDir = ( ) => HOME ;
28+ beforeEach ( function ( ) {
29+ th . clean ( ) ;
30+ next = { } ;
31+
32+ h = rewire ( '../../lib/helper' ) ;
33+ h . getCacheDir = ( ) => th . DIR ;
34+
35+ cache = rewire ( '../../lib/cache' ) ;
3436 cache . __set__ ( 'h' , h ) ;
3537 cache . init ( ) ;
3638
39+ session = rewire ( '../../lib/session' ) ;
3740 session . __set__ ( 'cache' , cache ) ;
41+
42+ plugin = rewire ( '../../lib/plugins/cache' ) ;
3843 plugin . __set__ ( 'cache' , cache ) ;
3944 plugin . __set__ ( 'session' , session ) ;
40- plugin . setNext ( NEXT ) ;
41- } ) ;
45+ plugin . init ( ) ;
4246
43- beforeEach ( function ( ) {
44- execSync ( 'rm -rf ' + HOME ) ;
45- fs . mkdirSync ( HOME ) ;
47+ plugin . setNext ( next ) ;
4648 } ) ;
4749
4850 describe ( '#getProblems' , function ( ) {
@@ -58,7 +60,7 @@ describe('plugin:cache', function() {
5860
5961 it ( 'should getProblems w/o cache ok' , function ( done ) {
6062 cache . del ( 'problems' ) ;
61- NEXT . getProblems = cb => cb ( null , PROBLEMS ) ;
63+ next . getProblems = cb => cb ( null , PROBLEMS ) ;
6264
6365 plugin . getProblems ( function ( e , problems ) {
6466 assert . equal ( e , null ) ;
@@ -69,7 +71,7 @@ describe('plugin:cache', function() {
6971
7072 it ( 'should getProblems w/o cache fail if client error' , function ( done ) {
7173 cache . del ( 'problems' ) ;
72- NEXT . getProblems = cb => cb ( 'client getProblems error' ) ;
74+ next . getProblems = cb => cb ( 'client getProblems error' ) ;
7375
7476 plugin . getProblems ( function ( e , problems ) {
7577 assert . equal ( e , 'client getProblems error' ) ;
@@ -93,7 +95,7 @@ describe('plugin:cache', function() {
9395 it ( 'should getProblem w/o cache ok' , function ( done ) {
9496 cache . set ( 'problems' , PROBLEMS ) ;
9597 cache . del ( '0.slug0.algorithms' ) ;
96- NEXT . getProblem = ( problem , cb ) => cb ( null , PROBLEMS [ 0 ] ) ;
98+ next . getProblem = ( problem , cb ) => cb ( null , PROBLEMS [ 0 ] ) ;
9799
98100 plugin . getProblem ( _ . clone ( PROBLEM ) , function ( e , problem ) {
99101 assert . equal ( e , null ) ;
@@ -105,7 +107,7 @@ describe('plugin:cache', function() {
105107 it ( 'should getProblem fail if client error' , function ( done ) {
106108 cache . set ( 'problems' , PROBLEMS ) ;
107109 cache . del ( '0.slug0.algorithms' ) ;
108- NEXT . getProblem = ( problem , cb ) => cb ( 'client getProblem error' ) ;
110+ next . getProblem = ( problem , cb ) => cb ( 'client getProblem error' ) ;
109111
110112 plugin . getProblem ( _ . clone ( PROBLEM ) , function ( e , problem ) {
111113 assert . equal ( e , 'client getProblem error' ) ;
@@ -171,7 +173,7 @@ describe('plugin:cache', function() {
171173 assert . equal ( session . getUser ( ) , null ) ;
172174 assert . equal ( session . isLogin ( ) , false ) ;
173175
174- NEXT . login = ( user , cb ) => cb ( null , user ) ;
176+ next . login = ( user , cb ) => cb ( null , user ) ;
175177
176178 plugin . login ( USER , function ( e , user ) {
177179 assert . equal ( e , null ) ;
@@ -188,7 +190,7 @@ describe('plugin:cache', function() {
188190 config . autologin . enable = false ;
189191 cache . del ( h . KEYS . user ) ;
190192
191- NEXT . login = ( user , cb ) => cb ( null , user ) ;
193+ next . login = ( user , cb ) => cb ( null , user ) ;
192194
193195 plugin . login ( USER , function ( e , user ) {
194196 assert . equal ( e , null ) ;
@@ -200,7 +202,7 @@ describe('plugin:cache', function() {
200202 } ) ;
201203
202204 it ( 'should login fail if client login error' , function ( done ) {
203- NEXT . login = ( user , cb ) => cb ( 'client login error' ) ;
205+ next . login = ( user , cb ) => cb ( 'client login error' ) ;
204206
205207 plugin . login ( USER , function ( e , user ) {
206208 assert . equal ( e , 'client login error' ) ;
@@ -220,5 +222,18 @@ describe('plugin:cache', function() {
220222 assert . equal ( session . isLogin ( ) , false ) ;
221223 done ( ) ;
222224 } ) ;
225+
226+ it ( 'should logout ok' , function ( done ) {
227+ // before logout
228+ cache . set ( h . KEYS . user , USER ) ;
229+ assert . deepEqual ( session . getUser ( ) , USER ) ;
230+ assert . equal ( session . isLogin ( ) , true ) ;
231+
232+ // after logout
233+ plugin . logout ( null , true ) ;
234+ assert . equal ( session . getUser ( ) , null ) ;
235+ assert . equal ( session . isLogin ( ) , false ) ;
236+ done ( ) ;
237+ } ) ;
223238 } ) ; // #user
224239} ) ;
0 commit comments