From b7c0321e4d0dcd5dfc9877067ab6f5a5f9772b39 Mon Sep 17 00:00:00 2001 From: Marcos Pereira Date: Tue, 15 Aug 2017 10:04:37 -0300 Subject: [PATCH] Added tests. --- .travis.yml | 8 ++ keycloak/connection.py | 26 +++-- keycloak/exceptions.py | 1 + keycloak/tests/test_connection.py | 153 ++++++++++++++++++++++++++++++ keycloak/urls_patterns.py | 16 ++++ requirements.txt | 3 +- 6 files changed, 200 insertions(+), 7 deletions(-) create mode 100644 .travis.yml create mode 100644 keycloak/tests/test_connection.py diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..1c3b4b6 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,8 @@ +language: python +python: + - "3.5" + - "pypy" +install: + - pip install -r requirements.txt +script: + python -m unittest discover diff --git a/keycloak/connection.py b/keycloak/connection.py index 06aa6ef..322b3dc 100644 --- a/keycloak/connection.py +++ b/keycloak/connection.py @@ -1,10 +1,24 @@ -""" - -""" - -import requests -from urllib.parse import urljoin, urlencode +# -*- 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 urllib.parse import urljoin from .exceptions import * +import requests class ConnectionManager(object): diff --git a/keycloak/exceptions.py b/keycloak/exceptions.py index 9d645b1..5b5694c 100644 --- a/keycloak/exceptions.py +++ b/keycloak/exceptions.py @@ -17,6 +17,7 @@ import requests + class KeycloakError(Exception): def __init__(self, error_message="", response_code=None, response_body=None): diff --git a/keycloak/tests/test_connection.py b/keycloak/tests/test_connection.py new file mode 100644 index 0000000..3d56cb9 --- /dev/null +++ b/keycloak/tests/test_connection.py @@ -0,0 +1,153 @@ +# -*- 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 httmock import urlmatch, response, HTTMock, all_requests + +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.get_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.get_headers(), {}) + + def test_clean_param_headers(self): + self.__conn.add_param_headers("test", "value") + self.assertEqual(self.__conn.get_headers(), + {"test": "value"}) + self.__conn.clean_headers() + self.assertEqual(self.__conn.get_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.get_headers(), + {"test": "value"}) + + def test_set_headers(self): + self.__conn.set_headers({"test": "value"}) + self.assertTrue(self.__conn.get_headers(), {"test": "value"}) + self.assertFalse(self.__conn.exist_param_headers("test_no")) \ No newline at end of file diff --git a/keycloak/urls_patterns.py b/keycloak/urls_patterns.py index 6d58d3a..d43b006 100644 --- a/keycloak/urls_patterns.py +++ b/keycloak/urls_patterns.py @@ -1,3 +1,19 @@ +# -*- 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 . URL_WELL_KNOWN = "realms/{realm-name}/.well-known/openid-configuration" URL_AUTH = "realms/{realm-name}/protocol/openid-connect/auth" diff --git a/requirements.txt b/requirements.txt index fea3d4a..713d481 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -requests==2.18.3 \ No newline at end of file +requests==2.18.3 +httmock==1.2.5 \ No newline at end of file