package Ġdev . b mac . github . web Ċ Ċ import Ġdev . b mac . github .* Ċ import Ġdev . b mac . github . storage . Key Storage Ċ import Ġorg . junit . jupiter . api . Test Ċ import Ġorg . junit . jupiter . api . extension . Extend With Ċ import Ġorg . mockito . Mockito Ċ import Ġorg . springframework . beans . factory . annotation . Autowired Ċ import Ġorg . springframework . boot . test . autoconfigure . web . servlet . Web Mvc Test Ċ import Ġorg . springframework . boot . test . mock . mockito . Mock Bean Ċ import Ġorg . springframework . http . HttpStatus Ċ import Ġorg . springframework . test . context . junit . jupiter . Spring Extension Ċ import Ġorg . springframework . test . web . servlet . MockMvc Ċ import Ġorg . springframework . test . web . servlet . request . MockMvc Request Builders Ċ import Ġorg . springframework . test . web . servlet . result . MockMvc Result Matchers Ċ Ċ @ Extend With ( Spring Extension :: class ) Ċ @ Web Mvc Test ( Web Controller :: class ) Ċ class ĠWeb Controller ITest (@ Autowired Ġval Ġmock Mvc : ĠMock Mvc ) Ġ{ ĊĊĠĠĠ Ġ@ Mock Bean ĊĠĠĠ Ġlateinit Ġvar Ġkey Storage : ĠKey Storage ĊĠĠĠ Ġ@ Mock Bean ĊĠĠĠ Ġlateinit Ġvar Ġgit Hub Util : ĠGitHub Util ĊĊĠĠĠ Ġprivate Ġval Ġcode Ġ= Ġ" 1 2 3 4 " ĊĠĠĠ Ġprivate Ġval Ġauth Ġ= Ġ" auth Token " ĊĠĠĠ Ġprivate Ġval Ġkid Ġ= Ġ" test " ĊĠĠĠ Ġprivate Ġval Ġcsrf Ġ= Ġ" csrf " ĊĠĠĠ Ġprivate Ġval Ġpayload Ġ= Ġget Payload () ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Confirm Page () Ġ{ ĊĠĠĠĠĠĠĠ ĠMockito .` when `( key Storage . get Payload ( kid )). thenReturn ( payload ) ĊĠĠĠĠĠĠĠ ĠMockito .` when `( key Storage . get Expiration ( kid )). thenReturn ( 1 0 0 ) ĊĠĠĠĠĠĠĠ Ġmock Mvc . perform ( MockMvc Request Builders . get ("/ init iate ? code =$ code & state =$ kid ")) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( MockMvc Result Matchers . status (). is Ok ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( MockMvc Result Matchers . xpath ("// form / input [@ name =' code '] /@ value "). string ( code )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( MockMvc Result Matchers . xpath ("// form / input [@ name =' id '] /@ value "). string ( kid )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( MockMvc Result Matchers . xpath ("// form / input [@ name =' csrf '] /@ value "). exists ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Perform Page () Ġ{ ĊĠĠĠĠĠĠĠ Ġsetup Ke yst ore () ĊĠĠĠĠĠĠĠ Ġsetup AccessToken () ĊĠĠĠĠĠĠĠ ĠMockito .` when `( git Hub Util . upload G pg Key ( payload . gpg Key !! , Ġauth )). thenReturn ( Status ( KeyType . G PG , Ġ 2 0 1 )) ĊĠĠĠĠĠĠĠ ĠMockito .` when `( git Hub Util . upload S ssh Key ( payload . ssh Key !! , Ġauth )). thenReturn ( Status ( KeyType . SSH , Ġ 2 0 1 )) ĊĠĠĠĠĠĠĠ Ġmock Mvc . perform ( MockMvc Request Builders . post ("/ perform ? code =$ code & id =$ kid & csrf =$ csrf ")) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( MockMvc Result Matchers . status (). is Ok ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( MockMvc Result Matchers . xpath ("// div [@ id =' success ']"). exists ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Perform Page With Bad Result () Ġ{ ĊĠĠĠĠĠĠĠ Ġsetup Ke yst ore () ĊĠĠĠĠĠĠĠ Ġsetup AccessToken () ĊĠĠĠĠĠĠĠ Ġval Ġmessage Ġ= Ġ" Error Ġwith ĠG PG " ĊĠĠĠĠĠĠĠ ĠMockito .` when `( git Hub Util . upload G pg Key ( payload . gpg Key !! , Ġauth )). thenReturn ( Status ( KeyType . G PG , Ġ 5 0 0 , ĠGH Errors (" error ", ĊĠĠĠĠĠĠĠĠĠĠĠ ĠlistOf ( GH Error (" G PG ", Ġ" G PG ", Ġ" unknown ", Ġmessage ))))) ĊĠĠĠĠĠĠĠ ĠMockito .` when `( git Hub Util . upload S ssh Key ( payload . ssh Key !! , Ġauth )). thenReturn ( Status ( KeyType . SSH , Ġ 2 0 1 )) ĊĠĠĠĠĠĠĠ Ġmock Mvc . perform ( MockMvc Request Builders . post ("/ perform ? code =$ code & id =$ kid & csrf =$ csrf ")) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( MockMvc Result Matchers . status (). is Ok ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( MockMvc Result Matchers . xpath ("// div [@ id =' errors ']"). exists ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( MockMvc Result Matchers . xpath ("// li [@ id =' G PG ']"). exists ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( MockMvc Result Matchers . xpath ("// li [@ id =' G PG ']/ p "). string ( message )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( MockMvc Result Matchers . xpath ("// li [@ id =' SSH ']"). does NotExist ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Perform Page With Two Bad Result () Ġ{ ĊĠĠĠĠĠĠĠ Ġsetup Ke yst ore () ĊĠĠĠĠĠĠĠ Ġsetup AccessToken () ĊĠĠĠĠĠĠĠ Ġval Ġgpg Message Ġ= Ġ" Error Ġwith ĠG PG " ĊĠĠĠĠĠĠĠ Ġval Ġssh Message Ġ= Ġ" Error Ġwith Ġssh " ĊĠĠĠĠĠĠĠ ĠMockito .` when `( git Hub Util . upload G pg Key ( payload . gpg Key !! , Ġauth )). thenReturn ( Status ( KeyType . G PG , Ġ 5 0 0 , ĊĠĠĠĠĠĠĠĠĠĠĠ ĠGH Errors ("", ĠlistOf ( GH Error ("", Ġ"", Ġ"", Ġgpg Message ))) ĊĠĠĠĠĠĠĠ Ġ)) ĊĠĠĠĠĠĠĠ ĠMockito .` when `( git Hub Util . upload S ssh Key ( payload . ssh Key !! , Ġauth )). thenReturn ( Status ( KeyType . SSH , Ġ 5 0 0 , ĊĠĠĠĠĠĠĠĠĠĠĠ ĠGH Errors ("", ĠlistOf ( GH Error ("", Ġ"", Ġ"", Ġssh Message ))) ĊĠĠĠĠĠĠĠ Ġ)) ĊĠĠĠĠĠĠĠ Ġmock Mvc . perform ( MockMvc Request Builders . post ("/ perform ? code =$ code & id =$ kid & csrf =$ csrf ")) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( MockMvc Result Matchers . status (). is Ok ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( MockMvc Result Matchers . xpath ("// div [@ id =' errors ']"). exists ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( MockMvc Result Matchers . xpath ("// li [@ id =' G PG ']"). exists ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( MockMvc Result Matchers . xpath ("// li [@ id =' SSH ']"). exists ()) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( MockMvc Result Matchers . xpath ("// li [@ id =' SSH ']/ p "). string ( ssh Message )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( MockMvc Result Matchers . xpath ("// li [@ id =' G PG ']/ p "). string ( gpg Message )) ĊĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Perform Bad Cs rf () Ġ{ ĊĠĠĠĠĠĠĠ Ġsetup Ke yst ore () ĊĠĠĠĠĠĠĠ Ġsetup AccessToken () ĊĠĠĠĠĠĠĠ Ġmock Mvc . perform ( MockMvc Request Builders . post ("/ perform ? code =$ code & id =$ kid & csrf = invalid ")) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( MockMvc Result Matchers . status (). ` is `( HttpStatus . FOR BID DEN . value ())) ĊĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġtest Perform Bad id () Ġ{ ĊĠĠĠĠĠĠĠ Ġsetup Ke yst ore () ĊĠĠĠĠĠĠĠ Ġsetup AccessToken () ĊĠĠĠĠĠĠĠ Ġmock Mvc . perform ( MockMvc Request Builders . post ("/ perform ? code =$ code & id = bad Id & csrf =$ csrf ")) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. andExpect ( MockMvc Result Matchers . status (). ` is `( HttpStatus . NOT _ FOUND . value ())) ĊĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġsetup Ke yst ore () Ġ{ ĊĠĠĠĠĠĠĠ ĠMockito .` when `( key Storage . get Payload ( kid )). thenReturn ( payload ) ĊĠĠĠĠĠĠĠ ĠMockito .` when `( key Storage . get Expiration ( kid )). thenReturn ( 1 0 0 ) ĊĠĠĠĠĠĠĠ ĠMockito .` when `( key Storage . get CSRF ( kid )). thenReturn ( csrf ) ĊĠĠĠĠĠĠĠ ĠMockito .` when `( key Storage . getState ( kid )). thenReturn ( Transaction State ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġsetup AccessToken () Ġ{ ĊĠĠĠĠĠĠĠ ĠMockito .` when `( git Hub Util . get Authentication Token ( code , Ġkid )). thenReturn ( auth ) ĊĠĠĠ Ġ} Ċ }