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

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

# Mainline
while getopts hDH12swjlecpnrb:W: AOPT
do
case $AOPT in
	H) HEADERLESS=0 ;;
	D) STARTDEVTOOLS=1 ;;
	1) DOPACK1=1 ;;
	2) DOPACK=2
	   DOPACK2=1 ;;
	s) DOSELENIUM=1 ;;
	w) DOSELENIUMWD=1 ;;
	j) DOJMETER=1 ;;
	e) DOJEST=1;;
	l) DOLOCUST=1 ;;
	p) DOPLAYWRIGHT=1 ;;
	c) DOCYPRESS=1 ;;
	n) DONEOLOAD=1 ;;
	r) DOBROWSERSTACK=1;;
	b) USEBROWSER=$OPTARG ;;
	W) WEBSERVERSECU=$OPTARG ;;
	h) echo "usage: $0 [-H] [-D] [-b firefox|chrome|chromium] [-1|-2] [-s] [-j] [-l] [-p] [-c] [-w] [-n] [-e] [-r] [-W opt]"
	   echo " where"
	   echo "-H is to run NOT headerless, default is headerless mode"
	   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 "-s run Selenium IDE tests"
	   echo "-w run Selenium WebDriver tests"
	   echo "-j run JMeter tests"
	   echo "-e run Jest tests"
	   echo "-l run Locust tests (run with -H for Firefox control panel)"
	   echo "-p run Playwright tests"
	   echo "-c run Cypress tests"
	   echo "-n run NeoLoad tests"
	   echo "-r 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 -s -2"
	   echo "  sh run_tests.sh -j -2"
	   echo "  sh run_tests.sh -l"
	   echo "  sh run_tests.sh -l -H"
	   echo "  sh run_tests.sh -p"
	   echo "  sh run_tests.sh -p -2"
	   echo "  sh run_tests.sh -p -b chromium"
	   echo "  sh run_tests.sh -c -D"
	   echo "  sh run_tests.sh -W v"
	   echo "  sh run_tests.sh -W p"
	   echo " "
	   exit 1;;
	*) echo "$AOPT is an invalid option" 
		exit 2;;
esac
done

shift $((OPTIND-1))

# 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/Trips01.side &
		else
			selenium-side-runner -c "$BROWSER" SeleniumIDE/Trips01.side
		fi
	fi
	
	if [ $DOPACK2 -eq 1 ]
	then
		echo "Running SeleniumIDE test two"
		selenium-side-runner -c "$BROWSER" SeleniumIDE/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
fi

# Locust
if [ $DOLOCUST -eq 1 ]
then

	if [ $HEADERLESS -eq 0 ]
	then
		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"
		sleep 1
		#locust -f locust/TZSite03.py --host http://theodorezacharia.co.uk
		/home/theodore/.local/bin/locust -f locust/TheoWare.py --host http://theoware.epizy.com
	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
if [ $DOPLAYWRIGHT -eq 1 ]
then
	# https://github.com/microsoft/playwright
	# https://playwright.dev/
	# dev test runner, folio
	# npx folio playwright/theoware.spec.js
	
	export BROWSER=$USEBROWSER
	case $DOPACK in
	2)
	node playwright/theoware_theory.js
	;;
	1)
	if [ $HEADERLESS -eq 0 ]
	then
		#xvfb-run node playwright/test02.js
#		node playwright/test02.js
		node playwright/menu_trips.js
		node playwright/art_random.js
	else
#		node playwright/test02.js
		node playwright/menu_trips.js
		node playwright/art_random.js
	fi
	;;
	*) echo "Unknown testpack $DOPACK" ;;
	esac
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/Test01/Test01.nlp
	else
		/home/theodore/Neoload7.6/bin/NeoLoadCmd -project ~/var/mywebsites/tests/Neoload/Test01/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

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