From 6ca1ca2aec662297935ffc952b3afc9a851ccaa8 Mon Sep 17 00:00:00 2001 From: Kyle Robbertze Date: Mon, 18 Oct 2021 12:17:34 +0000 Subject: [PATCH] feat: run API tests in CI (#1421) * feat: run API tests in CI * fix: remove incorrect test for guest user viewing themself --- .github/workflows/test.yml | 52 ++++++++++++++++++++++ api/Makefile | 4 ++ api/README.md | 4 +- api/libretimeapi/settings.py | 5 ++- api/libretimeapi/tests/test_permissions.py | 2 - 5 files changed, 62 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cc369dc29..b2b1d187d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -166,3 +166,55 @@ jobs: - name: Test run: make test working-directory: ${{ matrix.context }} + + test-with-database: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + context: [api] + release: [bionic, buster] + services: + postgres: + image: postgres + env: + POSTGRES_PASSWORD: libretime + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + + container: ghcr.io/libretime/libretime-dev:${{ matrix.release }} + defaults: + run: + shell: bash + env: + LIBRETIME_CONF_FILE: /tmp/libretime-test.conf + + steps: + - uses: actions/checkout@v2 + + - uses: actions/cache@v2 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ matrix.context }}-${{ hashFiles('**/setup.py') }} + restore-keys: | + ${{ runner.os }}-pip-${{ matrix.context }} + + - name: Setup libretime configuration + run: | + cat < $LIBRETIME_CONF_FILE + [general] + api_key = test_key + [database] + host = postgres + dbname = libretime + dbuser = postgres + dbpass = libretime + EOF + cat $LIBRETIME_CONF_FILE + + - name: Test + run: make test + working-directory: ${{ matrix.context }} diff --git a/api/Makefile b/api/Makefile index d83524aa7..dbdeb9431 100644 --- a/api/Makefile +++ b/api/Makefile @@ -9,3 +9,7 @@ MYPY_ARG := libretimeapi format: .format lint: .format-check .pylint .mypy clean: .clean + +test: $(VENV) + source $(VENV)/bin/activate + LIBRETIME_DEBUG=True $(VENV)/bin/libretime-api test libretimeapi diff --git a/api/README.md b/api/README.md index 17b8723a6..48a79abf3 100644 --- a/api/README.md +++ b/api/README.md @@ -30,13 +30,13 @@ sudo systemctl stop libretime-api sudo systemctl restart libretime-analyzer libretime-celery libretime-liquidsoap libretime-playout cd /vagrant/api sudo pip3 install -e . -sudo -u www-data LIBRETIME_DEBUG=True python3 bin/libretime-api runserver 0.0.0.0:8081 +sudo -u www-data LIBRETIME_DEBUG=True libretime-api runserver 0.0.0.0:8081 ``` Unit tests can be run in the vagrant environment using ``` -sudo -u www-data LIBRETIME_DEBUG=True python3 bin/libretime-api test libretimeapi +sudo -u www-data LIBRETIME_DEBUG=True libretime-api test libretimeapi ``` ## 3rd Party Licences diff --git a/api/libretimeapi/settings.py b/api/libretimeapi/settings.py index ebadf9dc4..0cd85addc 100644 --- a/api/libretimeapi/settings.py +++ b/api/libretimeapi/settings.py @@ -1,5 +1,6 @@ import configparser import os +import sys from .utils import get_random_string, read_config_file @@ -11,7 +12,9 @@ API_VERSION = "2.0.0" try: CONFIG = read_config_file(DEFAULT_CONFIG_PATH) -except IOError: +except IOError as e: + print(f"Unable to read config file {DEFAULT_CONFIG_PATH}", file=sys.stderr) + print(e, file=sys.stderr) CONFIG = configparser.ConfigParser() diff --git a/api/libretimeapi/tests/test_permissions.py b/api/libretimeapi/tests/test_permissions.py index a6dfaef4b..f0504e518 100644 --- a/api/libretimeapi/tests/test_permissions.py +++ b/api/libretimeapi/tests/test_permissions.py @@ -92,8 +92,6 @@ class TestPermissions(APITestCase): self.assertEqual( response.status_code, 403, msg="Invalid for model {}".format(model) ) - response = self.logged_in_test_model("users", "guest", GUEST, self.client.get) - self.assertEqual(response.status_code, 403, msg="Invalid for model users") def test_dj_get_permissions(self): for model in self.URLS: