Skip to content

Commit

Permalink
pytests
Browse files Browse the repository at this point in the history
  • Loading branch information
mamico committed Nov 26, 2023
1 parent 21ece5f commit d7b6b1e
Show file tree
Hide file tree
Showing 16 changed files with 423 additions and 176 deletions.
24 changes: 13 additions & 11 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
graft src/pas
graft docs
global-exclude *.pyc
include *.md
include *.txt
include *.yaml
include .coveragerc
include Makefile
recursive-include tests *.py
recursive-include tests *.gitkeep
recursive-include tests *.json
recursive-include tests *.yml
# --
include *.yml
include base.cfg
include bobtemplate.cfg
include buildout.cfg
include test_plone52.cfg
include test_plone60.cfg
include *.md
include *.txt
include *.yml
recursive-include src *.gitkeep
recursive-include src *.po
recursive-include src *.pot
recursive-include src *.robot
recursive-include src *.rst
recursive-include src *.sh
recursive-include src *.xml
recursive-include src *.zcml
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ start_string = "<!-- towncrier release notes start -->\n"
title_format = "## {version} ({project_date})"
template = "news/.changelog_template.jinja"
underlines = ["", "", ""]
issue_format = "[#{issue}](https://github.com/collective/pas.plugins.oidc/issues/{issue})"
issue_format = "[#{issue}](https://github.com/collective/pas.plugins.passwordstrength/issues/{issue})"

[[tool.towncrier.type]]
directory = "breaking"
Expand Down Expand Up @@ -162,7 +162,7 @@ omit = ["*/locales/*"]

[tool.bandit]
targets = "src"
exclude_dirs = ["tests", "src/pas/plugins/oidc/locales"]
exclude_dirs = ["tests", "src/pas/plugins/passwordstrength/locales"]

##
# Add extra configuration options in .meta.toml:
Expand Down
33 changes: 11 additions & 22 deletions src/pas/plugins/passwordstrength/testing.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,42 @@
from plone.app.robotframework.testing import REMOTE_LIBRARY_BUNDLE_FIXTURE
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.testing import z2
from plone.testing.zope import WSGI_SERVER_FIXTURE

import pas.plugins.passwordstrength


class PasPluginsPasswordstrengthLayer(PloneSandboxLayer):
class TestLayer(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.
import plone.app.dexterity

self.loadZCML(package=plone.app.dexterity)
import plone.restapi

self.loadZCML(package=plone.restapi)
self.loadZCML(package=pas.plugins.passwordstrength)

def setUpPloneSite(self, portal):
applyProfile(portal, "plone.restapi:default")
applyProfile(portal, "pas.plugins.passwordstrength:default")


PAS_PLUGINS_PASSWORDSTRENGTH_FIXTURE = PasPluginsPasswordstrengthLayer()
FIXTURE = TestLayer()


PAS_PLUGINS_PASSWORDSTRENGTH_INTEGRATION_TESTING = IntegrationTesting(
bases=(PAS_PLUGINS_PASSWORDSTRENGTH_FIXTURE,),
INTEGRATION_TESTING = IntegrationTesting(
bases=(FIXTURE,),
name="PasPluginsPasswordstrengthLayer:IntegrationTesting",
)


PAS_PLUGINS_PASSWORDSTRENGTH_FUNCTIONAL_TESTING = FunctionalTesting(
bases=(PAS_PLUGINS_PASSWORDSTRENGTH_FIXTURE,),
FUNCTIONAL_TESTING = FunctionalTesting(
bases=(FIXTURE,),
name="PasPluginsPasswordstrengthLayer:FunctionalTesting",
)


PAS_PLUGINS_PASSWORDSTRENGTH_ACCEPTANCE_TESTING = FunctionalTesting(
bases=(
PAS_PLUGINS_PASSWORDSTRENGTH_FIXTURE,
REMOTE_LIBRARY_BUNDLE_FIXTURE,
z2.ZSERVER_FIXTURE,
),
name="PasPluginsPasswordstrengthLayer:AcceptanceTesting",
RESTAPI_TESTING = FunctionalTesting(
bases=(FIXTURE, WSGI_SERVER_FIXTURE),
name="PasPluginsPasswordstrengthLayer:RestAPITesting",
)
Empty file.
66 changes: 0 additions & 66 deletions src/pas/plugins/passwordstrength/tests/robot/test_example.robot

This file was deleted.

74 changes: 0 additions & 74 deletions src/pas/plugins/passwordstrength/tests/test_setup.py

This file was deleted.

70 changes: 70 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from pas.plugins.passwordstrength.testing import FUNCTIONAL_TESTING
from pas.plugins.passwordstrength.testing import INTEGRATION_TESTING
from pas.plugins.passwordstrength.testing import RESTAPI_TESTING
from pathlib import Path
from pytest_plone import fixtures_factory
from requests.exceptions import ConnectionError

import pytest
import requests


pytest_plugins = ["pytest_plone"]


globals().update(
fixtures_factory(
(
(INTEGRATION_TESTING, "integration"),
(FUNCTIONAL_TESTING, "functional"),
(RESTAPI_TESTING, "restapi"),
)
)
)


@pytest.fixture(scope="session")
def docker_compose_file(pytestconfig):
"""Fixture pointing to the docker-compose file to be used."""
return Path(str(pytestconfig.rootdir)).resolve() / "tests" / "docker-compose.yml"


def is_responsive(url: str) -> bool:
try:
response = requests.get(url)
if response.status_code == 200:
return True
except ConnectionError:
return False


@pytest.fixture(scope="session")
def keycloak_service(docker_ip, docker_services):
"""Ensure that keycloak service is up and responsive."""
# `port_for` takes a container port and returns the corresponding host port
port = docker_services.port_for("keycloak", 8080)
url = f"http://{docker_ip}:{port}"
docker_services.wait_until_responsive(
timeout=50.0, pause=0.1, check=lambda: is_responsive(url)
)
return url


@pytest.fixture(scope="session")
def keycloak(keycloak_service):
return {
"issuer": f"{keycloak_service}/realms/plone-test",
"client_id": "plone",
"client_secret": "12345678", # nosec B105
"scope": ("openid", "profile", "email"),
}


@pytest.fixture
def wait_for():
def func(thread):
if not thread:
return
thread.join()

return func
41 changes: 41 additions & 0 deletions tests/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
version: "3.9"

services:
keycloak:
build:
context: keycloak
args:
KEYCLOAK_VERSION: 22.0.0
command: ['start-dev', '--import-realm']
depends_on:
- db
environment:
JAVA_OPTS_APPEND: -Dkeycloak.profile.feature.upload_scripts=enabled
KC_DB: postgres
KC_DB_PASSWORD: postgres
KC_DB_URL: jdbc:postgresql://db/keycloak
KC_DB_USERNAME: postgres
KC_HEALTH_ENABLED: false
KC_HTTP_ENABLED: true
KC_METRICS_ENABLED: false
KC_HOSTNAME_URL: http://127.0.0.1:8180/
KC_PROXY: reencrypt
KEYCLOAK_ADMIN: admin
KEYCLOAK_ADMIN_PASSWORD: admin
volumes:
- ./keycloak/import:/opt/keycloak/data/import
ports:
- 8180:8080

db:
image: postgres:14.9
healthcheck:
test: [ "CMD", "pg_isready", "-q", "-d", "postgres", "-U", "root" ]
timeout: 45s
interval: 5s
retries: 10
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: keycloak
POSTGRES_HOST: postgres
Loading

0 comments on commit d7b6b1e

Please sign in to comment.