Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@


class QueuedSampleNotFoundException(Exception):
"""Custom exception handler for queued sample not found."""

def __init__(self, message: str) -> None:
Exception.__init__(self)
"""Raised when a queued sample cannot be found by the given ID."""

def __init__(self, message="Queued sample not found."):
# By providing a default, all existing raise sites that call
# this exception without arguments will still work, and new
# calls can optionally provide a custom message.
super().__init__(message)
self.message = message


Expand Down
57 changes: 28 additions & 29 deletions tests/test_upload/test_controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,35 +170,34 @@ def test_upload_ftp_forbidden_extension(self, mock_shutil, mock_hash, mock_magic
QueuedSample.sha == filehash).first()
self.assertEqual(queued_sample, None)

# TODO: The methods below are not working due to decorator login_required not being mocked
# @mock.patch('mod_upload.controllers.login_required', side_effect=mock_decorator)
# def test_link_id_confirm_invalid(self, mock_login):
# """
# Try to confirm link for invalid sample and queue.
# """
# from mod_upload.controllers import link_id_confirm, QueuedSampleNotFoundException

# with self.assertRaises(QueuedSampleNotFoundException):
# link_id_confirm(1000, 1000)

# @mock.patch('mod_upload.controllers.redirect')
# @mock.patch('mod_upload.controllers.login_required', side_effect=mock_decorator)
# @mock.patch('mod_upload.controllers.QueuedSample')
# @mock.patch('mod_upload.controllers.Sample')
# def test_link_id_confirm(self, mock_sample, mock_queue, mock_login, mock_redirect):
# """
# Test confirm link for valid sample and queue.
# """
# from mod_upload.controllers import link_id_confirm

# mock_queue.query.filter.return_value.first.return_value.user_id = g.user
# mock_sample.query.filter.return_value.first.return_value.upload.user_id = g.user

# response = link_id_confirm(1, 1)

# self.assertEqual(response, mock_redirect())
# mock_queue.query.filter.assert_called_once_with(mock_queue.id == 1)
# mock_sample.query.filter.assert_called_once_with(mock_sample.id == 1)
def test_link_id_confirm_invalid(self):
"""Try to confirm link for invalid sample and queue."""
from mod_upload.controllers import (QueuedSampleNotFoundException,
link_id_confirm)

with self.assertRaises(QueuedSampleNotFoundException):
link_id_confirm.__wrapped__(1000, 1000)

@mock.patch('mod_upload.controllers.redirect')
@mock.patch('mod_upload.controllers.QueuedSample')
@mock.patch('mod_upload.controllers.Sample')
@mock.patch('mod_upload.controllers.g')
def test_link_id_confirm_valid(self, mock_g, mock_sample, mock_queue, mock_redirect):
"""Test confirm link for valid sample and queue."""
from mod_upload.controllers import link_id_confirm

# Mock g.user to have an 'id' attribute to satisfy the controller
mock_g.user.id = 1

# Ensure the mocked database queries return an object owned by that user
mock_queue.query.filter.return_value.first.return_value.user_id = mock_g.user.id
mock_sample.query.filter.return_value.first.return_value.upload.user_id = mock_g.user.id

response = link_id_confirm.__wrapped__(1, 1)

self.assertEqual(response, mock_redirect())
mock_queue.query.filter.assert_called_once()
mock_sample.query.filter.assert_called_once()

def test_create_hash_for_sample(self):
"""Test creating hash for temp file."""
Expand Down