Ejemplo n.º 1
0
def test_blocked_by_doctolib_par_availabilities():
    # Cas de base.

    start_date = "2021-04-03"
    base_url = "https://partners.doctolib.fr/centre-de-vaccinations-internationales/ville1/centre1?pid=practice-165752&enable_cookies_consent=1"  # noqa
    center_info = CenterInfo(departement="07",
                             nom="Mon Super Centre",
                             url=base_url)
    scrap_request = ScraperRequest(base_url, start_date, center_info)

    def app(request: httpx.Request) -> httpx.Response:
        assert "User-Agent" in request.headers

        if request.url.path == "/booking/centre1.json":
            path = Path("tests", "fixtures", "doctolib", "basic-booking.json")
            return httpx.Response(200,
                                  json=json.loads(
                                      path.read_text(encoding="utf-8")))

        return httpx.Response(403, text="Anti dDos")

    client = httpx.Client(transport=httpx.MockTransport(app))
    slots = DoctolibSlots(client=client, cooldown_interval=0)

    with pytest.raises(Blocked403):
        slots.fetch(scrap_request)
Ejemplo n.º 2
0
def test_blocked_by_doctolib_par_centre():
    # Cas de base.

    start_date = "2021-04-03"
    base_url = "https://partners.doctolib.fr/centre-de-vaccinations-internationales/ville1/centre1?pid=practice-165752&enable_cookies_consent=1"  # noqa
    scrap_request = ScraperRequest(base_url, start_date)

    def app(request: httpx.Request) -> httpx.Response:
        assert "User-Agent" in request.headers

        if request.url.path == "/booking/centre1.json":
            path = Path("tests", "fixtures", "doctolib", "basic-booking.json")
            return httpx.Response(403, text="Anti dDos")

        assert request.url.path == "/availabilities.json"
        params = dict(httpx.QueryParams(request.url.query))
        assert params == {
            "start_date": start_date,
            "visit_motive_ids": "2",
            "agenda_ids": "3",
            "insurance_sector": "public",
            "practice_ids": "4",
            "destroy_temporary": "true",
            "limit": str(DOCTOLIB_CONF.pagination["pages"]),
        }
        path = Path("tests", "fixtures", "doctolib", "basic-availabilities.json")
        return httpx.Response(200, json=json.loads(path.read_text(encoding="utf-8")))

    client = httpx.Client(transport=httpx.MockTransport(app))
    slots = DoctolibSlots(client=client, cooldown_interval=0)

    with pytest.raises(BlockedByDoctolibError):
        slots.fetch(scrap_request)
Ejemplo n.º 3
0
def test_doctolib_sends_creneau():
    # Given
    start_date = "2021-04-03"
    base_url = "https://partners.doctolib.fr/centre-de-vaccinations-internationales/ville1/centre1?pid=practice-165752&enable_cookies_consent=1"  # noqa
    center_info = CenterInfo(departement="07",
                             nom="Mon Super Centre",
                             url=base_url)
    scrap_request = ScraperRequest(base_url, start_date, center_info)

    def app(request: httpx.Request) -> httpx.Response:
        assert "User-Agent" in request.headers

        if request.url.path == "/booking/centre1.json":
            path = Path("tests", "fixtures", "doctolib", "basic-booking.json")
            return httpx.Response(200,
                                  json=json.loads(
                                      path.read_text(encoding="utf-8")))

        assert request.url.path == "/availabilities.json"
        path = Path("tests", "fixtures", "doctolib",
                    "basic-availabilities.json")
        return httpx.Response(200,
                              json=json.loads(
                                  path.read_text(encoding="utf-8")))

    client = httpx.Client(transport=httpx.MockTransport(app))
    q = SimpleQueue()
    slots = DoctolibSlots(client=client, cooldown_interval=0, creneau_q=q)

    # When
    slots.fetch(scrap_request)
    actual = []
    while not q.empty():
        actual.append(q.get())
    # Then
    assert len(actual) == 1
    assert actual[0] == Creneau(
        reservation_url=base_url,
        horaire=dateutil.parser.parse("2021-04-10T21:45:00.000+02:00"),
        type_vaccin=[Vaccine.PFIZER],
        lieu=Lieu(
            departement="07",
            plateforme=Plateforme.DOCTOLIB,
            url=base_url,
            nom="Mon Super Centre",
            internal_id="doctolib123456789pid165752",
            lieu_type="vaccination-center",
        ),
        dose=["1"],
    )
