Skip to content

Commit 023780d

Browse files
committed
further server work
1 parent f093e2f commit 023780d

File tree

3 files changed

+39
-57
lines changed

3 files changed

+39
-57
lines changed

mig/lib/coresvc/server.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import base64
3838
from collections import defaultdict, namedtuple
3939
from flask import Flask, request, Response
40+
import json
4041
import os
4142
import sys
4243
import threading
@@ -60,12 +61,28 @@ def http_error_from_status_code(http_status_code, http_url, description=None):
6061
return httpexceptions_by_code[http_status_code](description)
6162

6263

63-
def _create_user(user_dict, conf_path, **kwargs):
64+
def json_reponse_from_status_code(http_status_code, content):
65+
json_content = json.dumps(content)
66+
return Response(json_content, http_status_code, { 'Content-Type': 'application/json' })
67+
68+
69+
def _create_user(configuration, payload):
70+
user_dict = canonical_user(
71+
configuration, payload, _REQUEST_ARGS_POST_USER._fields)
72+
fill_user(user_dict)
73+
force_native_str_rec(user_dict)
74+
6475
try:
65-
useradm_create_user(user_dict, conf_path, keyword_auto, **kwargs)
66-
except Exception as exc:
67-
return 1
68-
return 0
76+
useradm_create_user(user_dict, configuration, keyword_auto, default_renew=True)
77+
except:
78+
raise http_error_from_status_code(500, None)
79+
user_email = user_dict['email']
80+
objects = search_users(configuration, {
81+
'email': user_email
82+
})
83+
if len(objects) != 1:
84+
raise http_error_from_status_code(400, None)
85+
return objects[0]
6986

7087

7188
def search_users(configuration, search_filter):
@@ -102,21 +119,12 @@ def POST_user():
102119
payload = request.get_json()
103120

104121
try:
105-
validated = _REQUEST_ARGS_POST_USER.ensure(payload)
122+
payload = _REQUEST_ARGS_POST_USER.ensure(payload)
106123
except PayloadException as vr:
107124
return http_error_from_status_code(400, None, vr.serialize())
108125

109-
user_dict = canonical_user(
110-
configuration, validated, _REQUEST_ARGS_POST_USER._fields)
111-
fill_user(user_dict)
112-
force_native_str_rec(user_dict)
113-
114-
ret = _create_user(user_dict, configuration, default_renew=True)
115-
if ret != 0:
116-
raise http_error_from_status_code(400, None)
117-
118-
greeting = 'hello client!'
119-
return Response(greeting, 201)
126+
user = _create_user(configuration, payload)
127+
return json_reponse_from_status_code(201, user)
120128

121129
return app
122130

