Merge lp:~jderose/usercouch/no-auth into lp:usercouch

Proposed by Jason Gerard DeRose
Status: Merged
Merged at revision: 19
Proposed branch: lp:~jderose/usercouch/no-auth
Merge into: lp:usercouch
Diff against target: 313 lines (+128/-34)
4 files modified
usercouch/__init__.py (+30/-15)
usercouch/misc.py (+2/-2)
usercouch/tests/__init__.py (+59/-7)
usercouch/tests/test_misc.py (+37/-10)
To merge this branch: bzr merge lp:~jderose/usercouch/no-auth
Reviewer Review Type Date Requested Status
Akshat Jain (community) Approve
Novacut Dev Pending
Review via email: mp+85024@code.launchpad.net

Description of the change

UserCouch.bootstrap() now takes an `auth` kwarg, which defaults to 'basic' and must be 'open', 'basic', or 'oauth'.

This required touching a lot of code here and there, but it's still a pretty small change.

To post a comment you must log in.
Revision history for this message
Akshat Jain (akshatj) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'usercouch/__init__.py'
2--- usercouch/__init__.py 2011-12-08 12:20:46 +0000
3+++ usercouch/__init__.py 2011-12-08 20:51:27 +0000
4@@ -37,10 +37,7 @@
5
6 __version__ = '11.12.0'
7
8-BASIC = """
9-[couch_httpd_auth]
10-require_valid_user = true
11-
12+OPEN = """
13 [httpd]
14 bind_address = 127.0.0.1
15 port = {port}
16@@ -64,6 +61,11 @@
17 [stats]
18 rate =
19 samples =
20+"""
21+
22+BASIC = OPEN + """
23+[couch_httpd_auth]
24+require_valid_user = true
25
26 [admins]
27 {username} = {hashed}
28@@ -81,6 +83,16 @@
29 """
30
31
32+def get_template(auth):
33+ if auth == 'open':
34+ return OPEN
35+ if auth == 'basic':
36+ return BASIC
37+ if auth == 'oauth':
38+ return OAUTH
39+ raise ValueError('invalid auth: {!r}'.format(auth))
40+
41+
42 def random_port():
43 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
44 sock.bind(('127.0.0.1', 0))
45@@ -102,13 +114,17 @@
46 )
47
48
49-def random_env(port, oauth=False):
50+def random_env(port, auth):
51+ if auth not in ('open', 'basic', 'oauth'):
52+ raise ValueError('invalid auth: {!r}'.format(auth))
53 env = {
54 'port': port,
55 'url': 'http://localhost:{}/'.format(port),
56- 'basic': random_basic(),
57 }
58- if oauth:
59+ if auth == 'basic':
60+ env['basic'] = random_basic()
61+ elif auth == 'oauth':
62+ env['basic'] = random_basic()
63 env['oauth'] = random_oauth()
64 return env
65
66@@ -182,14 +198,14 @@
67 def __del__(self):
68 self.kill()
69
70- def bootstrap(self, oauth=False, loglevel='notice'):
71+ def bootstrap(self, auth='basic', loglevel='notice'):
72 if self.__bootstraped:
73 raise Exception(
74 '{}.bootstrap() already called'.format(self.__class__.__name__)
75 )
76 self.__bootstraped = True
77 (sock, port) = random_port()
78- env = random_env(port, oauth)
79+ env = random_env(port, auth)
80 self.server = Server(env)
81 kw = {
82 'port': port,
83@@ -197,14 +213,13 @@
84 'views': self.paths.views,
85 'logfile': self.paths.logfile,
86 'loglevel': loglevel,
87- 'username': env['basic']['username'],
88- 'hashed': couch_hashed(env['basic']['password'], random_salt()),
89 }
90- if oauth:
91+ if auth in ('basic', 'oauth'):
92+ kw['username'] = env['basic']['username']
93+ kw['hashed'] = couch_hashed(env['basic']['password'], random_salt())
94+ if auth == 'oauth':
95 kw.update(env['oauth'])
96- config = OAUTH.format(**kw)
97- else:
98- config = BASIC.format(**kw)
99+ config = get_template(auth).format(**kw)
100 open(self.paths.ini, 'w').write(config)
101 sock.close()
102 self.start()
103
104=== modified file 'usercouch/misc.py'
105--- usercouch/misc.py 2011-10-02 11:34:55 +0000
106+++ usercouch/misc.py 2011-12-08 20:51:27 +0000
107@@ -45,11 +45,11 @@
108
109 class CouchTestCase(TestCase):
110 create_databases = []
111- oauth = False
112+ auth = 'basic'
113
114 def setUp(self):
115 self.tmpcouch = TempCouch()
116- self.env = self.tmpcouch.bootstrap(self.oauth)
117+ self.env = self.tmpcouch.bootstrap(self.auth)
118 for name in self.create_databases:
119 self.tmpcouch.server.put(None, name)
120
121
122=== modified file 'usercouch/tests/__init__.py'
123--- usercouch/tests/__init__.py 2011-10-02 11:11:04 +0000
124+++ usercouch/tests/__init__.py 2011-12-08 20:51:27 +0000
125@@ -81,6 +81,13 @@
126
127
128 class TestFunctions(TestCase):
129+ def test_get_template(self):
130+ self.assertIs(usercouch.get_template('open'), usercouch.OPEN)
131+ self.assertIs(usercouch.get_template('basic'), usercouch.BASIC)
132+ self.assertIs(usercouch.get_template('oauth'), usercouch.OAUTH)
133+ with self.assertRaises(ValueError) as cm:
134+ t = usercouch.get_template('nope')
135+ self.assertEqual(str(cm.exception), "invalid auth: 'nope'")
136
137 def test_random_port(self):
138 (sock, port) = usercouch.random_port()
139@@ -114,8 +121,20 @@
140 self.assertTrue(set(value).issubset(B32ALPHABET))
141
142 def test_random_env(self):
143- # oauth=False
144- env = usercouch.random_env(5634)
145+ # Test with bad auth
146+ with self.assertRaises(ValueError) as cm:
147+ env = usercouch.random_env(5634, 'magic')
148+ self.assertEqual(str(cm.exception), "invalid auth: 'magic'")
149+
150+ # auth='open'
151+ env = usercouch.random_env(5634, 'open')
152+ self.assertIsInstance(env, dict)
153+ self.assertEqual(set(env), set(['port', 'url']))
154+ self.assertEqual(env['port'], 5634)
155+ self.assertEqual(env['url'], 'http://localhost:5634/')
156+
157+ # auth='basic'
158+ env = usercouch.random_env(5634, 'basic')
159 self.assertIsInstance(env, dict)
160 self.assertEqual(set(env), set(['port', 'url', 'basic']))
161 self.assertEqual(env['port'], 5634)
162@@ -130,8 +149,8 @@
163 self.assertEqual(len(value), 24)
164 self.assertTrue(set(value).issubset(B32ALPHABET))
165
166- # oauth=True
167- env = usercouch.random_env(1718, oauth=True)
168+ # auth='oauth'
169+ env = usercouch.random_env(1718, 'oauth')
170 self.assertIsInstance(env, dict)
171 self.assertEqual(set(env), set(['port', 'url', 'basic', 'oauth']))
172 self.assertEqual(env['port'], 1718)
173@@ -317,7 +336,40 @@
174
175 def test_bootstrap(self):
176 #######################
177- # Test with oauth=False
178+ # Test with auth='open'
179+ tmp = TempDir()
180+ uc = usercouch.UserCouch(tmp.dir)
181+ self.assertFalse(path.exists(uc.paths.ini))
182+ env = uc.bootstrap('open')
183+ self.assertTrue(path.isfile(uc.paths.ini))
184+
185+ # check env
186+ self.assertIsInstance(env, dict)
187+ self.assertEqual(set(env), set(['port', 'url']))
188+ port = env['port']
189+ self.assertIsInstance(port, int)
190+ self.assertGreater(port, 1024)
191+ self.assertEqual(env['url'], 'http://localhost:{}/'.format(port))
192+
193+ # check UserCouch.server
194+ self.assertIsInstance(uc.server, microfiber.Server)
195+ self.assertEqual(uc.server.env, env)
196+ self.assertIsNot(uc.server.env, env) # Make sure deepcopy() is used
197+
198+ # check UserCouch.couchdb, make sure UserCouch.start() was called
199+ self.assertIsInstance(uc.couchdb, subprocess.Popen)
200+ self.assertIsNone(uc.couchdb.returncode)
201+
202+ # check that Exception is raised if you call bootstrap() more than once
203+ with self.assertRaises(Exception) as cm:
204+ uc.bootstrap()
205+ self.assertEqual(
206+ str(cm.exception),
207+ 'UserCouch.bootstrap() already called'
208+ )
209+
210+ #######################
211+ # Test with auth='basic'
212 tmp = TempDir()
213 uc = usercouch.UserCouch(tmp.dir)
214 self.assertFalse(path.exists(uc.paths.ini))
215@@ -359,11 +411,11 @@
216 )
217
218 #######################
219- # Test with oauth=True
220+ # Test with auth='oauth'
221 tmp = TempDir()
222 uc = usercouch.UserCouch(tmp.dir)
223 self.assertFalse(path.exists(uc.paths.ini))
224- env = uc.bootstrap(oauth=True)
225+ env = uc.bootstrap(auth='oauth')
226 self.assertTrue(path.isfile(uc.paths.ini))
227
228 # check env
229
230=== modified file 'usercouch/tests/test_misc.py'
231--- usercouch/tests/test_misc.py 2011-10-02 11:40:27 +0000
232+++ usercouch/tests/test_misc.py 2011-12-08 20:51:27 +0000
233@@ -44,12 +44,13 @@
234 tc.__del__()
235 self.assertIsNone(tc.couchdb)
236 self.assertFalse(path.exists(tc.basedir))
237-
238-
239+
240+
241 class SelfTest1(CouchTestCase):
242+ auth = 'open'
243
244 def test_self(self):
245- self.assertEqual(set(self.env), set(['port', 'url', 'basic']))
246+ self.assertEqual(set(self.env), set(['port', 'url']))
247 s = microfiber.Server(self.env)
248 self.assertEqual(
249 s.get('_all_dbs'),
250@@ -58,10 +59,10 @@
251
252
253 class SelfTest2(CouchTestCase):
254- oauth = True
255+ auth = 'basic'
256
257 def test_self(self):
258- self.assertEqual(set(self.env), set(['port', 'url', 'basic', 'oauth']))
259+ self.assertEqual(set(self.env), set(['port', 'url', 'basic']))
260 s = microfiber.Server(self.env)
261 self.assertEqual(
262 s.get('_all_dbs'),
263@@ -70,19 +71,45 @@
264
265
266 class SelfTest3(CouchTestCase):
267- create_databases = ['foo', 'bar']
268+ auth = 'oauth'
269
270 def test_self(self):
271- self.assertEqual(set(self.env), set(['port', 'url', 'basic']))
272+ self.assertEqual(set(self.env), set(['port', 'url', 'basic', 'oauth']))
273 s = microfiber.Server(self.env)
274 self.assertEqual(
275 s.get('_all_dbs'),
276- ['_replicator', '_users', 'bar', 'foo']
277+ ['_replicator', '_users']
278 )
279
280-
281+
282 class SelfTest4(CouchTestCase):
283- oauth = True
284+ auth = 'open'
285+ create_databases = ['foo', 'bar']
286+
287+ def test_self(self):
288+ self.assertEqual(set(self.env), set(['port', 'url']))
289+ s = microfiber.Server(self.env)
290+ self.assertEqual(
291+ s.get('_all_dbs'),
292+ ['_replicator', '_users', 'bar', 'foo']
293+ )
294+
295+
296+class SelfTest5(CouchTestCase):
297+ auth = 'basic'
298+ create_databases = ['foo', 'bar']
299+
300+ def test_self(self):
301+ self.assertEqual(set(self.env), set(['port', 'url', 'basic']))
302+ s = microfiber.Server(self.env)
303+ self.assertEqual(
304+ s.get('_all_dbs'),
305+ ['_replicator', '_users', 'bar', 'foo']
306+ )
307+
308+
309+class SelfTest6(CouchTestCase):
310+ auth = 'oauth'
311 create_databases = ['foo', 'bar']
312
313 def test_self(self):

Subscribers

People subscribed via source and target branches