Beispiel #1
0
    def test_create_database_cps(self):
        client = AsyncfluxClient()
        db_name = 'foo'

        # Using a string
        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 201)
            client.create_database(db_name, callback=self.stop_op)
            response = self.wait()
            self.assertIsInstance(response, Database)
            self.assertEqual(response.name, db_name)

            self.assert_mock_args(m, '/db', method='POST',
                                  body=json.dumps({'name': db_name}))

        # Existing database
        response_body = 'database %s exists' % db_name
        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 409, body=response_body)
            with self.assertRaisesRegexp(AsyncfluxError, response_body):
                client.create_database(db_name, callback=self.stop_op)
                response = self.wait()
                self.assertEqual(response, response_body)

            self.assert_mock_args(m, '/db', method='POST',
                                  body=json.dumps({'name': db_name}))
Beispiel #2
0
    def test_authenticate_cluster_admin(self):
        client = AsyncfluxClient()
        username = '******'
        password = '******'

        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 200)
            response = yield client.authenticate_cluster_admin(username,
                                                               password)
            self.assertTrue(response)

            self.assert_mock_args(m, '/cluster_admins/authenticate',
                                  auth_username=username,
                                  auth_password=password)

        # Invalid credentials
        response_body = 'Invalid username/password'
        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 401, body=response_body)
            response = yield client.authenticate_cluster_admin(username,
                                                               password)
            self.assertFalse(response)

            self.assert_mock_args(m, '/cluster_admins/authenticate',
                                  auth_username=username,
                                  auth_password=password)
    def test_get_shard_spaces(self):
        client = AsyncfluxClient()
        shard_spaces = [{'name': 'default', 'database': 'foo', 'regex': '/.*/',
                         'retentionPolicy': 'inf', 'shardDuration': '7d',
                         'replicationFactor': 1, 'split': 1},
                        {'name': 'second', 'database': 'bar', 'regex': '/.*/',
                         'retentionPolicy': '365d', 'shardDuration': '15m',
                         'replicationFactor': 5, 'split': 5},
                        {'name': 'default', 'database': 'bar', 'regex': '/.*/',
                         'retentionPolicy': 'inf', 'shardDuration': '7d',
                         'replicationFactor': 1, 'split': 1}]

        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 200, body=shard_spaces)
            response = yield client.get_shard_spaces()
            for actual, expected in zip(response, shard_spaces):
                self.assertIsInstance(actual, ShardSpace)
                self.assertEqual(actual.client, client)
                self.assertEqual(actual.name, expected['name'])
                self.assertIsInstance(actual.database, Database)
                self.assertEqual(actual.database.name, expected['database'])
                self.assertEqual(actual.regex, expected['regex'])
                self.assertEqual(actual.retention_policy,
                                 expected['retentionPolicy'])
                self.assertEqual(actual.shard_duration,
                                 expected['shardDuration'])
                self.assertEqual(actual.replication_factor,
                                 expected['replicationFactor'])
                self.assertEqual(actual.split, expected['split'])

                self.assert_mock_args(m, '/cluster/shard_spaces')
Beispiel #4
0
    def test_credential_properties_setters(self):
        client = AsyncfluxClient(username='******', password='******')
        username = '******'
        password = '******'
        client.username = username
        client.password = password

        self.assertEqual(client.username, username)
        self.assertEqual(client.password, password)
Beispiel #5
0
    def test_ping(self):
        client = AsyncfluxClient()
        response_body = {'status': 'ok'}

        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 200, body=response_body)
            response = yield client.ping()
            self.assertEqual(response, response_body)

            self.assert_mock_args(m, '/ping')
Beispiel #6
0
    def test_get_database_names(self):
        client = AsyncfluxClient()
        databases = [{'name': 'foo'}, {'name': 'bar'}]
        db_names = [db['name'] for db in databases]

        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 200, body=databases)
            response = yield client.get_database_names()
            self.assertEqual(response, db_names)

            self.assert_mock_args(m, '/db')
