Skip to content

Commit

Permalink
Merge pull request #1302 from ScilifelabDataCentre/DDS-1306_limit_pro…
Browse files Browse the repository at this point in the history
…ject_listing

Dds 1306 limit project listing
  • Loading branch information
i-oden authored Oct 19, 2022
2 parents ced2433 + f6c434c commit 4e4cbb5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,7 @@ Please add a _short_ line describing the PR you make, if the PR implements a spe

- Bug fix: Fix the Invite.projects database model ([#1290](https://github.com/ScilifelabDataCentre/dds_web/pull/1290))
- New endpoint: ListInvites - list invites ([#1294](https://github.com/ScilifelabDataCentre/dds_web/pull/1294))

## Sprint (2022-10-14 - 2022-10-28)

- Limit projects listing to active projects only; a `--show-all` flag can be used for listing all projects, active and inactive ([#1302](https://github.com/ScilifelabDataCentre/dds_web/pull/1302))
14 changes: 13 additions & 1 deletion dds_web/api/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,20 @@ def format_project_dict(self, current_user):
"Unit Personnel",
]

# Get info for projects
get_all = flask.request.json.get("show_all", False) if flask.request.json else False
all_filters = (
[] if get_all else [models.Project.is_active == True]
) # Default is to only get active projects
all_filters.append(
models.Project.public_id.in_([x.public_id for x in current_user.projects])
)

# Apply the filters
user_projects = models.Project.query.filter(sqlalchemy.and_(*all_filters)).all()

# Get info for all projects
for p in current_user.projects:
for p in user_projects:
project_info = {
"Project ID": p.public_id,
"Title": p.title,
Expand Down
41 changes: 41 additions & 0 deletions tests/test_project_listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import unittest

# Own
from dds_web import db
from dds_web.database import models
import tests

Expand Down Expand Up @@ -57,6 +58,46 @@ def test_list_proj_unit_user(client):
assert "Usage" in public_project.keys() and public_project["Usage"] is not None


def test_list_only_active_projects_unit_user(client):
"""Unit admin should be able to list only active projects without --show-all flag"""

# set one of the project as inactive
inactive_project: models.Project = models.Project.query.first()
inactive_project.is_active = False
db.session.commit()

token = tests.UserAuth(tests.USER_CREDENTIALS["unitadmin"]).token(client)
response = client.get(
tests.DDSEndpoint.LIST_PROJ,
headers=token,
json={"usage": True},
content_type="application/json",
)

assert response.status_code == http.HTTPStatus.OK
assert len(response.json.get("project_info")) == 4


def test_list_all_projects_unit_user(client):
"""Unit admin should be able to list inactive projects with the --show-all flag"""

# set one of the project as inactive
inactive_project: models.Project = models.Project.query.first()
inactive_project.is_active = False
db.session.commit()

token = tests.UserAuth(tests.USER_CREDENTIALS["unitadmin"]).token(client)
response = client.get(
tests.DDSEndpoint.LIST_PROJ,
headers=token,
json={"usage": True, "show_all": True},
content_type="application/json",
)

assert response.status_code == http.HTTPStatus.OK
assert len(response.json.get("project_info")) == 5


def test_proj_private_successful(client):
"""Successfully get the private key"""

Expand Down

0 comments on commit 4e4cbb5

Please sign in to comment.