# Author: Theodore Zacharia
# V0.2 - 15/10/2020
# Script to control running of tests for website checks

# Globals
HEADERLESS=1
SHOWUI=0
DOPACK=1	# default pack
DOPACK1=1	# default pack
DOPACK2=0
DOSELENIUM=0
DOSELENIUMWD=0
DOJMETER=0
DOLOCUST=0
DOPLAYWRIGHT0=0
DOPLAYWRIGHT=0
DOPUPPETEER=0
DOARTILLERY=0
DOCYPRESS=0
DOJEST=0
DONEOLOAD=0
DOBROWSERSTACK=0
DOCUCUMBER=0
DOK6=0
USEBROWSER=""
STARTDEVTOOLS=0
WEBSERVERSECU=""

# Mainline
while getopts hUDH12b:W:P: AOPT
do
case $AOPT in
	H) HEADERLESS=0 ;;
	U) SHOWUI=1 ;;
	D) STARTDEVTOOLS=1 ;;
	1) DOPACK1=1 ;;
	2) DOPACK=2
	   DOPACK2=1 ;;
	P) DOPACK1=0 ; DOPACK2=0 ; DOPACK=$OPTARG ;;
	b) USEBROWSER=$OPTARG ;;
	W) WEBSERVERSECU=$OPTARG ;;
	h) echo "usage: $0 [-H] [-D] [-P testpack] [-b firefox|chrome|chromium|msedge] [-1|-2] [-W opt] test..."
	   echo " where"
	   echo "-H is to run NOT headerless, default is headerless mode"
	   echo "-U is to show the tools development UI where applicable (can be passed into the tool by placing after)"
	   echo "-D is to start the development tool for the test type, NOT run the test itself (where appropriate)"
	   echo "-b is to set browser, firefox id the default, can set chromium, chrome or webkit"
	   echo "-1 is to run test pack 1, this is the default test pack"
	   echo "-2 is to run test pack 2 as well"
	   echo "-P pack to run a specific test pack"
	   echo "-r selenium to run Selenium IDE tests"
	   echo "-r seleniumwd to Selenium WebDriver tests"
	   echo "-r jmeter to run JMeter tests"
	   echo "-r jest to run Jest tests"
	   echo "-r locust to run Locust tests (run with -H for Firefox control panel)"
	   echo "-r playwright to run Playwright tests"
	   echo "-r cypress to run Cypress tests"
	   echo "-r neoload to run NeoLoad tests"
	   echo "-r browserstack to run BrowserStack tests"
	   echo "-W run web server security tests, where opt is:"
	   echo "   f=full set, v=vulnerability, p=penetration"
	   echo " "
	   echo "Examples:"
	   echo "  sh run_tests.sh -2 selenium"
	   echo "  sh run_tests.sh -2 seleniumwd"
	   echo "  sh run_tests.sh -2 jest"
	   echo "  sh run_tests.sh locust"
	   echo "  sh run_tests.sh -H locust"
	   echo "  sh run_tests.sh playwright"
	   echo "  sh run_tests.sh -U playwright"
	   echo "  sh run_tests.sh puppeteer"
	   echo "  sh run_tests.sh -H puppeteer"
	   echo "  sh run_tests.sh artillery"
	   echo "  sh run_tests.sh jmeter"
	   echo "  sh run_tests.sh -P 3 jmeter"
	   echo "  sh run_tests.sh playwright jmeter"
	   echo "  sh run_tests.sh -2 playwright"
	   echo "  sh run_tests.sh -H playwright --project MicrosoftEdge"
	   echo "  sh run_tests.sh -H playwright --repeat-each 12 --workers 6"
	   echo "  sh run_tests.sh -H playwright --project MicrosoftEdge --repeat-each 12 --workers 6"
	   echo "  sh run_tests.sh -b chromium playwright"
	   echo "  sh run_tests.sh -D cypress"
	   echo "  sh run_tests.sh -W v security"
	   echo "  sh run_tests.sh -W p security"
	   echo " "
	   exit 1;;
	*) echo "$AOPT is an invalid option" 
		exit 2;;
