package Ġcom . jetbrains . edu . integration . step ik Ċ Ċ import Ġcom . jetbrains . edu . learning . E du Settings Ċ import Ġcom . jetbrains . edu . learning . E du TestCase Ċ import Ġcom . jetbrains . edu . learning . Study Task Manager Ċ import Ġcom . jetbrains . edu . learning . auth Utils . Token Info Ċ import Ġcom . jetbrains . edu . learning . course Format . Remote Course Ċ import Ġcom . jetbrains . edu . learning . step ik .* Ċ import Ġorg . apache . http . Consts Ċ import Ġorg . apache . http . NameValuePair Ċ import Ġorg . apache . http . client . entity . Url Encoded Form Entity Ċ import Ġorg . apache . http . client . methods . Http Delete Ċ import Ġorg . apache . http . client . methods . HttpGet Ċ import Ġorg . apache . http . client . methods . HttpPost Ċ import Ġorg . apache . http . impl . client . Basic Cookie Store Ċ import Ġorg . apache . http . impl . client . Closeable HttpClient Ċ import Ġorg . apache . http . message . Basic NameValuePair Ċ import Ġorg . apache . http . util . Entity Utils Ċ import Ġjava . util .* Ċ import Ġjava . util . regex . Pattern ĊĊ Ċ abstract Ġclass ĠSte pi k TestCase Ġ: ĠE du TestCase () Ġ{ ĊĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠ Ġprivate Ġconst Ġval ĠCSRF Ġ= Ġ" csrf middleware token " ĊĠ Ġ} ĊĊĠ Ġprotected Ġlateinit Ġvar Ġuser : ĠSte pi k User ĊĊĠ Ġprivate Ġlateinit Ġvar ĠhttpClient : ĠClose able HttpClient ĊĊĠ Ġoverride Ġfun ĠsetUp () Ġ{ ĊĠĠĠ Ġsuper . setUp () ĊĊĠĠĠ Ġlogin () ĊĊĠĠĠ ĠhttpClient Ġ= ĠSte pi k Client . get Builder () ĊĠĠĠĠĠ Ġ. setDefault Headers ( listOf ( Step ik Authorized Client . get Authorization Header ( user . accessToken ))) ĊĠĠĠĠĠ Ġ. setDefault Cookie Store ( Basic Cookie Store ()). build () ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun ĠtearDown () Ġ{ ĊĠĠĠ Ġval Ġcourse Ġ= ĠStudy Task Manager . getInstance ( project ). course ĊĠĠĠ Ġif Ġ( course Ġis ĠRemote Course ) Ġ{ ĊĠĠĠĠĠ Ġremove Uploaded Course ( course . id , Ġcourse . get Less ons ( true ). map Ġ{ Ġit Ġ-> Ġit . id Ġ}) ĊĠĠĠ Ġ} ĊĠĠĠ ĠE du Settings . getInstance (). user Ġ= Ġnull ĊĠĠĠ Ġsuper . te ar Down () ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġlogin () Ġ{ ĊĠĠĠ Ġval Ġtoken Info Ġ= Ġget Tokens () !! ĊĠĠĠ Ġuser Ġ= ĠSte pi k Authorized Client . login ( token Info ) ĊĠĠĠ ĠE du Settings . getInstance (). user Ġ= Ġuser ĊĠĠĠ Ġprintln (" Logged Ġin Ġas Ġ${ user . firstName } Ġ${ user . lastName }") ĊĠ Ġ} ĊĊĠ Ġ/** ĊĠĠ Ġ* ĠThere Ġis Ġno ĠSte pi k ĠREST ĠAPI Ġto Ġdelete Ġcourses . ĠSo Ġcourse Ġremoval Ġis Ġperformed Ġby Ġreplic ating Ġbrowser Ġprocedure : ĊĠĠ Ġ* Ġ 1 . ĠGet Ġform Ġwith Ġcourse Ġremoval Ġconfirmation ĊĠĠ Ġ* Ġ 2 . ĠPost Ġconfirmation Ġproviding Ġcsrf Ġtoken Ġparsed Ġmanually ĊĠĠ Ġ*/ ĊĠ Ġprivate Ġfun Ġremove Uploaded Course ( course Id : ĠInt , Ġlesson Ids : ĠList < Int >) Ġ{ ĊĠĠĠ Ġprintln (" Removing Ġcourse Ġwith Ġid Ġ$ course Id ...") ĊĠĠĠ Ġval Ġdelete Link Ġ= Ġ"${ Step ik Names . STE PI K _ URL }/ course /$ course Id / delete /" ĊĠĠĠ Ġval Ġget Course Delete Ġ= ĠHttp Get ( delete Link ) ĊĠĠĠ Ġval Ġtext Ġ= ĠEntity Utils . toString ( httpClient . execute ( getC ourse Delete )?. entity ) ĊĠĠĠ Ġval Ġpost Course Delete Ġ= ĠHttp Post ( delete Link ) ĊĠĠĠ Ġpost Course Delete . add Header (" Refer er ", Ġdelete Link ) ĊĠĠĠ Ġpost Course Delete . entity Ġ= ĠUrl Encoded Form Entity ( listOf ( Basic NameValuePair ( ĊĠĠĠĠĠ ĠCSRF , Ġget CSRF Token ( text ))), ĠConst s . UTF _ 8 ) ĊĠĠĠ Ġval Ġpost Result Ġ= ĠhttpClient . execute ( post Course Delete ) ĊĠĠĠ ĠassertEquals (" Failed Ġto Ġremove Ġcourse Ġwith Ġid Ġ$ course Id ", Ġ 3 0 2 , Ġpost Result ?. status Line ?. statusCode ) ĊĠĠĠ Ġfor Ġ( lesson Id Ġin Ġlesson Ids ) Ġ{ ĊĠĠĠĠĠ Ġdelete Lesson ( lesson Id ) ĊĠĠĠ Ġ} ĊĠĠĠ Ġprintln (" Course Ġwith Ġid Ġ$ course Id Ġwas Ġsuccessfully Ġremoved ") ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġget CSRF Token ( text : ĠString ?): ĠString Ġ{ ĊĠĠĠ Ġval Ġpattern Ġ= ĠPattern . compile (" name ='$ CSRF ' Ġvalue =' ([ 0 - 9 a - zA - Z ]+ )' ") ĊĠĠĠ Ġval Ġmatcher Ġ= Ġpattern . matcher ( text ) ĊĠĠĠ Ġmatcher . find () ĊĠĠĠ Ġreturn Ġmatcher . group ( 1 ) ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġdelete Lesson ( lesson Id : ĠInt ) Ġ{ ĊĠĠĠ Ġval Ġdelete Request Ġ= ĠHttp Delete ( ĊĠĠĠĠĠ ĠSte pi k Names . STE PI K _ API _ URL Ġ+ ĠSte pi k Names . LESS ONS Ġ+ Ġlesson Id ) ĊĠĠĠ Ġdelete Request . add Header (" Refer er ", Ġ"${ Step ik Names . STE PI K _ URL }/ edit - lesson /$ lesson Id / step / 1 ") ĊĠĠĠ ĠhttpClient . execute ( delete Request ) ĊĠĠĠ Ġprintln (" Lesson Ġ$ lesson Id Ġdeleted ") ĊĠ Ġ} ĊĊĠ Ġfun Ġcheck Course Uploaded ( course : ĠRemote Course ) Ġ{ ĊĠĠĠ Ġval Ġuploaded Course Ġ= ĠSte pi k Connector . getC ourse Info ( user , Ġcourse . id , Ġtrue ) ĊĠĠĠ ĠassertNotNull (" Uploaded Ġcourse Ġnot Ġfound Ġamong Ġcourses Ġavailable Ġto Ġin structor ", Ġuploaded Course ) ĊĠĠĠ Ġprintln (" Course Ġwith Ġid Ġ${ ( uploaded Course Ġas ĠRemote Course ). id } Ġwas Ġuploaded Ġsuccessfully ") ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġget Tokens (): ĠToken Info ? Ġ{ ĊĠĠĠ Ġval Ġparameters Ġ= ĠArrayList < NameValuePair >( listOf ( Basic NameValuePair Ġ(" grant _ type ", Ġ" client _ credentials "))) ĊĊĠĠĠ Ġval Ġclient Secret Ġ= ĠSystem . getenv (" STE PI K _ TEST _ CLIENT _ SECRET ") ĊĠĠĠ Ġif Ġ( client Secret Ġ== Ġnull Ġ|| Ġclient Secret . isEmpty ()) Ġ{ ĊĠĠĠĠĠ ĠLOG . error (" Test Ġclient Ġsecret Ġis Ġnot Ġprovided ") ĊĠĠĠĠĠ Ġreturn Ġnull ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġval ĠclientId Ġ= ĠSystem . getenv (" STE PI K _ TEST _ CLIENT _ ID ") ĊĠĠĠ Ġif Ġ( clientId Ġ== Ġnull Ġ|| ĠclientId . isEmpty ()) Ġ{ ĊĠĠĠĠĠ ĠLOG . error (" Test Ġclient Ġid Ġis Ġnot Ġprovided ") ĊĠĠĠĠĠ Ġreturn Ġnull ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ// ĠIf Ġwe Ġcan 't Ġget Ġtokens , Ġthere Ġare Ġmight Ġa Ġproblem Ġwith Ġbasic Ġauth Ġthat Ġused Ġon ĠSte pi k Ġby Ġdefault ĊĠĠĠ Ġ// Ġand Ġdisabled Ġfor Ġus Ġas Ġwe Ġuse Ġoauth 2 Ġin Ġour Ġtests . ĊĠĠĠ Ġ// ĠCheck Ġthat : ĊĠĠĠ Ġ// Ġ 1 . ĠTeam city Ġdidn 't Ġchange Ġips Ġof Ġagent ĊĠĠĠ Ġ// Ġ 2 . ĠSte pi k Ġstill Ġhave Ġour Ġserver Ġin Ġwhitelist ĊĠĠĠ Ġreturn ĠSte pi k Authorized Client . get Tokens ( parameters , Ġ"$ clientId :$ client Secret ") ĊĠ Ġ} Ċ }