Ejemplo n.º 1
0
    def test_context(self, mocker):
        tmpfile = NamedTemporaryFile()
        pickle.dump({"username": "******", "password": "******"}, tmpfile)
        tmpfile.seek(0)

        mocker.patch.object(NNRecognizer, "recognize", return_value="ipsum")
        with Session(_mocker_app=app, session_file=tmpfile.file):
            pass
        tmpfile.seek(0)

        assert pickle.load(tmpfile)["cookies"]
Ejemplo n.º 2
0
    def test_properties(self, logged_session):
        cookie = logged_session.cookies

        sess = Session(_mocker_app=app)
        assert sess.proxies == {}

        assert isinstance(sess.timeout, httpx.Timeout)
        sess.timeout = httpx.Timeout(1.0)
        sess.timeout = 1
        sess.timeout = (1, 5)
        with pytest.raises(TypeError):
            sess.timeout = "1"

        assert isinstance(sess.cookies, httpx.Cookies)
        with pytest.raises(SessionException):
            sess.cookies = {}
        sess._cookies = {}
        sess._cache_store = {"key": "value"}
        sess.cookies = cookie
        assert sess._cache_store == {}
        assert sess._cookies == sess.cookies
Ejemplo n.º 3
0
    def test_init(self, mocker, check_login):
        tmpfile = NamedTemporaryFile()
        mocker.patch.object(NNRecognizer, "recognize", return_value="ipsum")
        sess = Session(_mocker_app=app, username="******", password="******")
        assert check_login(sess)
        cookie = sess.cookies
        sess.dump(tmpfile.file)
        tmpfile.seek(0)

        with pytest.warns(LoadWarning):
            sess = Session(_mocker_app=app, cookies=cookie)
            assert check_login(sess)

        sess = Session(_mocker_app=app, session_file=tmpfile.file)
        assert check_login(sess)
Ejemplo n.º 4
0
def create_client(username: str, password: str, _mocker_app=None) -> Client:
    """
    Create a new :class:`Client` with default options.
    To change :class:`Session` settings or preserve your session, use :class:`Session` and :class:`Client` instead.

    :param username: JAccount username.
    :param password: JAccount password.
    :param _mocker_app: An WSGI application to send requests to (for debug or test purposes).
    :return: an authenticated :class:`Client`.
    """
    sess = Session(username=username,
                   password=password,
                   _mocker_app=_mocker_app)
    return Client(session=sess)
Ejemplo n.º 5
0
 def test_base_url(self):
     respx.get("https://i.sjtu.edu.cn/test", content="1")
     respx.get("https://kbcx.sjtu.edu.cn/test", content="2")
     sess = Session()
     assert sess.base_url == "https://i.sjtu.edu.cn"
     assert sess.get("/test").text == "1"
     sess_2 = Session(base_url="https://kbcx.sjtu.edu.cn")
     assert sess_2.base_url == "https://kbcx.sjtu.edu.cn"
     assert sess_2.get("/test").text == "2"
Ejemplo n.º 6
0
def create_client(username: str,
                  password: str,
                  use_jcss: bool = True,
                  _mocker_app=None) -> Client:
    """
    Create a new :class:`Client` with default options.
    To change :class:`Session` settings or preserve your session, use :class:`Session` and :class:`Client` instead.

    :param username: JAccount username.
    :param password: JAccount password.
    :param use_jcss: Use JAccount Captcha Solver Service to recognize captcha instead of built-in ONNX based recognizer.
    :param _mocker_app: An WSGI application to send requests to (for debug or test purposes).
    :return: an authenticated :class:`Client`.
    """
    sess = Session(username=username,
                   password=password,
                   _mocker_app=_mocker_app,
                   ocr=JCSSRecognizer() if use_jcss else None)
    return Client(session=sess)
Ejemplo n.º 7
0
    def test_secure_req(self):
        respx.get("http://secure.page.edu.cn", content=httpx.ConnectionClosed())
        respx.get("http://secure.page.edu.cn:8889", content=httpx.ConnectionClosed())
        respx.get("https://fail.page.edu.cn", content=httpx.ConnectionClosed())
        respx.get("https://secure.page.edu.cn")
        sess = Session()

        resp = sess._secure_req(partial(httpx.get, "http://secure.page.edu.cn"))
        assert resp.status_code == 200

        resp = sess._secure_req(partial(httpx.get, "http://secure.page.edu.cn:8889"))
        assert resp.status_code == 200

        with pytest.raises(httpx.exceptions.NetworkError):
            sess._secure_req(partial(httpx.get, "https://fail.page.edu.cn"))
Ejemplo n.º 8
0
def logged_session(mocker):
    mocker.patch.object(NNRecognizer, "recognize", return_value="ipsum")
    sess = Session(_mocker_app=app, retry=[0], timeout=1)
    sess.login("FeiLin", "WHISPERS")
    return sess
Ejemplo n.º 9
0
    def test_load_dump(self, logged_session, check_login, tmp_path):
        tmp_file = NamedTemporaryFile()
        logged_session.dump(tmp_file.file)
        tmp_file.seek(0)
        sess = Session(_mocker_app=app)
        sess.load(tmp_file.file)
        assert check_login(sess)

        tmp_file = tmp_path / "tmpfile_1"
        # noinspection PyTypeChecker
        open(tmp_file, mode="a").close()
        logged_session.dump(tmp_file)
        sess = Session(_mocker_app=app)
        sess.load(tmp_file)
        assert check_login(sess)

        tmp_file = str(tmp_path / "tmpfile_2")
        open(tmp_file, mode="a").close()
        logged_session.dump(tmp_file)
        sess = Session(_mocker_app=app)
        sess.load(tmp_file)
        assert check_login(sess)

        with pytest.raises(TypeError):
            # noinspection PyTypeChecker
            sess.load(0)
        with pytest.raises(TypeError):
            # noinspection PyTypeChecker
            sess.dump(0)

        empty_file = NamedTemporaryFile()
        sess = Session(_mocker_app=app)
        with pytest.warns(LoadWarning):
            sess.load(empty_file.file)

        empty_file = tmp_path / "empty_file"
        # noinspection PyTypeChecker
        open(empty_file, mode="a").close()
        sess = Session(_mocker_app=app)
        with pytest.warns(LoadWarning):
            sess.load(empty_file)
Ejemplo n.º 10
0
    def test_loads_dumps(self, logged_session, check_login):
        cookie = logged_session.cookies
        dumps = logged_session.dumps()

        sess = Session(_mocker_app=app)
        sess.loads({"username": "******", "password": "******"})
        assert check_login(sess)

        with pytest.warns(LoadWarning):
            sess.loads({})
        assert sess.cookies == httpx.Cookies({})
        assert not sess._username
        assert not sess._password

        sess = Session(_mocker_app=app)
        with pytest.raises(TypeError):
            sess.loads({"cookies": "Cookie☆"})
        with pytest.warns(LoadWarning):
            sess.loads({"cookies": {}})
            sess.loads({"cookies": cookie})
        assert check_login(sess)
        with pytest.warns(DumpWarning):
            sess.dumps()

        sess = Session(_mocker_app=app)
        sess.loads(dumps)
        assert check_login(sess)

        # test auto renew mechanism
        logged_session.logout()
        sess = Session(_mocker_app=app)
        sess.loads(dumps)
        assert check_login(sess)
Ejemplo n.º 11
0
 def test_req_partial_url(self):
     respx.get("http://dummy.url/test_path", content="pass")
     assert Session(base_url="http://dummy.url").get("/test_path").text == "pass"