diff --git a/.vscode/settings.json b/.vscode/settings.json index 5749b6b..ff72368 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,16 +1,4 @@ { "python.formatting.provider": "autopep8", - "python.analysis.typeCheckingMode": "off", - "python.testing.unittestArgs": [ - "-v", - "-s", - "./backend", - "-p", - "*_test.py" - ], - "python.testing.pytestEnabled": true, - "python.testing.unittestEnabled": false, - "python.testing.pytestArgs": [ - "backend" - ] + "python.analysis.typeCheckingMode": "off" } \ No newline at end of file diff --git a/backend/test_blackboard_scraper.py b/backend/test_blackboard_scraper.py index d88f1cd..d2c6e77 100644 --- a/backend/test_blackboard_scraper.py +++ b/backend/test_blackboard_scraper.py @@ -1,16 +1,3 @@ -import os -import random -import time -import pytest -from dotenv import load_dotenv -from blackboard_scraper import BlackboardSession -from unittest.mock import MagicMock, patch -from usernames import usernames - - - -load_dotenv() - import os import random import time @@ -72,202 +59,297 @@ """ -@pytest.fixture -def session(): - username = os.getenv('TEST_USERNAME') - password = os.getenv('TEST_PASSWORD') - return BlackboardSession(username=username, password=password) +class TestBlackboardSession(unittest.TestCase): -@pytest.fixture -def random_username(): - return random.choice(list(usernames)) + def setUp(self): + load_dotenv() + self.username = os.getenv('TEST_USERNAME') + self.password = os.getenv('TEST_PASSWORD') + # * Login Tests *# -def test_valid_credentials_login(session): - session.login() - response = session.get_response() - expected_message = "Login successful." - assert response == expected_message + def test_valid_credentials_login(self): + session = BlackboardSession( + username=self.username, password=self.password) -def test_invalid_both_login(): - session = BlackboardSession( - username='InvalidUsername', password='InvalidPassword') - session.login() - response = session.get_response() - expected_error_message = "The username you entered cannot be identified." - assert response == expected_error_message + # Execute login + session.login() + response = session.get_response() -def test_failed_login_invalid_password(random_username): - session = BlackboardSession( - username=random_username, password='InvalidPassword') - session.login() - response = session.get_response() - error_messages = [ - "The password you entered was incorrect.", "Account has been disabled."] - assert response in error_messages + # Check the response + expected_message = "Login successful." + self.assertEqual(response, expected_message) -def test_failed_login_invalid_username(): - session = BlackboardSession( - username='InvalidUsername', password='InvalidPassword') - session.login() - response = session.get_response() - expected_error_message = "The username you entered cannot be identified." - assert response == expected_error_message + def test_invalid_both_login(self): + # Set up + username = 'InvalidUsername' + password = 'InvalidPassword' + session = BlackboardSession(username=username, password=password) + # Execute login + session.login() -@pytest.fixture -def mock_session(): - session = BlackboardSession() - session.is_logged_in = True - return session + response = session.get_response() + # Check the response + expected_error_message = "The username you entered cannot be identified." -def test_enable_instructors_logged_in(mock_session): - with patch.object(mock_session, '_get_request') as mock_get_request, \ - patch.object(mock_session, '_send_post_request') as mock_post_request: - mock_get_request.return_value.status_code = 200 - mock_get_request.return_value.content = ''' - - -
- -
- - - -
- - - ''' - mock_post_request.return_value.status_code = 302 - mock_post_request.return_value.headers = { - 'Location': 'https://kettering.blackboard.com'} - mock_session.enable_instructors() - assert mock_session.instructorsFound - assert abs(mock_session.last_activity_time - time.time()) < 1 - - -def test_enable_instructors_not_logged_in(): - session = BlackboardSession() - session.is_logged_in = False - session.enable_instructors() - assert session.response == "Not logged in." - assert not session.instructorsFound - assert session.last_activity_time is None - - -def test_enable_instructors_get_request_failed(mock_session): - with patch.object(mock_session, '_get_request') as mock_get_request, \ - patch('logging.error') as mock_logging_error: - mock_get_request.return_value.status_code = 500 - mock_session.enable_instructors() - assert not mock_session.instructorsFound - mock_logging_error.assert_called_once() - - -def test_enable_instructors_post_request_failed(mock_session): - with patch.object(mock_session, '_get_request') as mock_get_request, \ - patch.object(mock_session, '_send_post_request') as mock_post_request, \ - patch('logging.error') as mock_logging_error: - mock_get_request.return_value.status_code = 200 - mock_get_request.return_value.content = ''' - - -
- -
- - - -
- - - ''' - mock_post_request.return_value.status_code = 500 - mock_session.enable_instructors() - assert not mock_session.instructorsFound - mock_logging_error.assert_called_once() + self.assertEqual(response, expected_error_message) + def test_failed_login_invalid_password(self): -def test_get_courses_logged_in(mock_session): - with patch.object(mock_session, '_send_post_request') as mock_post_request: - mock_post_request.return_value.status_code = 200 - mock_post_request.return_value.content = ''' - -
- -
- - ''' - mock_session.get_courses() - expected_courses = {'Course 1': 'course1_link', - 'Course 2': 'course2_link'} - assert mock_session.courses == expected_courses - assert abs(mock_session.last_activity_time - time.time()) < 1 - - -def test_get_courses_not_logged_in(): - session = BlackboardSession() - session.is_logged_in = False - session.get_courses() - assert session.response == "Not logged in." - assert session.courses == {} - assert session.last_activity_time is None - - -def test_get_courses_no_courses(mock_session): - with patch.object(mock_session, '_send_post_request') as mock_post_request: - mock_post_request.return_value.status_code = 200 - mock_post_request.return_value.content = ''' - -
- -
- - ''' - mock_session.get_courses() - assert mock_session.response == "You are not currently enrolled in any courses." - assert mock_session.courses == {} + # selected a random username from usernames.py + username = random.choice(list(usernames)) + + invalid_password = 'InvalidPassword' + session = BlackboardSession( + username=username, password=invalid_password) + + # Execute login + session.login() + + response = session.get_response() + # Check the response + error_messages = [ + "The password you entered was incorrect.", "Account has been disabled." + ] -def test_get_courses_error_finding_course_list(mock_session): - with patch.object(mock_session, '_send_post_request') as mock_post_request, \ - patch('logging.error') as mock_logging_error: - mock_post_request.return_value.status_code = 500 - mock_session.get_courses() - assert isinstance(mock_session.response, Exception) - assert str(mock_session.response) == 'POST request failed.' - assert mock_session.courses == {} - mock_logging_error.assert_called_once() + self.assertTrue(response in error_messages) + def test_failed_login_invalid_username(self): + # Set up + invalid_username = 'InvalidUsername' + password = 'InvalidPassword' + session = BlackboardSession( + username=invalid_username, password=password) -def test_get_download_tasks_logged_in(mock_session): - with patch.object(mock_session, 'get_download_tasks') as mock_get_download_tasks: - mock_session.is_logged_in = True - mock_session.get_download_tasks() - mock_get_download_tasks.assert_called_once() + # Execute login + session.login() + response = session.get_response() -def test_get_download_tasks_not_logged_in(): - session = BlackboardSession() - session.is_logged_in = False - with patch.object(session, 'get_download_tasks') as mock_get_download_tasks: - session.get_download_tasks() + # Check the response + expected_error_message = "The username you entered cannot be identified." - # Running the tests - assert session.download_tasks == [] - assert session.downloadTasksFound == False - assert session.last_activity_time == None + self.assertEqual(response, expected_error_message) + # * Enable Instructors *# -if __name__ == "__main__": - pytest.main() + def test_enable_instructors_logged_in(self): + + # Set up + session = BlackboardSession() + session.is_logged_in = True + + # Mock the GET request + with patch.object(session, '_get_request') as mock_get_request: + mock_get_request.return_value.status_code = 200 + mock_get_request.return_value.content = ''' + + +
+ +
+ + + +
+ + + ''' + + # Mock the POST request + with patch.object(session, '_send_post_request') as mock_post_request: + mock_post_request.return_value.status_code = 302 + mock_post_request.return_value.headers = { + 'Location': 'https://kettering.blackboard.com'} + + # Execute enable_instructors + session.enable_instructors() + + # Check the response + self.assertTrue(session.instructorsFound) + self.assertAlmostEqual( + session.last_activity_time, time.time(), delta=1) + + def test_enable_instructors_not_logged_in(self): + + # Set up + session = BlackboardSession() + session.is_logged_in = False + + # Execute enable_instructors + session.enable_instructors() + + # Check the response + self.assertEqual(session.response, "Not logged in.") + self.assertFalse(session.instructorsFound) + self.assertIsNone(session.last_activity_time) + + def test_enable_instructors_get_request_failed(self): + + # Set up + session = BlackboardSession() + session.is_logged_in = True + + # Mock the GET request + with patch.object(session, '_get_request') as mock_get_request: + mock_get_request.return_value.status_code = 500 + + # Mock the logging.error function + with patch('logging.error') as mock_logging_error: + # Execute enable_instructors + session.enable_instructors() + + # Check the response + self.assertFalse(session.instructorsFound) + + # Check the logging.error call + mock_logging_error.assert_called_once_with( + "An error occurred enabling instructors: GET request failed." + ) + + def test_enable_instructors_post_request_failed(self): + + # Set up + session = BlackboardSession() + session.is_logged_in = True + + # Mock the GET request + with patch.object(session, '_get_request') as mock_get_request: + mock_get_request.return_value.status_code = 200 + mock_get_request.return_value.content = ''' + + +
+ +
+ + + +
+ + + ''' + + # Mock the POST request + with patch.object(session, '_send_post_request') as mock_post_request: + mock_post_request.return_value.status_code = 500 + + # Mock the logging.error function + with patch('logging.error') as mock_logging_error: + # Execute enable_instructors + session.enable_instructors() + + # Check the response + self.assertFalse(session.instructorsFound) + + # Check the logging.error call + mock_logging_error.assert_called_once_with( + "An error occurred enabling instructors: POST request failed." + ) + + # * Get Courses *# + + def test_get_courses_logged_in(self): + # Set up + session = BlackboardSession() + + session.is_logged_in = True + + # Mock the POST request + with patch.object(session, '_send_post_request') as mock_post_request: + mock_post_request.return_value.status_code = 200 + mock_post_request.return_value.content = ''' + +
+ +
+ + ''' + + # Execute get_courses + session.get_courses() + + # Check the response + expected_courses = { + 'Course 1': 'course1_link', + 'Course 2': 'course2_link' + } + self.assertEqual(session.courses, expected_courses) + self.assertAlmostEqual( + session.last_activity_time, time.time(), delta=1) + + def test_get_courses_not_logged_in(self): + + # Set up + session = BlackboardSession() + session.is_logged_in = False + + # Execute get_courses + session.get_courses() + + # Check the response + self.assertEqual(session.response, "Not logged in.") + self.assertEqual(session.courses, {}) + self.assertIsNone(session.last_activity_time) + + def test_get_courses_no_courses(self): + # Set up + session = BlackboardSession() + + session.is_logged_in = True + + # Mock the POST request + with patch.object(session, '_send_post_request') as mock_post_request: + mock_post_request.return_value.status_code = 200 + mock_post_request.return_value.content = ''' + +
+ +
+ + ''' + + # Execute get_courses + session.get_courses() + + # Check the response + self.assertEqual(session.response, + "You are not currently enrolled in any courses.") + self.assertEqual(session.courses, {}) + + def test_get_courses_error_finding_course_list(self): + # Set up + session = BlackboardSession() + + session.is_logged_in = True + + # Mock the POST request + with patch.object(session, '_send_post_request') as mock_post_request: + mock_post_request.return_value.status_code = 500 + + # Mock the logging.error function + with patch('logging.error') as mock_logging_error: + # Execute get_courses + session.get_courses() + + # Check the response + self.assertIsInstance(session.response, Exception) + self.assertEqual(str(session.response), 'POST request failed.') + self.assertEqual(session.courses, {}) + mock_logging_error.assert_called_once() + + +if __name__ == '__main__': + unittest.main() diff --git a/backend/test_blackboard_scraper_test_.py b/backend/test_blackboard_scraper_test_.py deleted file mode 100644 index d2c6e77..0000000 --- a/backend/test_blackboard_scraper_test_.py +++ /dev/null @@ -1,355 +0,0 @@ -import os -import random -import time -import unittest - -from dotenv import load_dotenv -from blackboard_scraper import BlackboardSession -from unittest.mock import MagicMock, patch -from usernames import usernames - -"""" -Test Case Senarios: - -#* Login *# - -- Valid credentials -- Invalid username -- Invalid password -- Invalid username and password - -# TODO: - -- Failed GET request -- Failed POST request -- HTML parsing failed - -#* Enable Instructors *# - -- Logged in -- Not logged in -- GET request failed -- POST request failed - -# TODO: - -- HTML parsing failed -- No instructors found -- Instructors found - -#* Get Courses *# - -- Logged in -- Not logged in -- No courses -- Error finding course list - -# TODO: - -- HTML parsing failed - -#* Get Download Tasks *# - -- Logged in -- Not logged in - -# TODO: - -- HTML parsing failed - -""" - - -class TestBlackboardSession(unittest.TestCase): - - def setUp(self): - load_dotenv() - self.username = os.getenv('TEST_USERNAME') - self.password = os.getenv('TEST_PASSWORD') - - # * Login Tests *# - - def test_valid_credentials_login(self): - - session = BlackboardSession( - username=self.username, password=self.password) - - # Execute login - session.login() - - response = session.get_response() - - # Check the response - expected_message = "Login successful." - - self.assertEqual(response, expected_message) - - def test_invalid_both_login(self): - # Set up - username = 'InvalidUsername' - password = 'InvalidPassword' - session = BlackboardSession(username=username, password=password) - - # Execute login - session.login() - - response = session.get_response() - - # Check the response - expected_error_message = "The username you entered cannot be identified." - - self.assertEqual(response, expected_error_message) - - def test_failed_login_invalid_password(self): - - # selected a random username from usernames.py - username = random.choice(list(usernames)) - - invalid_password = 'InvalidPassword' - session = BlackboardSession( - username=username, password=invalid_password) - - # Execute login - session.login() - - response = session.get_response() - - # Check the response - error_messages = [ - "The password you entered was incorrect.", "Account has been disabled." - ] - - self.assertTrue(response in error_messages) - - def test_failed_login_invalid_username(self): - # Set up - invalid_username = 'InvalidUsername' - password = 'InvalidPassword' - session = BlackboardSession( - username=invalid_username, password=password) - - # Execute login - session.login() - - response = session.get_response() - - # Check the response - expected_error_message = "The username you entered cannot be identified." - - self.assertEqual(response, expected_error_message) - - # * Enable Instructors *# - - def test_enable_instructors_logged_in(self): - - # Set up - session = BlackboardSession() - session.is_logged_in = True - - # Mock the GET request - with patch.object(session, '_get_request') as mock_get_request: - mock_get_request.return_value.status_code = 200 - mock_get_request.return_value.content = ''' - - -
- -
- - - -
- - - ''' - - # Mock the POST request - with patch.object(session, '_send_post_request') as mock_post_request: - mock_post_request.return_value.status_code = 302 - mock_post_request.return_value.headers = { - 'Location': 'https://kettering.blackboard.com'} - - # Execute enable_instructors - session.enable_instructors() - - # Check the response - self.assertTrue(session.instructorsFound) - self.assertAlmostEqual( - session.last_activity_time, time.time(), delta=1) - - def test_enable_instructors_not_logged_in(self): - - # Set up - session = BlackboardSession() - session.is_logged_in = False - - # Execute enable_instructors - session.enable_instructors() - - # Check the response - self.assertEqual(session.response, "Not logged in.") - self.assertFalse(session.instructorsFound) - self.assertIsNone(session.last_activity_time) - - def test_enable_instructors_get_request_failed(self): - - # Set up - session = BlackboardSession() - session.is_logged_in = True - - # Mock the GET request - with patch.object(session, '_get_request') as mock_get_request: - mock_get_request.return_value.status_code = 500 - - # Mock the logging.error function - with patch('logging.error') as mock_logging_error: - # Execute enable_instructors - session.enable_instructors() - - # Check the response - self.assertFalse(session.instructorsFound) - - # Check the logging.error call - mock_logging_error.assert_called_once_with( - "An error occurred enabling instructors: GET request failed." - ) - - def test_enable_instructors_post_request_failed(self): - - # Set up - session = BlackboardSession() - session.is_logged_in = True - - # Mock the GET request - with patch.object(session, '_get_request') as mock_get_request: - mock_get_request.return_value.status_code = 200 - mock_get_request.return_value.content = ''' - - -
- -
- - - -
- - - ''' - - # Mock the POST request - with patch.object(session, '_send_post_request') as mock_post_request: - mock_post_request.return_value.status_code = 500 - - # Mock the logging.error function - with patch('logging.error') as mock_logging_error: - # Execute enable_instructors - session.enable_instructors() - - # Check the response - self.assertFalse(session.instructorsFound) - - # Check the logging.error call - mock_logging_error.assert_called_once_with( - "An error occurred enabling instructors: POST request failed." - ) - - # * Get Courses *# - - def test_get_courses_logged_in(self): - # Set up - session = BlackboardSession() - - session.is_logged_in = True - - # Mock the POST request - with patch.object(session, '_send_post_request') as mock_post_request: - mock_post_request.return_value.status_code = 200 - mock_post_request.return_value.content = ''' - -
- -
- - ''' - - # Execute get_courses - session.get_courses() - - # Check the response - expected_courses = { - 'Course 1': 'course1_link', - 'Course 2': 'course2_link' - } - self.assertEqual(session.courses, expected_courses) - self.assertAlmostEqual( - session.last_activity_time, time.time(), delta=1) - - def test_get_courses_not_logged_in(self): - - # Set up - session = BlackboardSession() - session.is_logged_in = False - - # Execute get_courses - session.get_courses() - - # Check the response - self.assertEqual(session.response, "Not logged in.") - self.assertEqual(session.courses, {}) - self.assertIsNone(session.last_activity_time) - - def test_get_courses_no_courses(self): - # Set up - session = BlackboardSession() - - session.is_logged_in = True - - # Mock the POST request - with patch.object(session, '_send_post_request') as mock_post_request: - mock_post_request.return_value.status_code = 200 - mock_post_request.return_value.content = ''' - -
- -
- - ''' - - # Execute get_courses - session.get_courses() - - # Check the response - self.assertEqual(session.response, - "You are not currently enrolled in any courses.") - self.assertEqual(session.courses, {}) - - def test_get_courses_error_finding_course_list(self): - # Set up - session = BlackboardSession() - - session.is_logged_in = True - - # Mock the POST request - with patch.object(session, '_send_post_request') as mock_post_request: - mock_post_request.return_value.status_code = 500 - - # Mock the logging.error function - with patch('logging.error') as mock_logging_error: - # Execute get_courses - session.get_courses() - - # Check the response - self.assertIsInstance(session.response, Exception) - self.assertEqual(str(session.response), 'POST request failed.') - self.assertEqual(session.courses, {}) - mock_logging_error.assert_called_once() - - -if __name__ == '__main__': - unittest.main()