package Ġorg . dn j . book tracker . controller Ċ Ċ import Ġorg . dn j . book tracker . Book Tracker Ċ import Ġorg . dn j . book tracker . ErrorResponse Ċ import Ġorg . dn j . book tracker . Login Response Ċ import Ġorg . dn j . book tracker . User Ċ import Ġorg . dn j . book tracker . repo . UserRepository Ċ import Ġorg . dn j . book tracker . service . Auth Service Ċ import Ġorg . junit . jupiter . api . Assertions . assertEquals Ċ import Ġorg . junit . jupiter . api . Test Ċ import Ġorg . junit . jupiter . api . extension . Extend With Ċ import Ġorg . springframework . beans . factory . annotation . Autowired Ċ import Ġorg . springframework . boot . test . context . SpringBootTest Ċ import Ġorg . springframework . boot . test . web . client . Test Rest Template Ċ import Ġorg . springframework . boot . test . web . client . post For Object Ċ import Ġorg . springframework . http . Http Entity Ċ import Ġorg . springframework . http . HttpMethod Ċ import Ġorg . springframework . http . HttpStatus Ċ import Ġorg . springframework . test . context . Context Configuration Ċ import Ġorg . springframework . test . context . junit . jupiter . Spring Extension Ċ Ċ @ Extend With ( Spring Extension :: class ) Ċ @ SpringBootTest ( web Environment Ġ= ĠSpring Boot Test . Web Environment . RANDOM _ PORT ) Ċ @ Context Configuration ( classes Ġ= Ġ[ Book Tracker :: class ]) Ċ class ĠUser Controller Service Ġ{ ĊĊĠĠĠ Ġ@ Autowired ĊĠĠĠ Ġlateinit Ġvar Ġuser Repository : ĠUser Repository ĊĊĠĠĠ Ġ@ Autowired ĊĠĠĠ Ġlateinit Ġvar Ġrest : ĠTest Rest Template ĊĊĠĠĠ Ġ@ Autowired ĊĠĠĠ Ġlateinit Ġvar Ġauth Service : ĠAuth Service ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` create Ġnew Ġuser Ġand Ġlogin `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġuser Ġ= ĠUser (" New User ", Ġ" 1 2 3 ") ĊĠĠĠĠĠĠĠ Ġval Ġresponse Ġ= Ġrest . post For Object < Login Response > ("/ register ", Ġuser , ĠUser :: class ) ĊĠĠĠĠĠĠĠ Ġauth Service . validate Token (" Bearer Ġ${ response !!. jwt }") ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` cannot Ġcreate Ġtwo Ġusers Ġwith Ġthe Ġsame Ġname `() Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġuser 1 Ġ= ĠUser (" New User 1 ", Ġ" 1 2 3 ") ĊĠĠĠĠĠĠĠ Ġval Ġresponse 1 Ġ= Ġrest . post For Object < Login Response > ("/ register ", Ġuser 1 , ĠUser :: class ) ĊĠĠĠĠĠĠĠ Ġauth Service . validate Token (" Bearer Ġ${ response 1 !!. jwt }") ĊĊĠĠĠĠĠĠĠ Ġval Ġuser 2 Ġ= ĠUser (" New User 1 ", Ġ" 1 2 3 ") ĊĠĠĠĠĠĠĠ Ġval Ġresponse 2 Ġ= Ġrest . exchange ("/ register ", ĠHttpMethod . POST , ĠHttp Entity ( user 2 ), ĠError Response :: class . java ) ĊĠĠĠĠĠĠĠ ĠassertEquals ( HttpStatus . BAD _ REQUEST , Ġresponse 2 !!. statusCode ) ĊĠĠĠ Ġ} Ċ } Ċ