From 43762ddf9c98f200b764d29f43ee9e45fd0299f1 Mon Sep 17 00:00:00 2001
From: Joel Savitz <jsavitz@redhat.com>
Date: Fri, 22 Mar 2024 17:35:17 -0400
Subject: [PATCH 1/4] test: add initial basic testing script

Signed-off-by: Joel Savitz <jsavitz@redhat.com>
---
 test.sh | 145 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 145 insertions(+)
 create mode 100755 test.sh

diff --git a/test.sh b/test.sh
new file mode 100755
index 00000000..703066fc
--- /dev/null
+++ b/test.sh
@@ -0,0 +1,145 @@
+#!/bin/bash
+#
+# Testing script for singularity and orbit
+
+set -exuo pipefail
+
+# Reset the tests and mail directories
+rm -rf test email/logs/* email/mail/*
+mkdir -p test
+
+# This is a temporary workaround until we properly implement volumes
+chcon -R -t container_file_t email
+
+# TODO: login returns 401 so we don't set --fail on the curl command
+
+# Check that registration fails before user creation
+curl --url 'https://localhost/register' \
+  --verbose \
+  --insecure \
+  --fail \
+  --no-progress-meter \
+  --data "student_id=1234" \
+  | tee test/register_fail_no_user \
+  | grep "msg = no such student"
+
+# Check that login fails before user creation
+curl --url 'https://localhost/login' \
+  --verbose \
+  --insecure \
+  --no-progress-meter \
+  --data "username=user&password=pass" \
+  | tee test/login_fail_no_user \
+  | grep "msg = authentication failure"
+
+# Check that we can create a user
+  DOCKER="sudo podman" \
+  CONTAINER="singularity_orbit_1" \
+orbit/warpdrive.sh \
+  -u user -p pass -i 1234 -n \
+  | tee test/create_user \
+  | grep "credentials(username: user, password:pass)"
+
+# Check that registration fails with incorrect student id
+curl --url 'https://localhost/register' \
+  --verbose \
+  --insecure \
+  --fail \
+  --no-progress-meter \
+  --data "student_id=123" \
+  | tee test/register_fail_wrong \
+  | grep "msg = no such student"
+
+# Check that registration suceeds with correct student id
+curl --url 'https://localhost/register' \
+  --verbose \
+  --insecure \
+  --fail \
+  --no-progress-meter \
+  --data "student_id=1234" \
+  | tee test/register_success \
+  | grep "msg = welcome to the classroom"
+
+# Check that registration fails when student id is used for a second time
+curl --url 'https://localhost/register' \
+  --verbose \
+  --insecure \
+  --fail \
+  --no-progress-meter \
+  --data "student_id=1234" \
+  | tee test/register_fail_duplicate \
+  | grep "msg = no such student"
+
+# Check that login fails when credentials are invalid
+curl --url 'https://localhost/login' \
+  --verbose \
+  --insecure \
+  --no-progress-meter \
+  --data "username=user&password=invalid" \
+  | tee test/login_fail_invalid \
+  | grep "msg = authentication failure"
+
+# Check that login succeeds when credentials are valid
+curl --url 'https://localhost/login' \
+  --verbose \
+  --insecure \
+  --fail \
+  --no-progress-meter \
+  --data "username=user&password=pass" \
+  | tee test/login_success \
+  | grep "msg = user authenticated by password"
+
+# Check that the user can get the empty list of email on the server
+curl --url 'pop3s://localhost/' \
+  --verbose \
+  --insecure \
+  --fail \
+  --no-progress-meter \
+  --user user:pass \
+  | tee test/pop_get_empty \
+  | diff <(printf '\r\n') /dev/stdin
+
+CR=$(printf "\r")
+# Check that the user can send a message to the sever
+curl --url 'smtps://localhost' \
+  --verbose \
+  --insecure \
+  --fail \
+  --no-progress-meter \
+  --mail-from 'user@kdlp.underground.software' \
+  --mail-rcpt 'other@kdlp.underground.software' \
+  --upload-file - \
+  --user 'user:pass' \
+  > test/smtp_send_email <<EOF
+Subject: Message Subject$CR
+$CR
+To whom it may concern,$CR
+$CR
+Bottom text$CR
+EOF
+  
+# Check that the user can get the most recent message sent to the server
+curl --url 'pop3s://localhost/1' \
+  --verbose \
+  --insecure \
+  --fail \
+  --no-progress-meter \
+  --user user:pass
+  > test/pop_get_message
+test_pop_get_message=$?
+
+# Check that we can delete a user
+  DOCKER="sudo podman" \
+  CONTAINER="singularity_orbit_1" \
+orbit/warpdrive.sh \
+  -u user -w \
+  > test/delete_user
+
+# Check style compliance
+python -m venv orbit-dev
+source orbit-dev/bin/activate
+pip install -r orbit/dev-requirements.txt
+pushd orbit
+./test-style.sh
+popd
+

From 2dfa47dcb328e675971611fbd343ede709525d86 Mon Sep 17 00:00:00 2001
From: Joel Savitz <jsavitz@redhat.com>
Date: Fri, 22 Mar 2024 18:41:33 -0400
Subject: [PATCH 2/4] test: the script now works for prod and devel

Signed-off-by: Joel Savitz <jsavitz@redhat.com>
---
 test.sh | 45 +++++++++++++++++++++++++++++----------------
 1 file changed, 29 insertions(+), 16 deletions(-)

diff --git a/test.sh b/test.sh
index 703066fc..49cfcf22 100755
--- a/test.sh
+++ b/test.sh
@@ -13,8 +13,25 @@ chcon -R -t container_file_t email
 
 # TODO: login returns 401 so we don't set --fail on the curl command
 
+PORT=${PORT:-443}
+POP_PORT=${POP_PORT:-995}
+SMTP_PORT=${SMTP_PORT:-465}
+EMAIL_HOSTNAME="kdlp.underground.software"
+export DOCKER=${DOCKER:-"sudo podman"}
+export CONTAINER=${CONTAINER:-"singularity_orbit_1"}
+
+# TODO make staging work (dev.underground.software)
+
+if [ ! -z "DEVEL" ]; then
+	PORT=1443
+	POP_PORT=1995
+	SMTP_PORT=1465
+	EMAIL_HOSTNAME="localhost"
+	export DOCKER="podman"
+fi
+
 # Check that registration fails before user creation
-curl --url 'https://localhost/register' \
+curl --url "https://localhost:$PORT/register" \
   --verbose \
   --insecure \
   --fail \
@@ -24,7 +41,7 @@ curl --url 'https://localhost/register' \
   | grep "msg = no such student"
 
 # Check that login fails before user creation
-curl --url 'https://localhost/login' \
+curl --url "https://localhost:$PORT/login" \
   --verbose \
   --insecure \
   --no-progress-meter \
@@ -33,15 +50,13 @@ curl --url 'https://localhost/login' \
   | grep "msg = authentication failure"
 
 # Check that we can create a user
-  DOCKER="sudo podman" \
-  CONTAINER="singularity_orbit_1" \
 orbit/warpdrive.sh \
   -u user -p pass -i 1234 -n \
   | tee test/create_user \
   | grep "credentials(username: user, password:pass)"
 
 # Check that registration fails with incorrect student id
-curl --url 'https://localhost/register' \
+curl --url "https://localhost:$PORT/register" \
   --verbose \
   --insecure \
   --fail \
@@ -51,7 +66,7 @@ curl --url 'https://localhost/register' \
   | grep "msg = no such student"
 
 # Check that registration suceeds with correct student id
-curl --url 'https://localhost/register' \
+curl --url "https://localhost:$PORT/register" \
   --verbose \
   --insecure \
   --fail \
@@ -61,7 +76,7 @@ curl --url 'https://localhost/register' \
   | grep "msg = welcome to the classroom"
 
 # Check that registration fails when student id is used for a second time
-curl --url 'https://localhost/register' \
+curl --url "https://localhost:$PORT/register" \
   --verbose \
   --insecure \
   --fail \
@@ -71,7 +86,7 @@ curl --url 'https://localhost/register' \
   | grep "msg = no such student"
 
 # Check that login fails when credentials are invalid
-curl --url 'https://localhost/login' \
+curl --url "https://localhost:$PORT/login" \
   --verbose \
   --insecure \
   --no-progress-meter \
@@ -80,7 +95,7 @@ curl --url 'https://localhost/login' \
   | grep "msg = authentication failure"
 
 # Check that login succeeds when credentials are valid
-curl --url 'https://localhost/login' \
+curl --url "https://localhost:$PORT/login" \
   --verbose \
   --insecure \
   --fail \
@@ -90,7 +105,7 @@ curl --url 'https://localhost/login' \
   | grep "msg = user authenticated by password"
 
 # Check that the user can get the empty list of email on the server
-curl --url 'pop3s://localhost/' \
+curl --url "pop3s://localhost:$POP_PORT" \
   --verbose \
   --insecure \
   --fail \
@@ -101,13 +116,13 @@ curl --url 'pop3s://localhost/' \
 
 CR=$(printf "\r")
 # Check that the user can send a message to the sever
-curl --url 'smtps://localhost' \
+curl --url "smtps://localhost:$SMTP_PORT" \
   --verbose \
   --insecure \
   --fail \
   --no-progress-meter \
-  --mail-from 'user@kdlp.underground.software' \
-  --mail-rcpt 'other@kdlp.underground.software' \
+  --mail-from "user@$EMAIL_HOSTNAME" \
+  --mail-rcpt "other@$EMAIL_HOSTNAME" \
   --upload-file - \
   --user 'user:pass' \
   > test/smtp_send_email <<EOF
@@ -119,7 +134,7 @@ Bottom text$CR
 EOF
   
 # Check that the user can get the most recent message sent to the server
-curl --url 'pop3s://localhost/1' \
+curl --url "pop3s://localhost:$POP_PORT/1" \
   --verbose \
   --insecure \
   --fail \
@@ -129,8 +144,6 @@ curl --url 'pop3s://localhost/1' \
 test_pop_get_message=$?
 
 # Check that we can delete a user
-  DOCKER="sudo podman" \
-  CONTAINER="singularity_orbit_1" \
 orbit/warpdrive.sh \
   -u user -w \
   > test/delete_user

From 519233a5775725720ae4f6e20ec66de9cbb9202c Mon Sep 17 00:00:00 2001
From: Joel Savitz <jsavitz@redhat.com>
Date: Fri, 22 Mar 2024 18:47:20 -0400
Subject: [PATCH 3/4] test: now works with stagign

Signed-off-by: Joel Savitz <jsavitz@redhat.com>
---
 test.sh | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/test.sh b/test.sh
index 49cfcf22..82633179 100755
--- a/test.sh
+++ b/test.sh
@@ -13,6 +13,8 @@ chcon -R -t container_file_t email
 
 # TODO: login returns 401 so we don't set --fail on the curl command
 
+DEVEL=${DEVEL:-""}
+STAGING=${STAGING:-""}
 PORT=${PORT:-443}
 POP_PORT=${POP_PORT:-995}
 SMTP_PORT=${SMTP_PORT:-465}
@@ -20,9 +22,9 @@ EMAIL_HOSTNAME="kdlp.underground.software"
 export DOCKER=${DOCKER:-"sudo podman"}
 export CONTAINER=${CONTAINER:-"singularity_orbit_1"}
 
-# TODO make staging work (dev.underground.software)
+# NOTE: don't set DEVEL and STAGING at the same time
 
-if [ ! -z "DEVEL" ]; then
+if [ ! -z "$DEVEL" ]; then
 	PORT=1443
 	POP_PORT=1995
 	SMTP_PORT=1465
@@ -30,6 +32,10 @@ if [ ! -z "DEVEL" ]; then
 	export DOCKER="podman"
 fi
 
+if [ ! -z "$STAGING" ]; then
+	EMAIL_HOSTNAME="dev.underground.software"
+fi
+
 # Check that registration fails before user creation
 curl --url "https://localhost:$PORT/register" \
   --verbose \

From 3a8201ba95042c95ff61733388578b75d6e96323 Mon Sep 17 00:00:00 2001
From: Joel Savitz <jsavitz@redhat.com>
Date: Fri, 22 Mar 2024 18:47:52 -0400
Subject: [PATCH 4/4] .gitignore: add test dir to list

Signed-off-by: Joel Savitz <jsavitz@redhat.com>
---
 .gitignore | 1 +
 1 file changed, 1 insertion(+)

diff --git a/.gitignore b/.gitignore
index 1e3f352e..3cf784e1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -13,3 +13,4 @@ smtp
 tcp_server
 template.tar.gz
 .*.swp
+test
