@ file : Suppress (" ClassName ", Ġ" DE PREC ATION ") Ċ Ċ package Ġuk . gov . just ice . digital . h mp ps . oauth 2 server . service Ċ Ċ import Ġorg . assertj . core . api . Assertions . assertThat Ċ import Ġorg . assertj . core . api . Assertions . assertThat Thrown By Ċ import Ġorg . junit . jupiter . api . Test Ċ import Ġorg . mockito . Argument Matchers . any String Ċ import Ġorg . mockito . kotlin . any Ċ import Ġorg . mockito . kotlin . eq Ċ import Ġorg . mockito . kotlin . mock Ċ import Ġorg . mockito . kotlin . never Ċ import Ġorg . mockito . kotlin . verify Ċ import Ġorg . mockito . kotlin . wh enever Ċ import Ġuk . gov . just ice . digital . h mp ps . oauth 2 server . auth . model . Person Ċ import Ġuk . gov . just ice . digital . h mp ps . oauth 2 server . auth . model . User . M fa Preference Type Ċ import Ġuk . gov . just ice . digital . h mp ps . oauth 2 server . auth . model . User Helper . Companion . create Sample User Ċ import Ġuk . gov . just ice . digital . h mp ps . oauth 2 server . auth . model . User Token . TokenType Ċ import Ġuk . gov . just ice . digital . h mp ps . oauth 2 server . security . Lock ing Authentication Provider . M fa Unavailable Exception Ċ import Ġuk . gov . just ice . digital . h mp ps . oauth 2 server . security . User Retries Service Ċ import Ġuk . gov . just ice . digital . h mp ps . oauth 2 server . security . UserService Ċ import Ġuk . gov . just ice . digital . h mp ps . oauth 2 server . verify . Token Service Ċ import Ġuk . gov . service . notify . Notification Client Api Ċ import Ġjava . util . Optional Ċ Ċ internal Ġclass ĠM fa Service Test Ġ{ ĊĠ Ġprivate Ġval Ġtoken Service : ĠToken Service Ġ= Ġmock () ĊĠ Ġprivate Ġval ĠuserService : ĠUserService Ġ= Ġmock () ĊĠ Ġprivate Ġval Ġuser Retries Service : ĠUser Retries Service Ġ= Ġmock () ĊĠ Ġprivate Ġval Ġnotification Client Api : ĠNotification Client Api Ġ= Ġmock () ĊĠ Ġprivate Ġval Ġservice Ġ= ĠM fa Service ( ĊĠĠĠ Ġ" email Template ", ĊĠĠĠ Ġ" text Template ", ĊĠĠĠ Ġtoken Service , ĊĠĠĠ ĠuserService , ĊĠĠĠ Ġnotification Client Api , ĊĠĠĠ Ġuser Retries Service , ĊĠ Ġ) ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` validate And Remove M fa Code Ġnull `() Ġ{ ĊĠĠĠ ĠassertThat Thrown By Ġ{ Ġservice . validate And Remove M fa Code ("", Ġnull ) Ġ}. isInstanceOf ( M fa Flow Exception :: class . java ) ĊĠĠĠĠĠ Ġ. with Fail Message (" missing code ") ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` validate And Remove M fa Code Ġblank `() Ġ{ ĊĠĠĠ ĠassertThat Thrown By Ġ{ Ġservice . validate And Remove M fa Code ("", Ġ" ĠĠ Ġ") Ġ}. isInstanceOf ( M fa Flow Exception :: class . java ) ĊĠĠĠĠĠ Ġ. with Fail Message (" missing code ") ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` validate And Remove M fa Code Ġtoken Ġerror `() Ġ{ ĊĠĠĠ Ġval Ġuser Token Ġ= Ġcreate Sample User ( username Ġ= Ġ" user "). create Token ( TokenType . M FA ) ĊĠĠĠ Ġwhenever ( token Service . getToken ( any (), Ġany String ())). thenReturn ( Optional . of ( user Token )) ĊĠĠĠ Ġwhenever ( userService . find Master User Person Details ( any String ())). thenReturn ( Optional . of ( create Sample User ( username Ġ= Ġ" bob "))) ĊĊĠĠĠ Ġwhenever ( token Service . check Token ( any (), Ġany String ())). thenReturn ( Optional . of (" some problem ")) ĊĠĠĠ ĠassertThat Thrown By Ġ{ Ġservice . validate And Remove M fa Code ("", Ġ" some code ") Ġ}. isInstanceOf ( M fa Flow Exception :: class . java ) ĊĠĠĠĠĠ Ġ. with Fail Message (" some problem ") ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` validate And Remove M fa Code Ġsuccess `() Ġ{ ĊĠĠĠ Ġval Ġuser Token Ġ= Ġcreate Sample User ( username Ġ= Ġ" user "). create Token ( TokenType . M FA ) ĊĠĠĠ Ġwhenever ( token Service . getToken ( any (), Ġany String ())). thenReturn ( Optional . of ( user Token )) ĊĠĠĠ Ġwhenever ( userService . find Master User Person Details ( any String ())). thenReturn ( Optional . of ( create Sample User ( username Ġ= Ġ" bob "))) ĊĠĠĠ Ġservice . validate And Remove M fa Code (" some token ", Ġ" some code ") ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` validate And Remove M fa Code Ġsuccess Ġget Ġtoken Ġcall `() Ġ{ ĊĠĠĠ Ġval Ġuser Token Ġ= Ġcreate Sample User ( username Ġ= Ġ" user "). create Token ( TokenType . M FA ) ĊĠĠĠ Ġwhenever ( token Service . getToken ( any (), Ġany String ())). thenReturn ( Optional . of ( user Token )) ĊĠĠĠ Ġwhenever ( userService . find Master User Person Details ( any String ())). thenReturn ( Optional . of ( create Sample User ( username Ġ= Ġ" bob "))) ĊĠĠĠ Ġservice . validate And Remove M fa Code (" some token ", Ġ" some code ") ĊĊĠĠĠ Ġverify ( token Service ). getToken ( TokenType . M FA , Ġ" some token ") ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` validate And Remove M fa Code Ġsuccess Ġcheck Ġtoken Ġcall `() Ġ{ ĊĠĠĠ Ġval Ġuser Token Ġ= Ġcreate Sample User ( username Ġ= Ġ" user "). create Token ( TokenType . M FA ) ĊĠĠĠ Ġwhenever ( token Service . getToken ( any (), Ġany String ())). thenReturn ( Optional . of ( user Token )) ĊĠĠĠ Ġwhenever ( userService . find Master User Person Details ( any String ())). thenReturn ( Optional . of ( create Sample User ( username Ġ= Ġ" bob "))) ĊĠĠĠ Ġservice . validate And Remove M fa Code (" some token ", Ġ" some code ") ĊĊĠĠĠ Ġverify ( token Service ). check Token ( TokenType . M FA _ CODE , Ġ" some code ") ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` validate And Remove M fa Code Ġsuccess Ġfind Ġmaster Ġdetails `() Ġ{ ĊĠĠĠ Ġval Ġuser Token Ġ= Ġcreate Sample User ( username Ġ= Ġ" user "). create Token ( TokenType . M FA ) ĊĠĠĠ Ġwhenever ( token Service . getToken ( any (), Ġany String ())). thenReturn ( Optional . of ( user Token )) ĊĠĠĠ Ġwhenever ( userService . find Master User Person Details ( any String ())). thenReturn ( Optional . of ( create Sample User ( username Ġ= Ġ" bob "))) ĊĠĠĠ Ġservice . validate And Remove M fa Code (" some token ", Ġ" some code ") ĊĊĠĠĠ Ġverify ( userService ). find Master User Person Details (" user ") ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` validate And Remove M fa Code Ġsuccess Ġremove Ġtokens `() Ġ{ ĊĠĠĠ Ġval Ġuser Token Ġ= Ġcreate Sample User ( username Ġ= Ġ" user "). create Token ( TokenType . M FA ) ĊĠĠĠ Ġwhenever ( token Service . getToken ( any (), Ġany String ())). thenReturn ( Optional . of ( user Token )) ĊĠĠĠ Ġwhenever ( userService . find Master User Person Details ( any String ())). thenReturn ( Optional . of ( create Sample User ( username Ġ= Ġ" bob "))) ĊĠĠĠ Ġservice . validate And Remove M fa Code (" some token ", Ġ" some code ") ĊĊĠĠĠ Ġverify ( token Service ). remove Token ( TokenType . M FA , Ġ" some token ") ĊĠĠĠ Ġverify ( token Service ). remove Token ( TokenType . M FA _ CODE , Ġ" some code ") ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` validate And Remove M fa Code Ġsuccess Ġreset Ġretries Ġ` () Ġ{ ĊĠĠĠ Ġval Ġuser Token Ġ= Ġcreate Sample User ( username Ġ= Ġ" user "). create Token ( TokenType . M FA ) ĊĠĠĠ Ġwhenever ( token Service . getToken ( any (), Ġany String ())). thenReturn ( Optional . of ( user Token )) ĊĠĠĠ Ġwhenever ( userService . find Master User Person Details ( any String ())). thenReturn ( Optional . of ( create Sample User ( username Ġ= Ġ" bob "))) ĊĠĠĠ Ġservice . validate And Remove M fa Code (" some token ", Ġ" some code ") ĊĊĠĠĠ Ġverify ( user Retries Service ). reset Retries (" bob ") ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` validate And Remove M fa Code Ġaccount Ġlocked `() Ġ{ ĊĠĠĠ Ġval Ġuser Token Ġ= Ġcreate Sample User ( username Ġ= Ġ" user "). create Token ( TokenType . M FA ) ĊĠĠĠ Ġwhenever ( token Service . getToken ( any (), Ġany String ())). thenReturn ( Optional . of ( user Token )) ĊĠĠĠ Ġwhenever ( userService . find Master User Person Details ( any String ())). thenReturn ( ĊĠĠĠĠĠ ĠOptional . of ( create Sample User ( username Ġ= Ġ" bob ", Ġlocked Ġ= Ġtrue )) ĊĠĠĠ Ġ) ĊĠĠĠ ĠassertThat Thrown By Ġ{ ĊĠĠĠĠĠ Ġservice . validate And Remove M fa Code (" some token ", Ġ" some code ") ĊĠĠĠ Ġ}. isInstanceOf ( Login Flow Exception :: class . java ). with Fail Message (" locked ") ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` create Token And Send M fa Code Ġsuccess `() Ġ{ ĊĠĠĠ Ġval Ġuser Ġ= Ġcreate Sample User ( username Ġ= Ġ" bob ", Ġemail Ġ= Ġ" email ", Ġverified Ġ= Ġtrue ) ĊĠĠĠ Ġwhenever ( userService . get OrCreate User ( any String ())). thenReturn ( Optional . of ( user )) ĊĠĠĠ Ġwhenever ( token Service . create Token ( eq ( TokenType . M FA ), Ġany String ())). thenReturn (" some token ") ĊĠĠĠ Ġwhenever ( token Service . create Token ( eq ( TokenType . M FA _ CODE ), Ġany String ())). thenReturn (" some code ") ĊĠĠĠ Ġwhenever ( userService . find Master User Person Details ( any String ())). thenReturn ( Optional . of ( user )) ĊĊĠĠĠ ĠassertThat ( service . create Token And Send M fa Code (" user ")). isEqualTo ( ĊĠĠĠĠĠ ĠM fa Data ( ĊĠĠĠĠĠĠĠ Ġ" some token ", ĊĠĠĠĠĠĠĠ Ġ" some code ", ĊĠĠĠĠĠĠĠ ĠM fa Preference Type . EMAIL ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` create Token And Send M fa Code Ġby ĠEmail Ġcheck Ġemail Ġparams `() Ġ{ ĊĠĠĠ Ġval Ġuser Ġ= Ġcreate Sample User ( username Ġ= Ġ" bob ", Ġemail Ġ= Ġ" email ", Ġverified Ġ= Ġtrue ) ĊĠĠĠ Ġwhenever ( userService . get OrCreate User ( any String ())). thenReturn ( Optional . of ( user )) ĊĠĠĠ Ġwhenever ( token Service . create Token ( eq ( TokenType . M FA ), Ġany String ())). thenReturn (" some token ") ĊĠĠĠ Ġwhenever ( token Service . create Token ( eq ( TokenType . M FA _ CODE ), Ġany String ())). thenReturn (" some code ") ĊĠĠĠ Ġwhenever ( userService . find Master User Person Details ( any String ())). thenReturn ( Optional . of ( user )) ĊĊĠĠĠ Ġservice . create Token And Send M fa Code (" user ") ĊĊĠĠĠ Ġverify ( notification Client Api ). send Email ( ĊĠĠĠĠĠ Ġ" email Template ", ĊĠĠĠĠĠ Ġ" email ", ĊĠĠĠĠĠ Ġmap Of (" firstName " Ġto Ġ" first ", Ġ" code " Ġto Ġ" some code "), ĊĠĠĠĠĠ Ġnull ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` create Token And Send M fa Code Ġby Ġtext Ġcheck Ġtext Ġparams `() Ġ{ ĊĠĠĠ Ġval Ġuser Ġ= ĊĠĠĠĠĠ Ġcreate Sample User ( ĊĠĠĠĠĠĠĠ Ġusername Ġ= Ġ" bob ", ĊĠĠĠĠĠĠĠ Ġmobile Ġ= Ġ" 0 7 7 0 0 9 0 0 3 2 1 ", ĊĠĠĠĠĠĠĠ Ġmobile Verified Ġ= Ġtrue , ĊĠĠĠĠĠĠĠ Ġm fa Preference Ġ= ĠM fa Preference Type . TEXT ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġwhenever ( userService . get OrCreate User ( any String ())). thenReturn ( Optional . of ( user )) ĊĠĠĠ Ġwhenever ( token Service . create Token ( eq ( TokenType . M FA ), Ġany String ())). thenReturn (" some token ") ĊĠĠĠ Ġwhenever ( token Service . create Token ( eq ( TokenType . M FA _ CODE ), Ġany String ())). thenReturn (" some code ") ĊĠĠĠ Ġwhenever ( userService . find Master User Person Details ( any String ())). thenReturn ( Optional . of ( user )) ĊĊĠĠĠ Ġservice . create Token And Send M fa Code (" user ") ĊĊĠĠĠ Ġverify ( notification Client Api ). send Sms (" text Template ", Ġ" 0 7 7 0 0 9 0 0 3 2 1 ", Ġmap Of (" m fa Code " Ġto Ġ" some code "), Ġnull , Ġnull ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` create Token And Send M fa Code Ġby Ġsecondary ĠEmail Ġcheck Ġsecondary Ġemail Ġparams `() Ġ{ ĊĠĠĠ Ġval Ġuser Ġ= Ġcreate Sample User ( ĊĠĠĠĠĠ Ġusername Ġ= Ġ" first ", ĊĠĠĠĠĠ Ġemail Ġ= Ġ" bob @ digital . just ice . gov . uk ", ĊĠĠĠĠĠ Ġverified Ġ= Ġfalse , ĊĠĠĠĠĠ Ġmobile Ġ= Ġ" 0 7 7 0 0 9 0 0 3 2 1 ", ĊĠĠĠĠĠ Ġsecondary Email Ġ= Ġ" secondary Email ", ĊĠĠĠĠĠ Ġsecondary Email Verified Ġ= Ġtrue , ĊĠĠĠĠĠ Ġm fa Preference Ġ= ĠM fa Preference Type . SECOND ARY _ EMAIL , ĊĠĠĠ Ġ) ĊĠĠĠ Ġwhenever ( userService . get OrCreate User ( any String ())). thenReturn ( Optional . of ( user )) ĊĠĠĠ Ġwhenever ( token Service . create Token ( eq ( TokenType . M FA ), Ġany String ())). thenReturn (" some token ") ĊĠĠĠ Ġwhenever ( token Service . create Token ( eq ( TokenType . M FA _ CODE ), Ġany String ())). thenReturn (" some code ") ĊĠĠĠ Ġwhenever ( userService . find Master User Person Details ( any String ())). thenReturn ( Optional . of ( user )) ĊĊĠĠĠ Ġservice . create Token And Send M fa Code (" user ") ĊĊĠĠĠ Ġverify ( notification Client Api ). send Email ( ĊĠĠĠĠĠ Ġ" email Template ", ĊĠĠĠĠĠ Ġ" secondary Email ", ĊĠĠĠĠĠ Ġmap Of (" firstName " Ġto Ġ" first ", Ġ" code " Ġto Ġ" some code "), ĊĠĠĠĠĠ Ġnull ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` create Token And Send M fa Code ĠM fa Preference Ġemail Ġun verified Ġcode Ġsent Ġto Ġtext `() Ġ{ ĊĠĠĠ Ġval Ġuser Ġ= Ġcreate Sample User ( ĊĠĠĠĠĠ Ġusername Ġ= Ġ" first ", ĊĠĠĠĠĠ Ġemail Ġ= Ġ" bob @ digital . just ice . gov . uk ", ĊĠĠĠĠĠ Ġverified Ġ= Ġfalse , ĊĠĠĠĠĠ Ġmobile Ġ= Ġ" 0 7 7 0 0 9 0 0 3 2 1 ", ĊĠĠĠĠĠ Ġmobile Verified Ġ= Ġtrue , ĊĠĠĠĠĠ Ġsecondary Email Ġ= Ġ" secondary Email ", ĊĠĠĠĠĠ Ġsecondary Email Verified Ġ= Ġtrue , ĊĠĠĠĠĠ Ġm fa Preference Ġ= ĠM fa Preference Type . EMAIL ĊĠĠĠ Ġ) ĊĠĠĠ Ġwhenever ( userService . get OrCreate User ( any String ())). thenReturn ( Optional . of ( user )) ĊĠĠĠ Ġwhenever ( token Service . create Token ( eq ( TokenType . M FA ), Ġany String ())). thenReturn (" some token ") ĊĠĠĠ Ġwhenever ( token Service . create Token ( eq ( TokenType . M FA _ CODE ), Ġany String ())). thenReturn (" some code ") ĊĠĠĠ Ġwhenever ( userService . find Master User Person Details ( any String ())). thenReturn ( Optional . of ( user )) ĊĊĠĠĠ Ġservice . create Token And Send M fa Code (" user ") ĊĠĠĠ Ġverify ( notification Client Api ). send Sms (" text Template ", Ġ" 0 7 7 0 0 9 0 0 3 2 1 ", Ġmap Of (" m fa Code " Ġto Ġ" some code "), Ġnull , Ġnull ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` create Token And Send M fa Code ĠM fa Preference Ġtext Ġun verified Ġcode Ġsent Ġto Ġemail `() Ġ{ ĊĠĠĠ Ġval Ġuser Ġ= Ġcreate Sample User ( ĊĠĠĠĠĠ Ġusername Ġ= Ġ" first ", ĊĠĠĠĠĠ Ġemail Ġ= Ġ" bob @ digital . just ice . gov . uk ", ĊĠĠĠĠĠ Ġverified Ġ= Ġtrue , ĊĠĠĠĠĠ Ġmobile Ġ= Ġ" 0 7 7 0 0 9 0 0 3 2 1 ", ĊĠĠĠĠĠ Ġsecondary Email Ġ= Ġ" secondary Email ", ĊĠĠĠĠĠ Ġsecondary Email Verified Ġ= Ġtrue , ĊĠĠĠĠĠ Ġm fa Preference Ġ= ĠM fa Preference Type . TEXT ĊĠĠĠ Ġ) ĊĠĠĠ Ġwhenever ( userService . get OrCreate User ( any String ())). thenReturn ( Optional . of ( user )) ĊĠĠĠ Ġwhenever ( token Service . create Token ( eq ( TokenType . M FA ), Ġany String ())). thenReturn (" some token ") ĊĠĠĠ Ġwhenever ( token Service . create Token ( eq ( TokenType . M FA _ CODE ), Ġany String ())). thenReturn (" some code ") ĊĠĠĠ Ġwhenever ( userService . find Master User Person Details ( any String ())). thenReturn ( Optional . of ( user )) ĊĊĠĠĠ Ġservice . create Token And Send M fa Code (" user ") ĊĊĠĠĠ Ġverify ( notification Client Api ). send Email ( ĊĠĠĠĠĠ Ġ" email Template ", ĊĠĠĠĠĠ Ġ" bob @ digital . just ice . gov . uk ", ĊĠĠĠĠĠ Ġmap Of (" firstName " Ġto Ġ" first ", Ġ" code " Ġto Ġ" some code "), ĊĠĠĠĠĠ Ġnull ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` create Token And Send M fa Code ĠM fa Preference Ġsecondary Ġemail Ġun verified Ġcode Ġsent Ġto Ġemail `() Ġ{ ĊĠĠĠ Ġval Ġuser Ġ= Ġcreate Sample User ( ĊĠĠĠĠĠ Ġusername Ġ= Ġ" first ", ĊĠĠĠĠĠ Ġemail Ġ= Ġ" bob @ digital . just ice . gov . uk ", ĊĠĠĠĠĠ Ġverified Ġ= Ġtrue , ĊĠĠĠĠĠ Ġmobile Ġ= Ġ" 0 7 7 0 0 9 0 0 3 2 1 ", ĊĠĠĠĠĠ Ġmobile Verified Ġ= Ġtrue , ĊĠĠĠĠĠ Ġsecondary Email Ġ= Ġ" secondary Email ", ĊĠĠĠĠĠ Ġm fa Preference Ġ= ĠM fa Preference Type . SECOND ARY _ EMAIL ĊĠĠĠ Ġ) ĊĠĠĠ Ġwhenever ( userService . get OrCreate User ( any String ())). thenReturn ( Optional . of ( user )) ĊĠĠĠ Ġwhenever ( token Service . create Token ( eq ( TokenType . M FA ), Ġany String ())). thenReturn (" some token ") ĊĠĠĠ Ġwhenever ( token Service . create Token ( eq ( TokenType . M FA _ CODE ), Ġany String ())). thenReturn (" some code ") ĊĠĠĠ Ġwhenever ( userService . find Master User Person Details ( any String ())). thenReturn ( Optional . of ( user )) ĊĊĠĠĠ Ġservice . create Token And Send M fa Code (" user ") ĊĊĠĠĠ Ġverify ( notification Client Api ). send Email ( ĊĠĠĠĠĠ Ġ" email Template ", ĊĠĠĠĠĠ Ġ" bob @ digital . just ice . gov . uk ", ĊĠĠĠĠĠ Ġmap Of (" firstName " Ġto Ġ" first ", Ġ" code " Ġto Ġ" some code "), ĊĠĠĠĠĠ Ġnull ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` create Token And Send M fa Code Ġno Ġvalid Ġpreference `() Ġ{ ĊĠĠĠ Ġval Ġuser Ġ= Ġcreate Sample User ( username Ġ= Ġ" bob ") ĊĠĠĠ Ġwhenever ( userService . get OrCreate User ( any String ())). thenReturn ( Optional . of ( user )) ĊĠĠĠ Ġwhenever ( token Service . create Token ( eq ( TokenType . M FA ), Ġany String ())). thenReturn (" some token ") ĊĠĠĠ Ġwhenever ( token Service . create Token ( eq ( TokenType . M FA _ CODE ), Ġany String ())). thenReturn (" some code ") ĊĠĠĠ Ġwhenever ( userService . find Master User Person Details ( any String ())). thenReturn ( Optional . of ( user )) ĊĊĠĠĠ ĠassertThat Thrown By Ġ{ Ġservice . create Token And Send M fa Code (" user ") Ġ}. isInstanceOf ( M fa Unavailable Exception :: class . java ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` res end M fa Code Ġno Ġcode `() Ġ{ ĊĠĠĠ Ġval Ġuser Token Ġ= Ġcreate Sample User ( username Ġ= Ġ" user "). create Token ( TokenType . M FA ) ĊĠĠĠ Ġwhenever ( token Service . getToken ( any (), Ġany String ())). thenReturn ( Optional . of ( user Token )) ĊĊĠĠĠ Ġval Ġcode Ġ= Ġservice . res end M fa Code (" some token ", ĠM fa Preference Type . EMAIL ) ĊĠĠĠ ĠassertThat ( code ). isEqualTo ( null ) ĊĠĠĠ Ġverify ( userService , Ġnever ()). find Master User Person Details ( any String ()) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` res end M fa Code Ġcheck Ġcode `() Ġ{ ĊĠĠĠ Ġval Ġuser Ġ= Ġcreate Sample User ( username Ġ= Ġ" user ", Ġemail Ġ= Ġ" email ") ĊĠĠĠ Ġval Ġuser Token Ġ= Ġuser . create Token ( TokenType . M FA ) ĊĠĠĠ Ġval Ġuser Code Ġ= Ġuser . create Token ( TokenType . M FA _ CODE ) ĊĠĠĠ Ġwhenever ( token Service . getToken ( any (), Ġany String ())). thenReturn ( Optional . of ( user Token )) ĊĠĠĠ Ġwhenever ( userService . find Master User Person Details ( any String ())). thenReturn ( Optional . of ( user )) ĊĊĠĠĠ ĠassertThat ( service . res end M fa Code (" some token ", ĠM fa Preference Type . EMAIL )). isEqualTo ( user Code . token ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` res end M fa Code Ġcheck Ġemail `() Ġ{ ĊĠĠĠ Ġval Ġuser Ġ= Ġcreate Sample User ( username Ġ= Ġ" user ", Ġperson Ġ= ĠPerson (" Bob ", Ġ" Sm ith "), Ġemail Ġ= Ġ" email ") ĊĠĠĠ Ġval Ġuser Token Ġ= Ġuser . create Token ( TokenType . M FA ) ĊĠĠĠ Ġval Ġuser Code Ġ= Ġuser . create Token ( TokenType . M FA _ CODE ) ĊĠĠĠ Ġwhenever ( token Service . getToken ( any (), Ġany String ())). thenReturn ( Optional . of ( user Token )) ĊĠĠĠ Ġwhenever ( userService . find Master User Person Details ( any String ())). thenReturn ( Optional . of ( user )) ĊĊĠĠĠ Ġservice . res end M fa Code (" some token ", ĠM fa Preference Type . EMAIL ) ĊĊĠĠĠ Ġverify ( notification Client Api ). send Email ( ĊĠĠĠĠĠ Ġ" email Template ", ĊĠĠĠĠĠ Ġ" email ", ĊĠĠĠĠĠ Ġmap Of (" firstName " Ġto Ġ" Bob ", Ġ" code " Ġto Ġuser Code . token ), ĊĠĠĠĠĠ Ġnull ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` res end M fa Code Ġcheck Ġsecondary Ġemail `() Ġ{ ĊĠĠĠ Ġval Ġuser Ġ= Ġcreate Sample User ( ĊĠĠĠĠĠ Ġusername Ġ= Ġ" user ", ĊĠĠĠĠĠ Ġperson Ġ= ĠPerson (" Bob ", Ġ" Sm ith "), ĊĠĠĠĠĠ Ġsecondary Email Ġ= Ġ" email ", ĊĠĠĠĠĠ Ġsecondary Email Verified Ġ= Ġtrue ĊĠĠĠ Ġ) ĊĠĠĠ Ġval Ġuser Token Ġ= Ġuser . create Token ( TokenType . M FA ) ĊĠĠĠ Ġval Ġuser Code Ġ= Ġuser . create Token ( TokenType . M FA _ CODE ) ĊĠĠĠ Ġwhenever ( token Service . getToken ( any (), Ġany String ())). thenReturn ( Optional . of ( user Token )) ĊĠĠĠ Ġwhenever ( userService . find Master User Person Details ( any String ())). thenReturn ( Optional . of ( user )) ĊĊĠĠĠ Ġservice . res end M fa Code (" some token ", ĠM fa Preference Type . SECOND ARY _ EMAIL ) ĊĊĠĠĠ Ġverify ( notification Client Api ). send Email ( ĊĠĠĠĠĠ Ġ" email Template ", ĊĠĠĠĠĠ Ġ" email ", ĊĠĠĠĠĠ Ġmap Of (" firstName " Ġto Ġ" Bob ", Ġ" code " Ġto Ġuser Code . token ), ĊĠĠĠĠĠ Ġnull ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` res end M fa Code Ġcheck ĠText `() Ġ{ ĊĠĠĠ Ġval Ġuser Ġ= Ġcreate Sample User ( mobile Ġ= Ġ" 0 7 7 0 0 9 0 0 3 2 1 ", Ġmobile Verified Ġ= Ġtrue , Ġm fa Preference Ġ= ĠM fa Preference Type . TEXT ) ĊĠĠĠ Ġval Ġuser Token Ġ= Ġuser . create Token ( TokenType . M FA ) ĊĠĠĠ Ġval Ġuser Code Ġ= Ġuser . create Token ( TokenType . M FA _ CODE ) ĊĠĠĠ Ġwhenever ( token Service . getToken ( any (), Ġany String ())). thenReturn ( Optional . of ( user Token )) ĊĠĠĠ Ġwhenever ( userService . find Master User Person Details ( any String ())). thenReturn ( Optional . of ( user )) ĊĊĠĠĠ Ġservice . res end M fa Code (" some token ", ĠM fa Preference Type . TEXT ) ĊĊĠĠĠ Ġverify ( notification Client Api ). send Sms (" text Template ", Ġ" 0 7 7 0 0 9 0 0 3 2 1 ", Ġmap Of (" m fa Code " Ġto Ġuser Code . token ), Ġnull , Ġnull ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` Update ĠUser ĠM fa ĠPreference Ġto Ġtext `() Ġ{ ĊĠĠĠ Ġval Ġuser Ġ= Ġcreate Sample User ( username Ġ= Ġ" user ") ĊĠĠĠ Ġwhenever ( userService . find User ( any String ())). thenReturn ( Optional . of ( user )) ĊĠĠĠ Ġservice . update User M fa Preference ( M fa Preference Type . TEXT , Ġ" user ") ĊĠĠĠ ĠassertThat ( user . m fa Preference ). isEqualTo ( M fa Preference Type . TEXT ) ĊĠĠĠ Ġverify ( userService ). find User (" user ") ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` Update ĠUser ĠM fa ĠPreference Ġto Ġemail `() Ġ{ ĊĠĠĠ Ġval Ġuser Ġ= Ġcreate Sample User ( username Ġ= Ġ" user ", Ġm fa Preference Ġ= ĠM fa Preference Type . TEXT ) ĊĠĠĠ Ġwhenever ( userService . find User ( any String ())). thenReturn ( Optional . of ( user )) ĊĠĠĠ Ġservice . update User M fa Preference ( M fa Preference Type . EMAIL , Ġ" user ") ĊĠĠĠ ĠassertThat ( user . m fa Preference ). isEqualTo ( M fa Preference Type . EMAIL ) ĊĠĠĠ Ġverify ( userService ). find User (" user ") ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` Update ĠUser ĠM fa ĠPreference Ġto Ġsecondary Ġemail `() Ġ{ ĊĠĠĠ Ġval Ġuser Ġ= Ġcreate Sample User ( username Ġ= Ġ" user ", Ġm fa Preference Ġ= ĠM fa Preference Type . TEXT ) ĊĠĠĠ Ġwhenever ( userService . find User ( any String ())). thenReturn ( Optional . of ( user )) ĊĠĠĠ Ġservice . update User M fa Preference ( M fa Preference Type . SECOND ARY _ EMAIL , Ġ" user ") ĊĠĠĠ ĠassertThat ( user . m fa Preference ). isEqualTo ( M fa Preference Type . SECOND ARY _ EMAIL ) ĊĠĠĠ Ġverify ( userService ). find User (" user ") ĊĠ Ġ} Ċ } Ċ