esac
done

shift $((OPTIND-1))

# go through tests
for ATEST in ${@}
do
	# NOTE: at present the order of the tests is NOT preserved
	case $ATEST in
	selenium) DOSELENIUM=1 ;;
	seleniumwd) DOSELENIUMWD=1 ;;
	jmeter) DOJMETER=1 ;;
	jest) DOJEST=1;;
	locust) DOLOCUST=1 ;;
	playwright0) DOPLAYWRIGHT0=1 ;;
	playwright) DOPLAYWRIGHT=1 ;;
	puppeteer) DOPUPPETEER=1 ;;
	artillery) DOARTILLERY=1 ;;
	cypress) DOCYPRESS=1 ;;
	neoload) DONEOLOAD=1 ;;
	browserstack) DOBROWSERSTACK=1;;
	cucumber) DOCUCUMBER=1 ;;
	k6) DOK6=1;;
	security) ;;
	esac
done

# Selenium IDE tests
if [ $DOSELENIUM -eq 1 ]
then
	if [ -n "$USEBROWSER" ]
	then
		BROWSER="browserName=$USEBROWSER"
	else
		BROWSER="browserName=firefox"
	fi
	if [ $DOPACK1 -eq 1 ]
	then
		echo "Running SeleniumIDE test one"
		if [ $DOPACK2 -eq 1 ]
		then
			echo "running concurrently"
			selenium-side-runner -c "$BROWSER" SeleniumIDE/Theoware_Trips01.side &
		else
			selenium-side-runner -c "$BROWSER" SeleniumIDE/Theoware_Trips01.side
		fi
	fi
	
	if [ $DOPACK2 -eq 1 ]
	then
		echo "Running SeleniumIDE test two"
		selenium-side-runner -c "$BROWSER" SeleniumIDE/Theoware_art_random.side
	fi
fi 

# Selenium
if [ $DOSELENIUMWD -eq 1 ]
then
	node Selenium/Test01.js
fi

# JMeter
if [ $DOJMETER -eq 1 ]
then
	if [ $HEADERLESS -eq 0 ]
	then
		JMGUIMODE=""
	else
		JMGUIMODE="-n"
	fi

	if [ $DOPACK1 -eq 1 ]
	then
		echo "Running JMeter test for Pack01 (Infinity)"
		if [ $DOPACK2 -eq 1 ]
		then
			echo "running concurrently"
			/home/theodore/tools/apache-jmeter-5.3/bin/jmeter.sh $JMGUIMODE -t jmeter/TheoWareInfinity.jmx &
		else
			/home/theodore/tools/apache-jmeter-5.3/bin/jmeter.sh $JMGUIMODE -t jmeter/TheoWareInfinity.jmx
		fi
	fi

	if [ $DOPACK2 -eq 1 ]
	then
		echo "Running JMeter test for Pack02"
		/home/theodore/tools/apache-jmeter-5.3/bin/jmeter.sh $JMGUIMODE -t jmeter/TheoWare.jmx
	fi

	if [ $DOPACK -eq 3 ]
	then
		echo "Running JMeter test for Pack3"
		/home/theodore/tools/apache-jmeter-5.3/bin/jmeter.sh $JMGUIMODE -t jmeter/Test_YouTube.jmx
	fi
fi

# Locust
if [ $DOLOCUST -eq 1 ]
then

	if [ $HEADERLESS -eq 0 ]
	then
		#locust -f locust/TZSite03.py --host http://theodorezacharia.co.uk
		/home/theodore/.local/bin/locust -f locust/TheoWare.py --host http://theoware.epizy.com &
		sleep 2
		firefox http://127.0.0.1:8089/
		echo "refresh the browser page to complete numbers to start test"
		echo "STOP via UI and then CTRL-C to end test"
		echo "**** NOTE *****"
		echo "To terminate the process enter: pkill locust"
	else
		/home/theodore/.local/bin/locust -f locust/TheoWare.py --host http://theoware.epizy.com --headless --users 8 --hatch-rate 1 --run-time 60s
	fi
