Merge lp:~soren/surveilr/remember-service-owner into lp:surveilr

Proposed by Soren Hansen
Status: Merged
Approved by: Soren Hansen
Approved revision: 28
Merged at revision: 28
Proposed branch: lp:~soren/surveilr/remember-service-owner
Merge into: lp:surveilr
Diff against target: 115 lines (+28/-5)
2 files modified
surveilr/api/server/app.py (+13/-2)
surveilr/tests/api/server/test_app.py (+15/-3)
To merge this branch: bzr merge lp:~soren/surveilr/remember-service-owner
Reviewer Review Type Date Requested Status
Soren Hansen Pending
Review via email: mp+90630@code.launchpad.net

Commit message

Set Service.user based on authenticated user info.

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
1=== modified file 'surveilr/api/server/app.py'
2--- surveilr/api/server/app.py 2012-01-29 23:14:07 +0000
3+++ surveilr/api/server/app.py 2012-01-29 23:25:26 +0000
4@@ -52,9 +52,9 @@
5
6 def privileged(f):
7 @functools.wraps(f)
8- def wrapped(self, req, *args):
9+ def wrapped(self, req, *args, **kwargs):
10 if is_privileged(req):
11- return f(self, req, *args)
12+ return f(self, req, *args, **kwargs)
13 else:
14 return HTTPForbidden()
15 return wrapped
16@@ -118,6 +118,7 @@
17 user.save()
18 return Response(self.json_serialise(user))
19
20+ @privileged
21 def show(self, req, id):
22 """Called for GET requests to /users/{id}
23
24@@ -128,10 +129,12 @@
25 except riakalchemy.NoSuchObjectError:
26 return HTTPNotFound()
27
28+ @privileged
29 def delete(self, req, id):
30 """Called for DELETE requests to /users/{id}
31
32 Delete the given user"""
33+
34 models.User.get(id).delete()
35 return Response('')
36
37@@ -157,6 +160,12 @@
38 Creates the service, returns a JSON object with the ID assigned
39 to the service"""
40 data = json.loads(req.body)
41+
42+ data['user'] = [req.environ['surveilr.user']]
43+
44+ if 'plugins' not in data or not data['plugins']:
45+ data['plugins'] = []
46+
47 service = models.Service(**data)
48 service.save()
49 return Response(self.json_serialise(service))
50@@ -201,6 +210,7 @@
51
52 Logs a measurement against the service identified by {id}.
53 Returns an empty response"""
54+
55 data = json.loads(req.body)
56 service = models.Service.get(service_name)
57 data['service'] = [service]
58@@ -247,6 +257,7 @@
59
60 Using the Mapper object, it finds the relevant controller
61 based on the URL and delegates the call to that."""
62+
63 results = self.map.routematch(environ=req.environ)
64 if not results:
65 return exc.HTTPNotFound()
66
67=== modified file 'surveilr/tests/api/server/test_app.py'
68--- surveilr/tests/api/server/test_app.py 2012-01-28 21:09:53 +0000
69+++ surveilr/tests/api/server/test_app.py 2012-01-29 23:25:26 +0000
70@@ -104,9 +104,11 @@
71
72 def test_create_retrieve_service(self):
73 """Create, retrieve, delete, attempt to retrieve again (service)"""
74+ user = self._get_user()
75 req = Request.blank('/services',
76 method='POST',
77- POST=json.dumps({'name': 'this_or_the_other'}))
78+ POST=json.dumps({'name': 'this_or_the_other'}),
79+ environ={'surveilr.user': user})
80 resp = self.application(req)
81 self.assertEquals(resp.status_int, 200)
82
83@@ -124,11 +126,19 @@
84 resp = self.application(req)
85 self.assertEquals(resp.status_int, 404)
86
87+ def _get_user(self):
88+ user = models.User()
89+ user.save()
90+ self.addCleanup(user.delete)
91+ return user
92+
93 def test_add_remove_plugin_to_service(self):
94+ user = self._get_user()
95 url = 'http://foo.bar/'
96 req = Request.blank('/services',
97 method='POST',
98- POST=json.dumps({'name': 'this_or_the_other'}))
99+ POST=json.dumps({'name': 'this_or_the_other'}),
100+ environ={'surveilr.user': user})
101 resp = self.application(req)
102 self.assertEquals(resp.status_int, 200)
103
104@@ -158,9 +168,11 @@
105 self.assertEquals(plugins, [])
106
107 def test_create_retrieve_metric(self):
108+ user = self._get_user()
109 req = Request.blank('/services',
110 method='POST',
111- POST='{"name": "this_or_the_other"}')
112+ POST='{"name": "this_or_the_other"}',
113+ environ={'surveilr.user': user})
114 resp = self.application(req)
115 self.assertEquals(resp.status_int, 200)
116

Subscribers

People subscribed via source and target branches

to all changes: