-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from sakan811/patch
Adjust authorization header getter to write the headers to a .env file
- Loading branch information
Showing
8 changed files
with
350 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
from unittest.mock import patch, Mock, mock_open | ||
|
||
import pytest | ||
from playwright.sync_api import Page, Browser, BrowserContext | ||
|
||
from get_auth_headers import extract_x_headers, handle_request, update_env_example | ||
|
||
|
||
@pytest.fixture | ||
def mock_playwright(): | ||
with patch('get_auth_headers.sync_playwright') as mock_playwright: | ||
mock_browser = Mock(spec=Browser) | ||
mock_page = Mock(spec=Page) | ||
mock_context = Mock(spec=BrowserContext) | ||
|
||
mock_playwright.return_value.__enter__.return_value.chromium.launch.return_value = mock_browser | ||
mock_browser.new_page.return_value = mock_page | ||
|
||
yield mock_playwright, mock_browser, mock_page | ||
|
||
|
||
def test_extract_x_headers_navigation(mock_playwright): | ||
_, _, mock_page = mock_playwright | ||
|
||
extract_x_headers() | ||
|
||
# Check if the function navigates to Booking.com | ||
mock_page.goto.assert_called_once_with("https://www.booking.com") | ||
|
||
# Check if the function fills in the search input and presses Enter | ||
mock_page.fill.assert_called_once_with('input[name="ss"]', "Tokyo") | ||
mock_page.press.assert_called_once_with('input[name="ss"]', "Enter") | ||
|
||
|
||
def test_extract_x_headers_request_interception(mock_playwright): | ||
_, _, mock_page = mock_playwright | ||
|
||
extract_x_headers() | ||
|
||
# Check if request interception is set up | ||
mock_page.on.assert_called_once_with("request", handle_request) | ||
|
||
|
||
@patch('get_auth_headers.update_env_example') | ||
def test_handle_request_graphql(mock_update_env): | ||
mock_request = Mock() | ||
mock_request.url = "https://www.booking.com/dml/graphql?query=somequery" | ||
mock_request.headers = { | ||
'x-booking-context-action-name': 'searchresults', | ||
'user-agent': 'Mozilla/5.0', | ||
'content-type': 'application/json' | ||
} | ||
|
||
handle_request(mock_request) | ||
|
||
expected_env_vars = { | ||
'X_BOOKING_CONTEXT_ACTION_NAME': 'searchresults', | ||
'USER_AGENT': 'Mozilla/5.0' | ||
} | ||
mock_update_env.assert_called_once_with(expected_env_vars) | ||
|
||
|
||
def test_handle_request_non_graphql(): | ||
mock_request = Mock() | ||
mock_request.url = "https://www.booking.com/some-other-page" | ||
|
||
with patch('get_auth_headers.update_env_example') as mock_update_env: | ||
handle_request(mock_request) | ||
mock_update_env.assert_not_called() | ||
|
||
|
||
@patch('builtins.open', new_callable=mock_open, read_data="X_BOOKING_CONTEXT_ACTION_NAME=\nUSER_AGENT=\n") | ||
@patch('get_auth_headers.ENV_FILENAME', '.env.test') | ||
def test_update_env_example(mock_file): | ||
env_vars = { | ||
'X_BOOKING_CONTEXT_ACTION_NAME': 'searchresults', | ||
'USER_AGENT': 'Mozilla/5.0' | ||
} | ||
|
||
update_env_example(env_vars) | ||
|
||
# Check that open was called twice (once for reading, once for writing) | ||
assert mock_file.call_count == 2 | ||
|
||
# Check the read operation | ||
mock_file.assert_any_call('.env.example', 'r') | ||
|
||
# Check the write operation | ||
mock_file.assert_any_call('.env.test', 'w') | ||
|
||
# Check the content written | ||
handle = mock_file() | ||
handle.writelines.assert_called_once_with(['X_BOOKING_CONTEXT_ACTION_NAME=searchresults\n', 'USER_AGENT=Mozilla/5.0\n']) | ||
|
||
# Check that print was called with the correct message | ||
with patch('builtins.print') as mock_print: | ||
update_env_example(env_vars) | ||
mock_print.assert_called_once_with("Headers updated in .env.test file") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from unittest.mock import Mock, patch | ||
|
||
import pytest | ||
from playwright.sync_api import Request | ||
|
||
from get_auth_headers import handle_request | ||
|
||
|
||
@pytest.fixture | ||
def mock_request(): | ||
request = Mock(spec=Request) | ||
request.url = "https://www.booking.com/dml/graphql?query=somequery" | ||
request.headers = { | ||
'x-booking-context-action-name': 'searchresults', | ||
'user-agent': 'Mozilla/5.0', | ||
'content-type': 'application/json' | ||
} | ||
return request | ||
|
||
def test_handle_request_graphql(mock_request): | ||
with patch('get_auth_headers.update_env_example') as mock_update_env: | ||
handle_request(mock_request) | ||
|
||
expected_env_vars = { | ||
'X_BOOKING_CONTEXT_ACTION_NAME': 'searchresults', | ||
'USER_AGENT': 'Mozilla/5.0' | ||
} | ||
mock_update_env.assert_called_once_with(expected_env_vars) | ||
|
||
def test_handle_request_non_graphql(): | ||
non_graphql_request = Mock(spec=Request) | ||
non_graphql_request.url = "https://www.booking.com/some-other-page" | ||
|
||
with patch('get_auth_headers.update_env_example') as mock_update_env: | ||
handle_request(non_graphql_request) | ||
mock_update_env.assert_not_called() | ||
|
||
def test_handle_request_intercept_once(): | ||
with patch('get_auth_headers.update_env_example') as mock_update_env: | ||
request1 = Mock(spec=Request) | ||
request1.url = "https://www.booking.com/dml/graphql?query=somequery" | ||
request1.headers = {'x-test': 'value1'} | ||
|
||
request2 = Mock(spec=Request) | ||
request2.url = "https://www.booking.com/dml/graphql?query=anotherquery" | ||
request2.headers = {'x-test': 'value2'} | ||
|
||
handle_request(request1) | ||
handle_request(request2) | ||
|
||
mock_update_env.assert_called_once() | ||
|
||
def test_handle_request_extracts_correct_headers(): | ||
request = Mock(spec=Request) | ||
request.url = "https://www.booking.com/dml/graphql?query=somequery" | ||
request.headers = { | ||
'x-test1': 'value1', | ||
'x-test2': 'value2', | ||
'user-agent': 'TestAgent', | ||
'content-type': 'application/json' | ||
} | ||
|
||
with patch('get_auth_headers.update_env_example') as mock_update_env: | ||
handle_request(request) | ||
|
||
expected_env_vars = { | ||
'X_TEST1': 'value1', | ||
'X_TEST2': 'value2', | ||
'USER_AGENT': 'TestAgent' | ||
} | ||
mock_update_env.assert_called_once_with(expected_env_vars) | ||
|
||
@pytest.fixture(autouse=True) | ||
def reset_intercepted(): | ||
import get_auth_headers | ||
get_auth_headers.intercepted = False | ||
yield | ||
get_auth_headers.intercepted = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from unittest.mock import patch, mock_open | ||
|
||
import pytest | ||
|
||
from get_auth_headers import update_env_example | ||
|
||
|
||
@pytest.fixture | ||
def mock_env_file(): | ||
return "X_BOOKING_CONTEXT_ACTION_NAME=\nUSER_AGENT=\n" | ||
|
||
|
||
@pytest.mark.parametrize('env_filename', ['.env.test']) | ||
def test_update_env_example(mock_env_file, env_filename): | ||
with patch('builtins.open', mock_open(read_data=mock_env_file)) as mock_file, \ | ||
patch('get_auth_headers.ENV_FILENAME', env_filename): | ||
env_vars = { | ||
'X_BOOKING_CONTEXT_ACTION_NAME': 'searchresults', | ||
'USER_AGENT': 'Mozilla/5.0' | ||
} | ||
|
||
update_env_example(env_vars) | ||
|
||
# Check that open was called twice (once for reading, once for writing) | ||
assert mock_file.call_count == 2 | ||
|
||
# Check the read operation | ||
mock_file.assert_any_call('.env.example', 'r') | ||
|
||
# Check the write operation | ||
mock_file.assert_any_call(env_filename, 'w') | ||
|
||
# Check the content written | ||
handle = mock_file() | ||
handle.writelines.assert_called_once_with([ | ||
'X_BOOKING_CONTEXT_ACTION_NAME=searchresults\n', | ||
'USER_AGENT=Mozilla/5.0\n' | ||
]) | ||
|
||
|
||
def test_update_env_example_partial_update(): | ||
mock_file_content = "X_HEADER=old_value\nOTHER_HEADER=keep_this\n" | ||
with patch('builtins.open', mock_open(read_data=mock_file_content)) as mock_file, \ | ||
patch('get_auth_headers.ENV_FILENAME', '.env.test'): | ||
env_vars = { | ||
'X_HEADER': 'new_value' | ||
} | ||
|
||
update_env_example(env_vars) | ||
|
||
handle = mock_file() | ||
handle.writelines.assert_called_once_with([ | ||
'X_HEADER=new_value\n', | ||
'OTHER_HEADER=keep_this\n' | ||
]) | ||
|
||
|
||
def test_update_env_example_empty_file(): | ||
with patch('builtins.open', mock_open(read_data="")) as mock_file, \ | ||
patch('get_auth_headers.ENV_FILENAME', '.env.test'): | ||
env_vars = { | ||
'NEW_HEADER': 'new_value' | ||
} | ||
|
||
update_env_example(env_vars) | ||
|
||
handle = mock_file() | ||
handle.writelines.assert_called_once_with([]) | ||
|
||
|
||
def test_update_env_example_print_message(capsys): | ||
with patch('builtins.open', mock_open(read_data="")), \ | ||
patch('get_auth_headers.ENV_FILENAME', '.env.test'): | ||
update_env_example({}) | ||
|
||
captured = capsys.readouterr() | ||
assert captured.out == "Headers updated in .env.test file\n" |
Oops, something went wrong.