fi

# Playwright OLD
if [ $DOPLAYWRIGHT0 -eq 1 ]
then
	# https://github.com/microsoft/playwright
	# https://playwright.dev/
	# dev test runner, folio
	# npx folio playwright/theoware.spec.js
	# npx playwright test theoware_theory.js
	
	export BROWSER=$USEBROWSER
	cd playwright0
	case $DOPACK in
	2)
	node theoware_theory.js
	;;
	1)
	if [ $HEADERLESS -eq 0 ]
	then
		#xvfb-run node playwright/test02.js
#		node playwright/test02.js
		node menu_trips.js
		node art_random.js
	else
#		node playwright/test02.js
		node menu_trips.js
		node art_random.js
	fi
	;;
	*) echo "Unknown testpack $DOPACK" ;;
	esac
	cd
fi


# Playwright
if [ $DOPLAYWRIGHT -eq 1 ]
then
	shift 1
	# get any remaining arts
	echo "running with: $@"
	export BROWSER=$USEBROWSER
	if [ $HEADERLESS -eq 0 ]
	then
		HEADERLESSMODE="--headed"
	else
		HEADERLESSMODE=""
	fi
	if [ $SHOWUI -eq 1 ]
	then
		TSHOWUI="--ui"
	else
		TSHOWUI=""
	fi
	cd playwright

	case $DOPACK in
	3)
	npx playwright test $TSHOWUI torTest2.spec.ts  $HEADERLESSMODE $@
	EL=$?
	;;
	2)
	npx playwright test $TSHOWUI Tests/Theoware_theory $HEADERLESSMODE $@
	EL=1
	;;
	1)
	npx playwright test $TSHOWUI Tests/Theoware_art $HEADERLESSMODE $@
	EL=$?
	;;
	*) echo "Unknown testpack $DOPACK"
	EL=9
	;;
	esac

	if [ $EL -eq 0 ]
	then
		npx playwright show-report
	fi
	cd -

fi

# Puppeteer
if [ $DOPUPPETEER -eq 1 ]
then
	if [ $HEADERLESS -eq 0 ]
	then
		HEADERLESSMODE="-H"
	else
		HEADERLESSMODE=""
	fi

	cd puppeteer
	node TZ_apps.js -t $HEADERLESSMODE
	cd -
fi



# Artillery 
if [ $DOARTILLERY -eq 1 ]
then
	echo "Tests under"
	#npm run test
	cd artillery
	artillery run TZsite.yml
	cd -
fi

# Jest
if [ $DOJEST -eq 1 ]
then
	echo "Tests under"
	#npm run test
	cd playwright
	npx jest test --notify # --config=config.json
	cd -
fi

# Cypress
if [ $DOCYPRESS -eq 1 ]
then
	echo "Cypress at /home/theodore/.cache/Cypress/6.0.1"
	echo "Tests under ./cypress/integration/TheoWare"
	if [ -n "$USEBROWSER" ]
	then
		BROWSER="--browser $USEBROWSER"
	else
		BROWSER=""
	fi

	# explicit use the UI dev section
	if [ $STARTDEVTOOLS -eq 1 ]
	then
		./node_modules/.bin/cypress open $BROWSER
		# npm run cypress:open # this version will use the package.json
		# running tests from command line
		# ./node_modules/.bin/cypress run
	fi

	# explicit test section
	if [ $STARTDEVTOOLS -eq 0 ]
	then
		# different methods for running
		# run all tests in sequence
		#npx cypress run --spec "cypress/integration/TheoWare/*.js"

		echo "running concurrently"
		npm run cypress:run -- --spec "cypress/integration/TheoWare/menu_trips.js" $BROWSER &
		npx cypress run --spec "cypress/integration/TheoWare/art_random.js" $BROWSER
	fi

fi

