<commit_before>"""Tests for authentication tools"""
import unittest
import mock

from utility.authentication_tools import check_resource_availability


class TestResponse:
    """Mock Test Response after a request"""
    @property
    def content(self):
        """Mock the content from Requests.get(params).content"""
        return '["foo"]'


class TestUser:
    """Mock user"""
    @property
    def user_id(self):
        """Mockes user id. Used in Flask.g.user_session.user_id"""
        return "Jane"


class TestUserSession:
    """Mock user session"""
    @property
    def user_session(self):
        """Mock user session. Mocks Flask.g.user_session object"""
        return TestUser()


class TestCheckResourceAvailability(unittest.TestCase):
    """Test methods related to checking the resource availability"""
    @mock.patch('utility.authentication_tools.add_new_resource')
    @mock.patch('utility.authentication_tools.Redis')
    @mock.patch('utility.authentication_tools.g')
    @mock.patch('utility.authentication_tools.get_resource_id')
    def test_check_resource_availability_default_mask(
            self,
            resource_id_mock,
            g_mock,
            redis_mock,
            add_new_resource_mock):
        """Test the resource availability with default mask"""
        resource_id_mock.return_value = 1
        g_mock.return_value = mock.Mock()
        redis_mock.smembers.return_value = []
        test_dataset = mock.MagicMock()
        type(test_dataset).type = mock.PropertyMock(return_value="Test")
        add_new_resource_mock.return_value = {"default_mask": 2}
        self.assertEqual(check_resource_availability(test_dataset), 2)

    @mock.patch('utility.authentication_tools.requests.get')
    @mock.patch('utility.authentication_tools.add_new_resource')
    @mock.patch('utility.authentication_tools.Redis')
    @mock.patch('utility.authentication_tools.g')
    @mock.patch('utility.authentication_tools.get_resource_id')
    def test_check_resource_availability_non_default_mask(
            self,
            resource_id_mock,
            g_mock,
            redis_mock,
            add_new_resource_mock,
            requests_mock):
        """Test the resource availability with default mask"""
        resource_id_mock.return_value = 1
        g_mock.return_value = mock.Mock()
        redis_mock.smembers.return_value = []
        add_new_resource_mock.return_value = {"default_mask": 2}
        requests_mock.return_value = TestResponse()
        test_dataset = mock.MagicMock()
        type(test_dataset).type = mock.PropertyMock(return_value="Test")
        self.assertEqual(check_resource_availability(test_dataset),
                         ['foo'])

    @mock.patch('utility.authentication_tools.webqtlConfig.SUPER_PRIVILEGES',
                "SUPERUSER")
    @mock.patch('utility.authentication_tools.requests.get')
    @mock.patch('utility.authentication_tools.add_new_resource')
    @mock.patch('utility.authentication_tools.Redis')
    @mock.patch('utility.authentication_tools.g', TestUserSession())
    @mock.patch('utility.authentication_tools.get_resource_id')
    def test_check_resource_availability_of_super_user(
            self,
            resource_id_mock,
            redis_mock,
            add_new_resource_mock,
            requests_mock):
        """Test the resource availability if the user is the super user"""
        resource_id_mock.return_value = 1
        redis_mock.smembers.return_value = ["Jane"]
        add_new_resource_mock.return_value = {"default_mask": 2}
        requests_mock.return_value = TestResponse()
        test_dataset = mock.MagicMock()
        type(test_dataset).type = mock.PropertyMock(return_value="Test")
        self.assertEqual(check_resource_availability(test_dataset),
                         "SUPERUSER")

    @mock.patch('utility.authentication_tools.webqtlConfig.DEFAULT_PRIVILEGES',
                "<NAME>")
    def test_check_resource_availability_string_dataset(self):
        """Test the resource availability if the dataset is a string"""
        self.assertEqual(check_resource_availability("Test"),
                         "<NAME>")

    @mock.patch('utility.authentication_tools.webqtlConfig.DEFAULT_PRIVILEGES',
                "<NAME>")
    def test_check_resource_availability_temp(self):
        """Test the resource availability if the dataset is a string"""
        test_dataset = mock.MagicMock()
        type(test_dataset).type = mock.PropertyMock(return_value="Temp")
        self.assertEqual(check_resource_availability(test_dataset),
                         "<NAME>")<commit_msg>Add tests for "add_new_resource" method<commit_after>"""Tests for authentication tools"""
