Skip to content

Commit 5023140

Browse files
committed
test(protocol): Introduce account management test
Refs: #141
1 parent e08593e commit 5023140

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

automatoes/protocol.py

+10-12
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ def __kwargs_updater(self, method, **kwargs):
8484
protected['kid'] = kid
8585
protected.pop("jwk")
8686
data = kwargs.get("data")
87-
if data:
88-
kwargs['data'] = sign_request_v2(key, protected, data)
87+
kwargs['data'] = sign_request_v2(key, protected, data)
8988
if self.peasant.verify:
9089
kwargs['verify'] = self.peasant.verify
9190
return kwargs
@@ -130,9 +129,8 @@ def get_registration(self, account: Account):
130129
Get available account information from the server.
131130
"""
132131
headers = {'Content-Type': "application/jose+json"}
133-
response = self.post_as_get(account.uri, headers=headers,
134-
kid=self.account.uri,
135-
key=account.key, uri=account.uri)
132+
response = self.post(account.uri, headers=headers, kid=account.uri,
133+
key=account.key, uri=account.uri)
136134
if str(response.status_code).startswith("2"):
137135
return _json(response)
138136
raise AcmeError(response)
@@ -154,8 +152,8 @@ def new_account(self, account: Account, contacts: list,
154152
}
155153
headers = {'Content-Type': "application/jose+json"}
156154
path = self.peasant.directory()['newAccount']
157-
response = self.post_as_get(path, data=payload, headers=headers,
158-
key=account.key, uri=path)
155+
response = self.post(path, data=payload, headers=headers,
156+
key=account.key, uri=path)
159157

160158
uri = response.headers.get("Location")
161159

@@ -172,11 +170,11 @@ def new_account(self, account: Account, contacts: list,
172170
raise AcmeError(response)
173171

174172
def terms_from_directory(self):
175-
response = self.get_directory()
176-
if response.status_code == 200:
177-
if "meta" in response.json():
178-
if "termsOfService" in response.json()['meta']:
179-
return response.json()['meta']['termsOfService']
173+
# TODO: Check how to trigger an error here
174+
directory = self.peasant.directory()
175+
if "meta" in directory:
176+
if "termsOfService" in directory['meta']:
177+
return directory['meta']['termsOfService']
180178
return None
181179

182180

tests/account_management_test.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2019-2025 Flavio Garcia
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from automatoes.crypto import generate_rsa_key
16+
from automatoes.model import Account
17+
from cartola import security
18+
from tests import get_protocol, get_transport, PEEBLE_URL
19+
from tornado import testing
20+
21+
22+
class AccountTestCase(testing.AsyncTestCase):
23+
""" Test letsencrypt nonce
24+
"""
25+
26+
@testing.gen_test
27+
async def test_new_acount(self):
28+
protocol = get_protocol(get_transport())
29+
contact = "candango_{}_{}@candango.org".format(
30+
security.random_string(5, False, False),
31+
security.random_string(5, False, False)
32+
)
33+
# To check against the get_registration method after
34+
# TODO: check against more than one emails in the contacts
35+
peeble_term = ("data:text/plain,Do%20what%20thou%20wilt")
36+
account = Account(key=generate_rsa_key(4096))
37+
response = protocol.new_account(account, [contact], True)
38+
self.assertEqual(peeble_term, response.terms)
39+
self.assertEqual("valid", response.contents['status'])
40+
account.uri = response.uri
41+
self.assertEqual(PEEBLE_URL, "/".join(response.uri.split("/")[0:3]))
42+
self.assertEqual("my-account", "/".join(response.uri.split("/")[3:4]))
43+
self.assertIsInstance(response.uri.split("/")[4:5][0], str)
44+
response = protocol.get_registration(account)

0 commit comments

Comments
 (0)