@@ -18,8 +18,11 @@ describe('test/app/controller/user.test.js', () => {
1818
1919 describe ( '- Index' , ( ) => {
2020 it ( 'should GET /user ok' , async ( ) => {
21+ app . mockContext ( { user : { is_admin : true } } ) ;
2122 user . url = 'test_url' ;
2223 await user . save ( ) ;
24+ const topic = await ctx . service . topic . newAndSave ( 'title' , 'content' , 'share' , user . _id ) ;
25+ await ctx . service . reply . newAndSave ( 'content' , topic . _id , user . _id ) ;
2326
2427 const r1 = await app . httpRequest ( ) . get ( '/user' ) ;
2528 assert ( r1 . status === 404 ) ;
@@ -35,6 +38,22 @@ describe('test/app/controller/user.test.js', () => {
3538 assert ( / < a c l a s s = " d a r k " > ( [ ^ " ] + ) < \/ a > / g. exec ( res . text ) [ 1 ] === user . loginname ) ;
3639 assert ( / < s p a n c l a s s = " b i g " > ( [ ^ " ] + ) < \/ s p a n > 积 分 / g. exec ( res . text ) [ 1 ] === user . score . toString ( ) ) ;
3740 assert ( / “ ( [ \S \s ] + ) ” / g. exec ( res . text ) [ 1 ] . replace ( / [ \t \s ] + / g, '' ) === '这家伙很懒,什么个性签名都没有留下。' ) ;
41+
42+ user . url = 'http://test_url.com' ;
43+ await user . save ( ) ;
44+ const res1 = await app . httpRequest ( ) . get ( `/user/${ loginname } ` ) ;
45+ assert ( res1 . status === 200 ) ;
46+
47+ user . active = false ;
48+ await user . save ( ) ;
49+ const res2 = await app . httpRequest ( ) . get ( `/user/${ loginname } ` ) ;
50+ assert ( res2 . status === 200 ) ;
51+ const utility = require ( 'utility' ) ;
52+ const token = utility . md5 ( user . email + user . pass + app . config . session_secret ) ;
53+ assert ( / h r e f = " \/ a c t i v e _ a c c o u n t \? k e y = ( [ ^ & ] + ) & / g. exec ( res2 . text ) [ 1 ] === token ) ;
54+
55+ user . active = true ;
56+ await user . save ( ) ;
3857 } ) ;
3958 } ) ;
4059
@@ -155,6 +174,34 @@ describe('test/app/controller/user.test.js', () => {
155174 const authUser = cookies . find ( c => c . indexOf ( '$$$$' ) > - 1 ) ;
156175 assert ( / = ( [ ^ $ ] + ) \$ / g. exec ( authUser ) [ 1 ] === user . _id . toString ( ) ) ;
157176 assert ( login . headers . location === '/' ) ;
177+
178+ const login1 = await app . httpRequest ( ) . post ( '/passport/local' ) . send ( { name : user . loginname , pass : 'pass' } ) ;
179+ assert ( login1 . status === 302 ) ;
180+ assert ( login1 . headers . location === '/signin' ) ;
181+
182+ const login2 = await app . httpRequest ( ) . post ( '/passport/local' ) . send ( { name : user . email , pass : 'newpass' } ) ;
183+ assert ( login2 . status === 302 ) ;
184+ assert ( login2 . headers . location === '/' ) ;
185+
186+ const login3 = await app . httpRequest ( ) . post ( '/passport/local' ) . send ( { name : 'noExistedUser' , pass : 'pass' } ) ;
187+ assert ( login3 . status === 302 ) ;
188+ assert ( login3 . headers . location === '/signin' ) ;
189+
190+ user . active = false ;
191+ await user . save ( ) ;
192+ const login4 = await app . httpRequest ( ) . post ( '/passport/local' ) . send ( { name : user . loginname , pass : 'newpass' } ) ;
193+ assert ( login4 . status === 302 ) ;
194+ assert ( login4 . headers . location === '/signin' ) ;
195+ } ) ;
196+
197+ it ( 'should GET /passport/github redirect to auth url' , async ( ) => {
198+ const res = await ctx . app . httpRequest ( ) . get ( '/passport/github' ) ;
199+ assert ( / ^ h t t p s : \/ \/ g i t h u b .c o m \/ l o g i n \/ o a u t h \/ a u t h o r i z e \? r e s p o n s e _ t y p e = c o d e & r e d i r e c t _ u r i = h t t p / . test ( res . headers . location ) ) ;
200+ assert ( res . status === 302 ) ;
201+
202+ const res1 = await ctx . app . httpRequest ( ) . get ( '/passport/github/callback' ) ;
203+ assert ( / ^ h t t p s : \/ \/ g i t h u b .c o m \/ l o g i n \/ o a u t h \/ a u t h o r i z e \? r e s p o n s e _ t y p e = c o d e & r e d i r e c t _ u r i = h t t p / . test ( res1 . headers . location ) ) ;
204+ assert ( res1 . status === 302 ) ;
158205 } ) ;
159206
160207 it ( 'should POST /user/set_star no_login reject' , async ( ) => {
@@ -226,7 +273,11 @@ describe('test/app/controller/user.test.js', () => {
226273 const noExistedUser = await app . httpRequest ( ) . get ( '/user/no_user/collections' ) ;
227274 assert ( noExistedUser . status === 404 ) ;
228275 assert ( / < s t r o n g > ( [ \S \s ] + ) < \/ s t r o n g > / g. exec ( noExistedUser . text ) [ 1 ] === '这个用户不存在。' ) ;
229- await app . httpRequest ( ) . get ( `/user/${ user . loginname } /collections` ) . expect ( 200 ) ;
276+
277+ const topic = await ctx . service . topic . newAndSave ( 'title' , 'content' , 'share' , user . _id ) ;
278+ await ctx . service . topicCollect . newAndSave ( user . _id , topic . _id ) ;
279+ const res = await app . httpRequest ( ) . get ( `/user/${ user . loginname } /collections` ) ;
280+ assert ( res . status = 200 ) ;
230281 } ) ;
231282
232283 it ( 'should GET /user/:name/topics ok' , async ( ) => {
@@ -240,7 +291,11 @@ describe('test/app/controller/user.test.js', () => {
240291 const noExistedUser = await app . httpRequest ( ) . get ( '/user/no_user/replies' ) ;
241292 assert ( noExistedUser . status === 404 ) ;
242293 assert ( / < s t r o n g > ( [ \S \s ] + ) < \/ s t r o n g > / g. exec ( noExistedUser . text ) [ 1 ] === '这个用户不存在。' ) ;
243- await app . httpRequest ( ) . get ( `/user/${ user . loginname } /replies` ) . expect ( 200 ) ;
294+
295+ const topic = await ctx . service . topic . newAndSave ( 'title' , 'content' , 'share' , user . _id ) ;
296+ await ctx . service . reply . newAndSave ( 'content' , topic . _id , user . _id ) ;
297+ const res = await app . httpRequest ( ) . get ( `/user/${ user . loginname } /replies` ) ;
298+ assert ( res . status === 200 ) ;
244299 } ) ;
245300 } ) ;
246301} ) ;
0 commit comments