Ejemplo n.º 4
0
def test_doctolib():
    # Cas de base.

    start_date = "2021-04-03"
    base_url = "https://partners.doctolib.fr/centre-de-vaccinations-internationales/ville1/centre1?pid=practice-165752&enable_cookies_consent=1"  # noqa
    scrap_request = ScraperRequest(base_url, start_date)

    def app(request: httpx.Request) -> httpx.Response:
        assert "User-Agent" in request.headers

        if request.url.path == "/booking/centre1.json":
            path = Path("tests", "fixtures", "doctolib", "basic-booking.json")
            return httpx.Response(200, json=json.loads(path.read_text()))

        assert request.url.path == "/availabilities.json"
        params = dict(httpx.QueryParams(request.url.query))
        path = Path("tests", "fixtures", "doctolib",
                    "basic-availabilities.json")
        return httpx.Response(200, json=json.loads(path.read_text()))

    client = httpx.Client(transport=httpx.MockTransport(app))
    slots = DoctolibSlots(client=client, cooldown_interval=0)

    next_date = slots.fetch(scrap_request)
    assert next_date == '2021-04-10'
Ejemplo n.º 5
0
def test_blocked_by_doctolib_par_availabilities():
    # Cas de base.

    start_date = "2021-04-03"
    base_url = "https://partners.doctolib.fr/centre-de-vaccinations-internationales/ville1/centre1?pid=practice-165752&enable_cookies_consent=1"  # noqa
    scrap_request = ScraperRequest(base_url, start_date)

    def app(request: httpx.Request) -> httpx.Response:
        assert "User-Agent" in request.headers

        if request.url.path == "/booking/centre1.json":
            path = Path("tests", "fixtures", "doctolib", "basic-booking.json")
            return httpx.Response(200, json=json.loads(path.read_text()))

        return httpx.Response(403, text="Anti dDos")

    client = httpx.Client(transport=httpx.MockTransport(app))
    slots = DoctolibSlots(client=client, cooldown_interval=0)

    error = None
    try:
        next_date = slots.fetch(scrap_request)
    except Exception as e:
        error = e
    assert True is isinstance(error, BlockedByDoctolibError)
Ejemplo n.º 6
0
def test_doctolib_next_slot():
    # Cas de repli : c'est surprenant, mais parfois la liste des dispos
    # est vide, mais il y a un champ 'next_slot' qui contient la date de
    # la prochaine visite, que l'on utilise dans ce cas.

    start_date = "2021-04-03"
    base_url = "https://partners.doctolib.fr/centre-de-vaccinations-internationales/ville1/centre1?pid=practice-165752&enable_cookies_consent=1"  # noqa
    scrap_request = ScraperRequest(base_url, start_date)

    def app(request: httpx.Request) -> httpx.Response:
        assert "User-Agent" in request.headers

        if request.url.path == "/booking/centre1.json":
            path = Path("tests", "fixtures", "doctolib",
                        "next-slot-booking.json")
            return httpx.Response(200, json=json.loads(path.read_text()))

        assert request.url.path == "/availabilities.json"
        path = Path("tests", "fixtures", "doctolib",
                    "next-slot-availabilities.json")
        return httpx.Response(200, json=json.loads(path.read_text()))

    client = httpx.Client(transport=httpx.MockTransport(app))
    slots = DoctolibSlots(client=client, cooldown_interval=0)

    next_date = slots.fetch(scrap_request)
    # Next slot should not be used
    assert next_date is None
Ejemplo n.º 7
0
def test_doctolib_motive_categories():
    # Certains centres opèrent une distinction de motifs pour les professionnels de santé /
    # non professionnels de santé.
    # On doit gérer ces cas-là.

    start_date = "2021-04-03"
    base_url = "https://partners.doctolib.fr/centre-de-vaccinations-internationales/ville1/centre1?pid=practice-165752&enable_cookies_consent=1"  # noqa
    scrap_request = ScraperRequest(base_url, start_date)

    def app(request: httpx.Request) -> httpx.Response:
        assert "User-Agent" in request.headers

        if request.url.path == "/booking/centre1.json":
            path = Path("tests", "fixtures", "doctolib",
                        "category-booking.json")
            return httpx.Response(200, json=json.loads(path.read_text()))

        assert request.url.path == "/availabilities.json"
        path = Path("tests", "fixtures", "doctolib",
                    "category-availabilities.json")
        return httpx.Response(200, json=json.loads(path.read_text()))

    client = httpx.Client(transport=httpx.MockTransport(app))
    slots = DoctolibSlots(client=client, cooldown_interval=0)

    next_date = slots.fetch(scrap_request)
    assert next_date == '2021-04-10'