Skip to content

Commit

Permalink
Fixed Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
TheManWhoLikesToCode committed Jan 15, 2024
1 parent c220516 commit 18244f8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 139 deletions.
16 changes: 7 additions & 9 deletions backend/blackboard_scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def login(self):

login_send_response = self._send_post_request(
int_login_page_response.url, data=final_payload)

# parse the response using Beautiful Soup with html parser
soup = BeautifulSoup(login_send_response.content, "html.parser")

Expand Down Expand Up @@ -278,8 +278,7 @@ def enable_instructors(self):
self.set_InstructorsFound(True)
else:
self.set_InstructorsFound(False)
logging.error(
f"POST request failed with status code: {enable_instructors_response.status_code}")
raise Exception("POST request failed.")

self.last_activity_time = time.time()

Expand Down Expand Up @@ -381,22 +380,21 @@ def get_courses(self):
logging.error(f"An error occurred while getting courses: {e}")

def download_and_save_file(self):

if self.is_logged_in == False:
self.response = "Not logged in."
return

"""
Downloads and saves the taks passed from the get dwonload tasks function.
self modifies:
zipFound -- A boolean value indicating if the zip file was found.
last_activity_time -- The time of the last activity.
response -- The response of the download and save file attempt.
"""

if self.is_logged_in == False:
self.response = "Not logged in."
return

