aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBonfaceKilz2020-09-15 23:19:30 +0300
committerBonfaceKilz2020-09-16 00:12:40 +0300
commit6e776e94bdc2cea9923d70ff836ce1c02a1e1af9 (patch)
tree5cd63bf4c68d4bfd37754b3853ae22a654099d95
parent77d026e042a3eac9e3de4177ee374a37f2e24127 (diff)
downloadgenenetwork2-6e776e94bdc2cea9923d70ff836ce1c02a1e1af9.tar.gz
Add tests for authentication tools
Catches bugs in: - https://github.com/genenetwork/genenetwork2/pull/422/commits/70dbeeb5832711ed5271434e482c18bc7ea095b8 * wqflask/tests/utility/test_authentication_tools.py: New file. Add tests for "check_resource_availability".
-rw-r--r--wqflask/tests/utility/test_authentication_tools.py113
1 files changed, 113 insertions, 0 deletions
diff --git a/wqflask/tests/utility/test_authentication_tools.py b/wqflask/tests/utility/test_authentication_tools.py
new file mode 100644
index 00000000..59c53879
--- /dev/null
+++ b/wqflask/tests/utility/test_authentication_tools.py
@@ -0,0 +1,113 @@
+"""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',
+ "John Doe")
+ def test_check_resource_availability_string_dataset(self):
+ """Test the resource availability if the dataset is a string"""
+ self.assertEqual(check_resource_availability("Test"),
+ "John Doe")
+
+ @mock.patch('utility.authentication_tools.webqtlConfig.DEFAULT_PRIVILEGES',
+ "John Doe")
+ 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),
+ "John Doe")