import unittest
import mock

from utility.authentication_tools import check_resource_availability
from utility.authentication_tools import add_new_resource


class TestResponse:
    """Mock Test Response after a request"""
    @property
    def content(self):
        """Mock the content from Requests.get(params).content"""
        return '["foo"]'


class TestUser:
    """Mock user"""
    @property
    def user_id(self):
        """Mockes user id. Used in Flask.g.user_session.user_id"""
        return "Jane"


class TestUserSession:
    """Mock user session"""
    @property
    def user_session(self):
        """Mock user session. Mocks Flask.g.user_session object"""
        return TestUser()


def mock_add_resource(resource_ob, update=False):
    return resource_ob


class TestCheckResourceAvailability(unittest.TestCase):
    """Test methods related to checking the resource availability"""
    @mock.patch('utility.authentication_tools.add_new_resource')
    @mock.patch('utility.authentication_tools.Redis')
    @mock.patch('utility.authentication_tools.g')
    @mock.patch('utility.authentication_tools.get_resource_id')
    def test_check_resource_availability_default_mask(
            self,
            resource_id_mock,
            g_mock,
            redis_mock,
            add_new_resource_mock):
        """Test the resource availability with default mask"""
        resource_id_mock.return_value = 1
        g_mock.return_value = mock.Mock()
        redis_mock.smembers.return_value = []
        test_dataset = mock.MagicMock()
        type(test_dataset).type = mock.PropertyMock(return_value="Test")
        add_new_resource_mock.return_value = {"default_mask": 2}
        self.assertEqual(check_resource_availability(test_dataset), 2)

    @mock.patch('utility.authentication_tools.requests.get')
    @mock.patch('utility.authentication_tools.add_new_resource')
    @mock.patch('utility.authentication_tools.Redis')
    @mock.patch('utility.authentication_tools.g')
    @mock.patch('utility.authentication_tools.get_resource_id')
    def test_check_resource_availability_non_default_mask(
            self,
            resource_id_mock,
            g_mock,
            redis_mock,
            add_new_resource_mock,
            requests_mock):
        """Test the resource availability with default mask"""
        resource_id_mock.return_value = 1
        g_mock.return_value = mock.Mock()
        redis_mock.smembers.return_value = []
        add_new_resource_mock.return_value = {"default_mask": 2}
        requests_mock.return_value = TestResponse()
        test_dataset = mock.MagicMock()
        type(test_dataset).type = mock.PropertyMock(return_value="Test")
        self.assertEqual(check_resource_availability(test_dataset),
                         ['foo'])

    @mock.patch('utility.authentication_tools.webqtlConfig.SUPER_PRIVILEGES',
                "SUPERUSER")
    @mock.patch('utility.authentication_tools.requests.get')
    @mock.patch('utility.authentication_tools.add_new_resource')
    @mock.patch('utility.authentication_tools.Redis')
    @mock.patch('utility.authentication_tools.g', TestUserSession())
    @mock.patch('utility.authentication_tools.get_resource_id')
    def test_check_resource_availability_of_super_user(
            self,
            resource_id_mock,
            redis_mock,
            add_new_resource_mock,
            requests_mock):
        """Test the resource availability if the user is the super user"""
        resource_id_mock.return_value = 1
        redis_mock.smembers.return_value = ["Jane"]
        add_new_resource_mock.return_value = {"default_mask": 2}
        requests_mock.return_value = TestResponse()
        test_dataset = mock.MagicMock()
        type(test_dataset).type = mock.PropertyMock(return_value="Test")
        self.assertEqual(check_resource_availability(test_dataset),
                         "SUPERUSER")

    @mock.patch('utility.authentication_tools.webqtlConfig.DEFAULT_PRIVILEGES',
                "<NAME>")
    def test_check_resource_availability_string_dataset(self):
        """Test the resource availability if the dataset is a string"""
        self.assertEqual(check_resource_availability("Test"),
                         "<NAME>")

    @mock.patch('utility.authentication_tools.webqtlConfig.DEFAULT_PRIVILEGES',
                "<NAME>")
    def test_check_resource_availability_temp(self):
        """Test the resource availability if the dataset is a string"""
        test_dataset = mock.MagicMock()
        type(test_dataset).type = mock.PropertyMock(return_value="Temp")
        self.assertEqual(check_resource_availability(test_dataset),
                         "<NAME>")


