@@ -11,17 +11,31 @@ describe('test/app/controller/user.test.js', () => {
1111
1212 before ( async function ( ) {
1313 ctx = app . mockContext ( ) ;
14- loginname = `loginname_ ${ Date . now ( ) } ` ;
14+ loginname = `user_loginname_ ${ Date . now ( ) } ` ;
1515 email = `${ loginname } @test.com` ;
1616 user = await ctx . service . user . newAndSave ( 'name' , loginname , tools . bhash ( 'pass' ) , email , 'avatar_url' , 'active' ) ;
1717 assert ( user . loginname === loginname ) ;
1818 } ) ;
1919
2020 describe ( '- Index' , ( ) => {
2121 it ( 'should GET /user ok' , async ( ) => {
22- await app . httpRequest ( ) . get ( '/user' ) . expect ( 404 ) ;
23- await app . httpRequest ( ) . get ( '/user/unexisted_user' ) . expect ( 404 ) ;
24- await app . httpRequest ( ) . get ( `/user/${ loginname } ` ) . expect ( 200 ) ;
22+ user . url = 'test_url' ;
23+ await user . save ( ) ;
24+
25+ const r1 = await app . httpRequest ( ) . get ( '/user' ) ;
26+ assert ( r1 . status === 404 ) ;
27+ assert ( / < s t r o n g > ( [ \S \s ] + ) < \/ s t r o n g > / g. exec ( r1 . text ) [ 1 ] === 'Not Found' ) ;
28+
29+ const r2 = await app . httpRequest ( ) . get ( '/user/unexisted_user' ) ;
30+ assert ( r2 . status === 404 ) ;
31+ assert ( / < s t r o n g > ( [ \S \s ] + ) < \/ s t r o n g > / g. exec ( r2 . text ) [ 1 ] === '这个用户不存在。' ) ;
32+
33+ const res = await app . httpRequest ( ) . get ( `/user/${ loginname } ` ) ;
34+ assert ( res . status === 200 ) ;
35+ assert ( / < i m g s r c = " ( [ ^ " ] + ) " t i t l e = / g. exec ( res . text ) [ 1 ] === user . avatar ) ;
36+ assert ( / < a c l a s s = " d a r k " > ( [ ^ " ] + ) < \/ a > / g. exec ( res . text ) [ 1 ] === user . loginname ) ;
37+ 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 ( ) ) ;
38+ assert ( / “ ( [ \S \s ] + ) ” / g. exec ( res . text ) [ 1 ] . replace ( / [ \t \s ] + / g, '' ) === '这家伙很懒,什么个性签名都没有留下。' ) ;
2539 } ) ;
2640 } ) ;
2741
@@ -34,10 +48,14 @@ describe('test/app/controller/user.test.js', () => {
3448 } ;
3549
3650 it ( 'should GET /setting ok' , async ( ) => {
37- await app . httpRequest ( ) . get ( '/setting' ) . expect ( 403 ) ;
51+ const nologinRes = await app . httpRequest ( ) . get ( '/setting' ) ;
52+ assert ( nologinRes . status === 403 ) ;
53+ assert ( nologinRes . text === 'forbidden!' ) ;
3854
3955 app . mockContext ( { user } ) ;
4056 await app . httpRequest ( ) . get ( '/setting' ) . expect ( 200 ) ;
57+ const { text } = await app . httpRequest ( ) . get ( '/setting?save=success' ) ;
58+ assert ( / < s t r o n g > ( [ \S \s ] + ) < \/ s t r o n g > / g. exec ( text ) [ 1 ] === '保存成功。' ) ;
4159 } ) ;
4260
4361
@@ -60,15 +78,37 @@ describe('test/app/controller/user.test.js', () => {
6078 it ( 'should POST /setting change password ok' , async ( ) => {
6179 app . mockCsrf ( ) ;
6280 app . mockContext ( { user } ) ;
63- const { status } = await app . httpRequest ( )
81+ const res1 = await app . httpRequest ( )
82+ . post ( '/setting' )
83+ . type ( 'form' )
84+ . send ( {
85+ action : 'change_password' ,
86+ old_pass : '' ,
87+ new_pass : '' ,
88+ } ) ;
89+ assert ( res1 . status === 200 ) ;
90+ assert ( / < s t r o n g > ( [ \S \s ] + ) < \/ s t r o n g > / g. exec ( res1 . text ) [ 1 ] === '旧密码或新密码不得为空' ) ;
91+
92+ const res2 = await app . httpRequest ( )
93+ . post ( '/setting' )
94+ . type ( 'form' )
95+ . send ( {
96+ action : 'change_password' ,
97+ old_pass : 'worngPass' ,
98+ new_pass : 'worngPass' ,
99+ } ) ;
100+ assert ( res2 . status === 200 ) ;
101+ assert ( / < s t r o n g > ( [ \S \s ] + ) < \/ s t r o n g > / g. exec ( res2 . text ) [ 1 ] === '当前密码不正确。' ) ;
102+
103+ const res3 = await app . httpRequest ( )
64104 . post ( '/setting' )
65105 . type ( 'form' )
66106 . send ( {
67107 action : 'change_password' ,
68108 old_pass : 'pass' ,
69109 new_pass : 'newpass' ,
70110 } ) ;
71- assert ( status === 200 ) ;
111+ assert ( res3 . status === 200 ) ;
72112
73113 const savedUser = await ctx . service . user . getUserById ( user . _id ) ;
74114 const equal = tools . bcompare ( 'newpass' , savedUser . pass ) ;
@@ -78,36 +118,53 @@ describe('test/app/controller/user.test.js', () => {
78118
79119 describe ( '- Admin' , ( ) => {
80120 async function handleUserPost ( url ) {
121+ delete user . is_admin ;
81122 app . mockCsrf ( ) ;
82123 app . mockContext ( { user } ) ;
83124 const { text } = await app . httpRequest ( )
84125 . post ( url )
85- . type ( 'form' )
86126 . send ( )
87127 . expect ( 200 ) ;
88128 assert ( / < s t r o n g > ( [ \S \s ] + ) < \/ s t r o n g > / g. exec ( text ) [ 1 ] === '需要管理员权限。' ) ;
89129 }
90130
91131 async function handleAdminPost ( url , body , cb ) {
92- const adminName = Object . keys ( app . config . admins ) [ 0 ] ;
93- let admin = await ctx . service . user . getUserByLoginName ( adminName ) ;
94- if ( ! admin ) {
95- admin = await ctx . service . user . newAndSave ( adminName , adminName , 'pass' , '[email protected] ' , 'u' , 'active' ) ; 96- }
97- const auth_token = admin . _id + '$$$$' ;
98- app . mockCookies ( { [ app . config . auth_cookie_name ] : auth_token } ) ;
132+ // const adminName = Object.keys(app.config.admins)[0];
133+ // let admin = await ctx.service.user.getUserByLoginName(adminName);
134+ // if (!admin) {
135+ // admin = await ctx.service.user.newAndSave(adminName, adminName, 'pass', '[email protected] ', 'u', 'active'); 136+ // }
137+ // const auth_token = admin._id + '$$$$';
138+ // app.mockCookies({ [app.config.auth_cookie_name]: auth_token });
139+
140+ user . is_admin = true ;
99141 app . mockCsrf ( ) ;
100- app . mockContext ( { user : admin } ) ;
101- const res = await app . httpRequest ( )
102- . post ( url )
103- . type ( 'json' )
104- . send ( body )
105- . expect ( 200 ) ;
142+ app . mockContext ( { user } ) ;
143+ const res = await app . httpRequest ( ) . post ( url ) . type ( 'json' )
144+ . send ( body ) ;
145+ assert ( res . status === 200 ) ;
106146 assert ( res . body . status === 'success' ) ;
107147 const updatedUser = await ctx . service . user . getUserById ( user . _id ) ;
108148 cb ( updatedUser ) ;
109149 }
110150
151+ it ( 'should POST /passport/local set cookies' , async ( ) => {
152+ app . mockCsrf ( ) ;
153+ const login = await app . httpRequest ( ) . post ( '/passport/local' ) . send ( { name : user . loginname , pass : 'newpass' } ) ;
154+ assert ( login . status === 302 ) ;
155+ const cookies = login . headers [ 'set-cookie' ] ;
156+ const authUser = cookies . find ( c => c . indexOf ( '$$$$' ) > - 1 ) ;
157+ assert ( / = ( [ ^ $ ] + ) \$ / g. exec ( authUser ) [ 1 ] === user . _id . toString ( ) ) ;
158+ assert ( login . headers . location === '/' ) ;
159+ } ) ;
160+
161+ it ( 'should POST /user/set_star no_login reject' , async ( ) => {
162+ app . mockCsrf ( ) ;
163+ const res = await app . httpRequest ( ) . post ( '/user/set_star' ) . send ( ) ;
164+ assert ( res . status === 200 ) ;
165+ assert ( / < s t r o n g > ( [ \S \s ] + ) < \/ s t r o n g > / g. exec ( res . text ) [ 1 ] === '你还没有登录。' ) ;
166+ } ) ;
167+
111168 it ( 'should POST /user/set_star no_admin reject' , async ( ) => {
112169 await handleUserPost ( '/user/set_star' ) ;
113170 } ) ;
@@ -159,16 +216,31 @@ describe('test/app/controller/user.test.js', () => {
159216 } ) ;
160217 } ) ;
161218
219+ describe ( '- Stars' , ( ) => {
220+ it ( 'should GET /stars ok' , async ( ) => {
221+ await app . httpRequest ( ) . get ( '/stars' ) . expect ( 200 ) ;
222+ } ) ;
223+ } ) ;
224+
162225 describe ( '- Records' , ( ) => {
163226 it ( 'should GET /user/:name/collections ok' , async ( ) => {
227+ const noExistedUser = await app . httpRequest ( ) . get ( '/user/no_user/collections' ) ;
228+ assert ( noExistedUser . status === 404 ) ;
229+ assert ( / < s t r o n g > ( [ \S \s ] + ) < \/ s t r o n g > / g. exec ( noExistedUser . text ) [ 1 ] === '这个用户不存在。' ) ;
164230 await app . httpRequest ( ) . get ( `/user/${ user . loginname } /collections` ) . expect ( 200 ) ;
165231 } ) ;
166232
167233 it ( 'should GET /user/:name/topics ok' , async ( ) => {
234+ const noExistedUser = await app . httpRequest ( ) . get ( '/user/no_user/topics' ) ;
235+ assert ( noExistedUser . status === 404 ) ;
236+ assert ( / < s t r o n g > ( [ \S \s ] + ) < \/ s t r o n g > / g. exec ( noExistedUser . text ) [ 1 ] === '这个用户不存在。' ) ;
168237 await app . httpRequest ( ) . get ( `/user/${ user . loginname } /topics` ) . expect ( 200 ) ;
169238 } ) ;
170239
171240 it ( 'should GET /user/:name/replies ok' , async ( ) => {
241+ const noExistedUser = await app . httpRequest ( ) . get ( '/user/no_user/replies' ) ;
242+ assert ( noExistedUser . status === 404 ) ;
243+ assert ( / < s t r o n g > ( [ \S \s ] + ) < \/ s t r o n g > / g. exec ( noExistedUser . text ) [ 1 ] === '这个用户不存在。' ) ;
172244 await app . httpRequest ( ) . get ( `/user/${ user . loginname } /replies` ) . expect ( 200 ) ;
173245 } ) ;
174246 } ) ;
0 commit comments