Skip to content

Commit

Permalink
Added a check on fetch cookie response status code (#24)
Browse files Browse the repository at this point in the history
Closes #23
  • Loading branch information
Giglium authored Dec 9, 2023
1 parent b130d16 commit 0b1b202
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 21 deletions.
13 changes: 9 additions & 4 deletions src/vinted_scraper/vintedWrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ def _fetch_cookie(self) -> str:
5. If the session cookie cannot be fetched or doesn't match the expected format, it raises a RuntimeError.
"""
response = requests.get(self.baseurl, headers={"User-Agent": self.user_agent})

session_cookie = response.headers.get("Set-Cookie")
if session_cookie and "_vinted_fr_session=" in session_cookie:
return session_cookie.split("_vinted_fr_session=")[1].split(";")[0]
if 200 == response.status_code:
session_cookie = response.headers.get("Set-Cookie")
if session_cookie and "_vinted_fr_session=" in session_cookie:
return session_cookie.split("_vinted_fr_session=")[1].split(";")[0]
else:
raise RuntimeError(
f"Cannot fetch session cookie from {self.baseurl}, because of status code: {response.status_code} "
f"different from 200."
)

raise RuntimeError(f"Cannot fetch session cookie from {self.baseurl}")

Expand Down
28 changes: 19 additions & 9 deletions tests/test_vinted.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@

# isort: split
from src.vinted_scraper.vintedWrapper import VintedWrapper
from tests.utils import get_200_response
from tests.utils import BASE_URL, get_200_response, get_404_response


class TestVinted(unittest.TestCase):
def setUp(self):
self.baseurl = BASE_URL

def test_init_valid_url(self):
"""
Ensure that the initializer accepts a valid URL
"""
baseurl = "https://fakeurl.com"

with patch("requests.get", return_value=get_200_response()):
wrapper = VintedWrapper(baseurl)
self.assertEqual(wrapper.baseurl, baseurl)
wrapper = VintedWrapper(self.baseurl)
self.assertEqual(wrapper.baseurl, self.baseurl)

with patch("requests.get", return_value=get_200_response()):
wrapper = VintedScraper(baseurl)
self.assertEqual(wrapper.baseurl, baseurl)
wrapper = VintedScraper(self.baseurl)
self.assertEqual(wrapper.baseurl, self.baseurl)

def test_init_invalid_url(self):
"""
Expand All @@ -39,13 +41,21 @@ def test_init_invalid_cookie(self):
"""
Ensure that the initializer raises an error if it can find the session cookie
"""
baseurl = "https://fakeurl.com"
response = get_200_response()
response.headers = {}
response.headers = {} # Reset the headers to force an invalid cookies

with self.assertRaises(RuntimeError):
with patch("requests.get", return_value=response):
VintedWrapper(baseurl)
VintedWrapper(self.baseurl)

def test_init_invalid_cookie_status_code(self):
"""
Ensure that the initializer raises an error if it can find the session cookie because of a status code different
from 200.
"""
with self.assertRaises(RuntimeError):
with patch("requests.get", return_value=get_404_response()):
VintedWrapper(self.baseurl)


if __name__ == "__main__":
Expand Down
16 changes: 10 additions & 6 deletions tests/test_vinted_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
from src.vinted_scraper.models import VintedItem

# isort: split
from tests.utils import _read_data_from_file, get_200_response, get_scraper, get_wrapper
from tests.utils import (
BASE_URL,
_read_data_from_file,
get_200_response,
get_404_response,
get_scraper,
get_wrapper,
)


class TestItem(unittest.TestCase):
def setUp(self):
self.baseurl = "https://fakeurl.com"
self.baseurl = BASE_URL
self.response_200 = get_200_response()
self.wrapper = get_wrapper(self.baseurl)
self.scraper = get_scraper(self.baseurl)
Expand Down Expand Up @@ -45,9 +52,6 @@ def test_status_code_error(self):
"""
Test the case when a status code different from 200 is returned by the web service
"""
mock_response = Mock()
mock_response.status_code = 404
mock_response.headers = {}

with patch("requests.get", return_value=mock_response):
with patch("requests.get", return_value=get_404_response()):
self.assertRaises(RuntimeError, lambda: self.wrapper.item("id"))
10 changes: 8 additions & 2 deletions tests/test_vinted_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
from src.vinted_scraper.models import VintedItem

# isort: split
from tests.utils import _read_data_from_file, get_200_response, get_scraper, get_wrapper
from tests.utils import (
BASE_URL,
_read_data_from_file,
get_200_response,
get_scraper,
get_wrapper,
)


class TestVintedSearch(unittest.TestCase):
def setUp(self):
self.baseurl = "https://fakeurl.com"
self.baseurl = BASE_URL
self.response_200 = get_200_response()
self.wrapper = get_wrapper(self.baseurl)
self.scrapper = get_scraper(self.baseurl)
Expand Down
13 changes: 13 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
# isort: split
from src.vinted_scraper import VintedScraper, VintedWrapper

# TODO: Change str to final when 3.6+ support is dropped, because final was introduced from 3.8.
BASE_URL: str = "https://fakeurl.com"


def get_200_response() -> MagicMock:
"""
Expand All @@ -19,6 +22,16 @@ def get_200_response() -> MagicMock:
return response_200


def get_404_response() -> MagicMock:
"""
:return: a mocked 404 response using MagicMock already configured
"""
response_404 = MagicMock(spec=requests.Response)
response_404.status_code = 404
response_404.headers = {}
return response_404


def get_wrapper(url: str) -> VintedWrapper:
"""
:param url: a valid https url like: "https://fakeurl.com"
Expand Down

0 comments on commit 0b1b202

Please sign in to comment.