forked from feder-cr/Jobs_Applier_AI_Agent_AIHawk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_linkedIn_authenticator.py
102 lines (72 loc) · 3.92 KB
/
test_linkedIn_authenticator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from httpx import get
from numpy import place
import pytest
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from ai_hawk.authenticator import AIHawkAuthenticator, LinkedInAuthenticator, get_authenticator
from selenium.common.exceptions import NoSuchElementException, TimeoutException
@pytest.fixture
def mock_driver(mocker):
"""Fixture to mock the Selenium WebDriver."""
return mocker.Mock()
@pytest.fixture
def authenticator(mock_driver):
"""Fixture to initialize AIHawkAuthenticator with a mocked driver."""
return get_authenticator(mock_driver, platform='linkedin')
def test_handle_login(mocker, authenticator):
"""Test handling the AIHawk login process."""
mocker.patch.object(authenticator.driver, 'get')
mocker.patch.object(authenticator, 'prompt_for_credentials')
mocker.patch.object(authenticator, 'handle_security_checks')
# Mock current_url as a regular return value, not PropertyMock
mocker.patch.object(authenticator.driver, 'current_url',
return_value='https://www.linkedin.com/login')
authenticator.handle_login()
authenticator.driver.get.assert_called_with(
'https://www.linkedin.com/login')
authenticator.prompt_for_credentials.assert_called_once()
authenticator.handle_security_checks.assert_called_once()
def test_enter_credentials_success(mocker, authenticator):
"""Test entering credentials."""
email_mock = mocker.Mock()
password_mock = mocker.Mock()
mocker.patch.object(WebDriverWait, 'until', return_value=email_mock)
mocker.patch.object(authenticator.driver, 'find_element',
return_value=password_mock)
def test_is_logged_in_true(mock_driver):
# Mock the current_url to simulate a logged-in state
mock_driver.current_url = "https://www.linkedin.com/feed/"
authenticator = LinkedInAuthenticator(mock_driver)
assert authenticator.is_logged_in == True
def test_is_logged_in_false(mock_driver):
# Mock the current_url to simulate a logged-out state
mock_driver.current_url = "https://www.linkedin.com/login"
authenticator = LinkedInAuthenticator(mock_driver)
assert authenticator.is_logged_in == False
def test_is_logged_in_partial_keyword(mock_driver):
# Mock the current_url to simulate a URL containing a keyword but not logged in
mock_driver.current_url = "https://www.linkedin.com/jobs/search/"
authenticator = LinkedInAuthenticator(mock_driver)
assert authenticator.is_logged_in == True
def test_is_logged_in_no_linkedin(mock_driver):
# Mock the current_url to simulate a URL not related to LinkedIn
mock_driver.current_url = "https://www.example.com/feed/"
authenticator = LinkedInAuthenticator(mock_driver)
assert authenticator.is_logged_in == False
def test_handle_security_check_success(mocker, authenticator):
"""Test handling security check successfully."""
mocker.patch.object(WebDriverWait, 'until', side_effect=[
mocker.Mock(), # Security checkpoint detection
mocker.Mock() # Security check completion
])
authenticator.handle_security_checks()
# Verify WebDriverWait is called with EC.url_contains for both the challenge and feed
WebDriverWait(authenticator.driver, 10).until.assert_any_call(mocker.ANY)
WebDriverWait(authenticator.driver, 300).until.assert_any_call(mocker.ANY)
def test_handle_security_check_timeout(mocker, authenticator):
"""Test handling security check timeout."""
mocker.patch.object(WebDriverWait, 'until', side_effect=TimeoutException)
authenticator.handle_security_checks()
# Verify WebDriverWait is called with EC.url_contains for the challenge
WebDriverWait(authenticator.driver, 10).until.assert_any_call(mocker.ANY)