11const t = require ( '../test-lib/test.js' ) ;
22const assert = require ( 'assert' ) . strict ;
33
4- describe ( 'Login' , function ( ) {
4+ describe ( 'Login' , function ( ) {
55 let apos ;
66 let resetUserId ;
77 let resetToken ;
@@ -31,13 +31,13 @@ describe('Login', function() {
3131 } ) ;
3232 } ) ;
3333
34- after ( function ( ) {
34+ after ( function ( ) {
3535 return t . destroy ( apos ) ;
3636 } ) ;
3737
3838 // EXISTENCE
3939
40- it ( 'should initialize' , async function ( ) {
40+ it ( 'should initialize' , async function ( ) {
4141 assert ( apos ) ;
4242
4343 assert ( apos . modules [ '@apostrophecms/login' ] ) ;
@@ -117,7 +117,7 @@ describe('Login', function() {
117117 await loginModule . clearLoginAttempts ( username ) ;
118118 } ) ;
119119
120- it ( 'should be able to login a user with their username' , async function ( ) {
120+ it ( 'should be able to login a user with their username' , async function ( ) {
121121 const getLoggedInCookieValue =
122122 jar => jar . toJSON ( ) . cookies . find ( cookie => cookie . key === `${ apos . options . shortName } .loggedIn` ) . value ;
123123
@@ -182,7 +182,7 @@ describe('Login', function() {
182182 assert ( getLoggedInCookieValue ( jar ) === 'false' ) ;
183183 } ) ;
184184
185- it ( 'should be able to login a user with their email' , async function ( ) {
185+ it ( 'should be able to login a user with their email' , async function ( ) {
186186
187187 const jar = apos . http . jar ( ) ;
188188
@@ -242,7 +242,7 @@ describe('Login', function() {
242242 assert ( page . match ( / l o g g e d o u t / ) ) ;
243243 } ) ;
244244
245- it ( 'changing a user\'s password should invalidate sessions for that user' , async function ( ) {
245+ it ( 'changing a user\'s password should invalidate sessions for that user' , async function ( ) {
246246
247247 const jar = apos . http . jar ( ) ;
248248
@@ -356,7 +356,7 @@ describe('Login', function() {
356356
357357 } ) ;
358358
359- it ( 'changing a user\'s password should invalidate bearer tokens for that user' , async function ( ) {
359+ it ( 'changing a user\'s password should invalidate bearer tokens for that user' , async function ( ) {
360360
361361 // Log in
362362 let response = await apos . http . post ( '/api/v1/@apostrophecms/login/login' , {
@@ -425,7 +425,7 @@ describe('Login', function() {
425425
426426 } ) ;
427427
428- it ( 'api key should beat session when both are present' , async function ( ) {
428+ it ( 'api key should beat session when both are present' , async function ( ) {
429429 const jar = apos . http . jar ( ) ;
430430 await apos . http . post (
431431 '/api/v1/@apostrophecms/login/login' ,
@@ -463,7 +463,7 @@ describe('Login', function() {
463463 assert ( page2 . match ( / S y s t e m T a s k / ) ) ;
464464 } ) ;
465465
466- it ( 'should validate POST /login/reset-request' , async function ( ) {
466+ it ( 'should validate POST /login/reset-request' , async function ( ) {
467467 const jar = apos . http . jar ( ) ;
468468 await apos . http . get (
469469 '/' ,
@@ -485,7 +485,7 @@ describe('Login', function() {
485485 } ) ;
486486 } ) ;
487487
488- it ( 'should hide sensitive exceptions POST /login/reset-request' , async function ( ) {
488+ it ( 'should hide sensitive exceptions POST /login/reset-request' , async function ( ) {
489489 let log ;
490490 const orig = apos . util . error ;
491491 apos . util . error = ( m ) => {
@@ -532,7 +532,7 @@ describe('Login', function() {
532532 apos . util . error = orig ;
533533 } ) ;
534534
535- it ( 'should reset password POST /login/reset-request (request)' , async function ( ) {
535+ it ( 'should reset password POST /login/reset-request (request)' , async function ( ) {
536536 let args ;
537537 const orig = apos . login . email ;
538538 apos . login . email = ( req , ...a ) => {
@@ -610,7 +610,7 @@ describe('Login', function() {
610610 apos . login . email = orig ;
611611 } ) ;
612612
613- it ( 'should reset password GET /login/reset (validate)' , async function ( ) {
613+ it ( 'should reset password GET /login/reset (validate)' , async function ( ) {
614614 const user = await apos . doc . db . findOne ( { _id : resetUserId } ) ;
615615
616616 // Fail
@@ -667,7 +667,7 @@ describe('Login', function() {
667667 ) ;
668668 } ) ;
669669
670- it ( 'should reset password POST /login/reset (validate & reset)' , async function ( ) {
670+ it ( 'should reset password POST /login/reset (validate & reset)' , async function ( ) {
671671 const jar = apos . http . jar ( ) ;
672672 const user = await apos . doc . db . findOne ( { _id : resetUserId } ) ;
673673 await apos . http . get (
@@ -798,7 +798,7 @@ describe('Login', function() {
798798 assert ( page . match ( / l o g g e d i n / ) ) ;
799799 } ) ;
800800
801- it ( 'should find user by reset data' , async function ( ) {
801+ it ( 'should find user by reset data' , async function ( ) {
802802 let user = apos . user . newInstance ( ) ;
803803 user . title = 'getResetUser' ;
804804 user . email = 'getResetUser@example.com' ;
@@ -868,18 +868,39 @@ describe('Login', function() {
868868 ) ;
869869 } ) ;
870870
871- it ( 'should return an error with code 404 at GET login/whoami when user is not logged in' , async function ( ) {
871+ it ( 'should return an error with code 404 at GET login/whoami when user is not logged in' , async function ( ) {
872872 try {
873- await apos . http . get ( '/api/v1/@apostrophecms/login/whoami' ) ;
873+ const jar = apos . http . jar ( ) ;
874+ await apos . http . get (
875+ '/' ,
876+ {
877+ jar
878+ }
879+ ) ;
880+ await apos . http . post ( '/api/v1/@apostrophecms/login/whoami' ,
881+ {
882+ method : 'POST' ,
883+ body : {
884+ session : true
885+ } ,
886+ jar
887+ }
888+ ) ;
874889 assert . fail ( 'Expected error but got success' ) ;
875890 } catch ( err ) {
876891 assert . strictEqual ( err . status , 404 ) ;
877892 }
878893 } ) ;
879894
880- it ( 'should return user data at GET login/whoami when user is logged in' , async function ( ) {
895+ it ( 'should return user data at GET login/whoami when user is logged in' , async function ( ) {
881896
882897 const jar = apos . http . jar ( ) ;
898+ await apos . http . get (
899+ '/' ,
900+ {
901+ jar
902+ }
903+ ) ;
883904
884905 await apos . http . post (
885906 '/api/v1/@apostrophecms/login/login' ,
@@ -894,16 +915,28 @@ describe('Login', function() {
894915 }
895916 ) ;
896917
897- const whoamiResponse = await apos . http . get ( '/api/v1/@apostrophecms/login/whoami' , { jar } ) ;
918+ const whoamiResponse = await apos . http . post ( '/api/v1/@apostrophecms/login/whoami' , {
919+ method : 'POST' ,
920+ body : {
921+ session : true
922+ } ,
923+ jar
924+ } ) ;
898925 assert . ok ( whoamiResponse . _id ) ;
899926 assert . strictEqual ( whoamiResponse . username , 'HarryPutter' ) ;
900927 assert . strictEqual ( whoamiResponse . title , 'Extra Cool Putter' ) ;
901928 assert . strictEqual ( whoamiResponse . email , 'hputter@aol.com' ) ;
902929 } ) ;
903930
904- it ( 'should return user data with additional whoamiFields if explicitly added at GET login/whoami when user is logged in' , async function ( ) {
931+ it ( 'should return user data with additional whoamiFields if explicitly added to the login module options when user is logged in' , async function ( ) {
905932
906933 const jar = apos . http . jar ( ) ;
934+ await apos . http . get (
935+ '/' ,
936+ {
937+ jar
938+ }
939+ ) ;
907940
908941 apos . modules [ '@apostrophecms/login' ] . options . whoamiFields = [ 'role' ] ;
909942
@@ -920,13 +953,25 @@ describe('Login', function() {
920953 }
921954 ) ;
922955
923- const whoamiResponse = await apos . http . get ( '/api/v1/@apostrophecms/login/whoami' , { jar } ) ;
956+ const whoamiResponse = await apos . http . post ( '/api/v1/@apostrophecms/login/whoami' , {
957+ method : 'POST' ,
958+ body : {
959+ session : true
960+ } ,
961+ jar
962+ } ) ;
924963 assert . strictEqual ( whoamiResponse . role , 'admin' ) ;
925964 } ) ;
926965
927- it ( 'should not return user data with additional whoamiFields if not explicitly added at GET login/whoami when user is logged in' , async function ( ) {
966+ it ( 'should not return user data with additional whoamiFields if not explicitly added to the login module options when user is logged in' , async function ( ) {
928967
929968 const jar = apos . http . jar ( ) ;
969+ await apos . http . get (
970+ '/' ,
971+ {
972+ jar
973+ }
974+ ) ;
930975
931976 // Reset the whoamiFields to default (empty)
932977 apos . modules [ '@apostrophecms/login' ] . options . whoamiFields = [ ] ;
@@ -944,7 +989,13 @@ describe('Login', function() {
944989 }
945990 ) ;
946991
947- const whoamiResponse = await apos . http . get ( '/api/v1/@apostrophecms/login/whoami' , { jar } ) ;
992+ const whoamiResponse = await apos . http . post ( '/api/v1/@apostrophecms/login/whoami' , {
993+ method : 'POST' ,
994+ body : {
995+ session : true
996+ } ,
997+ jar
998+ } ) ;
948999 assert . ok ( ! ( 'role' in whoamiResponse ) ) ;
9491000 } ) ;
9501001} ) ;
0 commit comments