]> littlesong.place Git - littlesongplace.git/commitdiff
Add online tests
authorChris Fulljames <christianfulljames@gmail.com>
Tue, 21 Jan 2025 03:32:34 +0000 (22:32 -0500)
committerChris Fulljames <christianfulljames@gmail.com>
Tue, 21 Jan 2025 03:32:34 +0000 (22:32 -0500)
.github/workflows/deploy-test.yml
dev-requirements.txt
main.py
test/test_offline.py [moved from test.py with 98% similarity]
test/test_online.py [new file with mode: 0644]

index cebb365a5aed0fc39b1a35a446a86607d4415398..3f1f61f61c54c26e8ad891308a0abc8a200e0bb3 100644 (file)
@@ -22,10 +22,12 @@ jobs:
         pip install -r dev-requirements.txt
 
     - name: Run pytest
+      working-directory: ./test
       run: |
-        pytest test.py
+        pytest test_offline.py
 
   deploy-test-instance:
+    needs: run-offline-tests
     runs-on: ubuntu-latest
     steps:
     - uses: actions/checkout@v1
@@ -56,3 +58,25 @@ jobs:
 
           # Restart service
           sudo systemctl restart littlesongplace-test.service
+
+  run-online-tests:
+    runs-on: ubuntu-latest
+    needs: [run-offline-tests, deploy-test-instance]
+    steps:
+    - uses: actions/checkout@v1
+
+    - name: Set up Python
+      uses: actions/setup-python@v3
+      with:
+        python-version: "3.11"
+
+    - name: Install Dependencies
+      run: |
+        python -m pip install --upgrade pip
+        pip install -r dev-requirements.txt
+
+    - name: Run pytest
+      working-directory: ./test
+      run: |
+        pytest test_online.py
+
index 26b77f68be53ca5605724cc5dead0e5cd6d4132b..866d49a2658743191b0b62cb81cfe15be0eddfac 100644 (file)
@@ -1,2 +1,3 @@
 -r requirements.txt
 pytest
+requests
diff --git a/main.py b/main.py
index 925a04817beb6ef532d0df8eafc6fa735ca872a3..b6569271502624cc5a1aed6dc451c16add799941 100644 (file)
--- a/main.py
+++ b/main.py
@@ -41,7 +41,6 @@ root_logger.addHandler(handler)
 app = Flask(__name__)
 app.secret_key = os.environ["SECRET_KEY"] if "SECRET_KEY" in os.environ else "dev"
 app.config["MAX_CONTENT_LENGTH"] = 50 * 1024 * 1024
-app.logger.addHandler(handler)
 
 if "DATA_DIR" in os.environ:
     # Running on server behind proxy
similarity index 98%
rename from test.py
rename to test/test_offline.py
index f427854c248f99f6a78ae5890d0541714bc4d7f5..408eb1750cc427db6653413f7af13242a645714a 100644 (file)
--- a/test.py
@@ -171,7 +171,7 @@ def test_update_bio(client):
 ################################################################################
 
 def _test_upload_song(client, msg, error=False, songid=None, user="user", **kwargs):
-    song_file = open("test/sample-3s.mp3", "rb")
+    song_file = open("sample-3s.mp3", "rb")
 
     data = {
         "song": song_file,
@@ -301,7 +301,7 @@ def test_update_song_invalid_song(client):
     _create_user_and_song(client)
 
     data = {
-        "song": open("test/sample-3s.mp3", "rb"),
+        "song": open("sample-3s.mp3", "rb"),
         "title": "song title",
         "description": "song description",
         "tags": "tag",
@@ -315,7 +315,7 @@ def test_update_song_invalid_id(client):
     _create_user_and_song(client)
 
     data = {
-        "song": open("test/sample-3s.mp3", "rb"),
+        "song": open("sample-3s.mp3", "rb"),
         "title": "song title",
         "description": "song description",
         "tags": "tag",
@@ -330,7 +330,7 @@ def test_update_song_other_users_song(client):
     _create_user(client, "user2", login=True)
 
     data = {
-        "song": open("test/sample-3s.mp3", "rb"),
+        "song": open("sample-3s.mp3", "rb"),
         "title": "song title",
         "description": "song description",
         "tags": "tag",
@@ -380,7 +380,7 @@ def test_delete_song_other_users_song(client):
 def test_get_song(client):
     _create_user_and_song(client)
     response = client.get("/song/1/1")
-    with open("test/sample-3s.mp3", "rb") as mp3file:
+    with open("sample-3s.mp3", "rb") as mp3file:
         assert response.data == mp3file.read()
 
 def test_get_song_invalid_song(client):
diff --git a/test/test_online.py b/test/test_online.py
new file mode 100644 (file)
index 0000000..d311b8b
--- /dev/null
@@ -0,0 +1,53 @@
+import html
+import json
+import re
+
+import requests
+import pytest
+
+HOST = "http://littlesong.place:8000"
+
+def url(path):
+    return HOST + path
+
+@pytest.fixture(scope="module")
+def s():
+    s = requests.Session()
+    # User may already exist, but that's fine - we'll just ignore the signup error
+    response = s.post(url("/signup"), data={"username": "user", "password": "1234asdf!@#$", "password_confirm": "1234asdf!@#$"})
+    response = s.post(url("/login"), data={"username": "user", "password": "1234asdf!@#$"})
+    response.raise_for_status()
+    yield s
+
+def _get_song_list_from_page(page_contents):
+    matches = re.findall('data-song="(.*)">', page_contents)
+    return [json.loads(html.unescape(m)) for m in matches]
+
+def test_upload_and_delete_song(s):
+    response = s.post(
+        url("/upload-song"),
+        files={"song": open("sample-3s.mp3", "rb")},
+        data={
+            "title": "song title",
+            "description": "song description",
+            "tags": "tag1, tag2",
+            "collabs": "p1, p2",
+        },
+    )
+    response.raise_for_status()
+    songs = _get_song_list_from_page(response.text)
+    song = songs[0]
+
+    # Check song uploaded correctly
+    assert song["title"] == "song title"
+    assert song["description"] == "song description"
+    assert song["tags"] == ["tag1", "tag2"]
+    assert song["collaborators"] == ["p1", "p2"]
+
+    # Delete song
+    songid = song["songid"]
+    response = s.get(url(f"/delete-song/{songid}"), headers={"referer": "/users/user"})
+    response.raise_for_status()
+    songs = _get_song_list_from_page(response.text)
+    assert not any(song["songid"] == songid for song in songs)
+