aboutsummaryrefslogtreecommitdiffstats
path: root/pygithub3
diff options
context:
space:
mode:
Diffstat (limited to 'pygithub3')
-rw-r--r--pygithub3/exceptions.py6
-rw-r--r--pygithub3/requests/__init__.py34
-rw-r--r--pygithub3/requests/users/keys.py10
-rw-r--r--pygithub3/requests/users/user.py7
-rw-r--r--pygithub3/tests/requests/test_core.py72
-rw-r--r--pygithub3/tests/utils/requests.py11
6 files changed, 89 insertions, 51 deletions
diff --git a/pygithub3/exceptions.py b/pygithub3/exceptions.py
index 65881bb..c44c371 100644
--- a/pygithub3/exceptions.py
+++ b/pygithub3/exceptions.py
@@ -2,6 +2,12 @@
# -*- encoding: utf-8 -*-
+class InvalidBodySchema(Exception):
+ """ Raised when the 'valids_body' attribute of Resource isn't in a
+ valid form (required.issubsetof(schema))"""
+ pass
+
+
class DoesNotExists(Exception):
""" Raised when `Request` factory can't find the subclass """
pass
diff --git a/pygithub3/requests/__init__.py b/pygithub3/requests/__init__.py
index 2209fc5..a734895 100644
--- a/pygithub3/requests/__init__.py
+++ b/pygithub3/requests/__init__.py
@@ -8,7 +8,8 @@ try:
except ImportError:
import json
-from pygithub3.exceptions import DoesNotExists, UriInvalid, ValidationError
+from pygithub3.exceptions import (DoesNotExists, UriInvalid, ValidationError,
+ InvalidBodySchema)
from pygithub3.resources.base import Raw
ABS_IMPORT_PREFIX = 'pygithub3.requests'
@@ -16,9 +17,10 @@ ABS_IMPORT_PREFIX = 'pygithub3.requests'
class Body(object):
- def __init__(self, content, schema):
+ def __init__(self, content, schema, required):
self.content = content
self.schema = schema
+ self.required = required
def dumps(self):
if not self.content:
@@ -26,13 +28,19 @@ class Body(object):
return json.dumps(self.parse())
def parse(self):
- if not self.schema:
- return self.content
- if not hasattr(self.content, 'items'):
+ if self.schema and not hasattr(self.content, 'items'):
raise ValidationError("'%s' needs a content dictionary"
% self.__class__.__name__)
- return {key: self.content[key] for key in self.schema
+ parsed = {key: self.content[key] for key in self.schema
if key in self.content}
+ for attr_required in self.required:
+ if attr_required not in parsed:
+ raise ValidationError("'%s' attribute is required" %
+ attr_required)
+ if not parsed[attr_required]:
+ raise ValidationError("'%s' attribute can't be empty" %
+ attr_required)
+ return parsed or self.content
class Request(object):
@@ -40,7 +48,7 @@ class Request(object):
uri = ''
resource = Raw
- body_schema = ()
+ body_schema = {}
def __init__(self, **kwargs):
""" """
@@ -50,7 +58,7 @@ class Request(object):
def clean(self):
self.uri = self.clean_uri() or self.uri
- self.body = Body(self.clean_body(), self.body_schema)
+ self.body = Body(self.clean_body(), **self.clean_valid_body())
def clean_body(self):
return self.body
@@ -58,6 +66,16 @@ class Request(object):
def clean_uri(self):
return None
+ def clean_valid_body(self):
+ schema = set(self.body_schema.get('schema', ()))
+ required = set(self.body_schema.get('required', ()))
+ if not required.issubset(schema):
+ raise InvalidBodySchema(
+ "'%s:valid_body' attribute is invalid. "
+ "'%s required' isn't a subset of '%s schema'" % (
+ self.__class__.__name__, required, schema))
+ return dict(schema=schema, required=required)
+
def __getattr__(self, name):
return self.args.get(name)
diff --git a/pygithub3/requests/users/keys.py b/pygithub3/requests/users/keys.py
index 9afa3cd..23a30d9 100644
--- a/pygithub3/requests/users/keys.py
+++ b/pygithub3/requests/users/keys.py
@@ -21,13 +21,19 @@ class Add(Request):
resource = Key
uri = 'user/keys'
- body_schema = ('title', 'key')
+ body_schema = {
+ 'schema': ('title', 'key'),
+ 'required': ('title', 'key')
+ }
class Update(Request):
resource = Key
- body_schema = ('title', 'key')
+ body_schema = {
+ 'schema': ('title', 'key'),
+ 'required': ('title', 'key')
+ }
uri = 'user/keys/{key_id}'
diff --git a/pygithub3/requests/users/user.py b/pygithub3/requests/users/user.py
index 740b3dd..3267f76 100644
--- a/pygithub3/requests/users/user.py
+++ b/pygithub3/requests/users/user.py
@@ -21,8 +21,11 @@ class Update(Request):
resource = User
uri = 'user'
- body_schema = (
- 'name', 'email', 'blog', 'company', 'location', 'hireable', 'bio')
+ body_schema = {
+ 'schema': ('name', 'email', 'blog', 'company', 'location', 'hireable',
+ 'bio'),
+ 'required': (),
+ }
def clean_body(self):
if not self.body:
diff --git a/pygithub3/tests/requests/test_core.py b/pygithub3/tests/requests/test_core.py
index 98c5f3f..a46c1d9 100644
--- a/pygithub3/tests/requests/test_core.py
+++ b/pygithub3/tests/requests/test_core.py
@@ -5,10 +5,12 @@ from unittest import TestCase
from mock import Mock
from pygithub3.requests import Factory, Body, json, Request
-from pygithub3.exceptions import UriInvalid, DoesNotExists, ValidationError
+from pygithub3.exceptions import (UriInvalid, DoesNotExists, ValidationError,
+ InvalidBodySchema)
+from pygithub3.tests.utils.base import mock_json
from pygithub3.tests.utils.requests import (
- RequestWithArgs, RequestCleanedUri, RequestBodyWithSchema, mock_json,
- DummyRequest, RequestCleanedBody)
+ RequestWithArgs, RequestCleanedUri, RequestBodyInvalidSchema, DummyRequest,
+ RequestCleanedBody)
json.dumps = Mock(side_effect=mock_json)
json.loads = Mock(side_effect=mock_json)
@@ -35,7 +37,7 @@ class TestFactory(TestCase):
self.assertIsInstance(request, Request)
-class TestRequestUri(TestCase):
+class TestRequest(TestCase):
def test_SIMPLE_with_correct_args(self):
request = RequestWithArgs(arg1='arg1', arg2='arg2')
@@ -51,47 +53,45 @@ class TestRequestUri(TestCase):
request = RequestCleanedUri(notmatters='test')
self.assertEqual(str(request), 'URI')
+ def test_with_cleaned_body(self):
+ self.assertRaises(ValidationError, RequestCleanedBody)
-class TestRequestBody(TestCase):
-
- def test_with_schema_with_valid(self):
- request = RequestBodyWithSchema(body=dict(
- arg1='only', fake='t', fake1='t'))
- self.assertEqual(request.get_body(), dict(arg1='only'))
-
- def test_with_schema_with_invalid(self):
- request = RequestBodyWithSchema(body='invalid_data')
- self.assertRaises(ValidationError, request.get_body)
-
- def test_with_schema_without_body(self):
- request = RequestBodyWithSchema()
- self.assertIsNone(request.get_body())
+ def test_with_invalid_schema(self):
+ self.assertRaises(InvalidBodySchema, RequestBodyInvalidSchema)
- def test_without_schema(self):
+ def test_body_without_schema(self):
request = DummyRequest(body=dict(arg1='test'))
self.assertEqual(request.get_body(), dict(arg1='test'))
+ self.assertEqual(request.body.schema, set(()))
+ self.assertEqual(request.body.required, set(()))
- def test_without_schema_without_body(self):
+ def test_without_body_and_without_schema(self):
request = DummyRequest()
self.assertIsNone(request.get_body())
- def test_with_clean_body(self):
- self.assertRaises(ValidationError, RequestCleanedBody)
+class TestRequestBody(TestCase):
+
+ def setUp(self):
+ valid_body = dict(schema=('arg1', 'arg2'), required=('arg1', ))
+ self.b = Body({}, **valid_body)
+
+ def test_with_required(self):
+ self.b.content = dict(arg1='arg1')
+ self.assertEqual(self.b.dumps(), dict(arg1='arg1'))
+
+ def test_without_required(self):
+ self.b.content = dict(arg2='arg2')
+ self.assertRaises(ValidationError, self.b.dumps)
+
+ def test_with_invalid(self):
+ self.b.content = 'invalid'
+ self.assertRaises(ValidationError, self.b.dumps)
-class TestBodyParsers(TestCase):
+ def test_without_body(self):
+ self.b.content = None
+ self.assertIsNone(self.b.dumps())
def test_only_valid_keys(self):
- body = Body(
- dict(arg1='arg1', arg2='arg2', arg3='arg3', arg4='arg4'),
- ('arg1', 'arg3', 'arg4'))
- self.assertEqual(body.parse(), dict(arg1='arg1', arg3='arg3',
- arg4='arg4'))
-
- def test_none(self):
- body = Body({}, ('arg1', 'arg2'))
- self.assertEqual(body.parse(), {})
-
- def test_invalid_content(self):
- body = Body('invalid', ('arg1',))
- self.assertRaises(ValidationError, body.parse)
+ self.b.content = dict(arg1='arg1', arg2='arg2', fake='test')
+ self.assertEqual(self.b.dumps(), dict(arg1='arg1', arg2='arg2'))
diff --git a/pygithub3/tests/utils/requests.py b/pygithub3/tests/utils/requests.py
index ead94a6..cc92768 100644
--- a/pygithub3/tests/utils/requests.py
+++ b/pygithub3/tests/utils/requests.py
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
-from .base import Request, DummyResource, DummyRequest, mock_json
+from .base import Request, DummyResource, DummyRequest
from pygithub3.exceptions import ValidationError
@@ -19,9 +19,14 @@ class RequestCleanedUri(Request):
return 'URI'
-class RequestBodyWithSchema(Request):
+class RequestBodyInvalidSchema(Request):
+ """ It's invalid because body_schema[required] isn't a subset of
+ body_schema[schema] """
uri = 'URI'
- body_schema = ('arg1', 'arg2')
+ body_schema = {
+ 'schema': ('arg1', 'arg2'),
+ 'required': ('arg3', )
+ }
class RequestCleanedBody(Request):