Skip to content

Commit

Permalink
Add tests for Dandiset star functionality and update API responses
Browse files Browse the repository at this point in the history
- Implemented new test cases in `test_dandiset.py` to verify star/unstar actions, star count, and starred status for Dandisets.
- Updated API responses in `DandisetViewSet` to return the current star count when starring or unstarring a Dandiset.
- Added tests for unauthenticated access to star functionality and listing starred Dandisets.
  • Loading branch information
bendichter committed Dec 26, 2024
1 parent 61295ab commit dd9bd64
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
83 changes: 83 additions & 0 deletions dandiapi/api/tests/test_dandiset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1225,3 +1225,86 @@ def test_dandiset_rest_clear_active_uploads(
response = authenticated_api_client.get(f'/api/dandisets/{ds.identifier}/uploads/').json()
assert response['count'] == 0
assert len(response['results']) == 0


@pytest.mark.django_db
def test_dandiset_star(api_client, user, dandiset):
api_client.force_authenticate(user=user)
response = api_client.post(f'/api/dandisets/{dandiset.identifier}/star/')
assert response.status_code == 200
assert response.data == {'count': 1}
assert dandiset.stars.count() == 1
assert dandiset.stars.first().user == user


@pytest.mark.django_db
def test_dandiset_unstar(api_client, user, dandiset):
api_client.force_authenticate(user=user)
# First star it
api_client.post(f'/api/dandisets/{dandiset.identifier}/star/')
assert dandiset.stars.count() == 1

# Then unstar it
response = api_client.post(f'/api/dandisets/{dandiset.identifier}/unstar/')
assert response.status_code == 200
assert response.data == {'count': 0}
assert dandiset.stars.count() == 0


@pytest.mark.django_db
def test_dandiset_star_unauthenticated(api_client, dandiset):
response = api_client.post(f'/api/dandisets/{dandiset.identifier}/star/')
assert response.status_code == 401


@pytest.mark.django_db
def test_dandiset_star_count(api_client, user_factory, dandiset):
users = [user_factory() for _ in range(3)]
for user in users:
api_client.force_authenticate(user=user)
api_client.post(f'/api/dandisets/{dandiset.identifier}/star/')

response = api_client.get(f'/api/dandisets/{dandiset.identifier}/')
assert response.data['star_count'] == 3


@pytest.mark.django_db
def test_dandiset_is_starred(api_client, user, dandiset):
# Test unauthenticated
response = api_client.get(f'/api/dandisets/{dandiset.identifier}/')
assert response.data['is_starred'] is False

# Test authenticated but not starred
api_client.force_authenticate(user=user)
response = api_client.get(f'/api/dandisets/{dandiset.identifier}/')
assert response.data['is_starred'] is False

# Test after starring
api_client.post(f'/api/dandisets/{dandiset.identifier}/star/')
response = api_client.get(f'/api/dandisets/{dandiset.identifier}/')
assert response.data['is_starred'] is True


@pytest.mark.django_db
def test_dandiset_list_starred(api_client, user, dandiset_factory):
api_client.force_authenticate(user=user)
dandisets = [dandiset_factory() for _ in range(3)]

# Star 2 out of 3 dandisets
api_client.post(f'/api/dandisets/{dandisets[0].identifier}/star/')
api_client.post(f'/api/dandisets/{dandisets[1].identifier}/star/')

# List starred dandisets
response = api_client.get('/api/dandisets/starred/')
assert response.status_code == 200
assert response.data['count'] == 2
assert {d['identifier'] for d in response.data['results']} == {
dandisets[0].identifier,
dandisets[1].identifier,
}


@pytest.mark.django_db
def test_dandiset_list_starred_unauthenticated(api_client):
response = api_client.get('/api/dandisets/starred/')
assert response.status_code == 401
4 changes: 2 additions & 2 deletions dandiapi/api/views/dandiset.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ def star(self, request, dandiset__pk):
raise NotAuthenticated
dandiset = self.get_object()
dandiset.star(request.user)
return Response(None, status=status.HTTP_200_OK)
return Response({'count': dandiset.star_count}, status=status.HTTP_200_OK)

@swagger_auto_schema(
methods=['POST'],
Expand All @@ -542,7 +542,7 @@ def unstar(self, request, dandiset__pk):
raise NotAuthenticated
dandiset = self.get_object()
dandiset.unstar(request.user)
return Response(None, status=status.HTTP_200_OK)
return Response({'count': dandiset.star_count}, status=status.HTTP_200_OK)

@swagger_auto_schema(
methods=['GET'],
Expand Down

0 comments on commit dd9bd64

Please sign in to comment.