Merge lp:~jderose/usercouch/no-auth into lp:usercouch
- no-auth
- Merge into trunk
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 |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Akshat Jain (community) | Approve | ||
Novacut Dev | Pending | ||
Review via email:
|
Commit message
Description of the change
UserCouch.
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
![](/+icing/build/overlay/assets/skins/sam/images/close.gif)
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 | 37 | 37 | ||
6 | 38 | __version__ = '11.12.0' | 38 | __version__ = '11.12.0' |
7 | 39 | 39 | ||
12 | 40 | BASIC = """ | 40 | OPEN = """ |
9 | 41 | [couch_httpd_auth] | ||
10 | 42 | require_valid_user = true | ||
11 | 43 | |||
13 | 44 | [httpd] | 41 | [httpd] |
14 | 45 | bind_address = 127.0.0.1 | 42 | bind_address = 127.0.0.1 |
15 | 46 | port = {port} | 43 | port = {port} |
16 | @@ -64,6 +61,11 @@ | |||
17 | 64 | [stats] | 61 | [stats] |
18 | 65 | rate = | 62 | rate = |
19 | 66 | samples = | 63 | samples = |
20 | 64 | """ | ||
21 | 65 | |||
22 | 66 | BASIC = OPEN + """ | ||
23 | 67 | [couch_httpd_auth] | ||
24 | 68 | require_valid_user = true | ||
25 | 67 | 69 | ||
26 | 68 | [admins] | 70 | [admins] |
27 | 69 | {username} = {hashed} | 71 | {username} = {hashed} |
28 | @@ -81,6 +83,16 @@ | |||
29 | 81 | """ | 83 | """ |
30 | 82 | 84 | ||
31 | 83 | 85 | ||
32 | 86 | def get_template(auth): | ||
33 | 87 | if auth == 'open': | ||
34 | 88 | return OPEN | ||
35 | 89 | if auth == 'basic': | ||
36 | 90 | return BASIC | ||
37 | 91 | if auth == 'oauth': | ||
38 | 92 | return OAUTH | ||
39 | 93 | raise ValueError('invalid auth: {!r}'.format(auth)) | ||
40 | 94 | |||
41 | 95 | |||
42 | 84 | def random_port(): | 96 | def random_port(): |
43 | 85 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | 97 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
44 | 86 | sock.bind(('127.0.0.1', 0)) | 98 | sock.bind(('127.0.0.1', 0)) |
45 | @@ -102,13 +114,17 @@ | |||
46 | 102 | ) | 114 | ) |
47 | 103 | 115 | ||
48 | 104 | 116 | ||
50 | 105 | def random_env(port, oauth=False): | 117 | def random_env(port, auth): |
51 | 118 | if auth not in ('open', 'basic', 'oauth'): | ||
52 | 119 | raise ValueError('invalid auth: {!r}'.format(auth)) | ||
53 | 106 | env = { | 120 | env = { |
54 | 107 | 'port': port, | 121 | 'port': port, |
55 | 108 | 'url': 'http://localhost:{}/'.format(port), | 122 | 'url': 'http://localhost:{}/'.format(port), |
56 | 109 | 'basic': random_basic(), | ||
57 | 110 | } | 123 | } |
59 | 111 | if oauth: | 124 | if auth == 'basic': |
60 | 125 | env['basic'] = random_basic() | ||
61 | 126 | elif auth == 'oauth': | ||
62 | 127 | env['basic'] = random_basic() | ||
63 | 112 | env['oauth'] = random_oauth() | 128 | env['oauth'] = random_oauth() |
64 | 113 | return env | 129 | return env |
65 | 114 | 130 | ||
66 | @@ -182,14 +198,14 @@ | |||
67 | 182 | def __del__(self): | 198 | def __del__(self): |
68 | 183 | self.kill() | 199 | self.kill() |
69 | 184 | 200 | ||
71 | 185 | def bootstrap(self, oauth=False, loglevel='notice'): | 201 | def bootstrap(self, auth='basic', loglevel='notice'): |
72 | 186 | if self.__bootstraped: | 202 | if self.__bootstraped: |
73 | 187 | raise Exception( | 203 | raise Exception( |
74 | 188 | '{}.bootstrap() already called'.format(self.__class__.__name__) | 204 | '{}.bootstrap() already called'.format(self.__class__.__name__) |
75 | 189 | ) | 205 | ) |
76 | 190 | self.__bootstraped = True | 206 | self.__bootstraped = True |
77 | 191 | (sock, port) = random_port() | 207 | (sock, port) = random_port() |
79 | 192 | env = random_env(port, oauth) | 208 | env = random_env(port, auth) |
80 | 193 | self.server = Server(env) | 209 | self.server = Server(env) |
81 | 194 | kw = { | 210 | kw = { |
82 | 195 | 'port': port, | 211 | 'port': port, |
83 | @@ -197,14 +213,13 @@ | |||
84 | 197 | 'views': self.paths.views, | 213 | 'views': self.paths.views, |
85 | 198 | 'logfile': self.paths.logfile, | 214 | 'logfile': self.paths.logfile, |
86 | 199 | 'loglevel': loglevel, | 215 | 'loglevel': loglevel, |
87 | 200 | 'username': env['basic']['username'], | ||
88 | 201 | 'hashed': couch_hashed(env['basic']['password'], random_salt()), | ||
89 | 202 | } | 216 | } |
91 | 203 | if oauth: | 217 | if auth in ('basic', 'oauth'): |
92 | 218 | kw['username'] = env['basic']['username'] | ||
93 | 219 | kw['hashed'] = couch_hashed(env['basic']['password'], random_salt()) | ||
94 | 220 | if auth == 'oauth': | ||
95 | 204 | kw.update(env['oauth']) | 221 | kw.update(env['oauth']) |
99 | 205 | config = OAUTH.format(**kw) | 222 | config = get_template(auth).format(**kw) |
97 | 206 | else: | ||
98 | 207 | config = BASIC.format(**kw) | ||
100 | 208 | open(self.paths.ini, 'w').write(config) | 223 | open(self.paths.ini, 'w').write(config) |
101 | 209 | sock.close() | 224 | sock.close() |
102 | 210 | self.start() | 225 | self.start() |
103 | 211 | 226 | ||
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 | 45 | 45 | ||
109 | 46 | class CouchTestCase(TestCase): | 46 | class CouchTestCase(TestCase): |
110 | 47 | create_databases = [] | 47 | create_databases = [] |
112 | 48 | oauth = False | 48 | auth = 'basic' |
113 | 49 | 49 | ||
114 | 50 | def setUp(self): | 50 | def setUp(self): |
115 | 51 | self.tmpcouch = TempCouch() | 51 | self.tmpcouch = TempCouch() |
117 | 52 | self.env = self.tmpcouch.bootstrap(self.oauth) | 52 | self.env = self.tmpcouch.bootstrap(self.auth) |
118 | 53 | for name in self.create_databases: | 53 | for name in self.create_databases: |
119 | 54 | self.tmpcouch.server.put(None, name) | 54 | self.tmpcouch.server.put(None, name) |
120 | 55 | 55 | ||
121 | 56 | 56 | ||
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 | 81 | 81 | ||
127 | 82 | 82 | ||
128 | 83 | class TestFunctions(TestCase): | 83 | class TestFunctions(TestCase): |
129 | 84 | def test_get_template(self): | ||
130 | 85 | self.assertIs(usercouch.get_template('open'), usercouch.OPEN) | ||
131 | 86 | self.assertIs(usercouch.get_template('basic'), usercouch.BASIC) | ||
132 | 87 | self.assertIs(usercouch.get_template('oauth'), usercouch.OAUTH) | ||
133 | 88 | with self.assertRaises(ValueError) as cm: | ||
134 | 89 | t = usercouch.get_template('nope') | ||
135 | 90 | self.assertEqual(str(cm.exception), "invalid auth: 'nope'") | ||
136 | 84 | 91 | ||
137 | 85 | def test_random_port(self): | 92 | def test_random_port(self): |
138 | 86 | (sock, port) = usercouch.random_port() | 93 | (sock, port) = usercouch.random_port() |
139 | @@ -114,8 +121,20 @@ | |||
140 | 114 | self.assertTrue(set(value).issubset(B32ALPHABET)) | 121 | self.assertTrue(set(value).issubset(B32ALPHABET)) |
141 | 115 | 122 | ||
142 | 116 | def test_random_env(self): | 123 | def test_random_env(self): |
145 | 117 | # oauth=False | 124 | # Test with bad auth |
146 | 118 | env = usercouch.random_env(5634) | 125 | with self.assertRaises(ValueError) as cm: |
147 | 126 | env = usercouch.random_env(5634, 'magic') | ||
148 | 127 | self.assertEqual(str(cm.exception), "invalid auth: 'magic'") | ||
149 | 128 | |||
150 | 129 | # auth='open' | ||
151 | 130 | env = usercouch.random_env(5634, 'open') | ||
152 | 131 | self.assertIsInstance(env, dict) | ||
153 | 132 | self.assertEqual(set(env), set(['port', 'url'])) | ||
154 | 133 | self.assertEqual(env['port'], 5634) | ||
155 | 134 | self.assertEqual(env['url'], 'http://localhost:5634/') | ||
156 | 135 | |||
157 | 136 | # auth='basic' | ||
158 | 137 | env = usercouch.random_env(5634, 'basic') | ||
159 | 119 | self.assertIsInstance(env, dict) | 138 | self.assertIsInstance(env, dict) |
160 | 120 | self.assertEqual(set(env), set(['port', 'url', 'basic'])) | 139 | self.assertEqual(set(env), set(['port', 'url', 'basic'])) |
161 | 121 | self.assertEqual(env['port'], 5634) | 140 | self.assertEqual(env['port'], 5634) |
162 | @@ -130,8 +149,8 @@ | |||
163 | 130 | self.assertEqual(len(value), 24) | 149 | self.assertEqual(len(value), 24) |
164 | 131 | self.assertTrue(set(value).issubset(B32ALPHABET)) | 150 | self.assertTrue(set(value).issubset(B32ALPHABET)) |
165 | 132 | 151 | ||
168 | 133 | # oauth=True | 152 | # auth='oauth' |
169 | 134 | env = usercouch.random_env(1718, oauth=True) | 153 | env = usercouch.random_env(1718, 'oauth') |
170 | 135 | self.assertIsInstance(env, dict) | 154 | self.assertIsInstance(env, dict) |
171 | 136 | self.assertEqual(set(env), set(['port', 'url', 'basic', 'oauth'])) | 155 | self.assertEqual(set(env), set(['port', 'url', 'basic', 'oauth'])) |
172 | 137 | self.assertEqual(env['port'], 1718) | 156 | self.assertEqual(env['port'], 1718) |
173 | @@ -317,7 +336,40 @@ | |||
174 | 317 | 336 | ||
175 | 318 | def test_bootstrap(self): | 337 | def test_bootstrap(self): |
176 | 319 | ####################### | 338 | ####################### |
178 | 320 | # Test with oauth=False | 339 | # Test with auth='open' |
179 | 340 | tmp = TempDir() | ||
180 | 341 | uc = usercouch.UserCouch(tmp.dir) | ||
181 | 342 | self.assertFalse(path.exists(uc.paths.ini)) | ||
182 | 343 | env = uc.bootstrap('open') | ||
183 | 344 | self.assertTrue(path.isfile(uc.paths.ini)) | ||
184 | 345 | |||
185 | 346 | # check env | ||
186 | 347 | self.assertIsInstance(env, dict) | ||
187 | 348 | self.assertEqual(set(env), set(['port', 'url'])) | ||
188 | 349 | port = env['port'] | ||
189 | 350 | self.assertIsInstance(port, int) | ||
190 | 351 | self.assertGreater(port, 1024) | ||
191 | 352 | self.assertEqual(env['url'], 'http://localhost:{}/'.format(port)) | ||
192 | 353 | |||
193 | 354 | # check UserCouch.server | ||
194 | 355 | self.assertIsInstance(uc.server, microfiber.Server) | ||
195 | 356 | self.assertEqual(uc.server.env, env) | ||
196 | 357 | self.assertIsNot(uc.server.env, env) # Make sure deepcopy() is used | ||
197 | 358 | |||
198 | 359 | # check UserCouch.couchdb, make sure UserCouch.start() was called | ||
199 | 360 | self.assertIsInstance(uc.couchdb, subprocess.Popen) | ||
200 | 361 | self.assertIsNone(uc.couchdb.returncode) | ||
201 | 362 | |||
202 | 363 | # check that Exception is raised if you call bootstrap() more than once | ||
203 | 364 | with self.assertRaises(Exception) as cm: | ||
204 | 365 | uc.bootstrap() | ||
205 | 366 | self.assertEqual( | ||
206 | 367 | str(cm.exception), | ||
207 | 368 | 'UserCouch.bootstrap() already called' | ||
208 | 369 | ) | ||
209 | 370 | |||
210 | 371 | ####################### | ||
211 | 372 | # Test with auth='basic' | ||
212 | 321 | tmp = TempDir() | 373 | tmp = TempDir() |
213 | 322 | uc = usercouch.UserCouch(tmp.dir) | 374 | uc = usercouch.UserCouch(tmp.dir) |
214 | 323 | self.assertFalse(path.exists(uc.paths.ini)) | 375 | self.assertFalse(path.exists(uc.paths.ini)) |
215 | @@ -359,11 +411,11 @@ | |||
216 | 359 | ) | 411 | ) |
217 | 360 | 412 | ||
218 | 361 | ####################### | 413 | ####################### |
220 | 362 | # Test with oauth=True | 414 | # Test with auth='oauth' |
221 | 363 | tmp = TempDir() | 415 | tmp = TempDir() |
222 | 364 | uc = usercouch.UserCouch(tmp.dir) | 416 | uc = usercouch.UserCouch(tmp.dir) |
223 | 365 | self.assertFalse(path.exists(uc.paths.ini)) | 417 | self.assertFalse(path.exists(uc.paths.ini)) |
225 | 366 | env = uc.bootstrap(oauth=True) | 418 | env = uc.bootstrap(auth='oauth') |
226 | 367 | self.assertTrue(path.isfile(uc.paths.ini)) | 419 | self.assertTrue(path.isfile(uc.paths.ini)) |
227 | 368 | 420 | ||
228 | 369 | # check env | 421 | # check env |
229 | 370 | 422 | ||
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 | 44 | tc.__del__() | 44 | tc.__del__() |
235 | 45 | self.assertIsNone(tc.couchdb) | 45 | self.assertIsNone(tc.couchdb) |
236 | 46 | self.assertFalse(path.exists(tc.basedir)) | 46 | self.assertFalse(path.exists(tc.basedir)) |
239 | 47 | 47 | ||
240 | 48 | 48 | ||
241 | 49 | class SelfTest1(CouchTestCase): | 49 | class SelfTest1(CouchTestCase): |
242 | 50 | auth = 'open' | ||
243 | 50 | 51 | ||
244 | 51 | def test_self(self): | 52 | def test_self(self): |
246 | 52 | self.assertEqual(set(self.env), set(['port', 'url', 'basic'])) | 53 | self.assertEqual(set(self.env), set(['port', 'url'])) |
247 | 53 | s = microfiber.Server(self.env) | 54 | s = microfiber.Server(self.env) |
248 | 54 | self.assertEqual( | 55 | self.assertEqual( |
249 | 55 | s.get('_all_dbs'), | 56 | s.get('_all_dbs'), |
250 | @@ -58,10 +59,10 @@ | |||
251 | 58 | 59 | ||
252 | 59 | 60 | ||
253 | 60 | class SelfTest2(CouchTestCase): | 61 | class SelfTest2(CouchTestCase): |
255 | 61 | oauth = True | 62 | auth = 'basic' |
256 | 62 | 63 | ||
257 | 63 | def test_self(self): | 64 | def test_self(self): |
259 | 64 | self.assertEqual(set(self.env), set(['port', 'url', 'basic', 'oauth'])) | 65 | self.assertEqual(set(self.env), set(['port', 'url', 'basic'])) |
260 | 65 | s = microfiber.Server(self.env) | 66 | s = microfiber.Server(self.env) |
261 | 66 | self.assertEqual( | 67 | self.assertEqual( |
262 | 67 | s.get('_all_dbs'), | 68 | s.get('_all_dbs'), |
263 | @@ -70,19 +71,45 @@ | |||
264 | 70 | 71 | ||
265 | 71 | 72 | ||
266 | 72 | class SelfTest3(CouchTestCase): | 73 | class SelfTest3(CouchTestCase): |
268 | 73 | create_databases = ['foo', 'bar'] | 74 | auth = 'oauth' |
269 | 74 | 75 | ||
270 | 75 | def test_self(self): | 76 | def test_self(self): |
272 | 76 | self.assertEqual(set(self.env), set(['port', 'url', 'basic'])) | 77 | self.assertEqual(set(self.env), set(['port', 'url', 'basic', 'oauth'])) |
273 | 77 | s = microfiber.Server(self.env) | 78 | s = microfiber.Server(self.env) |
274 | 78 | self.assertEqual( | 79 | self.assertEqual( |
275 | 79 | s.get('_all_dbs'), | 80 | s.get('_all_dbs'), |
277 | 80 | ['_replicator', '_users', 'bar', 'foo'] | 81 | ['_replicator', '_users'] |
278 | 81 | ) | 82 | ) |
279 | 82 | 83 | ||
281 | 83 | 84 | ||
282 | 84 | class SelfTest4(CouchTestCase): | 85 | class SelfTest4(CouchTestCase): |
284 | 85 | oauth = True | 86 | auth = 'open' |
285 | 87 | create_databases = ['foo', 'bar'] | ||
286 | 88 | |||
287 | 89 | def test_self(self): | ||
288 | 90 | self.assertEqual(set(self.env), set(['port', 'url'])) | ||
289 | 91 | s = microfiber.Server(self.env) | ||
290 | 92 | self.assertEqual( | ||
291 | 93 | s.get('_all_dbs'), | ||
292 | 94 | ['_replicator', '_users', 'bar', 'foo'] | ||
293 | 95 | ) | ||
294 | 96 | |||
295 | 97 | |||
296 | 98 | class SelfTest5(CouchTestCase): | ||
297 | 99 | auth = 'basic' | ||
298 | 100 | create_databases = ['foo', 'bar'] | ||
299 | 101 | |||
300 | 102 | def test_self(self): | ||
301 | 103 | self.assertEqual(set(self.env), set(['port', 'url', 'basic'])) | ||
302 | 104 | s = microfiber.Server(self.env) | ||
303 | 105 | self.assertEqual( | ||
304 | 106 | s.get('_all_dbs'), | ||
305 | 107 | ['_replicator', '_users', 'bar', 'foo'] | ||
306 | 108 | ) | ||
307 | 109 | |||
308 | 110 | |||
309 | 111 | class SelfTest6(CouchTestCase): | ||
310 | 112 | auth = 'oauth' | ||
311 | 86 | create_databases = ['foo', 'bar'] | 113 | create_databases = ['foo', 'bar'] |
312 | 87 | 114 | ||
313 | 88 | def test_self(self): | 115 | def test_self(self): |