package Ġcom . jetbrains . edu . slow . integration . step ik Ċ Ċ import Ġcom . jetbrains . edu . learning . E du TestCase Ċ import Ġcom . jetbrains . edu . learning . Study Task Manager Ċ import Ġcom . jetbrains . edu . learning . USER _ AGENT Ċ import Ġcom . jetbrains . edu . learning . course Format . E du Course Ċ import Ġcom . jetbrains . edu . learning . edu Tools UserAgent Ċ import Ġcom . jetbrains . edu . learning . step ik . Step ik Names . get Step ik Api Url Ċ import Ġcom . jetbrains . edu . learning . step ik . Step ik Names . get Step ik Url Ċ import Ġcom . jetbrains . edu . learning . step ik . Step ik TestUtils Ċ import Ġcom . jetbrains . edu . learning . step ik . api . Mock Step ik Connector Ċ import Ġcom . jetbrains . edu . learning . step ik . api . Step ik Connector Ċ import Ġorg . apache . http . Consts Ċ 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 . impl . client . HttpClient s Ċ import Ġorg . apache . http . message . Basic Header Ċ import Ġorg . apache . http . message . Basic NameValuePair Ċ import Ġorg . apache . http . util . Entity Utils Ċ import Ġjava . util . regex . Pattern ĊĊ Ċ abstract Ġclass ĠSte pi k TestCase Ġ: ĠE du TestCase () Ġ{ ĊĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠ Ġprivate Ġconst Ġval ĠCSRF Ġ= Ġ" csrf middleware token " ĊĠ Ġ} ĊĊĠ Ġprivate Ġlateinit Ġvar ĠhttpClient : ĠClose able HttpClient ĊĊĠ Ġoverride Ġfun ĠsetUp () Ġ{ ĊĠĠĠ Ġsuper . setUp () ĊĠĠĠ Ġval Ġmock Step ik Connector Ġ= ĠSte pi k Connector . getInstance () Ġas ĠMock Step ik Connector ĊĠĠĠ Ġmock Step ik Connector . set BaseUrl ( get Step ik Url (), Ġtest Root Disposable ) ĊĠĠĠ Ġval Ġuser Ġ= ĠSte pi k TestUtils . login ( test Root Disposable ) ĊĠĠĠ ĠhttpClient Ġ= ĠHttpClient s . custom () ĊĠĠĠĠĠ Ġ. setDefault Headers ( listOf ( get Authorization Header ( user . get AccessToken () !! ), ĠBasic Header ( USER _ AGENT , Ġedu Tools UserAgent ))) ĊĠĠĠĠĠ Ġ. setDefault Cookie Store ( Basic Cookie Store ()). build () ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġget Authorization Header ( accessToken : ĠString ): ĠBasic Header Ġ{ ĊĠĠĠ Ġreturn ĠBasic Header (" Authorization ", Ġ" Bearer Ġ$ accessToken ") ĊĠ Ġ} ĊĊĠ Ġoverride Ġfun ĠtearDown () Ġ{ ĊĠĠĠ Ġval Ġmock Step ik Connector Ġ= ĠSte pi k Connector . getInstance () Ġas ĠMock Step ik Connector ĊĠĠĠ Ġmock Step ik Connector . set Helper BaseUrl () ĊĠĠĠ Ġval Ġcourse Ġ= ĠStudy Task Manager . getInstance ( project ). course ĊĠĠĠ Ġif Ġ( course Ġis ĠE du Course Ġ&& Ġcourse . is Step ik Remote ) Ġ{ ĊĠĠĠĠĠ Ġremove Uploaded Course ( course . id , Ġcourse . get Less ons (). map Ġ{ Ġit . id Ġ}) ĊĠĠĠ Ġ} ĊĠĠĠ Ġsuper . te ar Down () ĊĠ Ġ} ĊĊĠ Ġ/** ĊĠĠ Ġ* Ġ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 Ġ= Ġ"${ get Step ik 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 ( ĊĠĠĠĠĠ Ġget Step ik Api Url () Ġ+ Ġ" lessons /" Ġ+ Ġlesson Id ) ĊĠĠĠ Ġdelete Request . add Header (" Refer er ", Ġ"${ get Step ik Url () }/ edit - lesson /$ lesson Id / step / 1 ") ĊĠĠĠ ĠhttpClient . execute ( delete Request ) ĊĠĠĠ Ġprintln (" Lesson Ġ$ lesson Id Ġdeleted ") ĊĠ Ġ} ĊĊĠ Ġfun Ġcheck Course Uploaded ( course : ĠE du Course ) Ġ{ ĊĠĠĠ Ġval Ġuploaded Course Ġ= ĠSte pi k Connector . getInstance (). getC ourse Info ( course . id , Ġtrue ) ĊĠĠĠ ĠassertNotNull (" Uploaded Ġcourse Ġnot Ġfound Ġamong Ġcourses Ġavailable Ġto Ġin structor ", Ġuploaded Course ) ĊĠĠĠ Ġprintln (" Course Ġwith Ġid Ġ${ ( uploaded Course Ġas ĠE du Course ). id } Ġwas Ġuploaded Ġsuccessfully ") ĊĠ Ġ} Ċ } Ċ