Beispiel #7
0
    def test_get_cluster_admin_names(self):
        client = AsyncfluxClient()
        admins = [{'name': 'foo'}, {'name': 'bar'}]
        admin_names = [a['name'] for a in admins]

        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 200, body=admins)
            response = yield client.get_cluster_admin_names()
            self.assertListEqual(response, admin_names)

            self.assert_mock_args(m, '/cluster_admins')
Beispiel #8
0
    def test_get_cluster_admins(self):
        client = AsyncfluxClient()
        admins = [{'name': 'foo'}, {'name': 'bar'}]
        admin_names = [a['name'] for a in admins]

        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 200, body=admins)
            response = yield client.get_cluster_admins()
            self.assertEqual(len(response), len(admin_names))
            for r in response:
                self.assertIsInstance(r, ClusterAdmin)
                self.assertIn(r.name, admin_names)

            self.assert_mock_args(m, '/cluster_admins')
Beispiel #9
0
    def test_create_database(self):
        client = AsyncfluxClient()
        db_name = 'foo'

        # Using a string
        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 201)
            response = yield client.create_database(db_name)
            self.assertIsInstance(response, Database)
            self.assertEqual(response.name, db_name)

            self.assert_mock_args(m, '/db', method='POST',
                                  body=json.dumps({'name': db_name}))

        # Using an instance of Database
        db = Database(client, db_name)
        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 201)
            response = yield client.create_database(db)
            self.assertIsInstance(response, Database)
            self.assertEqual(response.name, db_name)

            self.assert_mock_args(m, '/db', method='POST',
                                  body=json.dumps({'name': db_name}))

        # Using an unsupported type
        with self.patch_fetch_mock(client) as m:
            re_exc_msg = r'^name_or_database must be an instance'
            with self.assertRaisesRegexp(TypeError, re_exc_msg):
                yield client.create_database(None)
            self.assertFalse(m.called)

        # Existing database
        response_body = 'database %s exists' % db_name
        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 409, body=response_body)
            with self.assertRaisesRegexp(AsyncfluxError, response_body):
                response = yield client.create_database(db_name)
                self.assertEqual(response, response_body)

            self.assert_mock_args(m, '/db', method='POST',
                                  body=json.dumps({'name': db_name}))
Beispiel #10
0
    def test_delete_cluster_admin(self):
        client = AsyncfluxClient()
        username = '******'

        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 200)
            response = yield client.delete_cluster_admin(username)
            self.assertIsNone(response)

            self.assert_mock_args(m, '/cluster_admins/%s' % username,
                                  method='DELETE')

        # Non-existing cluster admin
        response_body = "User %s doesn't 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 client.delete_cluster_admin(username)

            self.assert_mock_args(m, '/cluster_admins/%s' % username,
                                  method='DELETE')
Beispiel #11
0
    def test_change_cluster_admin_password(self):
        client = AsyncfluxClient()
        username = '******'
        password = '******'

        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 200)
            response = yield client.change_cluster_admin_password(username,
                                                                  password)
            self.assertIsNone(response)

            self.assert_mock_args(m, '/cluster_admins/%s' % username,
                                  method='POST',
                                  body=json.dumps({'password': password}))

        # Non-existing cluster admin
        response_body = 'Invalid user name %s' % 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 client.change_cluster_admin_password(username, password)

            self.assert_mock_args(m, '/cluster_admins/%s' % username,
                                  method='POST',
                                  body=json.dumps({'password': password}))

        # Invalid password
        password = '******'
        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 client.change_cluster_admin_password(username, password)

            self.assert_mock_args(m, '/cluster_admins/%s' % username,
                                  method='POST',
                                  body=json.dumps({'password': password}))
Beispiel #12
0
    def test_create_cluster_admin(self):
        client = AsyncfluxClient()
        username = '******'
        password = '******'

        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 200)
            response = yield client.create_cluster_admin(username, password)
            self.assertIsInstance(response, ClusterAdmin)
            self.assertEqual(response.name, username)

            self.assert_mock_args(m, '/cluster_admins', method='POST',
                                  body=json.dumps({'name': username,
                                                   'password': password}))

        # Existing cluster admin
        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 client.create_cluster_admin(username, password)

            self.assert_mock_args(m, '/cluster_admins', method='POST',
                                  body=json.dumps({'name': username,
                                                   'password': password}))

        # Invalid password
        password = '******'
        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 client.create_cluster_admin(username, password)

            self.assert_mock_args(m, '/cluster_admins', method='POST',
                                  body=json.dumps({'name': username,
                                                   'password': password}))
