From: Chris Fulljames Date: Tue, 21 Jan 2025 03:32:34 +0000 (-0500) Subject: Add online tests X-Git-Url: https://littlesong.place/gitweb/?a=commitdiff_plain;h=126d7535ea17f7acbeaa71d25580114460a67e9a;p=littlesongplace.git Add online tests --- diff --git a/.github/workflows/deploy-test.yml b/.github/workflows/deploy-test.yml index cebb365..3f1f61f 100644 --- a/.github/workflows/deploy-test.yml +++ b/.github/workflows/deploy-test.yml @@ -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 + diff --git a/dev-requirements.txt b/dev-requirements.txt index 26b77f6..866d49a 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,2 +1,3 @@ -r requirements.txt pytest +requests diff --git a/main.py b/main.py index 925a048..b656927 100644 --- 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 diff --git a/test.py b/test/test_offline.py similarity index 98% rename from test.py rename to test/test_offline.py index f427854..408eb17 100644 --- a/test.py +++ b/test/test_offline.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 index 0000000..d311b8b --- /dev/null +++ b/test/test_online.py @@ -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) +