# Neoload
if [ $DONEOLOAD -eq 1 ]
then
	# see doco at https://www.neotys.com/documents/doc/neoload/latest/en/html/#643.htm
	#/home/theodore/Neoload7.6/bin/NeoLoadGUI
	if [ $STARTDEVTOOLS -eq 1 ]
	then
		/home/theodore/Neoload7.6/bin/NeoLoadCmd -project ~/var/mywebsites/tests/neoload/src/Test01.nlp
	else
		/home/theodore/Neoload7.6/bin/NeoLoadCmd -project ~/var/mywebsites/tests/neoload/src/Test01.nlp -launch TheoWare-TestLoad
	fi
fi

# BrowserStack
if [ $DOBROWSERSTACK -eq 1 ]
then
	# see https://www.browserstack.com/local-testing/automate
	echo "BrowserStack at http://localhost:45454/"
	echo "Set for local testing add to code ==> caps.setCapability("browserstack.local", "true");"
	firefox http://localhost:45454/
	/home/theodore/tools/browserstack/BrowserStackLocal --key fjPgJqbqtN1yhVb53FZq --force-local
fi


# k6
if [ $DOK6 -eq 1 ]
then
	echo "Starting at $(date)"
	cd k6

	if [ $HEADERLESS -eq 0 ]
	then
		export K6_BROWSER_ENABLED=true
	else
		export K6_BROWSER_ENABLED=false
	fi
#	k6 run Theoware_art.js --out json=test.json --out influxdb=http://localhost:8086/k6
	case $DOPACK in
	1) export K6_BROWSER_ENABLED=false
		k6 run Theoware_art.js --out json=test.json --out csv=test.csv ;;
	2) export K6_BROWSER_ENABLED=true
		k6 run Theoware_art_browser.js --out json=test.json --out csv=test.csv ;;
	*) ;;
	esac
	cd -
fi

# Cucumber
if [ $DOCUCUMBER -eq 1 ]
then
	echo "Tests under"
	cd cucumber/hellocucumber
	mvn test
	cd -
fi

if [ -n "$WEBSERVERSECU" ]
then
	# See https://www.zaproxy.org/getting-started/
	# See https://github.com/wapiti-scanner/wapiti/blob/master/doc/wapiti.ronn
	echo "Starting at $(date)"
	PUBLIC_IPADDR=$(curl -s ifconfig.me)
	if [ "$WEBSERVERSECU" = "f" ]
	then
		echo "Running Web Server Security Checks - Full Set"
	fi

	echo "Web server public IP address is $PUBLIC_IPADDR"

	if [ "$WEBSERVERSECU" = "v" ] || [ "$WEBSERVERSECU" = "f" ]
	then
		echo "Running Web Server Security Checks - Vulnerability Checks"
		echo "Scan logs may be found in ~/.wapiti/scans"
		#flatpak run org.zaproxy.ZAP
		# If attack process is interrupted. Scan will be resumed next time unless you specify "--flush-attacks" or "--flush-session".
		wapiti -o wapiti --color -v 2 -u http://$PUBLIC_IPADDR/
		echo "Done at $(date)"
		echo "Scan logs may be found in ~/.wapiti/scans"
	fi

	if [ "$WEBSERVERSECU" = "p" ] || [ "$WEBSERVERSECU" = "f" ]
	then
		ZAPRPT=/home/theodore/var/mywebsites/tests/zap/theoware.com.report.xml
		echo "Running Web Server Security Checks - Penetration Checks"
		#/usr/bin/flatpak run --branch=stable --arch=x86_64 --command=zap org.zaproxy.ZAP -session /home/theodore/var/mywebsites/tests/zap/TheoWare01.session
		/usr/bin/flatpak run --branch=stable --arch=x86_64 --command=zap org.zaproxy.ZAP -quickurl http://$PUBLIC_IPADDR/ -quickout $ZAPRPT -session /home/theodore/var/mywebsites/tests/zap/TheoWare01.session
		echo "Done at $(date)"
		echo "Report found at $ZAPRPT"
	fi


	# look into remote audit
	# cd /home/theodore/tools/lynis
	# sh lynis audit system remote 77.103.131.144

fi