Beispiel #13
0
    def test_delete_database(self):
        client = AsyncfluxClient()
        db_name = 'foo'

        # Using a string
        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 204)
            response = yield client.delete_database(db_name)
            self.assertIsNone(response)

            self.assert_mock_args(m, '/db/%s' % db_name, method='DELETE')

        # Using an instance of Database
        db = Database(client, db_name)
        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 204)
            response = yield client.delete_database(db)
            self.assertIsNone(response)

            self.assert_mock_args(m, '/db/%s' % db_name, method='DELETE')

        # Using an unsupported type
        with self.patch_fetch_mock(client) as m:
            re_exc_msg = r'^name_or_database must be an instance'
            with self.assertRaisesRegexp(TypeError, re_exc_msg):
                yield client.delete_database(None)
            self.assertFalse(m.called)

        # Non-existing database
        response_body = "Database %s doesn't exist" % db_name
        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 400, body=response_body)
            with self.assertRaisesRegexp(AsyncfluxError, response_body):
                yield client.delete_database(db_name)

            self.assert_mock_args(m, '/db/%s' % db_name, method='DELETE')
Beispiel #14
0
    def test_change_user_permissions(self):
        client = AsyncfluxClient()
        db_name = 'foo'
        db = client[db_name]
        username = '******'
        read_from = '^$'
        write_to = '^$'

        # Update permissions
        payload = {'readFrom': read_from, 'writeTo': write_to}
        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 200)
            response = yield db.change_user_permissions(username,
                                                        read_from=read_from,
                                                        write_to=write_to)
            self.assertIsNone(response)

            self.assert_mock_args(m,
                                  '/db/%s/users/%s' % (db_name, username),
                                  method='POST',
                                  body=json.dumps(payload))

        # Non-existing user
        response_body = "Invalid username %s" % 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.change_user_permissions(username,
                                                 read_from=read_from,
                                                 write_to=write_to)

            self.assert_mock_args(m,
                                  '/db/%s/users/%s' % (db_name, username),
                                  method='POST',
                                  body=json.dumps(payload))

        # Invalid permission argument values
        exc_msg = 'You have to provide read and write permissions'
        with self.assertRaisesRegexp(ValueError, exc_msg):
            yield db.change_user_permissions(username, None, None)

        with self.assertRaisesRegexp(ValueError, exc_msg):
            yield db.change_user_permissions(username, read_from, None)

        with self.assertRaisesRegexp(ValueError, exc_msg):
            yield db.change_user_permissions(username, None, write_to)
Beispiel #15
0
    def test_change_user_password(self):
        client = AsyncfluxClient()
        db_name = 'foo'
        db = client[db_name]
        username = '******'
        password = '******'

        payload = {'password': password}
        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 200)
            response = yield db.change_user_password(username, password)
            self.assertIsNone(response)

            self.assert_mock_args(m,
                                  '/db/%s/users/%s' % (db_name, username),
                                  method='POST',
                                  body=json.dumps(payload))

        # Non-existing user
        response_body = "Invalid username %s" % 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.change_user_password(username, password)

            self.assert_mock_args(m,
                                  '/db/%s/users/%s' % (db_name, username),
                                  method='POST',
                                  body=json.dumps(payload))

        # Invalid password
        password = '******'
        payload = {'password': password}
        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.change_user_password(username, password)

            self.assert_mock_args(m,
                                  '/db/%s/users/%s' % (db_name, username),
                                  method='POST',
                                  body=json.dumps(payload))
Beispiel #16
0
    def test_delete(self):
        client = AsyncfluxClient()
        db_name = 'foo'
        db = Database(client, db_name)

        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 204)
            response = yield db.delete()
            self.assertIsNone(response)

            self.assert_mock_args(m, '/db/%s' % db_name, method='DELETE')

        # Non-existing databse
        response_body = "Database %s doesn't exist" % db_name
        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.delete()

            self.assert_mock_args(m, '/db/%s' % db_name, method='DELETE')
Beispiel #17
0
    def test_create_database(self):
        client = AsyncfluxClient()
        db_name = 'foo'

        # Using a string
        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 201)
            response = yield client.create_database(db_name)
            self.assertIsInstance(response, Database)
            self.assertEqual(response.name, db_name)

            self.assert_mock_args(m,
                                  '/db',
                                  method='POST',
                                  body=json.dumps({'name': db_name}))

        # Using an instance of Database
        db = Database(client, db_name)
        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 201)
            response = yield client.create_database(db)
            self.assertIsInstance(response, Database)
            self.assertEqual(response.name, db_name)

            self.assert_mock_args(m,
                                  '/db',
                                  method='POST',
                                  body=json.dumps({'name': db_name}))

        # Using an unsupported type
        with self.patch_fetch_mock(client) as m:
            re_exc_msg = r'^name_or_database must be an instance'
            with self.assertRaisesRegexp(TypeError, re_exc_msg):
                yield client.create_database(None)
            self.assertFalse(m.called)

        # Existing database
        response_body = 'database %s exists' % db_name
        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 409, body=response_body)
            with self.assertRaisesRegexp(AsyncfluxError, response_body):
                response = yield client.create_database(db_name)
                self.assertEqual(response, response_body)

            self.assert_mock_args(m,
                                  '/db',
                                  method='POST',
                                  body=json.dumps({'name': db_name}))
Beispiel #18
0
    def test_get_user_names(self):
        client = AsyncfluxClient()
        db_name = 'foo'
        db = client[db_name]
        users = [{
            'name': 'foo',
            'isAdmin': False,
            'writeTo': '.*',
            'readFrom': '.*'
        }, {
            'name': 'bar',
            'isAdmin': False,
            'writeTo': '.*',
            'readFrom': '.*'
        }]
        user_names = [u['name'] for u in users]

        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 200, body=users)
            response = yield db.get_user_names()
            self.assertListEqual(response, user_names)

            self.assert_mock_args(m, '/db/%s/users' % db_name)
Beispiel #19
0
    def test_delete(self):
        client = AsyncfluxClient()
        db_name = 'foo'
        db = client[db_name]
        username = '******'
        user = User(db, username)

        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 200)
            response = yield user.delete()
            self.assertIsNone(response)

            self.assert_mock_args(m, '/db/%s/users/%s' % (db_name, username),
                                  method='DELETE')

        # Non-existing user
        response_body = "User %s doesn't exist" % 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 user.delete()

            self.assert_mock_args(m, '/db/%s/users/%s' % (db_name, username),
                                  method='DELETE')
Beispiel #20
0
    def test_delete_database(self):
        client = AsyncfluxClient()
        db_name = 'foo'

        # Using a string
        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 204)
            response = yield client.delete_database(db_name)
            self.assertIsNone(response)

            self.assert_mock_args(m, '/db/%s' % db_name, method='DELETE')

        # Using an instance of Database
        db = Database(client, db_name)
        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 204)
            response = yield client.delete_database(db)
            self.assertIsNone(response)

            self.assert_mock_args(m, '/db/%s' % db_name, method='DELETE')

        # Using an unsupported type
        with self.patch_fetch_mock(client) as m:
            re_exc_msg = r'^name_or_database must be an instance'
            with self.assertRaisesRegexp(TypeError, re_exc_msg):
                yield client.delete_database(None)
            self.assertFalse(m.called)

        # Non-existing database
        response_body = "Database %s doesn't exist" % db_name
        with self.patch_fetch_mock(client) as m:
            self.setup_fetch_mock(m, 400, body=response_body)
            with self.assertRaisesRegexp(AsyncfluxError, response_body):
                yield client.delete_database(db_name)

            self.assert_mock_args(m, '/db/%s' % db_name, method='DELETE')
Beispiel #21
0
 def test_repr(self):
     host = 'localhost'
     port = 8086
     self.assertEqual(repr(AsyncfluxClient(str(host), port)),
                      "AsyncfluxClient('%s', %d)" % (host, port))
