Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 74 lines (61 sloc) 2.907 kB
193df57 @jehiah add authentication support
jehiah authored
1 import tornado.ioloop
2 import time
3 import logging
4 import subprocess
5
6 import test_shunt
7 import asyncmongo
8
9 TEST_TIMESTAMP = int(time.time())
10
11 class AuthenticationTest(test_shunt.MongoTest):
12 def setUp(self):
13 super(AuthenticationTest, self).setUp()
14 logging.info('creating user')
b6a25cf @jehiah run mongod on non std port for less conflicts
jehiah authored
15 pipe = subprocess.Popen('''echo -e 'use test;\n db.addUser("testuser", "testpass");\n exit;' | mongo --port 27018 --host 127.0.0.1''', shell=True)
193df57 @jehiah add authentication support
jehiah authored
16 pipe.wait()
17
18 def test_authentication(self):
19 try:
20 test_shunt.setup()
09f6433 Clean up callbacks
Denis Bychkov authored
21 db = asyncmongo.Client(pool_id='testauth', host='127.0.0.1', port=27018, dbname='test', dbuser='testuser',
22 dbpass='testpass', maxconnections=2)
193df57 @jehiah add authentication support
jehiah authored
23
24 def update_callback(response, error):
93282ad Add support for replica sets
Denis Bychkov authored
25 logging.info("UPDATE:")
07e56f0 @ajdavis Harden the unittests a little: close connections between tests, and s…
ajdavis authored
26 tornado.ioloop.IOLoop.instance().stop()
193df57 @jehiah add authentication support
jehiah authored
27 logging.info(response)
28 assert len(response) == 1
29 test_shunt.register_called('update')
07e56f0 @ajdavis Harden the unittests a little: close connections between tests, and s…
ajdavis authored
30
09f6433 Clean up callbacks
Denis Bychkov authored
31 db.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, upsert=True,
32 callback=update_callback)
33
193df57 @jehiah add authentication support
jehiah authored
34 tornado.ioloop.IOLoop.instance().start()
35 test_shunt.assert_called('update')
36
37 def query_callback(response, error):
07e56f0 @ajdavis Harden the unittests a little: close connections between tests, and s…
ajdavis authored
38 tornado.ioloop.IOLoop.instance().stop()
193df57 @jehiah add authentication support
jehiah authored
39 logging.info(response)
40 logging.info(error)
41 assert error is None
42 assert isinstance(response, dict)
43 assert response['_id'] == TEST_TIMESTAMP
44 assert response['test_count'] == 1
45 test_shunt.register_called('retrieved')
07e56f0 @ajdavis Harden the unittests a little: close connections between tests, and s…
ajdavis authored
46
193df57 @jehiah add authentication support
jehiah authored
47 db.test_stats.find_one({"_id" : TEST_TIMESTAMP}, callback=query_callback)
48 tornado.ioloop.IOLoop.instance().start()
49 test_shunt.assert_called('retrieved')
50 except:
51 tornado.ioloop.IOLoop.instance().stop()
52 raise
53
09f6433 Clean up callbacks
Denis Bychkov authored
54 def test_failed_auth(self):
55 try:
56 test_shunt.setup()
57 db = asyncmongo.Client(pool_id='testauth_f', host='127.0.0.1', port=27018, dbname='test', dbuser='testuser',
58 dbpass='wrong', maxconnections=2)
59
60 def query_callback(response, error):
61 tornado.ioloop.IOLoop.instance().stop()
62 logging.info(response)
63 logging.info(error)
64 assert isinstance(error, asyncmongo.AuthenticationError)
65 assert response is None
66 test_shunt.register_called('auth_failed')
67
68 db.test_stats.find_one({"_id" : TEST_TIMESTAMP}, callback=query_callback)
69 tornado.ioloop.IOLoop.instance().start()
70 test_shunt.assert_called('auth_failed')
71 except:
72 tornado.ioloop.IOLoop.instance().stop()
73 raise
Something went wrong with that request. Please try again.