def test_query_users(self): c1 = {'domain': 'test-customer3', 'db_name': 'test-customer3'} c2 = {'domain': 'test-customer4', 'db_name': 'test-customer4'} Customer().insert(c1) Customer().insert(c2) response = self.app.post(self.path + '/query', headers=self.headers, data=json.dumps({'query': { 'enabled': True }})) self.assertEqual(response.status_code, 200, 'Customers not found') self.assertGreaterEqual(json.loads(response.data)['total'], 2) response = self.app.post(self.path + '/query', headers=self.headers, data=json.dumps({ 'query': {}, 'filter': { 'domain': 1 } })) self.assertEqual(response.status_code, 200, 'Customers not found') self.assertGreaterEqual(json.loads(response.data)['total'], 2) for user in json.loads(response.data)['items']: self.assertEqual(list(user.keys()), ['domain'], 'Wrong keys')
def test_find_all_customers(self): c1 = {'domain': 'test', 'db_name': 'test'} c2 = {'domain': 'test2', 'db_name': 'test2'} Customer().insert(c1) Customer().insert(c2) customers = Customer().find() self.assertNotEqual(customers, None, 'Customer obj not created') self.assertIsInstance(customers.data, list, 'Customer data is not a list') remove1 = Customer().remove(c1) remove2 = Customer().remove(c2) self.assertEqual(remove1, True, 'Customer1 not removed') self.assertEqual(remove2, True, 'Customer2 not removed')
def test_remove_customer(self): c = {'domain': 'test', 'db_name': 'test'} Customer().insert(c) customer = Customer().find({'domain': 'test'}) remove = Customer().remove(c) self.assertEqual(remove, True, 'Customer not removed') deleted_customer = Customer().find({'domain': 'test'}) self.assertNotEqual(customer.data['enabled'], deleted_customer.data['enabled'], 'Deleted item enabled') self.assertNotEqual(customer.data['deleted'], deleted_customer.data['deleted'], 'Deleted item not deleted')
def test_find_customer_by_criteria(self): c = {'domain': 'test', 'db_name': 'test'} Customer().insert(c) not_found_customer = Customer().find({'domain': 'rip'}) self.assertEqual(not_found_customer.data, None, 'Non existing customer found') found_customer = Customer().find({'domain': 'test'}) self.assertNotEqual(found_customer.data, {}, 'Existing customer without data') remove = Customer().remove(c) self.assertEqual(remove, True, 'Customer not removed')
def list_customers(enabled=None): if enabled is None: data = Customer().find({}, projection={'domain': 1}).data else: data = Customer().find({ 'enabled': enabled }, projection={ 'domain': 1 }).data if type(data) is dict: return [{'name': data['domain']}] if data is not None: return [{'name': item['domain']} for item in data] return []
def test_find_customer_by_criteria_with_projection(self): c = {'domain': 'test', 'db_name': 'test'} keys = ['_id', 'enabled'] Customer().insert(c) found_customer = Customer().find(criteria={'domain': 'test'}, projection={ '_id': 1, 'enabled': 1 }) self.assertListEqual(list(found_customer.data.keys()), keys, 'Wrong keys while finding with projection') remove = Customer().remove(c) self.assertEqual(remove, True, 'Customer not removed')
def test_status(self): Customer().set_customer(TESTING_DATABASE) User().insert({ 'type': 'admin', 'first_name': 'status', 'last_name': 'status', 'username': '******', 'email': 'status', 'password': '******' }) logged_user = Login().login(Auth('status', 'status')) self.headers = { 'Content-Type': 'application/json', 'x-access-token': logged_user.data['token'] } response = self.app.get('/status', headers=self.headers) self.assertEqual(response.status_code, 200, 'Status not found') data = json.loads(response.data) keys = ['is_up', 'data_usage', 'info'] self.assertEqual('docker' in data, True, 'Missing docker status') self.assertEqual('mongo' in data, True, 'Missing mongo status') for key in keys: self.assertEqual(key in data['docker'], True, key + ' missing in docker status') self.assertEqual(key in data['mongo'], True, key + ' missing in mongo status')
def __init__(self, *args, **kwargs): super(AnsibleEngineTests, self).__init__(*args, **kwargs) # Drop previous database Customer().set_customer(TESTING_DATABASE) MongoEngine().drop_collection(TESTING_DATABASE, 'hosts') MongoEngine().drop_collection(TESTING_DATABASE, 'playbooks')
def post(): user = Login().get_username(request.headers['x-access-token']) if user and User().is_admin(user): data = validate_or_abort(CustomerSchema, request.get_json()) return response_by_success(Customer().insert(data)) return response_by_success(False)
def put(): user = Login().get_username(request.headers['x-access-token']) if user and User().is_admin(user): data = validate_or_abort(CustomerSchemaPut, request.get_json()) return response_by_success(Customer().update( criteria={'domain': data['domain']}, data=data['data'])) return response_by_success(False)
def test_find_customer(self): c = {'domain': 'test', 'db_name': 'test'} keys = ['_id', 'domain', 'db_name', 'enabled', 'deleted'] Customer().insert(c) customer = Customer().find({'domain': 'test'}) self.assertNotEqual(customer, None, 'Customer obj not created') self.assertIsInstance(customer.data, dict, 'Customer data is not a dict') self.assertEqual(customer.data['domain'], c['domain'], 'Domain not equal') self.assertEqual(customer.data['db_name'], c['db_name'], 'Database name not equal') self.assertListEqual(list(customer.data.keys()), keys, 'Keys are not equal') remove = Customer().remove(c) self.assertEqual(remove, True, 'Customer not removed')
def delete(): user = Login().get_username(request.headers['x-access-token']) if user and User().is_admin(user): data = validate_or_abort(CustomerSchemaDelete, request.get_json()) return response_by_success( Customer().remove(criteria={'domain': data['domain']}), is_remove=True) return response_by_success(False)
def test_healthcheck(self): Customer().set_customer(TESTING_DATABASE) response = self.app.get('/api/healthcheck') self.assertEqual(response.status_code, 200, 'Healthcheck not found') data = json.loads(response.data) self.assertEqual('ok' in data, True, 'Missing healthcheck')
def setUp(self): Customer().set_customer(TESTING_DATABASE) MongoEngine().drop_collection(TESTING_DATABASE, 'users') User().insert({ 'type': 'admin', 'first_name': 'admin', 'last_name': 'admin', 'username': '******', 'email': '*****@*****.**', 'password': '******' })
def post(): user = Login().get_username(request.headers['x-access-token']) if user and User().is_admin(user): data = validate_or_abort(QuerySchema, request.get_json()) customer = Customer().find( criteria=data['query'], projection=data['filter'] if 'filter' in data.keys() else {}) return parse_data(CustomerSchema, customer.data) return response_by_success(False)
def test_update_customer(self): customer = {'domain': 'test-customer2', 'db_name': 'test-customer2'} Customer().insert(customer) customer = {'domain': 'test-c2', 'db_name': 'test-c2', 'enabled': True} response = self.app.put(self.path, headers=self.headers, data=json.dumps({ 'domain': 'test-customer2', 'data': customer })) self.assertEqual(response.status_code, 200, 'Customer not updated')
def __init__(self, *args, **kwargs): super(DumpTests, self).__init__(*args, **kwargs) # Drop previous database Customer().set_customer(TESTING_DATABASE) MongoEngine().drop_collection(TESTING_DATABASE, 'hosts') MongoEngine().drop_collection(TESTING_DATABASE, 'playbooks') self.root = './' self.base_path = 'files-generated' self.domain = 'domain.com' self.sub_path_hosts = 'hosts' self.sub_path_playbooks = 'playbooks'
def test_create_and_delete_customer(self): customer = {'domain': 'test-customer', 'db_name': 'test-customer'} Customer().remove(customer) response = self.app.post(self.path, headers=self.headers, data=json.dumps(customer)) self.assertEqual(response.status_code, 200, 'Customer not created') response = self.app.delete(self.path, headers=self.headers, data=json.dumps({'domain': 'test-customer'})) self.assertEqual(response.status_code, 204, 'User not deleted')
def setUp(self): Customer().set_customer(TESTING_DATABASE) MongoEngine().drop_collection(TESTING_DATABASE, 'customers') User().insert({ 'type': 'admin', 'first_name': 'admin-user', 'last_name': 'admin-user', 'username': '******', 'email': 'admin-user', 'password': '******' }) logged_user = Login().login(Auth('admin-user', 'admin-user')) self.headers = { 'Content-Type': 'application/json', 'x-access-token': logged_user.data['token'] }
def __new__(cls, *args, **kwargs): if not cls.engine: cls.engine = super(TestingLogin, cls).__new__(cls, *args, **kwargs) Customer().set_customer(TESTING_DATABASE) MongoEngine().drop(TESTING_DATABASE) User().insert({ 'type': 'admin', 'first_name': 'admin', 'last_name': 'admin', 'username': '******', 'email': '*****@*****.**', 'password': '******' }) logged_user = Login().login(Auth('admin', 'admin')) cls.headers = { 'Content-Type': 'application/json', 'x-access-token': logged_user.data['token'] } return cls.engine
def test_login_and_logout(self): Customer().set_customer(TESTING_DATABASE) User().insert({ 'type': 'admin', 'first_name': 'usertest', 'last_name': 'usertest', 'username': '******', 'email': '*****@*****.**', 'password': '******' }) response = self.app.get( '/login', headers={"Authorization": "Basic dXNlcnRlc3Q6dXNlcnRlc3Q="}) self.assertEqual(response.status_code, 200) data = json.loads(response.data) self.assertNotEqual(data, "", 'There is no token') headers = { 'Content-Type': 'application/json', 'x-access-token': data['token'] } response = self.app.get('/logout', headers=headers) self.assertEqual(response.status_code, 200, 'Logout failed')
def setUp(self): Customer().set_customer(TESTING_DATABASE)
def before_request(): data = tldextract.extract(request.host) if Customer().is_customer(data.subdomain): Customer().set_customer(data.subdomain) else: abort(404, "Not found")
def create_user(u, c): if Customer().is_customer(c) is not None: Customer().set_customer(c) return User().insert(u) return False
def enable_customer(enabled, name): return Customer().update({'domain': name}, data={'enabled': enabled})
def setUp(self): Customer().set_customer(TESTING_DATABASE) MongoEngine().drop_collection(TESTING_DATABASE, 'machines')
def create_customer(c): return Customer().insert(c)
def __init__(self, *args, **kwargs): super(UserTests, self).__init__(*args, **kwargs) # Drop previous database Customer().set_customer(TESTING_DATABASE) MongoEngine().drop_collection(TESTING_DATABASE, 'users')
import os from src.classes.customer import Customer from src.classes.user import User BASE_DATABASE = os.environ.get('BASE_DATABASE', 'ipm_root') Customer().set_customer(BASE_DATABASE) User().insert({ 'type': 'admin', 'first_name': 'Admin', 'last_name': 'istrator', 'username': '******', 'email': '*****@*****.**', 'password': '******' })
def setUp(self): Customer().set_customer(TESTING_DATABASE) MongoEngine().drop_collection(TESTING_DATABASE, 'hosts') MongoEngine().drop_collection(TESTING_DATABASE, 'playbooks')