Commit e6f32785 authored by Emanuele Aina's avatar Emanuele Aina

test: Add functional test exercising db access and homepage rendering

* Use testing.postgresql to set up ephemeral Postgres instances
* Use debian:buster since the postgresql packages fail during
  installation on slim variants since they miss the folders where
  man pages should be installed
* Switch to buster to use libnss-unknown, as `initdb` complains when
  running as an unregistered (in the container) unprivileged user
Signed-off-by: Emanuele Aina's avatarEmanuele Aina <emanuele.aina@collabora.com>
parent e38cc8b4
image: debian:stretch-slim
image: debian:buster
before_script:
- export DEBIAN_FRONTEND=noninteractive
- apt-get update
- apt-get install --no-install-recommends -y python3-pytest
- apt-get install --no-install-recommends -y python3-setuptools python3-wheel python3-pip python3-yaml python3-phabricator python3-yattag ca-certificates python3-flask python3-flask-migrate python3-flask-script python3-flask-sqlalchemy python3-psycopg2 python3-oauthlib python3-blinker python3-requests-oauthlib python3-urlobject
- apt-get install --no-install-recommends -y python3-lxml python3-pytest python3-testing.postgresql
- pip3 install Flask-Dance
- adduser --no-create-home --disabled-password --gecos "" testuser
unit-tests:
stage: test
script:
- pytest-3 --verbose -ra --showlocals
- su testuser -c 'pytest-3 --verbose -ra --showlocals'
FROM debian:stretch-slim AS base
FROM debian:buster AS base
ARG DEBIAN_FRONTEND=noninteractive
......@@ -31,7 +31,14 @@ LABEL description="Apertis QA test results collection and reporting application
RUN apt update && \
apt install --no-install-recommends -y \
python3-pytest
python3-lxml \
python3-pytest \
python3-testing.postgresql
# this has to come after postgres is installed, as it breaks the `adduser postgres` call
RUN apt update && \
apt install --no-install-recommends -y \
libnss-unknown # needed to run as unprivileged user, as required by postgres instantiated by testing.postgresql
FROM base AS runtime
......
# Copyright © 2019 Collabora Ltd.
#
# SPDX-License-Identifier: MPL-2.0
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import json
import os
import sys
import unittest
import flask_migrate
import lxml.etree
import pytest
from testing.postgresql import Postgresql
# Import modules from the current and parent directory in case pytest is
# invoked in this folder or from the project root
sys.path += ['.', '..']
from testutils import open_relative, import_app_with_custom_db_instance
class FunctionalTestCase(unittest.TestCase):
def setUp(self):
self.postgresql = Postgresql()
self.app, self.db = import_app_with_custom_db_instance(self.postgresql)
migrate = flask_migrate.Migrate(self.app, self.db)
with self.app.app_context():
flask_migrate.upgrade()
def tearDown(self):
self.postgresql.stop()
def test_index(self):
client = self.app.test_client()
response = client.get('/')
assert response.status_code == 200
assert response.mimetype == 'text/html'
assert response.charset == 'utf-8'
parser = lxml.etree.HTMLParser(recover=False)
contents = lxml.etree.HTML(response.data.decode(response.charset))
assert contents.xpath('head/title') != None
assert contents.xpath('body') != None
......@@ -5,6 +5,7 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import importlib
import os
def open_relative(path, *args, **kwargs):
......@@ -12,3 +13,16 @@ def open_relative(path, *args, **kwargs):
dir_path = os.path.dirname(os.path.realpath(__file__))
final_path = os.path.join(dir_path, path)
return open(final_path, *args, **kwargs)
def import_app_with_custom_db_instance(postgresql):
config = postgresql.dsn()
os.environ['DB_USER'] = config['user']
os.environ['DB_PASSWORD'] = 'foo'
os.environ['DB_HOST'] = config['host']
os.environ['DB_PORT'] = str(config['port'])
os.environ['DB_NAME'] = config['database']
import webhook
importlib.reload(webhook) # ensure that it picks up the new settings
return webhook.app, webhook.db
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment