summaryrefslogtreecommitdiff
path: root/tests/pecantest
diff options
context:
space:
mode:
authorJim Rollenhagen <jim@jimrollenhagen.com>2019-09-26 09:43:27 -0400
committerJim Rollenhagen <jim@jimrollenhagen.com>2019-09-26 09:43:27 -0400
commite9c6edfe510f4ed407f8d2d84b4b931a382b48b3 (patch)
tree94bbd6a34bcf09e99f7ae1be88b19960192d6adb /tests/pecantest
parent1d73d6e50411ebc45fb96a6ed3c63ca91a500323 (diff)
downloadwsme-master.tar.gz
Retire github mirror, repo moved to opendevHEADmaster
Diffstat (limited to 'tests/pecantest')
-rw-r--r--tests/pecantest/setup.cfg6
-rw-r--r--tests/pecantest/setup.py22
-rw-r--r--tests/pecantest/test/__init__.py0
-rw-r--r--tests/pecantest/test/app.py15
-rw-r--r--tests/pecantest/test/controllers/__init__.py0
-rw-r--r--tests/pecantest/test/controllers/root.py21
-rw-r--r--tests/pecantest/test/controllers/ws.py150
-rw-r--r--tests/pecantest/test/model/__init__.py2
-rw-r--r--tests/pecantest/test/tests/__init__.py22
-rw-r--r--tests/pecantest/test/tests/config.py24
-rw-r--r--tests/pecantest/test/tests/test_ws.py247
11 files changed, 0 insertions, 509 deletions
diff --git a/tests/pecantest/setup.cfg b/tests/pecantest/setup.cfg
deleted file mode 100644
index 00ca220..0000000
--- a/tests/pecantest/setup.cfg
+++ /dev/null
@@ -1,6 +0,0 @@
-[nosetests]
-match=^test
-where=test
-nocapture=1
-cover-package=test
-cover-erase=1
diff --git a/tests/pecantest/setup.py b/tests/pecantest/setup.py
deleted file mode 100644
index b901f62..0000000
--- a/tests/pecantest/setup.py
+++ /dev/null
@@ -1,22 +0,0 @@
-# -*- coding: utf-8 -*-
-try:
- from setuptools import setup, find_packages
-except ImportError:
- from ez_setup import use_setuptools
- use_setuptools()
- from setuptools import setup, find_packages
-
-setup(
- name = 'test',
- version = '0.1',
- description = '',
- author = '',
- author_email = '',
- install_requires = [
- "pecan",
- ],
- test_suite = 'test',
- zip_safe = False,
- include_package_data = True,
- packages = find_packages(exclude=['ez_setup'])
-)
diff --git a/tests/pecantest/test/__init__.py b/tests/pecantest/test/__init__.py
deleted file mode 100644
index e69de29..0000000
--- a/tests/pecantest/test/__init__.py
+++ /dev/null
diff --git a/tests/pecantest/test/app.py b/tests/pecantest/test/app.py
deleted file mode 100644
index 727c357..0000000
--- a/tests/pecantest/test/app.py
+++ /dev/null
@@ -1,15 +0,0 @@
-from pecan import make_app
-from test import model
-
-def setup_app(config):
-
- model.init_model()
-
- return make_app(
- config.app.root,
- static_root = config.app.static_root,
- template_path = config.app.template_path,
- logging = getattr(config, 'logging', {}),
- debug = getattr(config.app, 'debug', False),
- force_canonical = getattr(config.app, 'force_canonical', True)
- )
diff --git a/tests/pecantest/test/controllers/__init__.py b/tests/pecantest/test/controllers/__init__.py
deleted file mode 100644
index e69de29..0000000
--- a/tests/pecantest/test/controllers/__init__.py
+++ /dev/null
diff --git a/tests/pecantest/test/controllers/root.py b/tests/pecantest/test/controllers/root.py
deleted file mode 100644
index 6e87267..0000000
--- a/tests/pecantest/test/controllers/root.py
+++ /dev/null
@@ -1,21 +0,0 @@
-from pecan import expose
-from webob.exc import status_map
-from .ws import AuthorsController
-from wsmeext.pecan import wsexpose
-
-
-class RootController(object):
- authors = AuthorsController()
-
- @expose('error.html')
- def error(self, status):
- try:
- status = int(status)
- except ValueError: # pragma: no cover
- status = 500
- message = getattr(status_map.get(status), 'explanation', '')
- return dict(status=status, message=message)
-
- @wsexpose()
- def divide_by_zero(self):
- 1 / 0
diff --git a/tests/pecantest/test/controllers/ws.py b/tests/pecantest/test/controllers/ws.py
deleted file mode 100644
index b35a0c6..0000000
--- a/tests/pecantest/test/controllers/ws.py
+++ /dev/null
@@ -1,150 +0,0 @@
-# encoding=utf8
-from pecan.rest import RestController
-
-from wsme.types import Base, text, wsattr
-
-import wsme
-import wsmeext.pecan
-
-import six
-
-
-class Author(Base):
- id = int
- firstname = text
- books = wsattr(['Book'])
-
- @staticmethod
- def validate(author):
- if author.firstname == 'Robert':
- raise wsme.exc.ClientSideError("I don't like this author!")
- return author
-
-
-class Book(Base):
- id = int
- name = text
- author = wsattr('Author')
-
-
-class BookNotFound(Exception):
- message = "Book with ID={id} Not Found"
- code = 404
-
- def __init__(self, id):
- message = self.message.format(id=id)
- super(BookNotFound, self).__init__(message)
-
-
-class NonHttpException(Exception):
- message = "Internal Exception for Book ID={id}"
- code = 684
-
- def __init__(self, id):
- message = self.message.format(id=id)
- super(NonHttpException, self).__init__(message)
-
-
-class BooksController(RestController):
-
- @wsmeext.pecan.wsexpose(Book, int, int)
- def get(self, author_id, id):
- book = Book(
- name=u"Les Confessions d’un révolutionnaire pour servir à "
- u"l’histoire de la révolution de février",
- author=Author(lastname=u"Proudhon")
- )
- return book
-
- @wsmeext.pecan.wsexpose(Book, int, int, body=Book)
- def put(self, author_id, id, book=None):
- book.id = id
- book.author = Author(id=author_id)
- return book
-
-
-class Criterion(Base):
- op = text
- attrname = text
- value = text
-
-
-class AuthorsController(RestController):
-
- _custom_actions = {
- 'json_only': ['GET'],
- 'xml_only': ['GET']
- }
-
- books = BooksController()
-
- @wsmeext.pecan.wsexpose([Author], [six.text_type], [Criterion])
- def get_all(self, q=None, r=None):
- if q:
- return [
- Author(id=i, firstname=value)
- for i, value in enumerate(q)
- ]
- if r:
- return [
- Author(id=i, firstname=c.value)
- for i, c in enumerate(r)
- ]
- return [
- Author(id=1, firstname=u'FirstName')
- ]
-
- @wsmeext.pecan.wsexpose(Author, int)
- def get(self, id):
- if id == 999:
- raise wsme.exc.ClientSideError('Wrong ID')
-
- if id == 998:
- raise BookNotFound(id)
-
- if id == 997:
- raise NonHttpException(id)
-
- if id == 996:
- raise wsme.exc.ClientSideError('Disabled ID', status_code=403)
-
- if id == 911:
- return wsme.api.Response(Author(),
- status_code=401)
- if id == 912:
- return wsme.api.Response(None, status_code=204)
-
- if id == 913:
- return wsme.api.Response('foo', status_code=200, return_type=text)
-
- author = Author()
- author.id = id
- author.firstname = u"aname"
- author.books = [
- Book(
- name=u"Les Confessions d’un révolutionnaire pour servir à "
- u"l’histoire de la révolution de février",
- )
- ]
- return author
-
- @wsmeext.pecan.wsexpose(Author, body=Author, status_code=201)
- def post(self, author):
- author.id = 10
- return author
-
- @wsmeext.pecan.wsexpose(None, int)
- def delete(self, author_id):
- print("Deleting", author_id)
-
- @wsmeext.pecan.wsexpose(Book, int, body=Author)
- def put(self, author_id, author=None):
- return author
-
- @wsmeext.pecan.wsexpose([Author], rest_content_types=('json',))
- def json_only(self):
- return [Author(id=1, firstname=u"aname", books=[])]
-
- @wsmeext.pecan.wsexpose([Author], rest_content_types=('xml',))
- def xml_only(self):
- return [Author(id=1, firstname=u"aname", books=[])]
diff --git a/tests/pecantest/test/model/__init__.py b/tests/pecantest/test/model/__init__.py
deleted file mode 100644
index ab4be6a..0000000
--- a/tests/pecantest/test/model/__init__.py
+++ /dev/null
@@ -1,2 +0,0 @@
-def init_model():
- pass
diff --git a/tests/pecantest/test/tests/__init__.py b/tests/pecantest/test/tests/__init__.py
deleted file mode 100644
index 04fa0a6..0000000
--- a/tests/pecantest/test/tests/__init__.py
+++ /dev/null
@@ -1,22 +0,0 @@
-import os
-from unittest import TestCase
-from pecan import set_config
-from pecan import testing
-
-__all__ = ['FunctionalTest']
-
-
-class FunctionalTest(TestCase):
- """
- Used for functional tests where you need to test your
- literal application and its integration with the framework.
- """
-
- def setUp(self):
- self.app = testing.load_test_app(os.path.join(
- os.path.dirname(__file__),
- 'config.py'
- ))
-
- def tearDown(self):
- set_config({}, overwrite=True)
diff --git a/tests/pecantest/test/tests/config.py b/tests/pecantest/test/tests/config.py
deleted file mode 100644
index 3f35dcf..0000000
--- a/tests/pecantest/test/tests/config.py
+++ /dev/null
@@ -1,24 +0,0 @@
-# Server Specific Configurations
-server = {
- 'port' : '8080',
- 'host' : '0.0.0.0'
-}
-
-# Pecan Application Configurations
-app = {
- 'root' : 'test.controllers.root.RootController',
- 'modules' : ['test'],
- 'static_root' : '%(confdir)s/../../public',
- 'template_path' : '%(confdir)s/../templates',
- 'errors' : {
- '404' : '/error/404',
- '__force_dict__' : True
- }
-}
-
-# Custom Configurations must be in Python dictionary format::
-#
-# foo = {'bar':'baz'}
-#
-# All configurations are accessible at::
-# pecan.conf
diff --git a/tests/pecantest/test/tests/test_ws.py b/tests/pecantest/test/tests/test_ws.py
deleted file mode 100644
index ae06650..0000000
--- a/tests/pecantest/test/tests/test_ws.py
+++ /dev/null
@@ -1,247 +0,0 @@
-from six.moves import http_client
-from test.tests import FunctionalTest
-import json
-import pecan
-import six
-
-
-used_status_codes = [400, 401, 403, 404, 500]
-http_response_messages = {}
-for code in used_status_codes:
- http_response_messages[code] = '%s %s' % (code, http_client.responses[code])
-
-class TestWS(FunctionalTest):
-
- def test_get_all(self):
- self.app.get('/authors')
-
- def test_optional_array_param(self):
- r = self.app.get('/authors?q=a&q=b')
- l = json.loads(r.body.decode('utf-8'))
- assert len(l) == 2
- assert l[0]['firstname'] == 'a'
- assert l[1]['firstname'] == 'b'
-
- def test_optional_indexed_array_param(self):
- r = self.app.get('/authors?q[0]=a&q[1]=b')
- l = json.loads(r.body.decode('utf-8'))
- assert len(l) == 2
- assert l[0]['firstname'] == 'a'
- assert l[1]['firstname'] == 'b'
-
- def test_options_object_array_param(self):
- r = self.app.get('/authors?r.value=a&r.value=b')
- l = json.loads(r.body.decode('utf-8'))
- assert len(l) == 2
- assert l[0]['firstname'] == 'a'
- assert l[1]['firstname'] == 'b'
-
- def test_options_indexed_object_array_param(self):
- r = self.app.get('/authors?r[0].value=a&r[1].value=b')
- l = json.loads(r.body.decode('utf-8'))
- assert len(l) == 2
- assert l[0]['firstname'] == 'a'
- assert l[1]['firstname'] == 'b'
-
- def test_get_author(self):
- a = self.app.get(
- '/authors/1.json',
- )
- a = json.loads(a.body.decode('utf-8'))
-
- assert a['id'] == 1
- assert a['firstname'] == 'aname'
-
- a = self.app.get(
- '/authors/1.xml',
- )
- body = a.body.decode('utf-8')
- assert '<id>1</id>' in body
- assert '<firstname>aname</firstname>' in body
-
- def test_post_body_parameter_validation(self):
- res = self.app.post(
- '/authors', '{"firstname": "Robert"}',
- headers={"Content-Type": "application/json"},
- expect_errors=True
- )
- self.assertEqual(res.status_int, 400)
- a = json.loads(res.body.decode('utf-8'))
- self.assertEqual(a['faultcode'], 'Client')
- self.assertEqual(a['faultstring'], "I don't like this author!")
-
- def test_post_body_parameter(self):
- res = self.app.post(
- '/authors', '{"firstname": "test"}',
- headers={"Content-Type": "application/json"}
- )
- assert res.status_int == 201
- a = json.loads(res.body.decode('utf-8'))
- assert a['id'] == 10
- assert a['firstname'] == 'test'
-
- def test_put_parameter_validate(self):
- res = self.app.put(
- '/authors/foobar', '{"firstname": "test"}',
- headers={"Content-Type": "application/json"},
- expect_errors=True
- )
- self.assertEqual(res.status_int, 400)
- a = json.loads(res.body.decode('utf-8'))
- self.assertEqual(
- a['faultstring'],
- "Invalid input for field/attribute author_id. "
- "Value: 'foobar'. unable to convert to int. Error: invalid "
- "literal for int() with base 10: 'foobar'")
-
- def test_clientsideerror(self):
- expected_status_code = 400
- expected_status = http_response_messages[expected_status_code]
- res = self.app.get(
- '/authors/999.json',
- expect_errors=True
- )
- self.assertEqual(res.status, expected_status)
- a = json.loads(res.body.decode('utf-8'))
- assert a['faultcode'] == 'Client'
-
- res = self.app.get(
- '/authors/999.xml',
- expect_errors=True
- )
- self.assertEqual(res.status, expected_status)
- assert '<faultcode>Client</faultcode>' in res.body.decode('utf-8')
-
- def test_custom_clientside_error(self):
- expected_status_code = 404
- expected_status = http_response_messages[expected_status_code]
- res = self.app.get(
- '/authors/998.json',
- expect_errors=True
- )
- self.assertEqual(res.status, expected_status)
- a = json.loads(res.body.decode('utf-8'))
- assert a['faultcode'] == 'Client'
-
- res = self.app.get(
- '/authors/998.xml',
- expect_errors=True
- )
- self.assertEqual(res.status, expected_status)
- assert '<faultcode>Client</faultcode>' in res.body.decode('utf-8')
-
- def test_custom_non_http_clientside_error(self):
- expected_status_code = 500
- expected_status = http_response_messages[expected_status_code]
- res = self.app.get(
- '/authors/997.json',
- expect_errors=True
- )
- self.assertEqual(res.status, expected_status)
- a = json.loads(res.body.decode('utf-8'))
- assert a['faultcode'] == 'Server'
-
- res = self.app.get(
- '/authors/997.xml',
- expect_errors=True
- )
- self.assertEqual(res.status, expected_status)
- assert '<faultcode>Server</faultcode>' in res.body.decode('utf-8')
-
- def test_clientsideerror_status_code(self):
- expected_status_code = 403
- expected_status = http_response_messages[expected_status_code]
- res = self.app.get(
- '/authors/996.json',
- expect_errors=True
- )
- self.assertEqual(res.status, expected_status)
- a = json.loads(res.body.decode('utf-8'))
- assert a['faultcode'] == 'Client'
-
- res = self.app.get(
- '/authors/996.xml',
- expect_errors=True
- )
- self.assertEqual(res.status, expected_status)
- assert '<faultcode>Client</faultcode>' in res.body.decode('utf-8')
-
- def test_non_default_response(self):
- expected_status_code = 401
- expected_status = http_response_messages[expected_status_code]
- res = self.app.get(
- '/authors/911.json',
- expect_errors=True
- )
- self.assertEqual(res.status_int, expected_status_code)
- self.assertEqual(res.status, expected_status)
-
- def test_non_default_response_return_type(self):
- res = self.app.get(
- '/authors/913',
- )
- self.assertEqual(res.status_int, 200)
- self.assertEqual(res.body, b'"foo"')
- self.assertEqual(res.content_length, 5)
-
- def test_non_default_response_return_type_no_content(self):
- res = self.app.get(
- '/authors/912',
- )
- self.assertEqual(res.status_int, 204)
- self.assertEqual(res.body, b'')
- self.assertEqual(res.content_length, 0)
-
- def test_serversideerror(self):
- expected_status_code = 500
- expected_status = http_response_messages[expected_status_code]
- res = self.app.get('/divide_by_zero.json', expect_errors=True)
- self.assertEqual(res.status, expected_status)
- a = json.loads(res.body.decode('utf-8'))
- assert a['faultcode'] == 'Server'
- assert a['debuginfo'] is None
-
- def test_serversideerror_with_debug(self):
- expected_status_code = 500
- expected_status = http_response_messages[expected_status_code]
- pecan.set_config({'wsme': {'debug': True}})
- res = self.app.get('/divide_by_zero.json', expect_errors=True)
- self.assertEqual(res.status, expected_status)
- a = json.loads(res.body.decode('utf-8'))
- assert a['faultcode'] == 'Server'
- assert a['debuginfo'].startswith('Traceback (most recent call last):')
-
- def test_json_only(self):
- res = self.app.get('/authors/json_only.json')
- assert res.status_int == 200
- body = json.loads(res.body.decode('utf-8'))
- assert len(body) == 1
- assert body[0]['firstname'] == u"aname"
- assert body[0]['books'] == []
- assert body[0]['id'] == 1
- res = self.app.get('/authors/json_only.xml', expect_errors=True)
-
- def test_xml_only(self):
- res = self.app.get('/authors/xml_only.xml')
- assert res.status_int == 200
- assert '<id>1</id>' in res.body.decode('utf-8')
- assert '<firstname>aname</firstname>' in res.body.decode('utf-8')
- assert '<books />' in res.body.decode('utf-8')
- res = self.app.get('/authors/xml_only.json', expect_errors=True)
-
- def test_body_parameter(self):
- res = self.app.put(
- '/authors/1/books/2.json',
- '{"name": "Alice au pays des merveilles"}',
- headers={"Content-Type": "application/json"}
- )
- book = json.loads(res.body.decode('utf-8'))
- assert book['id'] == 2
- assert book['author']['id'] == 1
-
- def test_no_content_type_if_no_return_type(self):
- if six.PY3:
- self.skipTest(
- "This test does not work in Python 3 until https://review.openstack.org/#/c/48439/ is merged")
- res = self.app.delete('/authors/4')
- assert "Content-Type" not in res.headers, res.headers['Content-Type']