From 1e42ee15281c341052478fdfa1a1fa84bd0924a6 Mon Sep 17 00:00:00 2001 From: Stephan Geulette Date: Thu, 10 Oct 2024 13:27:49 +0200 Subject: [PATCH] Added first test and gha --- .github/workflows/main.yml | 62 +++++++++++++++++++ gha.cfg | 4 ++ src/imio/fpaudit/browser/settings.py | 8 --- src/imio/fpaudit/configure.zcml | 3 + src/imio/fpaudit/testing.py | 37 +++++++++++ src/imio/fpaudit/tests/__init__.py | 0 .../fpaudit/tests/test_browser_settings.py | 34 ++++++++++ 7 files changed, 140 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/main.yml create mode 100644 gha.cfg create mode 100644 src/imio/fpaudit/testing.py create mode 100644 src/imio/fpaudit/tests/__init__.py create mode 100644 src/imio/fpaudit/tests/test_browser_settings.py diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..63cc7a0 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,62 @@ +# This workflow will install Python dependencies, run tests and lint with a variety of Python versions +# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions + +name: Push tests +# run-name: ${{ github.actor }} push tests + +on: + push: + pull_request: + +jobs: + test: + runs-on: ubuntu-22.04 + strategy: + fail-fast: false + matrix: + include: + - python: 2.7.18 + plone: 4.3 +# - python: 3.7.14 +# plone: 5.2 +# - python: 3.10.11 +# plone: "6.0" + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Set up pyenv and Python + uses: "gabrielfalcao/pyenv-action@v14" + with: + default: "${{ matrix.python }}" + - name: Setup Env + run: | + pip install --upgrade pip + pip install -r requirements-${{ matrix.plone }}.txt + - name: Cache eggs + uses: actions/cache@v3 + env: + cache-name: cache-eggs + with: + path: ~/buildout-cache/eggs + key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ matrix.python }}-${{ matrix.plone }} +# - name: Cache eggs restore +# id: cache-restore +# uses: actions/cache/restore@v3 +# env: +# cache-name: cache-eggs +# with: +# path: ~/buildout-cache/eggs +# key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ matrix.python }}-${{ matrix.plone }} + - name: buildout + run: | + sed -ie "s#test.cfg#test-${{matrix.plone}}.cfg#" gha.cfg + buildout -c gha.cfg annotate + buildout -c gha.cfg +# - name: Cache eggs save +# uses: actions/cache/save@v3 +# with: +# path: ~/buildout-cache/eggs +# key: ${{ steps.cache-restore.outputs.cache-primary-key }} + - name: test + run: | + bin/test -t !robot diff --git a/gha.cfg b/gha.cfg new file mode 100644 index 0000000..449d60f --- /dev/null +++ b/gha.cfg @@ -0,0 +1,4 @@ +[buildout] +extends = + test.cfg +eggs-directory = ~/buildout-cache/eggs diff --git a/src/imio/fpaudit/browser/settings.py b/src/imio/fpaudit/browser/settings.py index d2e4c20..aae32a9 100644 --- a/src/imio/fpaudit/browser/settings.py +++ b/src/imio/fpaudit/browser/settings.py @@ -71,14 +71,6 @@ def validate_settings(data): # noqa ) ) ids.append(entry["log_id"]) - if not entry["audit_log"].strip(): - raise Invalid( - _(u"You must provide a value for audit file name"), - ) - if not entry["log_format"].strip(): - raise Invalid( - _(u"You must provide a value for audit log format"), - ) class FPAuditSettings(RegistryEditForm): diff --git a/src/imio/fpaudit/configure.zcml b/src/imio/fpaudit/configure.zcml index f717f74..6aaf1f0 100644 --- a/src/imio/fpaudit/configure.zcml +++ b/src/imio/fpaudit/configure.zcml @@ -5,6 +5,9 @@ xmlns:plone="http://namespaces.plone.org/plone" i18n_domain="imio.fpaudit"> + + + diff --git a/src/imio/fpaudit/testing.py b/src/imio/fpaudit/testing.py new file mode 100644 index 0000000..9882341 --- /dev/null +++ b/src/imio/fpaudit/testing.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +from plone.app.testing import applyProfile +from plone.app.testing import FunctionalTesting +from plone.app.testing import IntegrationTesting +from plone.app.testing import PLONE_FIXTURE +from plone.app.testing import PloneSandboxLayer +from plone.app.testing import setRoles +from plone.app.testing import TEST_USER_ID + +import imio.fpaudit + + +class ImioFPAuditLayer(PloneSandboxLayer): + + defaultBases = (PLONE_FIXTURE,) + + def setUpZope(self, app, configurationContext): + # Load any other ZCML that is required for your tests. + # The z3c.autoinclude feature is disabled in the Plone fixture base layer. + self.loadZCML(package=imio.fpaudit) + + def setUpPloneSite(self, portal): + applyProfile(portal, "imio.fpaudit:default") + setRoles(portal, TEST_USER_ID, ["Manager"]) + + +IMIO_FPAUDIT_FIXTURE = ImioFPAuditLayer() + + +IMIO_FPAUDIT_INTEGRATION_TESTING = IntegrationTesting( + bases=(IMIO_FPAUDIT_FIXTURE,), name="ImioFPAuditLayer:IntegrationTesting" +) + + +IMIO_FPAUDIT_FUNCTIONAL_TESTING = FunctionalTesting( + bases=(IMIO_FPAUDIT_FIXTURE,), name="ImioFPAuditLayer:FunctionalTesting" +) diff --git a/src/imio/fpaudit/tests/__init__.py b/src/imio/fpaudit/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/imio/fpaudit/tests/test_browser_settings.py b/src/imio/fpaudit/tests/test_browser_settings.py new file mode 100644 index 0000000..5904032 --- /dev/null +++ b/src/imio/fpaudit/tests/test_browser_settings.py @@ -0,0 +1,34 @@ +from imio.fpaudit.browser.settings import IFPAuditSettings +from imio.fpaudit.testing import IMIO_FPAUDIT_INTEGRATION_TESTING +from z3c.form import validator +from zope.interface import Invalid + +import unittest + + +class TestSettings(unittest.TestCase): + + layer = IMIO_FPAUDIT_INTEGRATION_TESTING + + def setUp(self): + self.portal = self.layer["portal"] + + def test_validate_settings(self): + """Check invariant""" + invariants = validator.InvariantsValidator(None, None, None, IFPAuditSettings, None) + # test id uniqueness + data = { + "log_entries": [ + {"log_id": u"a", "audit_log": u"a.log", "log_format": u"%(asctime)s - %(message)s"}, + {"log_id": u"b", "audit_log": u"b.log", "log_format": u"%(asctime)s - %(message)s"}, + ] + } + self.assertFalse(invariants.validate(data)) + data = { + "log_entries": [ + {"log_id": u"a", "audit_log": u"a.log", "log_format": u"%(asctime)s - %(message)s"}, + {"log_id": u"a", "audit_log": u"b.log", "log_format": u"%(asctime)s - %(message)s"}, + ] + } + errors = invariants.validate(data) + self.assertTrue(isinstance(errors[0], Invalid))