@@ -390,6 +390,27 @@ describe('AccountManager', () => {
390390 } )
391391 } )
392392
393+ describe ( 'generateDeleteToken()' , ( ) => {
394+ it ( 'should generate and store an expiring delete token' , ( ) => {
395+ let tokenService = new TokenService ( )
396+ let options = { host, tokenService }
397+
398+ let accountManager = AccountManager . from ( options )
399+
400+ let aliceWebId = 'https://alice.example.com/#me'
401+ let userAccount = {
402+ webId : aliceWebId
403+ }
404+
405+ let token = accountManager . generateDeleteToken ( userAccount )
406+
407+ let tokenValue = accountManager . tokenService . verify ( 'delete-account' , token )
408+
409+ expect ( tokenValue . webId ) . to . equal ( aliceWebId )
410+ expect ( tokenValue ) . to . have . property ( 'exp' )
411+ } )
412+ } )
413+
393414 describe ( 'generateResetToken()' , ( ) => {
394415 it ( 'should generate and store an expiring reset token' , ( ) => {
395416 let tokenService = new TokenService ( )
@@ -404,7 +425,7 @@ describe('AccountManager', () => {
404425
405426 let token = accountManager . generateResetToken ( userAccount )
406427
407- let tokenValue = accountManager . tokenService . verify ( token )
428+ let tokenValue = accountManager . tokenService . verify ( 'reset-password' , token )
408429
409430 expect ( tokenValue . webId ) . to . equal ( aliceWebId )
410431 expect ( tokenValue ) . to . have . property ( 'exp' )
@@ -484,4 +505,75 @@ describe('AccountManager', () => {
484505 } )
485506 } )
486507 } )
508+
509+ describe ( 'sendDeleteAccountEmail()' , ( ) => {
510+ it ( 'should compose and send a delete account email' , ( ) => {
511+ let deleteToken = '1234'
512+ let tokenService = {
513+ generate : sinon . stub ( ) . returns ( deleteToken )
514+ }
515+
516+ let emailService = {
517+ sendWithTemplate : sinon . stub ( ) . resolves ( )
518+ }
519+
520+ let aliceWebId = 'https://alice.example.com/#me'
521+ let userAccount = {
522+ webId : aliceWebId ,
523+ 524+ }
525+
526+ let options = { host, tokenService, emailService }
527+ let accountManager = AccountManager . from ( options )
528+
529+ accountManager . getAccountDeleteUrl = sinon . stub ( ) . returns ( 'delete account url' )
530+
531+ let expectedEmailData = {
532+ 533+ webId : aliceWebId ,
534+ deleteUrl : 'delete account url'
535+ }
536+
537+ return accountManager . sendDeleteAccountEmail ( userAccount )
538+ . then ( ( ) => {
539+ expect ( accountManager . getAccountDeleteUrl )
540+ . to . have . been . calledWith ( deleteToken )
541+ expect ( emailService . sendWithTemplate )
542+ . to . have . been . calledWith ( 'delete-account' , expectedEmailData )
543+ } )
544+ } )
545+
546+ it ( 'should reject if no email service is set up' , done => {
547+ let aliceWebId = 'https://alice.example.com/#me'
548+ let userAccount = {
549+ webId : aliceWebId ,
550+ 551+ }
552+ let options = { host }
553+ let accountManager = AccountManager . from ( options )
554+
555+ accountManager . sendDeleteAccountEmail ( userAccount )
556+ . catch ( error => {
557+ expect ( error . message ) . to . equal ( 'Email service is not set up' )
558+ done ( )
559+ } )
560+ } )
561+
562+ it ( 'should reject if no user email is provided' , done => {
563+ let aliceWebId = 'https://alice.example.com/#me'
564+ let userAccount = {
565+ webId : aliceWebId
566+ }
567+ let emailService = { }
568+ let options = { host, emailService }
569+
570+ let accountManager = AccountManager . from ( options )
571+
572+ accountManager . sendDeleteAccountEmail ( userAccount )
573+ . catch ( error => {
574+ expect ( error . message ) . to . equal ( 'Account recovery email has not been provided' )
575+ done ( )
576+ } )
577+ } )
578+ } )
487579} )
0 commit comments