mig/shared/useradm.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,9 @@ def create_user(user, conf_path, db_path, force=False, verbose=False,
10271027
format as a first step.
10281028
"""
10291029

1030-
if conf_path:
1030+
if isinstance(conf_path, Configuration):
1031+
configuration = conf_path
1032+
elif conf_path:
10311033
if isinstance(conf_path, basestring):
10321034

10331035
# has been checked for accessibility above...

tests/test_mig_lib_coresvc.py

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ def before_each(self):
5252

5353
ensure_dirs_needed_by_create_user(self.configuration)
5454

55+
self.server_addr = ('localhost', 4567)
56+
self.server_thread = self._make_server(
57+
self.configuration, self.logger, self.server_addr)
58+
5559
def _provide_configuration(self):
5660
return 'testconfig'
5761

@@ -67,9 +71,6 @@ def issue_POST(self, request_path, **kwargs):
6771

6872
@unittest.skipIf(PY2, "Python 3 only")
6973
def test__GET_returns_not_found_for_missing_path(self):
70-
self.server_addr = ('localhost', 4567)
71-
self.server_thread = self._make_server(
72-
self.configuration, self.logger, self.server_addr)
7374
self.server_thread.start_wait_until_ready()
7475

7576
status, _ = self.issue_GET('/nonexistent')
@@ -78,9 +79,6 @@ def test__GET_returns_not_found_for_missing_path(self):
7879

7980
@unittest.skipIf(PY2, "Python 3 only")
8081
def test_GET_user__top_level_request(self):
81-
self.server_addr = ('localhost', 4567)
82-
self.server_thread = self._make_server(
83-
self.configuration, self.logger, self.server_addr)
8482
self.server_thread.start_wait_until_ready()
8583

8684
status, _ = self.issue_GET('/user')
@@ -96,42 +94,24 @@ def test_GET__user_userid_request_succeeds_with_status_ok(self):
9694
example_username_home_dir) # strip user from path
9795
test_state_dir = os.path.dirname(test_user_home)
9896
test_user_db_home = os.path.join(test_state_dir, "user_db_home")
99-
100-
self.server_addr = ('localhost', 4567)
101-
self.server_thread = self._make_server(
102-
self.configuration, self.logger, self.server_addr)
10397
self.server_thread.start_wait_until_ready()
10498

105-
the_url = '/user/%s' % (example_username,)
106-
status, content = self.issue_GET(the_url)
99+
status, content = self.issue_GET('/user/dummy-user')
107100

108101
self.assertEqual(status, 200)
109102
self.assertEqual(content, 'FOOBAR')
110103

111104
@unittest.skipIf(PY2, "Python 3 only")
112105
def test_GET_openid_user_username(self):
113-
flask_app = None
114-
115-
self.server_addr = ('localhost', 4567)
116-
self.server_thread = self._make_server(
117-
self.configuration, self.logger, self.server_addr)
118106
self.server_thread.start_wait_until_ready()
119107

120-
request_json = json.dumps({})
121-
request_data = codecs.encode(request_json, 'utf8')
122-
123108
status, content = self.issue_GET('/user/dummy-user')
124109

125110
self.assertEqual(status, 200)
126111
self.assertEqual(content, 'FOOBAR')
127112

128113
@unittest.skipIf(PY2, "Python 3 only")
129114
def test_POST_user__bad_input_data(self):
130-
flask_app = None
131-
132-
self.server_addr = ('localhost', 4567)
133-
self.server_thread = self._make_server(
134-
self.configuration, self.logger, self.server_addr)
135115
self.server_thread.start_wait_until_ready()
136116

137117
status, content = self.issue_POST('/user', request_json={
@@ -146,11 +126,6 @@ def test_POST_user__bad_input_data(self):
146126

147127
@unittest.skipIf(PY2, "Python 3 only")
148128
def test_POST_user(self):
149-
flask_app = None
150-
151-
self.server_addr = ('localhost', 4567)
152-
self.server_thread = self._make_server(
153-
self.configuration, self.logger, self.server_addr)
154129
self.server_thread.start_wait_until_ready()
155130

156131
status, content = self.issue_POST('/user', response_encoding='textual', request_json=dict(
@@ -164,7 +139,8 @@ def test_POST_user(self):
164139
))
165140

166141
self.assertEqual(status, 201)
167-
self.assertEqual(content, 'hello client!')
142+
self.assertIsInstance(content, dict)
143+
self.assertIn('unique_id', content)
168144

169145
def _make_configuration(self, test_logger, server_addr):
170146
configuration = self.configuration
@@ -195,9 +171,6 @@ def _on_instance(server):
195171

196172
class MigServerGrid_openid__existing_user(MigTestCase, HttpAssertMixin):
197173
def before_each(self):
198-
self.server_addr = None
199-
self.server_thread = None
200-
201174
ensure_dirs_needed_by_create_user(self.configuration)
202175

203176
user_dict = {
@@ -212,6 +185,10 @@ def before_each(self):
212185
create_user(user_dict, self.configuration,
213186
keyword_auto, default_renew=True)
214187

188+
self.server_addr = ('localhost', 4567)
189+
self.server_thread = self._make_server(
190+
self.configuration, self.logger, self.server_addr)
191+
215192
def _provide_configuration(self):
216193
return 'testconfig'
217194

@@ -221,11 +198,6 @@ def after_each(self):
221198

222199
@unittest.skipIf(PY2, "Python 3 only")
223200
def test_GET_openid_user_find(self):
224-
flask_app = None
225-
226-
self.server_addr = ('localhost', 4567)
227-
self.server_thread = self._make_server(
228-
self.configuration, self.logger, self.server_addr)
229201
self.server_thread.start_wait_until_ready()
230202

231203
status, content = self._issue_GET(self.server_addr, '/user/find', {

0 commit comments

Comments
 (0)