Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ProjectBusy endpoint #1446

Merged
merged 14 commits into from
Aug 7, 2023
1 change: 1 addition & 0 deletions SPRINTLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,4 @@ _Nothing merged in CLI during this sprint_
- New version: 2.4.0 ([#1443](https://github.com/ScilifelabDataCentre/dds_web/pull/1443))
- Bug fix: Web UI project listing fix ([#1445](https://github.com/ScilifelabDataCentre/dds_web/pull/1445))
- Documentation: Technical Overview, section Creating a Unit in the DDS ([#1449](https://github.com/ScilifelabDataCentre/dds_web/pull/1449))
- Empty endpoint: `ProjectBusy` ([#1446](https://github.com/ScilifelabDataCentre/dds_web/pull/1446))
48 changes: 12 additions & 36 deletions dds_web/api/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
ProjectBusyError,
S3ConnectionError,
NoSuchUserError,
VersionMismatchError,
)
from dds_web.api.user import AddUser
from dds_web.api.schemas import project_schemas, user_schemas
Expand Down Expand Up @@ -922,45 +923,20 @@ def give_project_access(project_list, current_user, user):


class ProjectBusy(flask_restful.Resource):
@auth.login_required(role=["Unit Admin", "Unit Personnel", "Project Owner", "Researcher"])
@auth.login_required
@logging_bind_request
@dbsession
@json_required
def put(self):
"""Set project to busy / not busy."""
# Get project ID, project and verify access
project_id = dds_web.utils.get_required_item(obj=flask.request.args, req="project")
project = dds_web.utils.collect_project(project_id=project_id)
dds_web.utils.verify_project_access(project=project)

# Get busy or not busy
request_json = flask.request.get_json(silent=True)
set_to_busy = request_json.get("busy") # Already checked by json_required
if set_to_busy is None:
raise DDSArgumentError(message="Missing information about setting busy or not busy.")
"""OLD ENDPOINT.
Previously set project status to busy.

if set_to_busy:
# Check if project is busy
if project.busy:
return {"ok": False, "message": "The project is already busy, cannot proceed."}

# Set project as busy
project.busy = True
else:
# Check if project is not busy
if not project.busy:
return {
"ok": False,
"message": "The project is already not busy, cannot proceed.",
}

# Set project to not busy
project.busy = False

return {
"ok": True,
"message": f"Project {project_id} was set to {'busy' if set_to_busy else 'not busy'}.",
}
TODO: Can remove from 2024. Will otherwise cause breaking changes for old CLI versions.
"""
raise VersionMismatchError(
message=(
"Your CLI version is trying to use functionality which is no longer in use. "
"Upgrade your version to the latest one and run your command again."
)
)


class ProjectInfo(flask_restful.Resource):
Expand Down
181 changes: 20 additions & 161 deletions tests/api/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -1335,49 +1335,13 @@ def test_set_busy_no_token(module_client):
assert "No token" in response.json.get("message")


def test_set_busy_superadmin_not_allowed(module_client):
"""Super admin cannot set project busy/not busy."""
token = tests.UserAuth(tests.USER_CREDENTIALS["superadmin"]).token(module_client)
response = module_client.put(
tests.DDSEndpoint.PROJECT_BUSY,
headers=token,
)
assert response.status_code == http.HTTPStatus.FORBIDDEN


def test_set_busy_no_args(module_client):
"""Args required to set busy/not busy."""
# Unit Personnel
token = tests.UserAuth(tests.USER_CREDENTIALS["unituser"]).token(module_client)
response = module_client.put(
tests.DDSEndpoint.PROJECT_BUSY,
headers=token,
)
assert response.status_code == http.HTTPStatus.BAD_REQUEST
assert "Required data missing" in response.json.get("message")

# Unit Admin
token = tests.UserAuth(tests.USER_CREDENTIALS["unitadmin"]).token(module_client)
response = module_client.put(
tests.DDSEndpoint.PROJECT_BUSY,
headers=token,
)
assert response.status_code == http.HTTPStatus.BAD_REQUEST
assert "Required data missing" in response.json.get("message")
def test_set_busy_invalid_version(module_client):
"""ProjectBusy endpoint is empty and should only return error message about invalid version."""
# Error messages
major_version_error: str = "You're using an old CLI version, please upgrade to the latest one."
busy_error: str = "Your CLI version is trying to use functionality which is no longer in use. Upgrade your version to the latest one and run your command again."

# Researcher
token = tests.UserAuth(tests.USER_CREDENTIALS["researchuser"]).token(module_client)
response = module_client.put(
tests.DDSEndpoint.PROJECT_BUSY,
headers=token,
)
assert response.status_code == http.HTTPStatus.BAD_REQUEST
assert "Required data missing" in response.json.get("message")


def test_set_busy_no_busy(module_client):
"""busy bool required."""
for username in ["researchuser", "projectowner", "unituser", "unitadmin"]:
for username in ["superadmin", "researchuser", "projectowner", "unituser", "unitadmin"]:
# Get user
user = models.User.query.filter_by(username=username).one_or_none()
assert user
Expand All @@ -1388,125 +1352,20 @@ def test_set_busy_no_busy(module_client):

# Authenticate and run
token = tests.UserAuth(tests.USER_CREDENTIALS[username]).token(module_client)
response = module_client.put(
tests.DDSEndpoint.PROJECT_BUSY,
headers=token,
query_string={"project": project.public_id},
json={"something": "notabool"},
)
assert response.status_code == http.HTTPStatus.BAD_REQUEST
assert "Missing information about setting busy or not busy." in response.json.get("message")


def test_set_busy_true(module_client):
"""Set project as busy."""
for username in ["researchuser", "projectowner", "unituser", "unitadmin"]:
# Get user
user = models.User.query.filter_by(username=username).one_or_none()
assert user

# Get project
project = user.projects[0]
assert project

# Set project to not busy
project.busy = False
db.session.commit()
assert not project.busy

# Authenticate and run
token = tests.UserAuth(tests.USER_CREDENTIALS[username]).token(module_client)
response = module_client.put(
tests.DDSEndpoint.PROJECT_BUSY,
headers=token,
query_string={"project": project.public_id},
json={"busy": True},
)
assert response.status_code == http.HTTPStatus.OK
assert f"Project {project.public_id} was set to busy." in response.json.get("message")


def test_set_not_busy_project_already_not_busy(module_client):
"""Set project as busy."""
for username in ["researchuser", "projectowner", "unituser", "unitadmin"]:
# Get user
user = models.User.query.filter_by(username=username).one_or_none()
assert user

# Get project
project = user.projects[0]
assert project

# Set project to not busy
project.busy = False
db.session.commit()
assert not project.busy

# Authenticate and run
token = tests.UserAuth(tests.USER_CREDENTIALS[username]).token(module_client)
response = module_client.put(
tests.DDSEndpoint.PROJECT_BUSY,
headers=token,
query_string={"project": project.public_id},
json={"busy": False},
)
assert response.status_code == http.HTTPStatus.OK
assert f"The project is already not busy, cannot proceed." in response.json.get("message")


def test_set_busy_false(module_client):
"""Set project as not busy."""
for username in ["researchuser", "projectowner", "unituser", "unitadmin"]:
# Get user
user = models.User.query.filter_by(username=username).one_or_none()
assert user

# Get project
project = user.projects[0]
assert project

# Set project to busy
project.busy = True
db.session.commit()
assert project.busy

# Authenticate and run
token = tests.UserAuth(tests.USER_CREDENTIALS[username]).token(module_client)
response = module_client.put(
tests.DDSEndpoint.PROJECT_BUSY,
headers=token,
query_string={"project": project.public_id},
json={"busy": False},
)
assert response.status_code == http.HTTPStatus.OK
assert f"Project {project.public_id} was set to not busy." in response.json.get("message")


def test_set_busy_project_already_busy(module_client):
"""Set a busy project as busy."""
for username in ["researchuser", "projectowner", "unituser", "unitadmin"]:
# Get user
user = models.User.query.filter_by(username=username).one_or_none()
assert user

# Get project
project = user.projects[0]
assert project

# Set project to busy
project.busy = True
db.session.commit()
assert project.busy

token = tests.UserAuth(tests.USER_CREDENTIALS[username]).token(module_client)
response = module_client.put(
tests.DDSEndpoint.PROJECT_BUSY,
headers=token,
query_string={"project": project.public_id},
json={"busy": True},
)
assert response.status_code == http.HTTPStatus.OK
assert "The project is already busy, cannot proceed." in response.json.get("message")
for version, error_message in {
token["X-CLI-Version"]: busy_error,
"1.9.9": major_version_error,
"2.1.9": busy_error,
}.items():
token["X-CLI-Version"] = version
response = module_client.put(
tests.DDSEndpoint.PROJECT_BUSY,
headers=token,
query_string={"project": project.public_id},
json={"something": "notabool"},
)
assert response.status_code == http.HTTPStatus.FORBIDDEN
assert error_message in response.json.get("message")


# Project usage
Expand Down
15 changes: 0 additions & 15 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,6 @@ def test_block_if_maintenance_active_none_approved_users(client: flask.testing.F
assert response.status_code == http.HTTPStatus.SERVICE_UNAVAILABLE
assert response.json and response.json.get("message") == "Maintenance of DDS is ongoing."

# ProjectBusy - "/proj/busy"
response = client.put(
DDSEndpoint.PROJECT_BUSY,
headers=token,
)
assert response.status_code == http.HTTPStatus.SERVICE_UNAVAILABLE
assert response.json and response.json.get("message") == "Maintenance of DDS is ongoing."

# ProjectInfo - "/proj/info"
response = client.get(
DDSEndpoint.PROJECT_INFO,
Expand Down Expand Up @@ -631,13 +623,6 @@ def test_block_if_maintenance_active_superadmin_ok(client: flask.testing.FlaskCl
)
assert response.status_code == http.HTTPStatus.FORBIDDEN

# ProjectBusy - "/proj/busy"
response = client.put(
DDSEndpoint.PROJECT_BUSY,
headers=token,
)
assert response.status_code == http.HTTPStatus.FORBIDDEN

# ProjectInfo - "/proj/info"
response = client.get(
DDSEndpoint.PROJECT_INFO,
Expand Down