def test_update_invitations(self): logging.debug("Start test_update_invitations") request = yield get_ws_request(self.io_loop) connection = yield websocket_connect(request) correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "invitations", "operation": "update", "correlation": correlation, "body": dict() })) message = yield wait_message(connection, correlation) validate_response( self, message, dict(status_code=405, correlation=correlation, operation="update", action="invitations", body_type=dict)) expected_message = "Operation update not supported for action invitations." self.assertTrue(message["body"]["message"] == expected_message, "Message is %s instead of '%s'" % (message["body"]["message"], expected_message)) connection.close() logging.debug("Completed test_update_invitations")
def test_invite_unauthorized(self): logging.debug("Start test_invite_unauthorized") request = yield get_ws_request(self.io_loop, username="******") connection = yield websocket_connect(request) correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "invitations", "operation": "create", "correlation": correlation, "body": dict() })) message = yield wait_message(connection, correlation) validate_response( self, message, dict(status_code=403, correlation=correlation, operation="create", action="invitations", body_type=dict)) expected_message = "Operation create forbidden for action invitations." self.assertTrue(message["body"]["message"] == expected_message, "Message is %s instead of '%s'" % (message["body"]["message"], expected_message)) connection.close() logging.debug("Completed test_invite_unauthorized")
def create_settings_test(self): logging.debug("Start create_settings_test") request = yield get_ws_request(self.io_loop) connection = yield websocket_connect(request) correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "settings", "operation": "create", "correlation": correlation, "body": dict() })) message = yield wait_message(connection, correlation) validate_response( self, message, dict(status_code=405, correlation=correlation, operation="create", action="settings", body_type=dict)) expected_message = "Operation create not supported for action settings." self.assertTrue(message["body"]["message"] == expected_message, "Message is %s instead of '%s'" % (message["body"]["message"], expected_message)) connection.close() logging.debug("Completed create_settings_test")
def create_instances_test(self): logging.debug("Start create_instances_test") request = yield get_ws_request(self.io_loop) connection = yield websocket_connect(request) chart_id = str(ObjectId()) correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "instances", "operation": "create", "correlation": correlation, "body": dict(namespace="default", uid=chart_id) })) message = yield wait_message(connection, correlation) validate_response( self, message, dict(status_code=404, correlation=correlation, operation="create", action="instances", body_type=dict)) expected_message = "Cannot find Chart %s" % chart_id self.assertTrue(message["body"]["message"] == expected_message, "Message is %s instead of '%s'" % (message["body"]["message"], expected_message)) connection.close() logging.debug("Completed create_instances_test")
def forbidden_update_test(self): logging.debug("Start forbidden_update_test") request = yield get_ws_request(self.io_loop, username="******") connection = yield websocket_connect(request) correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "settings", "operation": "update", "correlation": correlation, "body": dict() })) message = yield wait_message(connection, correlation) validate_response( self, message, dict(status_code=403, correlation=correlation, operation="update", action="settings", body_type=dict)) expected_message = "Operation update forbidden for action settings." self.assertTrue(message["body"]["message"] == expected_message, "Message is %s instead of '%s'" % (message["body"]["message"], expected_message)) connection.close() logging.debug("Completed forbidden_update_test")
def create_user_test(self): logging.debug("Start create_user_test") request = yield get_ws_request(self.io_loop) connection = yield websocket_connect(request) correlation = str(uuid.uuid4())[:10] connection.write_message( json.dumps({ "action": "users", "operation": "create", "correlation": correlation, "body": dict() })) message = yield wait_message(connection, correlation) validate_response( self, message, dict(status_code=405, correlation=correlation, operation="create", action="users", body_type=dict)) logging.debug("Completed create_user_test")
def create_instances_test(self): logging.debug("Start create_instances_test") request = yield get_ws_request(self.io_loop) connection = yield websocket_connect(request) chart_id = str(ObjectId()) correlation = str(uuid.uuid4())[:10] connection.write_message( json.dumps({ "action": "instances", "operation": "create", "correlation": correlation, "body": dict(namespace="default", uid=chart_id) })) message = yield wait_message(connection, correlation) validate_response( self, message, dict(status_code=404, correlation=correlation, operation="create", action="instances", body_type=dict)) expected_message = "Cannot find Chart %s" % chart_id self.assertTrue( message["body"]["message"] == expected_message, "Message is %s instead of '%s'" % (message["body"]["message"], expected_message)) connection.close() logging.debug("Completed create_instances_test")
def test_create_invitations(self): logging.debug("Start test_create_invitations") user_email = "*****@*****.**" % str(uuid.uuid4())[:10] self.addCleanup(self._delete_user, user_email) request = yield get_ws_request(self.io_loop) connection = yield websocket_connect(request) correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "invitations", "operation": "create", "correlation": correlation, "body": { "emails": [user_email], "note": "Test note" } })) message = yield wait_message(connection, correlation) validate_response( self, message, dict(status_code=200, correlation=correlation, operation="created", action="invitations")) database = MongoClient("mongodb://%s:27017/" % get_api_address()).elastickube user = database.Users.find_one({"email": user_email}) invitation_token = user["invite_token"] data = dict(email=user_email, password="******", firstname="firstname", lastname="lastname") yield AsyncHTTPClient(self.io_loop).fetch( "http://%s/api/v1/auth/signup" % get_api_address(), method='POST', headers={ELASTICKUBE_VALIDATION_TOKEN_HEADER: invitation_token}, body=json.dumps(data)) correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "users", "operation": "watch", "correlation": correlation })) message = yield wait_message(connection, correlation) new_user = None for user in message["body"]: if user["email"] == user_email: new_user = user break self.assertIsNotNone(new_user) connection.close() logging.debug("Completed test_create_invitations")
def update_settings_test(self): logging.debug("Start update_settings_test") request = yield get_ws_request(self.io_loop) connection = yield websocket_connect(request) correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "settings", "operation": "watch", "correlation": correlation })) message = yield wait_message(connection, correlation) validate_response( self, message, dict(status_code=200, correlation=correlation, operation="watched", action="settings", body_type=list)) self.assertTrue(len(message["body"]) > 0, "No Settings returned as part of the response") self.assertTrue(len(message["body"]) < 2, "Multiple Settings returned as part of the response") settings = message["body"][0] correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "settings", "operation": "update", "correlation": correlation, "body": settings })) message = yield wait_message(connection, correlation) validate_response( self, message, dict(status_code=200, correlation=correlation, operation="updated", action="settings", body_type=dict)) previous_version = settings["metadata"]["resourceVersion"] self.assertTrue(message["body"]["metadata"]["resourceVersion"] > previous_version, "resourceVersion is equal or lower than before, %s %s " % ( previous_version, message["body"]["metadata"]["resourceVersion"])) connection.close() logging.debug("Completed update_settings_test")
def create_user_test(self): logging.debug("Start create_user_test") request = yield get_ws_request(self.io_loop) connection = yield websocket_connect(request) correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "users", "operation": "create", "correlation": correlation, "body": dict() })) message = yield wait_message(connection, correlation) validate_response( self, message, dict(status_code=405, correlation=correlation, operation="create", action="users", body_type=dict)) logging.debug("Completed create_user_test")
def test_watch_charts(self): logging.debug("Start test_watch_charts") request = yield get_ws_request(self.io_loop) connection = yield websocket_connect(request) correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "charts", "operation": "watch", "correlation": correlation })) deserialized_message = yield wait_message(connection, correlation) self.assertTrue(deserialized_message["status_code"] == 200, "Status code is %d instead of 200" % deserialized_message["status_code"]) self.assertTrue(deserialized_message["correlation"] == correlation, "Correlation is %s instead of %s" % (deserialized_message["correlation"], correlation)) self.assertTrue(deserialized_message["operation"] == "watched", "Operation is %s instead of watched" % deserialized_message["operation"]) self.assertTrue(deserialized_message["action"] == "charts", "Action is %s instead of charts" % deserialized_message["action"]) self.assertTrue(isinstance(deserialized_message["body"], list), "Body is not a list but %s" % type(deserialized_message["body"])) self.assertTrue(len(deserialized_message["body"]) > 0, "No charts returned as part of the response") correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "charts", "operation": "watch", "correlation": correlation })) deserialized_message = yield wait_message(connection, correlation) self.assertTrue(deserialized_message["status_code"] == 400, "Status code is %d instead of 400" % deserialized_message["status_code"]) self.assertTrue(deserialized_message["correlation"] == correlation, "Correlation is %s instead of %s" % (deserialized_message["correlation"], correlation)) self.assertTrue(deserialized_message["operation"] == "watched", "Operation is %s instead of watched" % deserialized_message["operation"]) self.assertTrue(deserialized_message["action"] == "charts", "Action is %s instead of charts" % deserialized_message["action"]) self.assertTrue(isinstance(deserialized_message["body"], dict), "Body is not a dict but %s" % type(deserialized_message["body"])) self.assertTrue(deserialized_message["body"]["message"] == "Action already watched.", "Message is %s instead of 'Action already watched.'" % deserialized_message["body"]["message"]) correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "charts", "operation": "unwatch", "correlation": correlation })) deserialized_message = yield wait_message(connection, correlation) self.assertTrue(deserialized_message['status_code'] == 200, "Status code is %d instead of 200" % deserialized_message['status_code']) self.assertTrue(deserialized_message["correlation"] == correlation, "Correlation is %s instead of %s" % (deserialized_message["correlation"], correlation)) self.assertTrue(deserialized_message['operation'] == "unwatched", "Operation is %s instead of unwatched" % deserialized_message['operation']) self.assertTrue(deserialized_message['action'] == "charts", "Action is %s instead of charts" % deserialized_message['action']) self.assertTrue(isinstance(deserialized_message['body'], dict), "Body is not a dict but %s" % type(deserialized_message['body'])) self.assertTrue(len(deserialized_message['body'].keys()) == 0, "Body is not empty") connection.close() logging.debug("Completed test_watch_charts")
def create_invitations_test(self): logging.debug("Start create_invitations_test") user_email = "*****@*****.**" % str(uuid.uuid4())[:10] self.addCleanup(self._delete_user, user_email) request = yield get_ws_request(self.io_loop) connection = yield websocket_connect(request) correlation = str(uuid.uuid4())[:10] connection.write_message( json.dumps({ "action": "invitations", "operation": "create", "correlation": correlation, "body": { "emails": [user_email], "note": "Test note" } })) message = yield wait_message(connection, correlation) validate_response( self, message, dict(status_code=200, correlation=correlation, operation="created", action="invitations")) database = MongoClient("mongodb://localhost:27017/").elastickube user = database.Users.find_one({"email": user_email}) invitation_token = user["invite_token"] data = dict(email=user_email, password="******", firstname="firstname", lastname="lastname") yield AsyncHTTPClient(self.io_loop).fetch( "http://localhost/api/v1/auth/signup", method='POST', headers={ELASTICKUBE_VALIDATION_TOKEN_HEADER: invitation_token}, body=json.dumps(data)) correlation = str(uuid.uuid4())[:10] connection.write_message( json.dumps({ "action": "users", "operation": "watch", "correlation": correlation })) message = yield wait_message(connection, correlation) new_user = None for user in message["body"]: if user["email"] == user_email: new_user = user break self.assertIsNotNone(new_user) connection.close() logging.debug("Completed update_settings_test")
def watch_instances_test(self): logging.debug("Start watch_instances_test") request = yield get_ws_request(self.io_loop) connection = yield websocket_connect(request) correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "instances", "operation": "watch", "correlation": correlation })) message = yield wait_message(connection, correlation) validate_response( self, message, dict(status_code=200, correlation=correlation, operation="watched", action="instances", body_type=list)) self.assertTrue(len(message["body"]) > 0, "No instances returned as part of the response") correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "instances", "operation": "watch", "correlation": correlation, })) message = yield wait_message(connection, correlation) validate_response( self, message, dict(status_code=400, correlation=correlation, operation="watched", action="instances", body_type=dict)) self.assertTrue(message["body"]["message"] == "Action already watched.", "Message is %s instead of 'Action already watched.'" % message["body"]["message"]) correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "instances", "operation": "watch", "correlation": correlation, "body": {"namespace": "kube-system"} })) message = yield wait_message(connection, correlation) validate_response( self, message, dict(status_code=200, correlation=correlation, operation="watched", action="instances", body_type=list)) self.assertTrue(len(message["body"]) > 0, "No instances returned as part of the response") correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "instances", "operation": "watch", "correlation": correlation, "body": {"namespace": "kube-system"} })) message = yield wait_message(connection, correlation) validate_response( self, message, dict(status_code=400, correlation=correlation, operation="watched", action="instances", body_type=dict)) self.assertTrue(message["body"]["message"] == "Action already watched.", "Message is %s instead of 'Action already watched.'" % message["body"]["message"]) correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "instances", "operation": "unwatch", "correlation": correlation, "body": {"namespace": "kube-system"} })) message = yield wait_message(connection, correlation) validate_response( self, message, dict(status_code=200, correlation=correlation, operation="unwatched", action="instances", body_type=dict)) self.assertTrue(len(message['body'].keys()) == 0, "Body is not empty") correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "instances", "operation": "unwatch", "correlation": correlation, "body": {"namespace": "kube-system"} })) message = yield wait_message(connection, correlation) validate_response( self, message, dict(status_code=400, correlation=correlation, operation="unwatched", action="instances", body_type=dict)) self.assertTrue(message["body"]["message"] == "Action not previously watch.", "Message is %s instead of 'Action not previously watch.'" % message["body"]["message"]) correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "instances", "operation": "unwatch", "correlation": correlation })) message = yield wait_message(connection, correlation) validate_response( self, message, dict(status_code=200, correlation=correlation, operation="unwatched", action="instances", body_type=dict)) self.assertTrue(len(message['body'].keys()) == 0, "Body is not empty") correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "instances", "operation": "unwatch", "correlation": correlation })) message = yield wait_message(connection, correlation) validate_response( self, message, dict(status_code=400, correlation=correlation, operation="unwatched", action="instances", body_type=dict)) self.assertTrue(message["body"]["message"] == "Action not previously watch.", "Message is %s instead of 'Action not previously watch.'" % message["body"]["message"]) connection.close() logging.debug("Completed watch_instances_test")
def test_message_validation(self): logging.debug("Start test_message_validation") request = yield get_ws_request(self.io_loop) connection = yield websocket_connect(request) connection.write_message("No JSON object") message = yield connection.read_message() self.assertTrue(message == '"Message \'No JSON object\' cannot be deserialized"', "Received %s instead of '\"Message 'No JSON object' cannot be deserialized\"'" % message) connection.write_message(json.dumps({ "operation": "create", "correlation": 123 })) message = yield connection.read_message() expected_message = "\"Message {\\\"operation\\\": \\\"create\\\", \\\"correlation\\\": 123}"\ " does not contain 'action'\"" self.assertTrue(message == expected_message, "Received %s instead of '%s'" % (message, expected_message)) connection.write_message(json.dumps({ "action": "users", "correlation": 123 })) message = yield connection.read_message() expected_message = "\"Message {\\\"action\\\": \\\"users\\\", \\\"correlation\\\": 123}" \ " does not contain 'operation'\"" self.assertTrue(message == expected_message, "Received %s instead of '%s'" % (message, expected_message)) connection.write_message(json.dumps({ "action": "users", "operation": "create" })) message = yield connection.read_message() expected_message = "\"Message {\\\"action\\\": \\\"users\\\", \\\"operation\\\": \\\"create\\\"}" \ " does not contain 'correlation'\"" self.assertTrue(message == expected_message, "Received %s instead of '%s'" % (message, expected_message)) correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "fake_action", "operation": "create", "correlation": correlation })) deserialized_message = yield wait_message(connection, correlation) self.assertTrue(deserialized_message['status_code'] == 400, "Status code is %d instead of 400" % deserialized_message['status_code']) self.assertTrue(deserialized_message['correlation'] == correlation, "Correlation is %s instead of %s" % (deserialized_message["correlation"], correlation)) self.assertTrue(deserialized_message['operation'] == "create", "Operation is %s instead of create" % deserialized_message['operation']) self.assertTrue(deserialized_message['action'] == "fake_action", "Action is %s instead of fake_action" % deserialized_message['action']) self.assertTrue(isinstance(deserialized_message['body'], dict), "Body is not a dict but %s" % type(deserialized_message['body'])) expected_message = "Action fake_action not supported." self.assertTrue(deserialized_message["body"]["message"] == expected_message, "Message is %s instead of '%s'" % (deserialized_message['body']["message"], expected_message)) correlation = str(uuid.uuid4())[:10] connection.write_message(json.dumps({ "action": "users", "operation": "fake_operation", "correlation": correlation })) deserialized_message = yield wait_message(connection, correlation) self.assertTrue(deserialized_message['status_code'] == 400, "Status code is %d instead of 400" % deserialized_message['status_code']) self.assertTrue(deserialized_message['correlation'] == correlation, "Correlation is %s instead of %s" % (deserialized_message["correlation"], correlation)) self.assertTrue(deserialized_message['operation'] == "fake_operation", "Operation is %s instead of fake_operation" % deserialized_message['operation']) self.assertTrue(deserialized_message['action'] == "users", "Action is %s instead of users" % deserialized_message['action']) self.assertTrue(isinstance(deserialized_message['body'], dict), "Body is not a dict but %s" % type(deserialized_message['body'])) expected_message = "Operation fake_operation not supported." self.assertTrue(deserialized_message["body"]["message"] == expected_message, "Message is %s instead of '%s'" % (deserialized_message['body']["message"], expected_message)) connection.close() logging.debug("Completed test_message_validation")