def create_project(trace_id, organization_id, data_body): # Get logging pm_logger = common_utils.begin_logger(trace_id, __name__, inspect.currentframe()) # Parse JSON try: body_object = json.loads(data_body) project_name = body_object["name"] description = body_object["description"] except Exception as e: return common_utils.error_exception(MsgConst.ERR_REQUEST_202, HTTPStatus.BAD_REQUEST, e, pm_logger, True) # Validate list_error = validate_project(trace_id, project_name) if list_error: return common_utils.error_validate(MsgConst.ERR_REQUEST_201, HTTPStatus.UNPROCESSABLE_ENTITY, list_error, pm_logger) # Create Project project_id = str(uuid.uuid4()) if common_utils.is_null(description): description = None try: pm_projects.create_projects(trace_id, project_id, project_name, description, organization_id) except PmError as e: return common_utils.error_exception(MsgConst.ERR_DB_403, HTTPStatus.INTERNAL_SERVER_ERROR, e, pm_logger, True) try: project_item = pm_projects.get_projects(trace_id, project_id, convert_response=True) except PmError as e: return common_utils.error_exception(MsgConst.ERR_402, HTTPStatus.INTERNAL_SERVER_ERROR, e, pm_logger, True) # return data response response = common_utils.get_response_by_response_body( HTTPStatus.CREATED, project_item[0]) return common_utils.response(response, pm_logger)
def test_create_project_success(self): test_user_id = affiliation_template_user_id.format(str(1)) test_organization_id = project_organization_id_template.format(str(1)) date_now = common_utils.get_current_date() # handler path_parameters = {"organization_id": test_organization_id} project_create[ "organization_id"] = project_organization_id_template.format( str(1)) event = event_create.get_event_object( trace_id=test_user_id, path_parameters=path_parameters, body=json.dumps(project_create)) response = projects.create_project_handler(event, {}) # Get data response response_body = json.loads(response['body']) id = response_body['id'] name = response_body['name'] description = response_body['description'] organization_id = response_body['organizationId'] created_at = response_body['createdAt'] updated_at = response_body['updatedAt'] status_code = response['statusCode'] # Check data response self.assertEqual(name, project_create['name']) self.assertEqual(description, project_create['description']) self.assertEqual(organization_id, project_create['organization_id']) self.assertGreaterEqual(created_at, date_now) self.assertGreaterEqual(updated_at, date_now) self.assertEqual(status_code, HTTPStatus.CREATED.value) # Check data organization in database project_database = pm_projects.get_projects( test_user_id, id, convert_response=True)[0] self.assertEqual(name, project_database['name']) self.assertEqual(description, project_database['description']) self.assertEqual(organization_id, project_database['organizationId']) self.assertGreaterEqual(project_database['createdAt'], date_now) self.assertGreaterEqual(project_database['updatedAt'], date_now) pm_projects.delete_projects(test_user_id, id)
def test_update_project_success(self): test_organization_id = template_organization_id.format(str(1)) test_project_id = template_project_id.format(str(1)) test_user_id = affiliation_template_user_id.format(str(1)) date_now = common_utils.get_current_date() # handler path_parameters = { "project_id": test_project_id, "organization_id": test_organization_id } event_mock = event_create.get_event_object( path_parameters=path_parameters, trace_id=test_user_id, body=json.dumps(project_update)) response = projects.update_project_handler(event_mock, {}) # Get data response response_body = json.loads(response['body']) id = response_body['id'] name = response_body['name'] description = response_body['description'] createdAt = response_body['createdAt'] updatedAt = response_body['updatedAt'] status_code = response['statusCode'] # Get data in database projects_database = pm_projects.get_projects( trace_id, test_project_id) # Check data self.assertEqual(id, test_project_id) self.assertEqual(name, projects_database[0]["ProjectName"]) self.assertEqual(description, projects_database[0]['Description']) self.assertEqual(createdAt, projects_database[0]['CreatedAt']) self.assertEqual(updatedAt, projects_database[0]['UpdatedAt']) self.assertGreaterEqual(projects_database[0]['UpdatedAt'], date_now) self.assertEqual(status_code, HTTPStatus.OK.value)
def test_get_project_success(self): test_organization_id = template_organization_id.format(str(1)) test_user_id = affiliation_template_user_id.format(str(1)) project_id = projects_id_template.format(1) # handler path_parameters = { "organization_id": test_organization_id, "project_id": project_id } event_mock = event_create.get_event_object( path_parameters=path_parameters, trace_id=test_user_id) response = projects.get_project_handler(event_mock, {}) # Get data response status_code = response['statusCode'] response_body = json.loads(response['body']) id = response_body['id'] name = response_body['name'] description = response_body['description'] organizationId = response_body['organizationId'] createdAt = response_body['createdAt'] updatedAt = response_body['updatedAt'] status_code = response['statusCode'] # Get data in database filter = Attr('OrganizationID').eq(test_organization_id) project = pm_projects.get_projects( trace_id, project_id, filter, convert_response=True)[0] # Check data self.assertEqual(id, project['id']) self.assertEqual(name, project['name']) self.assertEqual(description, project['description']) self.assertEqual(organizationId, project['organizationId']) self.assertEqual(createdAt, project['createdAt']) self.assertEqual(updatedAt, project['updatedAt']) self.assertEqual(status_code, HTTPStatus.OK.value)
def test_delete_project_success(self): test_organization_id = template_organization_id.format(str(1)) test_mail_address = affiliation_template_address.format(str(1)) test_user_id = affiliation_template_user_id.format(str(1)) project_id = projects_id_template.format(998) # Create data delete tmp_projects = copy.copy(projects_template) tmp_projects['ProjectID'] = project_id pm_projects.create_projects(trace_id, tmp_projects['ProjectID'], tmp_projects['ProjectName'], tmp_projects['Description'], tmp_projects['OrganizationID']) # handler path_parameters = { "organization_id": test_organization_id, "project_id": project_id } event_mock = event_create.get_event_object( email=test_mail_address, path_parameters=path_parameters, trace_id=test_user_id) response = projects.delete_project_handler(event_mock, {}) # Get data response status_code = response['statusCode'] response_body = json.loads(response['body']) # Get data in database filter = Attr('OrganizationID').eq(test_organization_id) project = pm_projects.get_projects( trace_id, project_id, filter, convert_response=True) # Check data self.assertEqual(len(project), 0) self.assertEqual(response_body, None) self.assertEqual(status_code, HTTPStatus.NO_CONTENT)
def update_project(trace_id, project_id, organization_id, data_body): # Get logging pm_logger = common_utils.begin_logger(trace_id, __name__, inspect.currentframe()) # Parse JSON try: body_object = json.loads(data_body) project_name = body_object["name"] description = body_object["description"] except Exception as e: return common_utils.error_exception(MsgConst.ERR_REQUEST_202, HTTPStatus.BAD_REQUEST, e, pm_logger, True) # Validate list_error = validate_project(trace_id, project_name) if list_error: return common_utils.error_validate(MsgConst.ERR_REQUEST_201, HTTPStatus.UNPROCESSABLE_ENTITY, list_error, pm_logger) # Get project try: project_item = pm_projects.get_projects_by_organization_id( trace_id, project_id, organization_id) except PmError as err: return common_utils.error_exception(MsgConst.ERR_402, HTTPStatus.INTERNAL_SERVER_ERROR, err, pm_logger, True) if not project_item: return common_utils.error_common(MsgConst.ERR_301, HTTPStatus.NOT_FOUND, pm_logger) # update project if common_utils.is_null(description): description = None attribute = { 'ProjectName': { "Value": project_name }, 'Description': { "Value": description } } updated_at = project_item[0]['UpdatedAt'] try: pm_projects.update_project(trace_id, project_id, attribute, updated_at) except PmError as err: return common_utils.error_exception(MsgConst.ERR_DB_404, HTTPStatus.INTERNAL_SERVER_ERROR, err, pm_logger, True) # Get data update try: project_result = pm_projects.get_projects(trace_id, project_id, convert_response=True) except PmError as err: return common_utils.error_exception(MsgConst.ERR_402, HTTPStatus.INTERNAL_SERVER_ERROR, err, pm_logger, True) response = common_utils.get_response_by_response_body( HTTPStatus.OK, project_result[0]) # return data response return common_utils.response(response, pm_logger)