diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5a51cf239..ed7a87711 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,22 +15,22 @@ jobs: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - uses: actions/checkout@v4 - - name: Fetch base and install Poetry - run: | - git fetch origin ${{github.base_ref}} - pipx install poetry==2.1.1 - - name: Setup Python uses: actions/setup-python@v4 with: python-version: '3.11' - cache: 'poetry' + + - name: Fetch base and install Poetry + run: | + git fetch origin ${{github.base_ref}} + pipx install poetry==2.1.1 - name: Copy base.html for use in unit tests run: | cp --force designsafe/templates/base.j2 designsafe/templates/base.html - run: | + env use python3.11 poetry sync - name: Run Server-side unit tests and generate coverage report @@ -42,19 +42,19 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Fetch base and install Poetry - run: | - git fetch origin ${{github.base_ref}} - pipx install poetry==2.1.1 - - name: Setup Python uses: actions/setup-python@v4 with: python-version: '3.11' - cache: 'poetry' + + - name: Fetch base and install Poetry + run: | + git fetch origin ${{github.base_ref}} + pipx install poetry==2.1.1 - name: Install Python Packages run: | + env use python3.11 poetry sync - name: Run Server-side linting with pytest diff --git a/designsafe/apps/workspace/tests.py b/designsafe/apps/workspace/tests.py index 0e09e8851..4d69fb18a 100644 --- a/designsafe/apps/workspace/tests.py +++ b/designsafe/apps/workspace/tests.py @@ -2,6 +2,7 @@ from mock import patch from django.test import TestCase from .models.app_descriptions import AppDescription +from .models.user_favorites import UserFavorite from unittest import skip from django.urls import reverse from django.contrib.auth import get_user_model @@ -114,3 +115,84 @@ def test_job_submit_parse_urls(self): def test_licensed_apps(self): # TODO: test to make sure the licensing stuff works pass + +class TestUserFavoritesApiViews(TestCase): + fixtures = ["user-data", "auth"] + + def setUp(self): + user = get_user_model().objects.get(pk=2) + user.set_password("user/password") + user.save() + self.client.login(username="ds_user", password="user/password") + + def test_add_favorite(self): + url = reverse("workspace_api:user_favorite_list") + response = self.client.post( + url, + json.dumps({"tool_id": "test-app-1"}), + content_type="application/json", + ) + data = response.json() + + self.assertEqual(response.status_code, 200) + self.assertTrue(data["success"]) + self.assertTrue(data["created"]) + self.assertTrue( + UserFavorite.objects.filter(user__pk=2, tool_id="test-app-1").exists() + ) + + def test_add_favorite_missing_tool_id(self): + url = reverse("workspace_api:user_favorite_list") + response = self.client.post( + url, json.dumps({}), content_type="application/json" + ) + self.assertEqual(response.status_code, 400) + + def test_list_favorites(self): + url = reverse("workspace_api:user_favorite_list") + self.client.post( + url, + json.dumps({"tool_id": "test-app-1"}), + content_type="application/json", + ) + + response = self.client.get(url) + data = response.json() + + self.assertEqual(response.status_code, 200) + self.assertEqual(len(data), 1) + self.assertEqual(data[0]["tool_id"], "test-app-1") + + def test_remove_favorite(self): + add_url = reverse("workspace_api:user_favorite_list") + self.client.post( + add_url, + json.dumps({"tool_id": "test-app-1"}), + content_type="application/json", + ) + self.assertTrue( + UserFavorite.objects.filter(user__pk=2, tool_id="test-app-1").exists() + ) + + remove_url = reverse("workspace_api:remove_favorite_tool") + response = self.client.post( + remove_url, + json.dumps({"tool_id": "test-app-1"}), + content_type="application/json", + ) + data = response.json() + + self.assertEqual(response.status_code, 200) + self.assertTrue(data["success"]) + self.assertFalse( + UserFavorite.objects.filter(user__pk=2, tool_id="test-app-1").exists() + ) + + def test_remove_favorite_missing_tool_id(self): + url = reverse("workspace_api:remove_favorite_tool") + response = self.client.post( + url, json.dumps({}), content_type="application/json" + ) + self.assertEqual(response.status_code, 400) + +