package Ġorg . simple . clin ic . user Ċ Ċ import Ġcom . f 2 pr ate ek . rx . preferences 2 . Preference Ċ import Ġcom . google . common . truth . Truth . assertThat Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . rules . Rule Chain Ċ import Ġorg . simple . clin ic . App Database Ċ import Ġorg . simple . clin ic . Test Clin ic App Ċ import Ġorg . simple . shared Test Code . TestData Ċ import Ġorg . simple . clin ic . app config . Country Ċ import Ġorg . simple . clin ic . facility . Facility Ċ import Ġorg . simple . clin ic . facility . Facility Repository Ċ import Ġorg . simple . clin ic . rules . Local Authentication Rule Ċ import Ġorg . simple . clin ic . rules . Save Database Rule Ċ import Ġorg . simple . clin ic . user . User . LoggedIn Status . LOG GED _ IN Ċ import Ġorg . simple . clin ic . user . User . LoggedIn Status . UN AUTHORIZ ED Ċ import Ġorg . simple . shared Test Code . util . Rules Ċ import Ġorg . simple . shared Test Code . util . Rx Errors Rule Ċ import Ġorg . simple . clin ic . util . to Optional Ċ import Ġjava . util . Optional Ċ import Ġjava . util . UUID Ċ import Ġjavax . inject . Inject ĊĊ Ċ class ĠUser Session Android Test Ġ{ ĊĊĠ Ġ@ Inject ĊĠ Ġlateinit Ġvar Ġuser Session : ĠUser Session ĊĊĠ Ġ@ Inject ĊĠ Ġlateinit Ġvar Ġapp Database : ĠApp Database ĊĊĠ Ġ@ Inject ĊĠ Ġlateinit Ġvar ĠtestData : ĠTest Data ĊĊĠ Ġ@ Inject ĊĠ Ġlateinit Ġvar Ġfacility Repository : ĠFac ility Repository ĊĊĠ Ġ@ Inject ĊĠ Ġlateinit Ġvar Ġselected Country Preference : ĠPreference < Optional < Country >> ĊĊĠ Ġ@ Inject ĊĠ Ġlateinit Ġvar Ġuser : ĠUser ĊĊĠ Ġ@ Inject ĊĠ Ġlateinit Ġvar Ġfacility : ĠFac ility ĊĊĠ Ġ@ get : Rule ĊĠ Ġval Ġrule Chain : ĠRule Chain Ġ= ĠRules ĊĠĠĠĠĠ Ġ. global () ĊĠĠĠĠĠ Ġ. around ( Local Authentication Rule ()) ĊĠĠĠĠĠ Ġ. around ( Save Database Rule ()) ĊĊĠ Ġ@ Before ĊĠ Ġfun ĠsetUp () Ġ{ ĊĠĠĠ ĠTest Clin ic App . app Component (). inject ( this ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġwhen _ user _ is _ logged _ out _ then _ all _ app _ data _ should _ get _ cleared () Ġ{ ĊĠĠĠ Ġuser Session . logout (). blocking Get () ĊĊĠĠĠ ĠassertThat ( user Session . logged In User (). blocking First (). isPresent ()). isFalse () ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġwhen _ logged _ in _ user _ is _ cleared _ the _ local _ saved _ user _ must _ be _ removed _ from _ database () Ġ{ ĊĠĠĠ ĠassertThat ( user Session . is User Present Loc ally ()). isTrue () ĊĊĠĠĠ Ġuser Session . clear LoggedIn User (). blocking Await () ĊĠĠĠ ĠassertThat ( user Session . is User Present Loc ally ()). isFalse () ĊĊĠĠĠ Ġval Ġuser Ġ= Ġuser Session . logged In User Immediate () ĊĠĠĠ ĠassertThat ( user ). isNull () ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġun author izing _ the _ user _ must _ work _ as _ expected () Ġ{ ĊĠĠĠ ĠassertThat ( user Session . logged In User Immediate () !!. logged In Status ). isEqualTo ( LOG GED _ IN ) ĊĊĠĠĠ Ġuser Session . un authorize (). blocking Await () ĊĊĠĠĠ ĠassertThat ( user Session . logged In User Immediate () !!. logged In Status ). isEqualTo ( UN AUTHORIZ ED ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġwhen _ user _ is _ logged _ out _ then _ the _ selected _ country _ preference _ must _ not _ be _ removed () Ġ{ ĊĠĠĠ Ġval Ġcountry Ġ= ĠtestData . country ( iso CountryCode Ġ= Ġ" IN ") ĊĠĠĠ Ġselected Country Preference . set ( country . to Optional ()) ĊĊĠĠĠ Ġuser Session . logout (). blocking Get () ĊĊĠĠĠ ĠassertThat ( selected Country Preference . get ()). isEqualTo ( Optional . of ( country )) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġfetching _ the _ user _ and _ facility _ details _ should _ work _ as _ expected () Ġ{ ĊĠĠĠ Ġ// Ġgiven ĊĠĠĠ Ġval Ġnew Facility Ġ= ĠTest Data . facility ( ĊĠĠĠĠĠĠĠ Ġuuid Ġ= ĠUUID . fromString (" 5 6 ddc 7 df - 6 a 8 1 - 4 2 bc - 8 6 5 9 - 7 8 b 0 ccb 5 1 edd "), ĊĠĠĠĠĠĠĠ Ġsync Group Ġ= Ġ" bb 9 2 c 0 8 2 - d 7 4 9 - 4 7 8 a - bd 2 a - fe 7 f 1 0 0 5 5 ef 0 " ĊĠĠĠ Ġ) ĊĠĠĠ Ġfacility Repository . save ( listOf ( new Facility )) ĊĊĠĠĠ Ġfacility Repository . setCurrent Facility Immediate ( new Facility ) ĊĊĠĠĠ Ġ// Ġwhen ĊĠĠĠ Ġval Ġuser Facility Details Ġ= Ġuser Session . user Facility Details () ĊĊĠĠĠ Ġ// Ġthen ĊĠĠĠ Ġval Ġexpected Ġ= ĠUser Facility Details ( ĊĠĠĠĠĠĠĠ ĠuserId Ġ= Ġuser . uuid , ĊĠĠĠĠĠĠĠ Ġcurrent Facility Id Ġ= Ġnew Facility . uuid , ĊĠĠĠĠĠĠĠ Ġcurrent Sync GroupId Ġ= Ġnew Facility . sync Group ĊĠĠĠ Ġ) ĊĠĠĠ ĠassertThat ( user Facility Details ). isEqualTo ( expected ) ĊĠ Ġ} Ċ } Ċ