class TestAddNewResource(unittest.TestCase):
    """Test cases for add_new_resource method"""
    @mock.patch('utility.authentication_tools.webqtlConfig.DEFAULT_PRIVILEGES',
                "<NAME>")
    @mock.patch('utility.authentication_tools.add_resource', mock_add_resource)
    @mock.patch('utility.authentication_tools.get_group_code')
    def test_add_new_resource_if_publish_datatype(self, group_code_mock):
        """Test add_new_resource if dataset type is 'publish'"""
        group_code_mock.return_value = "Test"
        test_dataset = mock.MagicMock()
        type(test_dataset).type = mock.PropertyMock(return_value="Publish")
        type(test_dataset).id = mock.PropertyMock(return_value=10)
        expected_value = {
            "owner_id": "none",
            "default_mask": "<NAME>",
            "group_masks": {},
            "name": "Test_None",
            "data": {
                "dataset": 10,
                "trait": None
            },
            "type": "dataset-publish"
        }
        self.assertEqual(add_new_resource(test_dataset),
                         expected_value)

    @mock.patch('utility.authentication_tools.webqtlConfig.DEFAULT_PRIVILEGES',
                "<NAME>")
    @mock.patch('utility.authentication_tools.add_resource', mock_add_resource)
    @mock.patch('utility.authentication_tools.get_group_code')
    def test_add_new_resource_if_geno_datatype(self, group_code_mock):
        """Test add_new_resource if dataset type is 'geno'"""
        group_code_mock.return_value = "Test"
        test_dataset = mock.MagicMock()
        type(test_dataset).name = mock.PropertyMock(return_value="Geno")
        type(test_dataset).type = mock.PropertyMock(return_value="Geno")
        type(test_dataset).id = mock.PropertyMock(return_value=20)
        expected_value = {
            "owner_id": "none",
            "default_mask": "<NAME>",
            "group_masks": {},
            "name": "Geno",
            "data": {
                "dataset": 20,
            },
            "type": "dataset-geno"
        }
        self.assertEqual(add_new_resource(test_dataset),
                         expected_value)

    @mock.patch('utility.authentication_tools.webqtlConfig.DEFAULT_PRIVILEGES',
                "<NAME>")
    @mock.patch('utility.authentication_tools.add_resource', mock_add_resource)
    @mock.patch('utility.authentication_tools.get_group_code')
    def test_add_new_resource_if_other_datatype(self, group_code_mock):
        """Test add_new_resource if dataset type is not 'geno' or 'publish'"""
        group_code_mock.return_value = "Test"
        test_dataset = mock.MagicMock()
        type(test_dataset).name = mock.PropertyMock(return_value="Geno")
        type(test_dataset).type = mock.PropertyMock(return_value="other")
        type(test_dataset).id = mock.PropertyMock(return_value=20)
        expected_value = {
            "owner_id": "none",
            "default_mask": "<NAME>",
            "group_masks": {},
            "name": "Geno",
            "data": {
                "dataset": 20,
            },
            "type": "dataset-probeset"
        }
        self.assertEqual(add_new_resource(test_dataset),
                         expected_value)
{'fraction_non_alphanumeric': 0.10625691262442724, 'fraction_numerical': 0.002054036972665508, 'mean_word_length': 2.982069833280906, 'pattern_counts': {'":': 28, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3743498', 'n_tokens_mistral': 3665, 'n_tokens_neox': 3586, 'n_words': 574}