Skip to content

Commit

Permalink
Added support to isAdmin argument on user database creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
puentesarrin committed Jan 5, 2015
1 parent 0ed2136 commit c480531
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
7 changes: 4 additions & 3 deletions asyncflux/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ def get_users(self):
raise gen.Return(users)

@asyncflux_coroutine
def create_user(self, username, password, read_from='.*', write_to='.*'):
payload = {'name': username, 'password': password,
def create_user(self, username, password, is_admin=False, read_from='.*',
write_to='.*'):
payload = {'name': username, 'password': password, 'isAdmin': is_admin,
'readFrom': read_from, 'writeTo': write_to}
yield self.client.request('/db/%(database)s/users',
{'database': self.name}, method='POST',
body=payload)
new_user = user.User(self, username, is_admin=False,
new_user = user.User(self, username, is_admin=is_admin,
read_from=read_from, write_to=write_to)
raise gen.Return(new_user)

Expand Down
29 changes: 18 additions & 11 deletions tests/database_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,59 +78,66 @@ def test_create_user(self):
db = client[db_name]
username = 'foo'
password = 'fubar'
is_admin = True
read_from = '.*'
write_to = '.*'

payload = {'name': username, 'password': password, 'isAdmin': is_admin,
'readFrom': read_from, 'writeTo': write_to}
with self.patch_fetch_mock(client) as m:
self.setup_fetch_mock(m, 200)
response = yield db.create_user(username, password)
response = yield db.create_user(username, password,
is_admin=is_admin,
read_from=read_from,
write_to=write_to)
self.assertIsInstance(response, User)
self.assertEqual(response.name, username)

payload = {'name': username, 'password': password,
'readFrom': read_from, 'writeTo': write_to}
self.assert_mock_args(m, '/db/%s/users' % db_name, method='POST',
body=json.dumps(payload))

# Existing database user
payload = {'name': username, 'password': password, 'isAdmin': is_admin,
'readFrom': read_from, 'writeTo': write_to}
response_body = 'User %s already exists' % username
with self.patch_fetch_mock(client) as m:
self.setup_fetch_mock(m, 400, body=response_body)
with self.assertRaisesRegexp(AsyncfluxError, response_body):
yield db.create_user(username, password)
yield db.create_user(username, password, is_admin=is_admin,
read_from=read_from, write_to=write_to)

payload = {'name': username, 'password': password,
'readFrom': read_from, 'writeTo': write_to}
self.assert_mock_args(m, '/db/%s/users' % db_name, method='POST',
body=json.dumps(payload))

# Invalid password
password = 'bar'
payload = {'name': username, 'password': password, 'isAdmin': is_admin,
'readFrom': read_from, 'writeTo': write_to}
response_body = ('Password must be more than 4 and less than 56 '
'characters')
with self.patch_fetch_mock(client) as m:
self.setup_fetch_mock(m, 400, body=response_body)
with self.assertRaisesRegexp(AsyncfluxError, response_body):
yield db.create_user(username, password)
yield db.create_user(username, password, is_admin=is_admin,
read_from=read_from, write_to=write_to)

payload = {'name': username, 'password': password,
'readFrom': read_from, 'writeTo': write_to}
self.assert_mock_args(m, '/db/%s/users' % db_name, method='POST',
body=json.dumps(payload))

# Non-default permissions
read_from = '^$'
write_to = '^$'
payload = {'name': username, 'password': password, 'isAdmin': is_admin,
'readFrom': read_from, 'writeTo': write_to}
with self.patch_fetch_mock(client) as m:
self.setup_fetch_mock(m, 200)
response = yield db.create_user(username, password,
is_admin=is_admin,
read_from=read_from,
write_to=write_to)
self.assertIsInstance(response, User)
self.assertEqual(response.name, username)

payload = {'name': username, 'password': password,
'readFrom': read_from, 'writeTo': write_to}
self.assert_mock_args(m, '/db/%s/users' % db_name, method='POST',
body=json.dumps(payload))

Expand Down

0 comments on commit c480531

Please sign in to comment.