Beispiel #22
0
 def test_non_callable(self):
     client = AsyncfluxClient()
     with self.assertRaisesRegexp(TypeError, 'callback must be a callable'):
         client.get_databases(callback='this is not a callable')
Beispiel #23
0
    def test_class_instantiation(self):
        client = AsyncfluxClient()
        self.assertEqual(client.host, 'localhost')
        self.assertEqual(client.port, 8086)
        self.assertEqual(client.base_url, 'http://*****:*****@remotehost:8089')
        self.assertEqual(client.host, 'remotehost')
        self.assertEqual(client.port, 8089)
        self.assertEqual(client.base_url, 'http://*****:*****@remotehost:8089')
        self.assertEqual(client.host, 'remotehost')
        self.assertEqual(client.port, 8089)
        self.assertEqual(client.base_url, 'https://remotehost:8089')
        self.assertEqual(client.username, 'user')
        self.assertEqual(client.password, 'pass')

        client = AsyncfluxClient(username='******')
        self.assertEqual(client.host, 'localhost')
        self.assertEqual(client.port, 8086)
        self.assertEqual(client.base_url, 'http://localhost:8086')
        self.assertEqual(client.username, 'me')
        self.assertEqual(client.password, 'root')

        client = AsyncfluxClient(password='******')
        self.assertEqual(client.host, 'localhost')
        self.assertEqual(client.port, 8086)
        self.assertEqual(client.base_url, 'http://localhost:8086')
        self.assertEqual(client.username, 'root')
        self.assertEqual(client.password, 'mysecurepassword')

        client = AsyncfluxClient(is_secure=True)
        self.assertEqual(client.host, 'localhost')
        self.assertEqual(client.port, 8086)
        self.assertEqual(client.base_url, 'https://localhost:8086')
        self.assertEqual(client.username, 'root')
        self.assertEqual(client.password, 'root')

        self.assertRaisesRegexp(ValueError, 'Invalid URL scheme: ftp',
                                AsyncfluxClient, 'ftp://localhost:23')
        self.assertRaisesRegexp(TypeError,
                                'port must be an instance of int',
                                AsyncfluxClient,
                                port='bar')
Beispiel #24
0
 def test_non_callable(self):
     client = AsyncfluxClient()
     with self.assertRaisesRegexp(TypeError, 'callback must be a callable'):
         client.get_databases(callback='this is not a callable')
Beispiel #25
0
    def test_create_user(self):
        client = AsyncfluxClient()
        db_name = 'foo'
        db = client[db_name]
        username = '******'
        password = '******'
        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,
                                            is_admin=is_admin,
                                            read_from=read_from,
                                            write_to=write_to)
            self.assertIsInstance(response, User)
            self.assertEqual(response.name, username)

            self.assert_mock_args(m,
                                  '/db/%s/users' % db_name,
                                  method='POST',
                                  body=json.dumps(payload))

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

            self.assert_mock_args(m,
                                  '/db/%s/users' % db_name,
                                  method='POST',
                                  body=json.dumps(payload))

        # Invalid permission argument values
        exc_msg = 'You have to provide read and write permissions'
        with self.assertRaisesRegexp(ValueError, exc_msg):
            yield db.create_user(username,
                                 password,
                                 is_admin=is_admin,
                                 read_from=read_from)

        with self.assertRaisesRegexp(ValueError, exc_msg):
            yield db.create_user(username,
                                 password,
                                 is_admin=is_admin,
                                 write_to=write_to)

        # 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,
                                     is_admin=is_admin,
                                     read_from=read_from,
                                     write_to=write_to)

            self.assert_mock_args(m,
                                  '/db/%s/users' % db_name,
                                  method='POST',
                                  body=json.dumps(payload))

        # Invalid password
        password = '******'
        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,
                                     is_admin=is_admin,
                                     read_from=read_from,
                                     write_to=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)

            self.assert_mock_args(m,
                                  '/db/%s/users' % db_name,
                                  method='POST',
                                  body=json.dumps(payload))