Merge lp:~soren/surveilr/extend-client into lp:surveilr

Proposed by Soren Hansen
Status: Merged
Approved by: Soren Hansen
Approved revision: 29
Merged at revision: 29
Proposed branch: lp:~soren/surveilr/extend-client
Merge into: lp:surveilr
Diff against target: 701 lines (+448/-98)
4 files modified
surveilr/admin.py (+55/-3)
surveilr/api/client.py (+108/-24)
surveilr/tests/api/test_client.py (+204/-52)
surveilr/tests/test_admin.py (+81/-19)
To merge this branch: bzr merge lp:~soren/surveilr/extend-client
Reviewer Review Type Date Requested Status
Soren Hansen Pending
Review via email: mp+90631@code.launchpad.net

Commit message

Extend client to deal with Services and Metrics, and expose get() and delete() methods.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'surveilr/admin.py'
--- surveilr/admin.py 2012-01-28 21:09:53 +0000
+++ surveilr/admin.py 2012-01-29 23:36:23 +0000
@@ -96,14 +96,63 @@
96 print '--user %s --api_key %s' % (user.user_id, user.key)96 print '--user %s --api_key %s' % (user.user_id, user.key)
9797
9898
99class ShowUser(Command):
100 def __call__(self):
101 user = self.client.get_user(self.args[0])
102
103 print 'User:'
104 print 'ID:', user.user_id
105 print 'Admin:', user.admin
106
107
108class DeleteUser(Command):
109 def __call__(self):
110 self.client.delete_user(self.args[0])
111 print 'User deleted'
112
113
114class ShowService(Command):
115 def __call__(self):
116 service = self.client.get_service(self.args[0])
117
118 print 'Service:'
119 print 'ID:', service.id
120 print 'Name:', service.name
121 print 'Plugins:', service.plugins
122
123
124class DeleteService(Command):
125 def __call__(self):
126 self.client.delete_service(self.args[0])
127 print 'Service deleted'
128
129
130class CreateService(Command):
131 def get_optparser(self):
132 optparser = super(CreateService, self).get_optparser()
133 optparser.add_option('-p', action='append', dest='plugins',
134 help='Make the new user an admin')
135 return optparser
136
137 def __call__(self):
138 service = self.client.new_service(self.args[0],
139 self.options.plugins)
140 print 'Service:'
141 print 'ID:', service.id
142
143
144def usage():
145 for cmd_name in commands:
146 cmd = commands[cmd_name]()
147 print cmd.optparser.get_usage().strip()
148
149
99def main(argv=None):150def main(argv=None):
100 if argv is None: # pragma: nocover151 if argv is None: # pragma: nocover
101 argv = sys.argv[1:]152 argv = sys.argv[1:]
102153
103 if not len(argv):154 if not len(argv):
104 for cmd_name in commands:155 usage()
105 cmd = commands[cmd_name]()
106 print cmd.optparser.get_usage().strip()
107 return False156 return False
108157
109 if argv[0] in commands:158 if argv[0] in commands:
@@ -114,6 +163,9 @@
114 except surveilr.api.client.UnauthorizedError:163 except surveilr.api.client.UnauthorizedError:
115 print 'Action not permitted'164 print 'Action not permitted'
116 return False165 return False
166 else:
167 usage()
168 return False
117169
118170
119if __name__ == '__main__': # pragma: nocover171if __name__ == '__main__': # pragma: nocover
120172
=== modified file 'surveilr/api/client.py'
--- surveilr/api/client.py 2012-01-28 21:09:53 +0000
+++ surveilr/api/client.py 2012-01-29 23:36:23 +0000
@@ -34,8 +34,22 @@
34 self.auth = auth34 self.auth = auth
3535
36 def new_user(self, *args, **kwargs):36 def new_user(self, *args, **kwargs):
37 user = User.new(self, *args, **kwargs)37 return User.new(self, *args, **kwargs)
38 return user38
39 def get_user(self, *args, **kwargs):
40 return User.get(self, *args, **kwargs)
41
42 def delete_user(self, *args, **kwargs):
43 return User.delete(self, *args, **kwargs)
44
45 def new_service(self, *args, **kwargs):
46 return Service.new(self, *args, **kwargs)
47
48 def get_service(self, *args, **kwargs):
49 return Service.get(self, *args, **kwargs)
50
51 def delete_service(self, *args, **kwargs):
52 return Service.delete(self, *args, **kwargs)
3953
40 def send_req(self, url_tail, method, body):54 def send_req(self, url_tail, method, body):
41 http = httplib2.Http()55 http = httplib2.Http()
@@ -49,13 +63,6 @@
49 raise UnauthorizedError(resp.reason)63 raise UnauthorizedError(resp.reason)
50 return contents64 return contents
5165
52 def req(self, obj_type, action, data):
53 if action == 'create':
54 method = 'POST'
55 url_tail = '/%ss' % (obj_type.url_part)
56
57 return self.send_req(url_tail, method=method, body=json.dumps(data))
58
5966
60class SurveilrDirectClient(SurveilrClient):67class SurveilrDirectClient(SurveilrClient):
61 def __init__(self, *args, **kwargs):68 def __init__(self, *args, **kwargs):
@@ -73,27 +80,104 @@
7380
7481
75class APIObject(object):82class APIObject(object):
76 def __init__(self, client):83 keys = []
84
85 def __init__(self, client, **kwargs):
77 self.client = client86 self.client = client
7887 for key in self.keys:
79 def req(self, action, data):88 setattr(self, key, kwargs[key])
80 return self.client.req(type(self), action, data)89
90 @classmethod
91 def req(cls, client, action, data, parent=None):
92 if action == 'create':
93 kwargs = {'method': 'POST', 'body': json.dumps(data)}
94 url_tail = '/%ss' % (cls.url_part)
95 elif action == 'get':
96 kwargs = {'method': 'GET'}
97 url_tail = '/%ss/%s' % (cls.url_part, data)
98 elif action == 'delete':
99 kwargs = {'method': 'DELETE'}
100 url_tail = '/%ss/%s' % (cls.url_part, data)
101
102 if parent is not None:
103 url_tail = parent.instance_url_tail() + url_tail
104
105 return client.send_req(url_tail, **kwargs)
106
107 def instance_url_tail(self):
108 return '/%ss/%s' % (self.url_part, self.id)
109
110 @classmethod
111 def delete(cls, client, id):
112 return cls.req(client, 'delete', id)
113
114 @classmethod
115 def get(cls, client, id):
116 return cls.json_deserialise(client, cls.req(client, 'get', id))
117
118 def __repr__(self):
119 return ('<%s object%s>' %
120 (self.__class__.__name__,
121 ''.join([', %s=%r' %
122 (key, getattr(self, key)) for key in self.keys])))
81123
82124
83class User(APIObject):125class User(APIObject):
84 url_part = 'user'126 url_part = 'user'
127 keys = ['id', 'key', 'admin']
85128
86 def __init__(self, client, obj_data=None):129 @classmethod
87 self.client = client130 def json_deserialise(cls, client, s):
88 obj_data = json.loads(obj_data)131 d = json.loads(s)
89 self.user_id = obj_data['id']132 return cls(client, id=d.get('id'),
90 self.key = obj_data['key']133 key=d.get('key'),
91 self.admin = obj_data['admin']134 admin=d.get('admin', False))
92135
93 @classmethod136 @classmethod
94 def new(cls, client, admin=False):137 def new(cls, client, admin=False):
95 return cls(client, client.req(cls, 'create', {'admin': admin}))138 return cls.json_deserialise(client,
96139 cls.req(client,
97 def __repr__(self):140 'create',
98 return ('<User object, user_id=%r, key=%r, admin=%r>' %141 {'admin': admin}))
99 (self.user_id, self.key, self.admin))142
143
144class Service(APIObject):
145 url_part = 'service'
146 keys = ['id', 'name', 'plugins']
147
148 @classmethod
149 def json_deserialise(cls, client, s):
150 d = json.loads(s)
151 return cls(client, id=d.get('id'),
152 name=d.get('name'),
153 plugins=d.get('plugins', []))
154
155 @classmethod
156 def new(cls, client, name, plugins=None):
157 return cls.json_deserialise(client,
158 cls.req(client, 'create',
159 {'name': name,
160 'plugins': plugins}))
161
162 def new_metric(self, *args, **kwargs):
163 return Metric.new(self.client, self, *args, **kwargs)
164
165
166class Metric(APIObject):
167 url_part = 'metric'
168 keys = ['id', 'service', 'timestamp', 'metrics']
169
170 @classmethod
171 def json_deserialise(cls, client, s):
172 d = json.loads(s)
173 return cls(client, id=d.get('id'),
174 service=d.get('service'),
175 timestamp=d.get('timestamp'),
176 metrics=d.get('metrics'))
177
178 @classmethod
179 def new(cls, client, service, metrics):
180 return cls.json_deserialise(client,
181 cls.req(client, 'create',
182 {'metrics': metrics},
183 parent=service))
100184
=== modified file 'surveilr/tests/api/test_client.py'
--- surveilr/tests/api/test_client.py 2012-01-28 21:09:53 +0000
+++ surveilr/tests/api/test_client.py 2012-01-29 23:36:23 +0000
@@ -5,7 +5,7 @@
5from surveilr.api import client5from surveilr.api import client
66
77
8class APIClientTests(tests.TestCase):8class SurveilrClientTests(tests.TestCase):
9 test_url = 'http://somewhere:1234/else'9 test_url = 'http://somewhere:1234/else'
10 test_auth = ('foo', 'bar')10 test_auth = ('foo', 'bar')
1111
@@ -16,19 +16,31 @@
16 auth = self.test_auth16 auth = self.test_auth
17 return client.SurveilrClient(self.test_url, self.test_auth)17 return client.SurveilrClient(self.test_url, self.test_auth)
1818
19 @mock.patch('surveilr.api.client.User')19 def test_client_class_actions(self):
20 def test_new_user(self, User):20 """client.{new,get,delete}_{user,service} proxied to class methods"""
21 test_args = ('foo', 'bar', 'baz')21
22 test_kwargs = {'foo': 'bar',22 for op in ['delete', 'new', 'get']:
23 'baz': 'wibble'}23 for t in ['User', 'Service']:
2424 @mock.patch('surveilr.api.client.%s' % t)
25 api_client = self._get_client()25 def test_action(cls):
26 user = api_client.new_user(*test_args, **test_kwargs)26 test_args = ('foo', 'bar', 'baz')
27 User.new.assert_called_with(api_client, *test_args, **test_kwargs)27 test_kwargs = {'foo': 'bar',
28 self.assertEquals(user, User.new.return_value)28 'baz': 'wibble'}
29 api_client = self._get_client()
30 client_obj_func = getattr(api_client,
31 '%s_%s' % (op, t.lower()))
32
33 user = client_obj_func(*test_args, **test_kwargs)
34
35 cls_func = getattr(cls, op)
36 cls_func.assert_called_with(api_client, *test_args,
37 **test_kwargs)
38 self.assertEquals(user, cls_func.return_value)
39 test_action()
2940
30 @mock.patch('surveilr.api.client.httplib2')41 @mock.patch('surveilr.api.client.httplib2')
31 def test_send_req(self, httplib2):42 def test_send_req(self, httplib2):
43 # Setup
32 api_client = self._get_client()44 api_client = self._get_client()
3345
34 class FakeResponse(object):46 class FakeResponse(object):
@@ -37,16 +49,24 @@
3749
38 http = httplib2.Http.return_value50 http = httplib2.Http.return_value
39 http.request.return_value = (FakeResponse(200), 'resp')51 http.request.return_value = (FakeResponse(200), 'resp')
52
53 # Exercise
40 client_response = api_client.send_req('tail', 'METHOD', 'body')54 client_response = api_client.send_req('tail', 'METHOD', 'body')
55
56 # Verify that the credentials are passed
41 http.add_credentials.assert_called_with(*self.test_auth)57 http.add_credentials.assert_called_with(*self.test_auth)
58 # Verify that the given method is used, the body is passed correctly,
59 # and that the url is constructed correctlyi
42 http.request.assert_called_with(self.test_url + '/tail',60 http.request.assert_called_with(self.test_url + '/tail',
43 method='METHOD',61 method='METHOD',
44 body='body')62 body='body')
4563
64 # Verify that the response comes back intact
46 self.assertEquals(client_response, 'resp')65 self.assertEquals(client_response, 'resp')
4766
48 @mock.patch('surveilr.api.client.httplib2')67 @mock.patch('surveilr.api.client.httplib2')
49 def test_send_req_403_reraises_unauthorized_error(self, httplib2):68 def test_send_req_403_reraises_unauthorized_error(self, httplib2):
69 # Setup
50 api_client = self._get_client()70 api_client = self._get_client()
5171
52 class FakeResponse(object):72 class FakeResponse(object):
@@ -57,58 +77,190 @@
5777
58 http = httplib2.Http.return_value78 http = httplib2.Http.return_value
59 http.request.return_value = (FakeResponse(403), 'resp')79 http.request.return_value = (FakeResponse(403), 'resp')
80
81 # Exercise and verify
60 self.assertRaises(client.UnauthorizedError,82 self.assertRaises(client.UnauthorizedError,
61 api_client.send_req, 'tail', 'METHOD', 'body')83 api_client.send_req, 'tail', 'METHOD', 'body')
6284
63 def test_req(self):85 def test_req_create_with_parend(self):
64 api_client = self._get_client()86 api_client = self._get_client()
6587
66 test_data = {'foo': ['bar', 'baz', 2]}88 test_data = {'foo': ['bar', 'baz', 2]}
6789
68 class FakeObjType(object):90 class FakeObjType(client.APIObject):
69 url_part = 'stuff'91 url_part = 'stuff'
7092
71 with mock.patch_object(api_client, 'send_req') as send_req:93 parent = FakeObjType(api_client)
72 api_client.req(FakeObjType(), 'create', test_data)94 parent.id = 7
95 with mock.patch_object(api_client, 'send_req') as send_req:
96 FakeObjType(api_client).req(api_client, 'create', test_data,
97 parent=parent)
98 self.assertEquals(send_req.call_args[0][0], '/stuffs/7/stuffs')
99 self.assertEquals(send_req.call_args[1]['method'], 'POST')
100 self.assertEquals(json.loads(send_req.call_args[1]['body']),
101 test_data)
102
103 def test_req_create(self):
104 api_client = self._get_client()
105
106 test_data = {'foo': ['bar', 'baz', 2]}
107
108 class FakeObjType(client.APIObject):
109 url_part = 'stuff'
110
111 with mock.patch_object(api_client, 'send_req') as send_req:
112 FakeObjType(api_client).req(api_client, 'create', test_data)
73 self.assertEquals(send_req.call_args[0][0], '/stuffs')113 self.assertEquals(send_req.call_args[0][0], '/stuffs')
74 self.assertEquals(send_req.call_args[1]['method'], 'POST')114 self.assertEquals(send_req.call_args[1]['method'], 'POST')
75 self.assertEquals(json.loads(send_req.call_args[1]['body']),115 self.assertEquals(json.loads(send_req.call_args[1]['body']),
76 test_data)116 test_data)
77117
78 def test_apiobject(self):118 def test_req_get(self):
119 api_client = self._get_client()
120
121 class FakeObjType(client.APIObject):
122 url_part = 'stuff'
123
124 with mock.patch_object(api_client, 'send_req') as send_req:
125 FakeObjType(api_client).req(api_client, 'get', 'someid')
126 self.assertEquals(send_req.call_args[0][0], '/stuffs/someid')
127 self.assertEquals(send_req.call_args[1]['method'], 'GET')
128
129 def test_req_delete(self):
130 api_client = self._get_client()
131
132 class FakeObjType(client.APIObject):
133 url_part = 'stuff'
134
135 with mock.patch_object(api_client, 'send_req') as send_req:
136 FakeObjType(api_client).req(api_client, 'delete', 'someid')
137 self.assertEquals(send_req.call_args[0][0], '/stuffs/someid')
138 self.assertEquals(send_req.call_args[1]['method'], 'DELETE')
139
140
141class APIObjectTests(tests.TestCase):
142 def _test_req(self):
79 client_obj = mock.Mock()143 client_obj = mock.Mock()
80 data = mock.Sentinel()144 data = mock.Sentinel()
81145
82 class TestObject(client.APIObject):146 class TestObject(client.APIObject):
83 pass147 url_part = 'test'
84148
85 obj = TestObject(client_obj)149 obj = TestObject(client_obj)
86 obj.req('ACTION', data)150 obj.req('ACTION', data)
87151
88 client_obj.req.assert_called_with(TestObject, 'ACTION', data)152 client_obj.req.assert_called_with(TestObject, 'ACTION', data)
89153
90154 def _test_get_or_delete(self, op, req_return=None):
91class APITypeTests(tests.TestCase):155 client_obj = mock.Mock()
92 def _test_user_new(self, admin):156
93 client_obj = mock.Mock()157 class TestObject(client.APIObject):
94 client_obj.req.return_value = json.dumps({'id': 'testid',158 url_part = 'test'
159
160 @classmethod
161 def json_deserialise(inner_self, client, s):
162 self.assertEquals(s, 'lots of json')
163
164 req = mock.Mock()
165
166 TestObject.req.return_value = req_return
167
168 getattr(TestObject, op)(client_obj, 'testid')
169 TestObject.req.assert_called_with(client_obj, op, 'testid')
170
171 def test_delete(self):
172 self._test_get_or_delete('delete')
173
174 def test_get(self):
175 self._test_get_or_delete('get', 'lots of json')
176
177
178class UserTests(tests.TestCase):
179 def _test_new(self, admin):
180 # Setup
181 client_obj = mock.Mock()
182 with mock.patch_object(client.User, 'req') as client_req:
183 client_req.return_value = json.dumps({'id': 'testid',
95 'key': 'testkey',184 'key': 'testkey',
96 'admin': admin})185 'admin': admin})
97 user = client.User.new(client_obj)186
98 client_obj.req.assert_called_with(client.User, 'create',187 # Exercise
188 user = client.User.new(client_obj)
189
190 # Verify
191 client_req.assert_called_with(client_obj, 'create',
99 {'admin': False})192 {'admin': False})
100193
101 self.assertEquals(user.user_id, 'testid')194 self.assertEquals(user.id, 'testid')
102 self.assertEquals(user.key, 'testkey')195 self.assertEquals(user.key, 'testkey')
103 self.assertEquals(user.admin, admin)196 self.assertEquals(user.admin, admin)
104 self.assertEquals(repr(user), "<User object, user_id=u'testid', "197 self.assertEquals(repr(user), "<User object, id=u'testid', "
105 "key=u'testkey', admin=%r>" % user.admin)198 "key=u'testkey', admin=%r>" %
199 user.admin)
106200
107 def test_user_new_admin(self):201 def test_user_new_admin(self):
108 self._test_user_new(True)202 self._test_new(True)
109203
110 def test_user_new_not_admin(self):204 def test_user_new_not_admin(self):
111 self._test_user_new(False)205 self._test_new(False)
206
207
208class ServiceTests(tests.TestCase):
209 def test_new(self):
210 # Setup
211 test_plugins = ['http://h:1/p']
212 client_obj = mock.Mock()
213 with mock.patch_object(client.Service, 'req') as service_req:
214 service_req.return_value = json.dumps({'id': 'testid',
215 'name': 'testname',
216 'plugins': test_plugins})
217
218 # Exercise
219 service = client.Service.new(client_obj, 'testname',
220 plugins=test_plugins)
221
222 # Verify
223 service_req.assert_called_with(client_obj, 'create',
224 {'name': 'testname',
225 'plugins': test_plugins})
226
227 self.assertEquals(service.id, 'testid')
228 self.assertEquals(service.name, 'testname')
229 self.assertEquals(service.plugins, test_plugins)
230 self.assertEquals(repr(service), "<Service object, id=u'testid', "
231 "name=u'testname', "
232 "plugins=[u'http://h:1/p']>")
233
234
235class MetricTests(tests.TestCase):
236 def test_service_metric_new(self):
237 # Setup
238 client_obj = mock.Mock()
239 service_obj = client.Service(client_obj, id='testid',
240 name='testname', plugins=[])
241
242 with mock.patch_object(client, 'Metric') as Metric:
243 service_obj.new_metric({'foo': 1234})
244
245 Metric.new.assert_called_with(client_obj, service_obj,
246 {'foo': 1234})
247
248
249 def test_new(self):
250 # Setup
251 client_obj = mock.Mock()
252 service_obj = mock.Sentinel()
253 with mock.patch_object(client.Metric, 'req') as metric_req:
254 metric_req.return_value = json.dumps({})
255
256 # Exercise
257 client.Metric.new(client_obj, service_obj, {'something': 1234})
258
259 # Verify
260 metric_req.assert_called_with(client_obj,
261 'create',
262 {'metrics': {'something': 1234}},
263 parent=service_obj)
112264
113265
114class DirectClientTests(tests.TestCase):266class DirectClientTests(tests.TestCase):
@@ -122,18 +274,18 @@
122 from webob import Request274 from webob import Request
123275
124 api_client = client.SurveilrDirectClient({})276 api_client = client.SurveilrDirectClient({})
125 api_client.app = mock.Mock()277 with mock.patch_object(api_client, 'app') as app:
126 response = mock.Mock()278 response = mock.Mock()
127 response.body = 'response'279 response.body = 'response'
128 api_client.app.return_value = response280 app.return_value = response
129281
130 client_response = api_client.send_req('tail', 'METHOD', 'body')282 client_response = api_client.send_req('tail', 'METHOD', 'body')
131283
132 args = api_client.app.call_args[0]284 args = app.call_args[0]
133 self.assertEquals(type(args[0]), Request)285 self.assertEquals(type(args[0]), Request)
134286
135 req = args[0]287 req = args[0]
136 self.assertEquals(req.path, 'tail')288 self.assertEquals(req.path, 'tail')
137 self.assertEquals(req.method, 'METHOD')289 self.assertEquals(req.method, 'METHOD')
138 self.assertEquals(req.body, 'body')290 self.assertEquals(req.body, 'body')
139 self.assertEquals(client_response, 'response')291 self.assertEquals(client_response, 'response')
140292
=== modified file 'surveilr/tests/test_admin.py'
--- surveilr/tests/test_admin.py 2012-01-28 21:09:53 +0000
+++ surveilr/tests/test_admin.py 2012-01-29 23:36:23 +0000
@@ -126,24 +126,6 @@
126126
127 self.assertIsNone(auth)127 self.assertIsNone(auth)
128128
129 def test_CreateUser_admin(self):
130 create_user = admin.CreateUser()
131
132 create_user.init(['--admin'])
133 create_user.client = mock.Mock()
134 create_user()
135
136 create_user.client.new_user.assert_called_with(admin=True)
137
138 def test_CreateUser_non_admin(self):
139 create_user = admin.CreateUser()
140
141 create_user.init([])
142 create_user.client = mock.Mock()
143 create_user()
144
145 create_user.client.new_user.assert_called_with(admin=False)
146
147 def _replace_stdout_with_stringio(self):129 def _replace_stdout_with_stringio(self):
148 saved_stdout = sys.stdout130 saved_stdout = sys.stdout
149131
@@ -163,12 +145,23 @@
163 self.assertEquals(sys.stdout.getvalue(),145 self.assertEquals(sys.stdout.getvalue(),
164 'Action not permitted\n')146 'Action not permitted\n')
165147
166 def test_cmd_list(self):148 def test_cmd_list_if_no_command_given(self):
167 self._replace_stdout_with_stringio()149 self._replace_stdout_with_stringio()
168 ret = admin.main([])150 ret = admin.main([])
169151
170 self.assertEquals(ret, False)152 self.assertEquals(ret, False)
171 stdout = sys.stdout.getvalue()153 stdout = sys.stdout.getvalue()
154 self._check_cmd_list(stdout)
155
156 def test_cmd_list_if_invalid_command_given(self):
157 self._replace_stdout_with_stringio()
158 ret = admin.main(['this is not a valid command'])
159
160 self.assertEquals(ret, False)
161 stdout = sys.stdout.getvalue()
162 self._check_cmd_list(stdout)
163
164 def _check_cmd_list(self, stdout):
172 self.assertEquals(len(stdout.split('\n')) - 1, len(admin.commands))165 self.assertEquals(len(stdout.split('\n')) - 1, len(admin.commands))
173166
174 expected_commands = set(admin.commands.keys())167 expected_commands = set(admin.commands.keys())
@@ -180,3 +173,72 @@
180 found_commands.add(cmd)173 found_commands.add(cmd)
181174
182 self.assertEquals(expected_commands, found_commands)175 self.assertEquals(expected_commands, found_commands)
176
177 def test_CreateUser_admin(self):
178 create_user = admin.CreateUser()
179
180 create_user.init(['--admin'])
181 create_user.client = mock.Mock()
182 create_user()
183
184 create_user.client.new_user.assert_called_with(admin=True)
185
186 def test_CreateUser_non_admin(self):
187 create_user = admin.CreateUser()
188
189 create_user.init([])
190 create_user.client = mock.Mock()
191 create_user()
192
193 create_user.client.new_user.assert_called_with(admin=False)
194
195 def test_CreateService_no_plugins(self):
196 create_service = admin.CreateService()
197
198 create_service.init(['servicename'])
199 create_service.client = mock.Mock()
200 create_service()
201
202 create_service.client.new_service.assert_called_with('servicename',
203 None)
204
205 def test_CreateService_with_plugins(self):
206 create_service = admin.CreateService()
207
208 plugins = ['http://foo/bar', 'http://baz/wibble']
209
210 args = ['servicename']
211 for plugin in plugins:
212 args += ['-p', plugin]
213
214 create_service.init(args)
215 create_service.client = mock.Mock()
216 create_service()
217
218 create_service.client.new_service.assert_called_with('servicename',
219 plugins)
220
221 def test_ShowUser(self):
222 self._test_Show_or_Delete('show', 'user')
223
224 def test_ShowService(self):
225 self._test_Show_or_Delete('show', 'service')
226
227 def test_DeleteUser(self):
228 self._test_Show_or_Delete('delete', 'user')
229
230 def test_DeleteService(self):
231 self._test_Show_or_Delete('delete', 'service')
232
233 def _test_Show_or_Delete(self, show_or_delete, type_name):
234 cmd = getattr(admin, '%s%s' % (show_or_delete.capitalize(),
235 type_name.capitalize()))()
236
237 cmd.init(['testid'])
238 cmd.client = mock.Mock()
239 cmd()
240
241 method_prefix = show_or_delete == 'show' and 'get' or 'delete'
242 method_name = '%s_%s' % (method_prefix, type_name)
243 method = getattr(cmd.client, method_name)
244 method.assert_called_with('testid')

Subscribers

People subscribed via source and target branches

to all changes: