From be8752ed58fa946b767e4f3b88a8cacce7d5b8ff Mon Sep 17 00:00:00 2001
From: Elias Lundell <36220731+LogFlames@users.noreply.github.com>
Date: Sun, 8 Dec 2024 22:00:47 +0100
Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=20ci:=20add=20ci=20for=20tests=20o?=
 =?UTF-8?q?n=20backend=20(#27)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* :construction_worker: add ci

* try connect to serveo from github runner

* Add tags

* disable host-key checking

* add timeout and fix test path
---
 .github/workflows/backend_test.yml            | 75 +++++++++++++++++++
 backend/bittan/bittan/tests/mail/test_mail.py |  4 +-
 2 files changed, 78 insertions(+), 1 deletion(-)
 create mode 100644 .github/workflows/backend_test.yml

diff --git a/.github/workflows/backend_test.yml b/.github/workflows/backend_test.yml
new file mode 100644
index 0000000..88d593c
--- /dev/null
+++ b/.github/workflows/backend_test.yml
@@ -0,0 +1,75 @@
+name: Backend Tests
+
+on:
+  push:
+    branches:
+      - main
+  pull_request:
+    branches:
+      - main
+
+jobs:
+  build-and-test:
+    runs-on: ubuntu-latest
+
+    services:
+      postgres:
+        image: postgres:15
+        ports:
+          - 5432:5432
+        env:
+          POSTGRES_DB: bittan_db
+          POSTGRES_USER: postgres
+          POSTGRES_PASSWORD: postgres
+        options: >-
+          --health-cmd="pg_isready -U postgres"
+          --health-interval=10s
+          --health-timeout=5s
+          --health-retries=30
+
+    steps:
+      - name: Setup postgres etc/hosts
+        run: |
+          echo "127.0.0.1 postgres" | sudo tee -a /etc/hosts
+
+      - name: Checkout code
+        uses: actions/checkout@v4
+
+      - name: Set up Python
+        uses: actions/setup-python@v4
+        with:
+          python-version: 3.13
+
+      - name: Install dependencies
+        working-directory: ./backend/bittan
+        run: |
+          python3 -m pip install --upgrade pip
+          python3 -m pip install -r requirements.txt
+
+      - name: Run tests
+        working-directory: ./backend/bittan
+        run: |
+          timeout=30
+          start_time=$SECONDS
+
+          ssh -o "StrictHostKeyChecking no" -R 80:localhost:8000 serveo.net > serveo.log 2>&1 &
+          PID=$!
+
+          while true; do
+            sleep 1
+            echo "Checking if Serveo is up..."
+            if grep -q 'HTTP' "serveo.log"; then
+              break
+            fi
+            if (( SECONDS - start_time >= timeout )); then
+              echo "Timeout reached. Exiting."
+              exit 1
+            fi
+          done
+
+          url=$(grep -oP 'https://[a-f0-9]+\.serveo\.net' "serveo.log")
+          export APPLICATION_URL=$url
+
+          python3 manage.py test ./bittan/tests --exclude-tag=no_ci
+
+          kill $PID
diff --git a/backend/bittan/bittan/tests/mail/test_mail.py b/backend/bittan/bittan/tests/mail/test_mail.py
index 9fe9742..bc366fc 100644
--- a/backend/bittan/bittan/tests/mail/test_mail.py
+++ b/backend/bittan/bittan/tests/mail/test_mail.py
@@ -1,7 +1,8 @@
-from django.test import TestCase
+from django.test import TestCase, tag
 from bittan.mail import send_mail, make_qr_image
 from bittan.mail import MailError, InvalidRecieverAddressError
 
+@tag("no_ci")
 class SendMailTest(TestCase):
 
 	def setUp(self):
@@ -30,6 +31,7 @@ def test_make_qr_image(self):
 		self.assertEqual(type(qr), bytes)
 		self.assertGreater(len(qr), 1)
 
+	@tag("no_ci")
 	def test_send_qr_in_mail(self):
 		qr = make_qr_image("This is some content!")
 		send_mail("bittantest@gmail.com", "QR Test", "This test mail contains a QR code.", qr)