current_dir = os.path.dirname(os.path.abspath(__file__))
if os.path.basename(current_dir) != 'backend':
session_files_path = os.path.join(
Expand Down
180 changes: 50 additions & 130 deletions backend/test_blackboard_scraper.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import os
import random
import time
import unittest

from dotenv import load_dotenv
from blackboard_scraper import BlackboardSession
from unittest.mock import patch
from unittest.mock import MagicMock, patch
from usernames import usernames

""""
Expand Down Expand Up @@ -59,13 +62,17 @@

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):
# Set up
username = 'Free8864'
password = '#CFi^F6TTwot2j'
session = BlackboardSession(username=username, password=password)

session = BlackboardSession(
username=self.username, password=self.password)

# Execute login
session.login()
Expand Down Expand Up @@ -134,21 +141,26 @@ def test_failed_login_invalid_username(self):
# * Enable Instructors *#

def test_enable_instructors_logged_in(self):

# Set up
username = 'Free8864'
password = '#CFi^F6TTwot2j'
session = BlackboardSession(username=username, password=password)
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 = '''
<html>
<html>
<body>
<form id="moduleEditForm">
<input type="hidden" value="nonce_value">
<input type="hidden" value="fake_nonce_value">
</form>
</html>
<table id="blockAttributes_table_jsListFULL_Student_1_1_body">
<tr id="FULL_Student_1_1_row:_123_456"></tr>
<tr id="FULL_Student_1_1_row:_789_101"></tr>
</table>
</body>
</html>
'''

# Mock the POST request
Expand All @@ -166,10 +178,9 @@ def test_enable_instructors_logged_in(self):
session.last_activity_time, time.time(), delta=1)

def test_enable_instructors_not_logged_in(self):

# Set up
username = 'Free8864'
password = '#CFi^F6TTwot2j'
session = BlackboardSession(username=username, password=password)
session = BlackboardSession()
session.is_logged_in = False

# Execute enable_instructors
Expand All @@ -181,10 +192,9 @@ def test_enable_instructors_not_logged_in(self):
self.assertIsNone(session.last_activity_time)

def test_enable_instructors_get_request_failed(self):

# Set up
username = 'Free8864'
password = '#CFi^F6TTwot2j'
session = BlackboardSession(username=username, password=password)
session = BlackboardSession()
session.is_logged_in = True

# Mock the GET request
Expand All @@ -201,23 +211,29 @@ def test_enable_instructors_get_request_failed(self):

# Check the logging.error call
mock_logging_error.assert_called_once_with(
f"GET request failed with status code: {mock_get_request.return_value.status_code}")
"An error occurred enabling instructors: GET request failed."
)

def test_enable_instructors_post_request_failed(self):

# Set up
username = 'Free8864'
password = '#CFi^F6TTwot2j'
session = BlackboardSession(username=username, password=password)
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 = '''
<html>
<form id="moduleEditForm">
<input type="hidden" value="nonce_value">
</form>
<body>
<form id="moduleEditForm">
<input type="hidden" value="fake_nonce_value">
</form>
<table id="blockAttributes_table_jsListFULL_Student_1_1_body">
<tr id="FULL_Student_1_1_row:_123_456"></tr>
<tr id="FULL_Student_1_1_row:_789_101"></tr>
</table>
</body>
</html>
'''

Expand All @@ -235,15 +251,15 @@ def test_enable_instructors_post_request_failed(self):

# Check the logging.error call
mock_logging_error.assert_called_once_with(
f"POST request failed with status code: {mock_post_request.return_value.status_code}")
"An error occurred enabling instructors: POST request failed."
)

# * Get Courses *#

def test_get_courses_logged_in(self):
# Set up
username = 'Free8864'
password = '#CFi^F6TTwot2j'
session = BlackboardSession(username=username, password=password)
session = BlackboardSession()

session.is_logged_in = True

# Mock the POST request
Expand Down Expand Up @@ -273,10 +289,9 @@ def test_get_courses_logged_in(self):
session.last_activity_time, time.time(), delta=1)

def test_get_courses_not_logged_in(self):

# Set up
username = 'Free8864'
password = '#CFi^F6TTwot2j'
session = BlackboardSession(username=username, password=password)
session = BlackboardSession()
session.is_logged_in = False

# Execute get_courses
Expand All @@ -289,9 +304,8 @@ def test_get_courses_not_logged_in(self):

def test_get_courses_no_courses(self):
# Set up
username = 'Free8864'
password = '#CFi^F6TTwot2j'
session = BlackboardSession(username=username, password=password)
session = BlackboardSession()

session.is_logged_in = True

# Mock the POST request
Expand All @@ -317,9 +331,8 @@ def test_get_courses_no_courses(self):

def test_get_courses_error_finding_course_list(self):
# Set up
username = 'Free8864'
password = '#CFi^F6TTwot2j'
session = BlackboardSession(username=username, password=password)
session = BlackboardSession()

session.is_logged_in = True

# Mock the POST request
Expand All @@ -337,99 +350,6 @@ def test_get_courses_error_finding_course_list(self):
self.assertEqual(session.courses, {})
mock_logging_error.assert_called_once()

# * Get Download Tasks *#


def test_get_download_tasks_logged_in(self):
# Set up
username = 'Free8864'
password = '#CFi^F6TTwot2j'
session = BlackboardSession(username=username, password=password)
session.is_logged_in = True
session.courses = {
'Course 1': 'course1_link',
'Course 2': 'course2_link'
}

with patch.object(session, '_get_request') as mock_get_request:
mock_get_request.side_effect = [
type('', (), {'status_code': 200, 'content': '''
<html>
<body>
<div id="containerdiv">
<ul>
<li>
<a href="/course1_link/assignment1">Assignment 1</a>
<div class="details">
<a href="download_link1">Download</a>
</div>
</li>
<li>
<a href="/course1_link/assignment2">Assignment 2</a>
<div class="details">
<a href="download_link2">Download</a>
</div>
</li>
</ul>
</div>
</body>
</html>
'''}),
type('', (), {'status_code': 200, 'content': '''
<html>
<body>
<div id="containerdiv">
<ul>
<li>
<a href="/course2_link/assignment1">Assignment 1</a>
<div class="details">
<a href="download_link1">Download</a>
</div>
</li>
<li>
<a href="/course2_link/assignment2">Assignment 2</a>
<div class="details">
<a href="download_link2">Download</a>
</div>
</li>
</ul>
</div>
</body>
</html>
'''})
]

# Call the method
session.get_download_tasks()

# Assert the result
expected_result = [
('Course 1', 'Assignment 1', 'download_link1'),
('Course 1', 'Assignment 2', 'download_link2'),
('Course 2', 'Assignment 1', 'download_link1'),
('Course 2', 'Assignment 2', 'download_link2'),
]

self.assertEqual(session.download_tasks, expected_result)
self.assertTrue(session.downloadTasksFound)
self.assertAlmostEqual(
session.last_activity_time, time.time(), delta=1)

def test_get_download_tasks_not_logged_in(self):
# Set up
username = 'Free8864'
password = '#CFi^F6TTwot2j'
session = BlackboardSession(username=username, password=password)
session.is_logged_in = False

# Execute get_download_tasks
session.get_download_tasks()

# Check the response
self.assertEqual(session.response, "Not logged in.")
self.assertFalse(session.downloadTasksFound)
self.assertIsNone(session.last_activity_time)


if __name__ == '__main__':
unittest.main()

0 comments on commit 18244f8

Please sign in to comment.