示例#1
0
    def test_logging_get(self, mocker, requests_mock):
        logging_debug_mock = mocker.patch.object(logging.Logger, 'debug')
        logging_error_mock = mocker.patch.object(logging.Logger, 'error')
        requests_mock.get("https://demo.dev/1", [
            dict(status_code=200, json=dict(hello="fish")),
            dict(status_code=200, json=dict(hello="bear")),
        ])
        conn = Connector("https://demo.dev", "user", "pass")
        conn.get("/1")

        logging_error_mock.assert_not_called()
        logging_debug_mock.assert_has_calls([
            mocker.call('{"request": {'
                        '"method": "GET", "base_url": "https://demo.dev", '
                        '"path": "/1", "payload": null}'
                        '}'),
            mocker.call('GET https://demo.dev/1 200'),
            mocker.call(
                '{"request_data": '
                '{"method": "GET", "base_url": "https://demo.dev", "path": "/1"}, '
                '"response": {"status_code": 200, "headers": {}, '
                '"content": "b\'{\\"hello\\": \\"fish\\"}\'", '
                '"json_data": {"hello": "fish"}}'
                '}')
        ])
示例#2
0
 def test_reset_with_path(self, requests_mock):
     self.mock_paths(requests_mock)
     conn = Connector("https://demo.dev", "user", "pass")
     assert conn.get("/1").json == dict(hello="fish")
     assert conn.get("/2").json == dict(solong="fish")
     conn.reset("/2")
     assert conn.get("/1").json == dict(hello="bear")
     assert conn.get("/2").json == dict(solong="bear")
示例#3
0
 def test_get_session_relogin_delete_bad_token_header(self, requests_mock):
     # The steps that are executed during this test:
     #  1. Issue GET request to /data endpoint and get back 401.
     #  2. Remove auth token from headers
     #  3. POST to session endpoint and get back 201.
     #  4. Issue GET request to /data endpoint and get bac 200.
     #
     # If the header would not be removed, the steps executed would be:
     #  1. Issue GET request to /data endpoint and get back 401.
     #  2. POST to session endpoint and get back 401.
     #  3. Auth exception is raised.
     #
     # This is why we mocked the POST request twice: to test that the
     # token is not present. And because mocked endpoints are matched in
     # LIFO order, the test works as described before.
     requests_mock.get("https://demo.dev/data", [
         dict(status_code=401), dict(status_code=200),
     ])
     requests_mock.post(
         "https://demo.dev/sessions", status_code=201,
         headers={"X-Auth-Token": "123", "Location": "/sessions/3"},
     )
     requests_mock.post(
         "https://demo.dev/sessions", status_code=401,
         request_headers={"x-auth-token": "abc"},
     )
     conn = Connector("https://demo.dev", "user", "pass")
     conn.set_session_auth_data("/sessions", token="abc")
     r = conn.get("/data")
     assert r.status == 200
     assert r.json is None
示例#4
0
 def test_get_caching_non_ok(self, requests_mock):
     requests_mock.get("https://demo.dev/data", [
         dict(status_code=404, json=dict(error="bad")),
         dict(status_code=500, json=dict(really="bad")),
     ])
     conn = Connector("https://demo.dev", None, None)
     assert conn.get("/data").json == dict(error="bad")
     assert conn.get("/data").json == dict(really="bad")
示例#5
0
 def test_get_caching_ok(self, requests_mock):
     requests_mock.get("https://demo.dev/data", [
         dict(status_code=200, json=dict(hello="fish")),
         dict(status_code=200, json=dict(solong="fish")),
     ])
     conn = Connector("https://demo.dev", None, None)
     assert conn.get("/data").json == dict(hello="fish")
     assert conn.get("/data").json == dict(solong="fish")
示例#6
0
 def test_get_non_200(self, requests_mock):
     requests_mock.get(
         "https://demo.dev/data", status_code=404, json=dict(error="bad"),
     )
     conn = Connector("https://demo.dev", "user", "pass")
     r = conn.get("/data")
     assert r.status == 404
     assert r.json == dict(error="bad")
示例#7
0
 def test_get_no_auth(self, requests_mock):
     requests_mock.get(
         "https://demo.dev/data", status_code=200, json=dict(hello="fish"),
     )
     conn = Connector("https://demo.dev", "user", "pass")
     r = conn.get("/data")
     assert r.status == 200
     assert r.json == dict(hello="fish")
示例#8
0
 def test_get_use_existing_session(self, requests_mock):
     requests_mock.get(
         "https://demo.dev/data", status_code=200,
         request_headers={"X-Auth-Token": "123"},
     )
     conn = Connector("https://demo.dev", "user", "pass")
     conn.set_session_auth_data("/sessions", "/sessions/1", "123")
     r = conn.get("/data")
     assert r.status == 200
     assert r.json is None
示例#9
0
 def test_get_session_relogin(self, requests_mock):
     requests_mock.get("https://demo.dev/data", [
         dict(status_code=401), dict(status_code=200),
     ])
     requests_mock.post(
         "https://demo.dev/sessions", status_code=201,
         headers={"X-Auth-Token": "123", "Location": "/sessions/3"},
     )
     conn = Connector("https://demo.dev", "user", "pass")
     conn.set_session_auth_data("/sessions")
     r = conn.get("/data")
     assert r.status == 200
     assert r.json is None
示例#10
0
 def test_get_basic_relogin(self, requests_mock):
     # Define more specific mock aafter less specific ones, since they are
     # matched in bottom-up order.
     requests_mock.get("https://demo.dev/data", status_code=401)
     requests_mock.get(
         "https://demo.dev/test", status_code=200,
         request_headers=dict(Authorization="Basic dXNlcjpwYXNz"),
     )
     requests_mock.get(
         "https://demo.dev/data", status_code=200,
         request_headers=dict(Authorization="Basic dXNlcjpwYXNz"),
     )
     conn = Connector("https://demo.dev", "user", "pass")
     conn.set_basic_auth_data("/test")
     r = conn.get("/data")
     assert r.status == 200
     assert r.json is None
示例#11
0
 def test_get_non_json_body(self, requests_mock):
     requests_mock.get("https://demo.dev/data", status_code=200)
     conn = Connector("https://demo.dev", "user", "pass")
     r = conn.get("/data")
     assert r.status == 200
     assert r.json is None