Skip to content

Commit

Permalink
Add unit test for ProjectActionView #488
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Druez <tdruez@nexb.com>
  • Loading branch information
tdruez committed Sep 14, 2023
1 parent 330689a commit 8ea8b8b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
31 changes: 31 additions & 0 deletions scanpipe/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import json
import shutil
import uuid
from pathlib import Path
from unittest import mock

Expand Down Expand Up @@ -153,6 +154,36 @@ def test_scanpipe_views_project_list_filters_exclude_page(self, mock_paginate_by
expected = '<a href="?sort=" class="dropdown-item is-active">Newest</a>'
self.assertContains(response, expected)

def test_scanpipe_views_project_actions_view(self):
url = reverse("project_action")
response = self.client.get(url)
self.assertEqual(405, response.status_code)

response = self.client.post(url)
self.assertEqual(404, response.status_code)

data = {"action": "does_not_exists"}
response = self.client.post(url, data=data)
self.assertEqual(404, response.status_code)

data = {"action": "delete"}
response = self.client.post(url, data=data)
self.assertEqual(404, response.status_code)

random_uuid = uuid.uuid4()
data = {
"action": "delete",
"selected_ids": f"{self.project1.uuid},{random_uuid}",
}
response = self.client.post(url, data=data, follow=True)
self.assertRedirects(response, reverse("project_list"))
expected = '<div class="message-body">1 projects have been delete.</div>'
self.assertContains(response, expected, html=True)
expected = (
f'<div class="message-body">Project {random_uuid} does not exist.</div>'
)
self.assertContains(response, expected, html=True)

def test_scanpipe_views_project_details_is_archived(self):
url = self.project1.get_absolute_url()
expected1 = "WARNING: This project is archived and read-only."
Expand Down
3 changes: 2 additions & 1 deletion scanpipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core.exceptions import SuspiciousFileOperation
from django.core.exceptions import ValidationError
from django.core.files.storage.filesystem import FileSystemStorage
from django.db.models import Prefetch
from django.db.models.manager import Manager
Expand Down Expand Up @@ -981,7 +982,7 @@ def perform_action(self, action, project_uuid, action_kwargs=None):
messages.error(self.request, f"Project {project_uuid} does not exist.")
except RunInProgressError as error:
messages.error(self.request, str(error))
except AttributeError:
except (AttributeError, ValidationError):
raise Http404

def get_success_message(self, action, count):
Expand Down

0 comments on commit 8ea8b8b

Please sign in to comment.