From 7122490a4fb10b47ea01bbae1a0da80ee81c7a1c Mon Sep 17 00:00:00 2001 From: Richard Nemeth Date: Fri, 13 May 2022 10:05:49 +0200 Subject: [PATCH] style: added lint checks --- .circleci/config.yml | 37 ------- .github/workflows/lint.yaml | 29 ++++++ .gitignore | 4 +- .pre-commit-config.yaml | 16 +++ CONTRIBUTING.md | 15 +++ LICENSE | 2 +- README.md | 24 ++--- docs/Makefile | 2 +- docs/source/conf.py | 70 ++++++------- docs/source/index.rst | 1 - requirements.txt | 4 +- setup.cfg | 2 +- setup.py | 4 +- test_keycloak_init.sh | 35 +++++++ tests/conftest.py | 14 +++ tests/test_connection.py | 189 ----------------------------------- tests/test_keycloak_admin.py | 9 ++ tox.env | 4 + tox.ini | 32 ++++++ 19 files changed, 213 insertions(+), 280 deletions(-) delete mode 100644 .circleci/config.yml create mode 100644 .github/workflows/lint.yaml create mode 100644 .pre-commit-config.yaml create mode 100644 CONTRIBUTING.md create mode 100755 test_keycloak_init.sh create mode 100644 tests/conftest.py delete mode 100644 tests/test_connection.py create mode 100644 tests/test_keycloak_admin.py create mode 100644 tox.env create mode 100644 tox.ini diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index e2ab2c3..0000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,37 +0,0 @@ -version: 2 -jobs: - build: - docker: - - image: circleci/python:3.6.1 - - working_directory: ~/repo - - steps: - - checkout - - restore_cache: - keys: - - v1-dependencies-{{ checksum "requirements.txt" }} - # fallback to using the latest cache if no exact match is found - - v1-dependencies- - - - run: - name: install dependencies - command: | - python3 -m venv venv - . venv/bin/activate - pip install -r requirements.txt - - - save_cache: - paths: - - ./venv - key: v1-dependencies-{{ checksum "requirements.txt" }} - - - run: - name: run tests - command: | - . venv/bin/activate - python3 -m unittest discover - - - store_artifacts: - path: test-reports - destination: test-reports \ No newline at end of file diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml new file mode 100644 index 0000000..4747077 --- /dev/null +++ b/.github/workflows/lint.yaml @@ -0,0 +1,29 @@ +name: Python package linting + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + python-version: ["3.7", "3.8", "3.9", "3.10"] + + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install tox + - name: Check linting, formatting + run: | + tox -e check diff --git a/.gitignore b/.gitignore index 4c8d46d..f02f888 100644 --- a/.gitignore +++ b/.gitignore @@ -104,4 +104,6 @@ ENV/ main.py main2.py s3air-authz-config.json -.vscode \ No newline at end of file +.vscode +.docker +.pytest_cache diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..806a12c --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,16 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.2.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files + - repo: https://github.com/compilerla/conventional-pre-commit + rev: v1.2.0 + hooks: + - id: conventional-pre-commit + stages: [ commit-msg ] + args: [ ] # optional: list of Conventional Commits types to allow diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..4ddc6cf --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,15 @@ +# Contributing + +Commits to this project must adhere to the [Conventional Commits +specification](https://www.conventionalcommits.org/en/v1.0.0/) that will allow +us to automate version bumps and changelog entry creation. + +After cloning this repository, you must install the pre-commit hook for +conventional commits: + +```sh +python3 -m venv .venv +source .venv/bin/activate +python3 -m pip install pre-commit +pre-commit install --install-hooks -t pre-commit -t pre-push -t commit-msg +``` diff --git a/LICENSE b/LICENSE index f193f7d..781617c 100644 --- a/LICENSE +++ b/LICENSE @@ -17,4 +17,4 @@ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index 68a2dc5..e59333d 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ The documentation for python-keycloak is available on [readthedocs](http://pytho * [Agriness Team](http://www.agriness.com/pt/) * [Marcos Pereira](marcospereira.mpj@gmail.com) -* [Martin Devlin](https://bitbucket.org/devlinmpearson/) +* [Martin Devlin](https://bitbucket.org/devlinmpearson/) * [Shon T. Urbas](https://bitbucket.org/surbas/) * [Markus Spanier](https://bitbucket.org/spanierm/) * [Remco Kranenburg](https://bitbucket.org/Remco47/) @@ -119,13 +119,13 @@ keycloak_admin = KeycloakAdmin(server_url="http://localhost:8080/auth/", user_realm_name="only_if_other_realm_than_master", client_secret_key="client-secret", verify=True) - -# Add user + +# Add user new_user = keycloak_admin.create_user({"email": "example@example.com", "username": "example@example.com", "enabled": True, "firstName": "Example", - "lastName": "Example"}) + "lastName": "Example"}) # Add user and raise exception if username already exists # exist_ok currently defaults to True for backwards compatibility reasons @@ -135,8 +135,8 @@ new_user = keycloak_admin.create_user({"email": "example@example.com", "firstName": "Example", "lastName": "Example"}, exist_ok=False) - -# Add user and set password + +# Add user and set password new_user = keycloak_admin.create_user({"email": "example@example.com", "username": "example@example.com", "enabled": True, @@ -144,7 +144,7 @@ new_user = keycloak_admin.create_user({"email": "example@example.com", "lastName": "Example", "credentials": [{"value": "secret","type": "password",}]}) -# Add user and specify a locale +# Add user and specify a locale new_user = keycloak_admin.create_user({"email": "example@example.fr", "username": "example@example.fr", "enabled": True, @@ -152,7 +152,7 @@ new_user = keycloak_admin.create_user({"email": "example@example.fr", "lastName": "Example", "attributes": { "locale": ["fr"] - }) + }) # User counter count_users = keycloak_admin.users_count() @@ -167,7 +167,7 @@ user_id_keycloak = keycloak_admin.get_user_id("example@example.com") user = keycloak_admin.get_user("user-id-keycloak") # Update User -response = keycloak_admin.update_user(user_id="user-id-keycloak", +response = keycloak_admin.update_user(user_id="user-id-keycloak", payload={'firstName': 'Example Update'}) # Update User Password @@ -181,7 +181,7 @@ credential = keycloak_admin.get_credential(user_id='user_id', credential_id='cre # Delete User Credential response = keycloak_admin.delete_credential(user_id='user_id', credential_id='credential_id') - + # Delete User response = keycloak_admin.delete_user(user_id="user-id-keycloak") @@ -189,7 +189,7 @@ response = keycloak_admin.delete_user(user_id="user-id-keycloak") consents = keycloak_admin.consents_user(user_id="user-id-keycloak") # Send User Action -response = keycloak_admin.send_update_account(user_id="user-id-keycloak", +response = keycloak_admin.send_update_account(user_id="user-id-keycloak", payload=json.dumps(['UPDATE_PASSWORD'])) # Send Verify Email @@ -260,7 +260,7 @@ group = keycloak_admin.create_group({"name": "Example Group"}) # Get all groups groups = keycloak_admin.get_groups() -# Get group +# Get group group = keycloak_admin.get_group(group_id='group_id') # Get group by name diff --git a/docs/Makefile b/docs/Makefile index 28027de..c86fc18 100644 --- a/docs/Makefile +++ b/docs/Makefile @@ -17,4 +17,4 @@ help: # Catch-all target: route all unknown targets to Sphinx using the new # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). %: Makefile - @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) \ No newline at end of file + @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/docs/source/conf.py b/docs/source/conf.py index 9dc7661..e86bf36 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -32,37 +32,37 @@ import sphinx_rtd_theme # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ - 'sphinx.ext.autodoc', - 'sphinx.ext.intersphinx', - 'sphinx.ext.todo', - 'sphinx.ext.viewcode', + "sphinx.ext.autodoc", + "sphinx.ext.intersphinx", + "sphinx.ext.todo", + "sphinx.ext.viewcode", ] # Add any paths that contain templates here, relative to this directory. -templates_path = ['_templates'] +templates_path = ["_templates"] # The suffix(es) of source filenames. # You can specify multiple suffix as a list of string: # # source_suffix = ['.rst', '.md'] -source_suffix = '.rst' +source_suffix = ".rst" # The master toctree document. -master_doc = 'index' +master_doc = "index" # General information about the project. -project = 'python-keycloak' -copyright = '2017, Marcos Pereira' -author = 'Marcos Pereira' +project = "python-keycloak" +copyright = "2017, Marcos Pereira" +author = "Marcos Pereira" # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. -version = '0.27.0' +version = "0.27.0" # The full version, including alpha/beta/rc tags. -release = '0.27.0' +release = "0.27.0" # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. @@ -74,13 +74,13 @@ language = None # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. # This patterns also effect to html_static_path and html_extra_path -exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] +exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"] add_function_parentheses = False add_module_names = True # The name of the Pygments (syntax highlighting) style to use. -pygments_style = 'sphinx' +pygments_style = "sphinx" # If true, `todo` and `todoList` produce output, else they produce nothing. todo_include_todos = True @@ -91,7 +91,7 @@ todo_include_todos = True # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. # -html_theme = 'sphinx_rtd_theme' +html_theme = "sphinx_rtd_theme" html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] # Theme options are theme-specific and customize the look and feel of a theme @@ -103,7 +103,7 @@ html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +html_static_path = ["_static"] html_use_smartypants = False @@ -116,7 +116,7 @@ html_show_copyright = True # # This is required for the alabaster theme # refs: http://alabaster.readthedocs.io/en/latest/installation.html#sidebars -#html_sidebars = { +# html_sidebars = { # '**': [ # 'about.html', # 'navigation.html', @@ -124,13 +124,13 @@ html_show_copyright = True # 'searchbox.html', # 'donate.html', # ] -#} +# } # -- Options for HTMLHelp output ------------------------------------------ # Output file base name for HTML help builder. -htmlhelp_basename = 'python-keycloakdoc' +htmlhelp_basename = "python-keycloakdoc" # -- Options for LaTeX output --------------------------------------------- @@ -139,15 +139,12 @@ latex_elements = { # The paper size ('letterpaper' or 'a4paper'). # # 'papersize': 'letterpaper', - # The font size ('10pt', '11pt' or '12pt'). # # 'pointsize': '10pt', - # Additional stuff for the LaTeX preamble. # # 'preamble': '', - # Latex figure (float) alignment # # 'figure_align': 'htbp', @@ -157,8 +154,13 @@ latex_elements = { # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ - (master_doc, 'python-keycloak.tex', 'python-keycloak Documentation', - 'Marcos Pereira', 'manual'), + ( + master_doc, + "python-keycloak.tex", + "python-keycloak Documentation", + "Marcos Pereira", + "manual", + ), ] @@ -166,10 +168,7 @@ latex_documents = [ # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). -man_pages = [ - (master_doc, 'python-keycloak', 'python-keycloak Documentation', - [author], 1) -] +man_pages = [(master_doc, "python-keycloak", "python-keycloak Documentation", [author], 1)] # -- Options for Texinfo output ------------------------------------------- @@ -178,10 +177,13 @@ man_pages = [ # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ - (master_doc, 'python-keycloak', 'python-keycloak Documentation', - author, 'python-keycloak', 'One line description of project.', - 'Miscellaneous'), + ( + master_doc, + "python-keycloak", + "python-keycloak Documentation", + author, + "python-keycloak", + "One line description of project.", + "Miscellaneous", + ), ] - - - diff --git a/docs/source/index.rst b/docs/source/index.rst index 6675352..1521772 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -304,4 +304,3 @@ Main methods:: # Delete the key keycloak_admin.delete_component(component['id']) - diff --git a/requirements.txt b/requirements.txt index a353c7f..81b69d5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,6 @@ python-jose>=1.4.0 twine==1.13.0 jose~=1.0.0 setuptools~=54.2.0 -urllib3>=1.26.5 \ No newline at end of file +urllib3>=1.26.5 +pytest>=7.1.2 +pytest-cov>=3.0.0 diff --git a/setup.cfg b/setup.cfg index d10244e..de85eb1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -9,4 +9,4 @@ line_length = 99 profile = "black" [tool.flake8] -max-line-length = 99 \ No newline at end of file +max-line-length = 99 diff --git a/setup.py b/setup.py index c228051..8596ab2 100644 --- a/setup.py +++ b/setup.py @@ -16,8 +16,8 @@ setup( description="python-keycloak is a Python package providing access to the Keycloak API.", long_description=long_description, long_description_content_type="text/markdown", - packages=["keycloak", "keycloak.authorization", "keycloak.tests"], - install_requires=["requests>=2.20.0", "python-jose>=1.4.0", "urllib>=1.26.0"], + packages=["keycloak", "keycloak.authorization"], + install_requires=["requests>=2.20.0", "python-jose>=1.4.0", "urllib3>=1.26.0"], tests_require=["httmock>=1.2.5"], classifiers=[ "Programming Language :: Python :: 3", diff --git a/test_keycloak_init.sh b/test_keycloak_init.sh new file mode 100755 index 0000000..84abae0 --- /dev/null +++ b/test_keycloak_init.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +CMD_ARGS=$1 +KEYCLOAK_DOCKER_IMAGE="quay.io/keycloak/keycloak:latest" + +echo "${CMD_ARGS}" + +function keycloak_stop() { + docker stop unittest_keycloak &> /dev/null + docker rm unittest_keycloak &> /dev/null +} + +function keycloak_start() { + echo "Starting keycloak docker container" + docker run -d --name unittest_keycloak -e KEYCLOAK_ADMIN="${KEYCLOAK_ADMIN}" -e KEYCLOAK_ADMIN_PASSWORD="${KEYCLOAK_ADMIN_PASSWORD}" -p "${KEYCLOAK_PORT}:8080" "${KEYCLOAK_DOCKER_IMAGE}" start-dev + SECONDS=0 + until curl localhost:8080; do + sleep 5; + if [ ${SECONDS} -gt 180 ]; then + echo "Timeout exceeded"; + exit 1; + fi + done +} + +# Ensuring that postgres is stopped in case of CTRL-C +trap keycloak_stop err exit + +keycloak_stop # In case it did not shut down correctly last time. +keycloak_start + +eval ${CMD_ARGS} +RETURN_VALUE=$? + +exit ${RETURN_VALUE} diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..305c8b5 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,14 @@ +import os + +import pytest + + +@pytest.fixture +def env(): + class KeycloakTestEnv(object): + KEYCLOAK_HOST = os.environ["KEYCLOAK_HOST"] + KEYCLOAK_PORT = os.environ["KEYCLOAK_PORT"] + KEYCLOAK_ADMIN = os.environ["KEYCLOAK_ADMIN"] + KEYCLOAK_ADMIN_PASSWORD = os.environ["KEYCLOAK_ADMIN_PASSWORD"] + + return KeycloakTestEnv() diff --git a/tests/test_connection.py b/tests/test_connection.py deleted file mode 100644 index eec86a3..0000000 --- a/tests/test_connection.py +++ /dev/null @@ -1,189 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright (C) 2017 Marcos Pereira -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with this program. If not, see . -from unittest import mock - -from httmock import urlmatch, response, HTTMock, all_requests - -from keycloak import KeycloakAdmin, KeycloakOpenID -from keycloak.connection import ConnectionManager - -try: - import unittest -except ImportError: - import unittest2 as unittest - - -class TestConnection(unittest.TestCase): - def setUp(self): - self._conn = ConnectionManager(base_url="http://localhost/", headers={}, timeout=60) - - @all_requests - def response_content_success(self, url, request): - headers = {"content-type": "application/json"} - content = b"response_ok" - return response(200, content, headers, None, 5, request) - - def test_raw_get(self): - with HTTMock(self.response_content_success): - resp = self._conn.raw_get("/known_path") - self.assertEqual(resp.content, b"response_ok") - self.assertEqual(resp.status_code, 200) - - def test_raw_post(self): - @urlmatch(path="/known_path", method="post") - def response_post_success(url, request): - headers = {"content-type": "application/json"} - content = "response".encode("utf-8") - return response(201, content, headers, None, 5, request) - - with HTTMock(response_post_success): - resp = self._conn.raw_post("/known_path", {"field": "value"}) - self.assertEqual(resp.content, b"response") - self.assertEqual(resp.status_code, 201) - - def test_raw_put(self): - @urlmatch(netloc="localhost", path="/known_path", method="put") - def response_put_success(url, request): - headers = {"content-type": "application/json"} - content = "response".encode("utf-8") - return response(200, content, headers, None, 5, request) - - with HTTMock(response_put_success): - resp = self._conn.raw_put("/known_path", {"field": "value"}) - self.assertEqual(resp.content, b"response") - self.assertEqual(resp.status_code, 200) - - def test_raw_get_fail(self): - @urlmatch(netloc="localhost", path="/known_path", method="get") - def response_get_fail(url, request): - headers = {"content-type": "application/json"} - content = "404 page not found".encode("utf-8") - return response(404, content, headers, None, 5, request) - - with HTTMock(response_get_fail): - resp = self._conn.raw_get("/known_path") - - self.assertEqual(resp.content, b"404 page not found") - self.assertEqual(resp.status_code, 404) - - def test_raw_post_fail(self): - @urlmatch(netloc="localhost", path="/known_path", method="post") - def response_post_fail(url, request): - headers = {"content-type": "application/json"} - content = str(["Start can't be blank"]).encode("utf-8") - return response(404, content, headers, None, 5, request) - - with HTTMock(response_post_fail): - resp = self._conn.raw_post("/known_path", {"field": "value"}) - self.assertEqual(resp.content, str(["Start can't be blank"]).encode("utf-8")) - self.assertEqual(resp.status_code, 404) - - def test_raw_put_fail(self): - @urlmatch(netloc="localhost", path="/known_path", method="put") - def response_put_fail(url, request): - headers = {"content-type": "application/json"} - content = str(["Start can't be blank"]).encode("utf-8") - return response(404, content, headers, None, 5, request) - - with HTTMock(response_put_fail): - resp = self._conn.raw_put("/known_path", {"field": "value"}) - self.assertEqual(resp.content, str(["Start can't be blank"]).encode("utf-8")) - self.assertEqual(resp.status_code, 404) - - def test_add_param_headers(self): - self._conn.add_param_headers("test", "value") - self.assertEqual(self._conn.headers, {"test": "value"}) - - def test_del_param_headers(self): - self._conn.add_param_headers("test", "value") - self._conn.del_param_headers("test") - self.assertEqual(self._conn.headers, {}) - - def test_clean_param_headers(self): - self._conn.add_param_headers("test", "value") - self.assertEqual(self._conn.headers, {"test": "value"}) - self._conn.clean_headers() - self.assertEqual(self._conn.headers, {}) - - def test_exist_param_headers(self): - self._conn.add_param_headers("test", "value") - self.assertTrue(self._conn.exist_param_headers("test")) - self.assertFalse(self._conn.exist_param_headers("test_no")) - - def test_get_param_headers(self): - self._conn.add_param_headers("test", "value") - self.assertTrue(self._conn.exist_param_headers("test")) - self.assertFalse(self._conn.exist_param_headers("test_no")) - - def test_get_headers(self): - self._conn.add_param_headers("test", "value") - self.assertEqual(self._conn.headers, {"test": "value"}) - - def test_KeycloakAdmin_custom_header(self): - class FakeToken: - @staticmethod - def get(string_val): - return "faketoken" - - fake_token = FakeToken() - - with mock.patch.object( - KeycloakOpenID, "__init__", return_value=None - ) as mock_keycloak_open_id: - with mock.patch( - "keycloak.keycloak_openid.KeycloakOpenID.token", return_value=fake_token - ): - with mock.patch( - "keycloak.connection.ConnectionManager.__init__", return_value=None - ) as mock_connection_manager: - with mock.patch( - "keycloak.connection.ConnectionManager.__del__", return_value=None - ) as mock_connection_manager_delete: - server_url = "https://localhost/auth/" - username = "admin" - password = "secret" - realm_name = "master" - - headers = {"Custom": "test-custom-header"} - KeycloakAdmin( - server_url=server_url, - username=username, - password=password, - realm_name=realm_name, - verify=False, - custom_headers=headers, - ) - - mock_keycloak_open_id.assert_called_with( - server_url=server_url, - realm_name=realm_name, - client_id="admin-cli", - client_secret_key=None, - verify=False, - custom_headers=headers, - ) - - expected_header = { - "Authorization": "Bearer faketoken", - "Content-Type": "application/json", - "Custom": "test-custom-header", - } - - mock_connection_manager.assert_called_with( - base_url=server_url, headers=expected_header, timeout=60, verify=False - ) - mock_connection_manager_delete.assert_called_once_with() diff --git a/tests/test_keycloak_admin.py b/tests/test_keycloak_admin.py new file mode 100644 index 0000000..e138084 --- /dev/null +++ b/tests/test_keycloak_admin.py @@ -0,0 +1,9 @@ +from keycloak import KeycloakAdmin + + +def test_keycloak_admin_init(env): + KeycloakAdmin( + server_url=f"http://{env.KEYCLOAK_HOST}:{env.KEYCLOAK_PORT}", + username=env.KEYCLOAK_ADMIN, + password=env.KEYCLOAK_ADMIN_PASSWORD, + ) diff --git a/tox.env b/tox.env new file mode 100644 index 0000000..49cea83 --- /dev/null +++ b/tox.env @@ -0,0 +1,4 @@ +KEYCLOAK_ADMIN=admin +KEYCLOAK_ADMIN_PASSWORD=admin +KEYCLOAK_HOST={env:KEYCLOAK_HOST:localhost} +KEYCLOAK_PORT=8080 diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..93a2639 --- /dev/null +++ b/tox.ini @@ -0,0 +1,32 @@ +[tox] +envlist = check, docs, tests + +[testenv] +install_command = pip install {opts} {packages} + +[testenv:check] +deps = + black + isort + flake8 +commands = + black --check --diff keycloak tests docs + isort -c --df keycloak tests docs + flake8 keycloak tests docs + +[testenv:tests] +deps = + -rrequirements.txt +setenv = file|tox.env +commands = + ./test_keycloak_init.sh "pytest -v --cov=keycloak --cov-report term-missing {posargs}" + +[flake8] +max-line-length = 99 + +[black] +line-length = 99 + +[isort] +line_length = 99 +profile = "black"