def get(): if 'x-access-token' in request.headers: token = request.headers['x-access-token'] current_user = Login().token_access(str(token)) return response_by_success(Login().logout( current_user.data['username']))
def test_login_and_logout(self): User().insert({ 'type': 'admin', 'first_name': 'Admin', 'last_name': '1', 'username': '******', 'email': '*****@*****.**', 'password': '******' }) logged_user = Login().login(Auth('admin1', 'admin1')) self.assertNotEqual(logged_user, None, 'Existing user not logged in') status = Login().logout('admin1') self.assertEqual(status, True, 'Non existing user not logged out')
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 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 test_check_logged_user_props(self): u = { 'type': 'admin', 'first_name': 'Admin', 'last_name': '1', 'username': '******', 'email': '*****@*****.**', 'password': '******' } keys = ['_id', 'public_id', 'token', 'username', 'exp', 'login_time'] User().insert(u) user = User().find({'username': '******'}) logged_user = Login().login(Auth('admin1', 'admin1')) self.assertNotEqual(logged_user, None, 'Logged user obj not created') self.assertIsInstance(logged_user.data, dict, 'User data is not a dict') self.assertListEqual(list(logged_user.data.keys()), keys, 'Keys are not equal') self.assertEqual(logged_user.data['public_id'], user.data['public_id'], 'Public id not equal') self.assertEqual(logged_user.data['username'], user.data['username'], 'Username not equal')
def test_token(self): u = { 'type': 'admin', 'first_name': 'Admin', 'last_name': '1', 'username': '******', 'email': '*****@*****.**', 'password': '******' } keys = ['_id', 'public_id', 'token', 'username', 'exp', 'login_time'] User().insert(u) logged_user = Login().login(Auth('admin1', 'admin1')) self.assertNotEqual(logged_user, None, 'Logged user obj not created') self.assertIsInstance(logged_user.data, dict, 'User data is not a dict') self.assertListEqual(list(logged_user.data.keys()), keys, 'Keys are not equal') token = logged_user.data['token'] data = jwt.decode(token, JWT_ENC_KEY, algorithms=['HS256']) self.assertEqual(logged_user.data['public_id'], data['public_id'], 'Public id from token not equal')
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 get(): user = Login().get_username(request.headers['x-access-token']) is_admin = User().is_admin(user) return { 'mongo': MongoEngine().status(is_admin), 'docker': DockerEngine().status() }, 200
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 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 get(): auth = request.authorization if not auth or not auth.username or not auth.password: return make_response( 'Could not verify.', 401, {'WWW-Authenticate': 'Basic realm="Login required."'}) login = Login().login(auth) if not login or not login.data: return response_by_success(False) return parse_data(LoginSchema, login.data)
def decorated(*args, **kwargs): token = None if 'x-access-token' in request.headers: token = request.headers['x-access-token'] if not token: return response_with_message(message="Token is missing", code=401) current_user = Login().token_access(str(token)) if current_user.data is None: return response_with_message(message="Invalid token", code=401) return f(*args, **kwargs)
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_logout_non_logged_in_user(self): status = Login().logout('admin1') self.assertEqual(status, False, 'Non existing user not logged out')
def test_login_non_existing_user(self): logged_user = Login().login(Auth('123', '456')) self.assertEqual(logged_user, False, 'Non existing user logged in')
def get(): username = Login().get_username(request.headers['x-access-token']) user = User().find({'username': username}) return parse_data(UserSchema, user.data) if user and user.data \ else response_by_success(False)