diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000..1bd73fe --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,29 @@ +name: Tests +on: + push: + branches: + - master + pull_request: + branches: + - master +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Setup Chrome + uses: browser-actions/setup-chrome@v1 + with: + chrome-version: 129 + install-chromedriver: true + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.11' + - name: Install dependencies + run: pip install -r requirements.txt + - name: Run tests and collect coverage + run: coverage run runtests.py diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index dc698f8..0000000 --- a/.travis.yml +++ /dev/null @@ -1,27 +0,0 @@ -dist: xenial -language: python -python: - - "3.7" - - "3.8" - - "3.9" -env: - - DJANGO_VERSION=2.2.27 - - DJANGO_VERSION=3.2.12 - - DJANGO_VERSION=4.0.3 -branches: - except: - - media -before_install: - - wget https://github.com/mozilla/geckodriver/releases/download/v0.25.0/geckodriver-v0.25.0-linux64.tar.gz - - mkdir geckodriver - - tar xzf geckodriver-v0.25.0-linux64.tar.gz -C geckodriver - - export PATH=$PATH:$PWD/geckodriver -install: - - pip install -q django==$DJANGO_VERSION - - pip install -q -e . - - pip install -q selenium - - pip install -q coveralls -script: - - coverage run --source publications --omit publications/six.py publications/tests/__main__.py -after_success: - - coveralls diff --git a/README.md b/README.md index 00ba0f3..a3c4bc0 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ django-publications A Django app for managing scientific publications. -[![Build Status](https://travis-ci.org/lucastheis/django-publications.svg?branch=develop)](https://travis-ci.org/lucastheis/django-publications) +[![CI](https://github.com/lucastheis/django-publications/actions/workflows/ci.yaml/badge.svg)](https://github.com/lucastheis/django-publications/actions) [![Coverage Status](https://coveralls.io/repos/github/lucastheis/django-publications/badge.svg)](https://coveralls.io/github/lucastheis/django-publications) Screenshots diff --git a/publications/settings/__init__.py b/publications/settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/publications/settings/test.py b/publications/settings/test.py new file mode 100644 index 0000000..eaa201d --- /dev/null +++ b/publications/settings/test.py @@ -0,0 +1,67 @@ +from pathlib import Path + +SECRET_KEY = 'test#ca=(=^t8h1a*1ee65_%m$sr38vdd_)!riw9rs17we41no4l3yd' + +BASE_DIR = Path(__file__).resolve().parent + +INSTALLED_APPS = ( + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.staticfiles', + 'django.contrib.messages', + 'publications', +) + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': ':memory:', + } +} + +MIDDLEWARE = ( + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', +) + +ROOT_URLCONF = 'publications.tests.urls' + +STATIC_URL = '/static/' +STATICFILES_DIRS = [] +STATIC_ROOT = BASE_DIR / 'build' / 'static' + +MEDIA_URL = '/media/' +MEDIA_ROOT = BASE_DIR / 'build/media/' # Where user uploads live during development. + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages' + ], + }, + }, +] + +STORAGES = { + 'default': { + 'BACKEND': 'django.core.files.storage.FileSystemStorage', + 'OPTIONS': { + 'location': MEDIA_ROOT, + 'base_url': MEDIA_URL, + }, + }, + 'staticfiles': { + 'BACKEND': 'django.contrib.staticfiles.storage.StaticFilesStorage', + }, +} + diff --git a/publications/tests/__init__.py b/publications/tests/__init__.py index 3cae6a7..a2bbeba 100644 --- a/publications/tests/__init__.py +++ b/publications/tests/__init__.py @@ -1,2 +1,2 @@ -from tests import Tests -from tests_live import LiveTests +from .tests import Tests +from .tests_live import LiveTests diff --git a/publications/tests/__main__.py b/publications/tests/__main__.py deleted file mode 100644 index 1831800..0000000 --- a/publications/tests/__main__.py +++ /dev/null @@ -1,70 +0,0 @@ -import os -import sys -from django.conf import settings, global_settings - -BASE_DIR = os.path.dirname(__file__) -DEBUG = False -INSTALLED_APPS = ( - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.staticfiles', - 'django.contrib.messages', - 'publications', -) -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), - } -} -MIDDLEWARE_CLASSES = ( - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.common.CommonMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware', -) - -ROOT_URLCONF = 'publications.tests.urls' - -settings_dict = { - 'MEDIA_ROOT': os.path.join(BASE_DIR, 'media'), - 'MEDIA_URL': '/media/', - 'STATIC_ROOT': os.path.join(BASE_DIR, 'static'), - 'STATIC_URL': '/static/', - 'DEBUG': DEBUG, - 'INSTALLED_APPS': INSTALLED_APPS, - 'DATABASES': DATABASES, - 'MIDDLEWARE_CLASSES': MIDDLEWARE_CLASSES, - 'ROOT_URLCONF': ROOT_URLCONF} - -if hasattr(global_settings, 'TEMPLATE_CONTEXT_PROCESSORS'): - settings_dict['TEMPLATE_CONTEXT_PROCESSORS'] = \ - tuple(global_settings.TEMPLATE_CONTEXT_PROCESSORS) + ('django.core.context_processors.request',) -else: - settings_dict['TEMPLATES'] = [ - { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages' - ], - }, - }, - ] - -settings_dict['MIDDLEWARE'] = MIDDLEWARE_CLASSES - -settings.configure(**settings_dict) - -import django - -from django import setup -from django.test.runner import DiscoverRunner -setup() -sys.exit(DiscoverRunner(verbosity=1).run_tests(['publications'])) diff --git a/publications/tests/tests.py b/publications/tests/tests.py index 2cfcd11..cc68db6 100644 --- a/publications/tests/tests.py +++ b/publications/tests/tests.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- + from django.test import TestCase from django.contrib.auth.models import User from django.urls import reverse diff --git a/publications/tests/tests_live.py b/publications/tests/tests_live.py index 5556f3a..7200110 100644 --- a/publications/tests/tests_live.py +++ b/publications/tests/tests_live.py @@ -1,21 +1,29 @@ # -*- coding: utf-8 -*- from django.test import LiveServerTestCase -from django.contrib.auth.models import User +from django.contrib.auth import get_user_model from selenium import webdriver -from time import sleep +from selenium.webdriver.common.by import By +from selenium.webdriver.support.expected_conditions import presence_of_element_located +from selenium.webdriver.support.wait import WebDriverWait from publications.tests import tests from publications.models import Publication + class LiveTests(LiveServerTestCase): fixtures = ['initial_data.json', 'test_data.json'] urls = 'publications.tests.urls' @classmethod def setUpClass(cls): - options = webdriver.firefox.options.Options() + options = webdriver.ChromeOptions() options.add_argument('--headless') - cls.selenium = webdriver.Firefox(options=options) + options.add_argument('--disable-gpu') + options.add_argument('--no-sandbox') + + cls.selenium = webdriver.Chrome(options=options) + cls.wait = WebDriverWait(cls.selenium, timeout=5) + super(LiveTests, cls).setUpClass() @@ -26,24 +34,27 @@ def tearDownClass(cls): def setUp(self): - User.objects.create_superuser('admin', 'admin@test.de', 'admin') + get_user_model().objects.create_superuser('admin', 'admin@test.de', 'admin') # login self.selenium.get('{0}{1}'.format(self.live_server_url, '/admin/')) - username_input = self.selenium.find_element_by_name("username") + username_input = self.selenium.find_element(By.NAME, 'username') username_input.send_keys('admin') - password_input = self.selenium.find_element_by_name("password") + password_input = self.selenium.find_element(By.NAME, 'password') password_input.send_keys('admin') - self.selenium.find_element_by_xpath('//input[@value="Log in"]').click() + self.selenium.find_element(By.XPATH, '//input[@value="Log in"]').click() def test_import_bibtex(self): count = Publication.objects.count() - self.selenium.get('{0}{1}'.format(self.live_server_url, '/admin/publications/publication/import_bibtex/')) - bibliography_input = self.selenium.find_element_by_name("bibliography") + self.selenium.get( + '{0}{1}'.format(self.live_server_url, '/admin/publications/publication/import_bibtex/') + ) + self.wait.until(presence_of_element_located((By.NAME, 'bibliography'))) + bibliography_input = self.selenium.find_element(By.NAME, 'bibliography') bibliography_input.send_keys(tests.TEST_BIBLIOGRAPHY) - self.selenium.find_element_by_xpath('//input[@value="Import"]').click() + self.selenium.find_element(By.XPATH, '//input[@value="Import"]').click() self.assertEqual(Publication.objects.count() - count, tests.TEST_BIBLIOGRAPHY_COUNT) @@ -52,5 +63,6 @@ def test_import_bibtex_button(self): count = Publication.objects.count() self.selenium.get('{0}{1}'.format(self.live_server_url, '/admin/publications/publication/')) - self.selenium.find_element_by_link_text('Import BibTex').click() - self.selenium.find_element_by_xpath('//input[@value="Import"]').click() + self.wait.until(presence_of_element_located((By.LINK_TEXT, 'Import BibTex'))) + self.selenium.find_element(By.LINK_TEXT, 'Import BibTex').click() + self.selenium.find_element(By.XPATH, '//input[@value="Import"]').click() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..b65fcd9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +coverage==7.5.1 +django==5.1.2 +pillow==10.3.0 +selenium==4.24.0 diff --git a/runtests.py b/runtests.py new file mode 100644 index 0000000..4151d5a --- /dev/null +++ b/runtests.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + +import os +import sys + +os.environ['DJANGO_SETTINGS_MODULE'] = 'publications.settings.test' + +import django +from django.conf import settings +from django.test.utils import get_runner + + +def main(): + django.setup() + + TestRunner = get_runner(settings) + test_runner = TestRunner() + + failures = test_runner.run_tests(['publications.tests']) + + sys.exit(bool(failures)) + + +if __name__ == '__main__': + main()