package Ġes . gu ill erm oo re ll ana . key note dex . backend . api . signup Ċ Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . any OrNull Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . do Return Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . eq Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . mock Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . wh enever Ċ import Ġes . gu ill erm oo re ll ana . key note dex . backend . data . Ke yn ote dex Storage Ċ import Ġes . gu ill erm oo re ll ana . key note dex . backend . data . users . User Ċ import Ġes . gu ill erm oo re ll ana . key note dex . backend . test App Ċ import Ġio . ktor . http . ContentType Ċ import Ġio . ktor . http . HttpHeaders Ċ import Ġio . ktor . http . HttpMethod Ċ import Ġio . ktor . http . HttpStatusCode Ċ import Ġio . ktor . http . form Url Encode Ċ import Ġio . ktor . locations . locations Ċ import Ġio . ktor . server . testing . Test Application Engine Ċ import Ġio . ktor . server . testing . handle Request Ċ import Ġio . ktor . server . testing . set Body Ċ import Ġorg . hamcrest . Core Matchers . equalTo Ċ import Ġorg . hamcrest . Matcher Assert . assertThat Ċ import Ġorg . junit . Test Ċ Ċ class ĠPost SignUp Test Ġ{ ĊĊĠĠĠ Ġprivate Ġval Ġtest Id Ġ= Ġ" test Id " ĊĠĠĠ Ġprivate Ġval Ġtest Password Ġ= Ġ" long Test Password " ĊĠĠĠ Ġprivate Ġval Ġtest User Ġ= ĠUser ( test Id ) ĊĠĠĠ Ġprivate Ġval Ġmock Storage : ĠKe yn ote dex Storage Ġ= Ġmock Ġ{ Ġ} ĊĊĠĠĠ Ġprivate Ġval ĠTest Application Engine . endpoint ĊĠĠĠĠĠĠĠ Ġget () Ġ= Ġapplication . locations . href ( SignUp Endpoint ()) ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when ĠuserId Ġand Ġpassword Ġprovided Ġthen Ġuser Ġis Ġcreated `() Ġ= Ġtest App ( mock Storage ) Ġ{ ĊĠĠĠĠĠĠĠ Ġhandle Request ( HttpMethod . Post , Ġendpoint ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġadd Header ( HttpHeaders . ContentType , ĠContentType . Application . Form Url Encoded . toString ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġset Body ( listOf (" userId " Ġto Ġtest Id , Ġ" password " Ġto Ġtest Password ). form Url Encode ()) ĊĠĠĠĠĠĠĠ Ġ}. apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response . status (), ĠequalTo ( HttpStatusCode . Created )) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when Ġpassword Ġis Ġshort Ġthen Ġpre condition Ġfailed `() Ġ= Ġtest App ( mock Storage ) Ġ{ ĊĠĠĠĠĠĠĠ Ġhandle Request ( HttpMethod . Post , Ġendpoint ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġadd Header ( HttpHeaders . ContentType , ĠContentType . Application . Form Url Encoded . toString ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġset Body ( listOf (" userId " Ġto Ġtest Id , Ġ" password " Ġto Ġtest Password . substring ( 0 .. 2 )). form Url Encode ()) ĊĠĠĠĠĠĠĠ Ġ}. apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response . status (), ĠequalTo ( HttpStatusCode . Precondition Failed )) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when ĠuserId Ġis Ġshort Ġthen Ġpre condition Ġfailed `() Ġ= Ġtest App ( mock Storage ) Ġ{ ĊĠĠĠĠĠĠĠ Ġhandle Request ( HttpMethod . Post , Ġendpoint ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġadd Header ( HttpHeaders . ContentType , ĠContentType . Application . Form Url Encoded . toString ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġset Body ( listOf (" userId " Ġto Ġtest Id . substring ( 0 .. 2 ), Ġ" password " Ġto Ġtest Password ). form Url Encode ()) ĊĠĠĠĠĠĠĠ Ġ}. apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response . status (), ĠequalTo ( HttpStatusCode . Precondition Failed )) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when ĠuserId Ġis Ġinvalid Ġthen Ġpre condition Ġfailed `() Ġ= Ġtest App ( mock Storage ) Ġ{ ĊĠĠĠĠĠĠĠ Ġhandle Request ( HttpMethod . Post , Ġendpoint ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġadd Header ( HttpHeaders . ContentType , ĠContentType . Application . Form Url Encoded . toString ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġset Body ( listOf (" userId " Ġto Ġ" %% %^ & * () Â £ ", Ġ" password " Ġto Ġtest Password ). form Url Encode ()) ĊĠĠĠĠĠĠĠ Ġ}. apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response . status (), ĠequalTo ( HttpStatusCode . Precondition Failed )) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` when Ġdata Ġis Ġprovided Ġand Ġuser Ġexists Ġthen Ġreturns Ġconflict `() Ġ= Ġtest App ( mock Storage ) Ġ{ ĊĠĠĠĠĠĠĠ Ġwhenever ( mock Storage . retrieve User ( eq ( test Id ), Ġany OrNull ())) Ġdo Return Ġtest User ĊĠĠĠĠĠĠĠ Ġhandle Request ( HttpMethod . Post , Ġendpoint ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġadd Header ( HttpHeaders . ContentType , ĠContentType . Application . Form Url Encoded . toString ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġset Body ( listOf (" userId " Ġto Ġtest Id , Ġ" password " Ġto Ġtest Password ). form Url Encode ()) ĊĠĠĠĠĠĠĠ Ġ}. apply Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠassertThat ( response . status (), ĠequalTo ( HttpStatusCode . Conflict )) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ } Ċ