Skip to content

Commit

Permalink
Add Directory.meta (fixes certbot#2768)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba committed Apr 6, 2016
1 parent 23167ec commit e407663
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
12 changes: 10 additions & 2 deletions acme/acme/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ class Directory(jose.JSONDeSerializable):

_REGISTERED_TYPES = {}

class Meta(jose.JSONObjectWithFields):
"""Directory Meta."""
terms_of_service = jose.Field('terms-of-service', omitempty=True)
website = jose.Field('website', omitempty=True)
caa_identities = jose.Field('caa-identities', omitempty=True)

@classmethod
def _canon_key(cls, key):
return getattr(key, 'resource_type', key)
Expand All @@ -137,10 +143,11 @@ def register(cls, resource_body_cls):

def __init__(self, jobj):
canon_jobj = util.map_keys(jobj, self._canon_key)
if not set(canon_jobj).issubset(self._REGISTERED_TYPES):
if not set(canon_jobj).issubset(
set(self._REGISTERED_TYPES).union(['meta'])):
# TODO: acme-spec is not clear about this: 'It is a JSON
# dictionary, whose keys are the "resource" values listed
# in {{https-requests}}'z
# in {{https-requests}}'
raise ValueError('Wrong directory fields')
# TODO: check that everything is an absolute URL; acme-spec is
# not clear on that
Expand All @@ -163,6 +170,7 @@ def to_partial_json(self):

@classmethod
def from_json(cls, jobj):
jobj['meta'] = cls.Meta.from_json(jobj.pop('meta', {}))
try:
return cls(jobj)
except ValueError as error:
Expand Down
18 changes: 15 additions & 3 deletions acme/acme/messages_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ def setUp(self):
self.dir = Directory({
'new-reg': 'reg',
mock.MagicMock(resource_type='new-cert'): 'cert',
'meta': Directory.Meta(
terms_of_service='https://example.com/acme/terms',
website='https://www.example.com/',
caa_identities=['example.com'],
),
})

def test_init_wrong_key_value_error(self):
Expand All @@ -111,9 +116,16 @@ def test_getattr(self):
def test_getattr_fails_with_attribute_error(self):
self.assertRaises(AttributeError, self.dir.__getattr__, 'foo')

def test_to_partial_json(self):
self.assertEqual(
self.dir.to_partial_json(), {'new-reg': 'reg', 'new-cert': 'cert'})
def test_to_json(self):
self.assertEqual(self.dir.to_json(), {
'new-reg': 'reg',
'new-cert': 'cert',
'meta': {
'terms-of-service': 'https://example.com/acme/terms',
'website': 'https://www.example.com/',
'caa-identities': ['example.com'],
},
})

def test_from_json_deserialization_error_on_wrong_key(self):
from acme.messages import Directory
Expand Down

0 comments on commit e407663

Please sign in to comment.