Exemplo n.º 1
0
 def test_404(self):
     status_code = 404
     headers, body = sample("invalid", status_code)
     with pytest.raises(BadRequest) as exc_info:
         sansio.decipher_response(status_code, headers, body)
     assert exc_info.value.status_code == http.HTTPStatus(status_code)
     assert str(exc_info.value) == "Not Found"
Exemplo n.º 2
0
    def test_next(self):
        status_code = 200
        headers, body = sample("projects_page_1", status_code)
        data, rate_limit, more = sansio.decipher_response(
            status_code, headers, body)
        assert (
            more ==
            "https://gitlab.com/api/v4/groups/gitlab-org/projects?archived=false&id=gitlab-org&order_by=created_at&owned=false&page=2&per_page=20&simple=false&sort=desc&starred=false&with_custom_attributes=false&with_issues_enabled=false&with_merge_requests_enabled=false"
        )
        assert rate_limit.remaining == 598
        assert data[0]["name"] == "many-branches"

        headers, body = sample("projects_page_2", status_code)
        data, rate_limit, more = sansio.decipher_response(
            status_code, headers, body)
        assert (
            more ==
            "https://gitlab.com/api/v4/groups/gitlab-org/projects?archived=false&id=gitlab-org&order_by=created_at&owned=false&page=3&per_page=20&simple=false&sort=desc&starred=false&with_custom_attributes=false&with_issues_enabled=false&with_merge_requests_enabled=false"
        )
        assert rate_limit.remaining == 598
        assert data[0]["name"] == "gitlab-svgs"

        headers, body = sample("projects_page_last", status_code)
        data, rate_limit, more = sansio.decipher_response(
            status_code, headers, body)
        assert more is None
        assert rate_limit.remaining == 599
        assert data[0]["name"] == "GitLab CI"
Exemplo n.º 3
0
 def test_4XX_message(self):
     status_code = 400
     message = json.dumps({"message": "it went bad"}).encode("UTF-8")
     headers = {"content-type": "application/json; charset=utf-8"}
     with pytest.raises(BadRequest) as exc_info:
         sansio.decipher_response(status_code, headers, message)
     assert exc_info.value.status_code == http.HTTPStatus(status_code)
     assert str(exc_info.value) == "it went bad"
Exemplo n.º 4
0
 def test_403_forbidden(self):
     status_code = 403
     headers = {
         "content-type": "application/json; charset=utf-8",
         "ratelimit-limit": "2",
         "ratelimit-remaining": "1",
         "ratelimit-reset": "1",
     }
     with pytest.raises(BadRequest) as exc_info:
         sansio.decipher_response(status_code, headers, b"")
     assert exc_info.value.status_code == http.HTTPStatus(status_code)
Exemplo n.º 5
0
 def test_403_rate_limit_exceeded(self):
     status_code = 403
     headers = {
         "content-type": "application/json; charset=utf-8",
         "ratelimit-limit": "2",
         "ratelimit-remaining": "0",
         "ratelimit-reset": "1",
     }
     body = json.dumps({"message": "oops"}).encode("UTF-8")
     with pytest.raises(RateLimitExceeded) as exc_info:
         sansio.decipher_response(status_code, headers, body)
     assert exc_info.value.status_code == http.HTTPStatus(status_code)
Exemplo n.º 6
0
 async def test__request_with_body(self):
     """Make sure that that abstract method is implemented properly."""
     request_headers = sansio.create_headers("gidgetlab")
     gl = gl_tornado.GitLabAPI("gidgetlab")
     # This leads to a 404.
     tornado_call = await gl._request(
         "POST",
         "https://gitlab.com/api/v4/templates/licenses/mit",
         request_headers,
         b"bogus",
     )
     with pytest.raises(BadRequest):
         sansio.decipher_response(*tornado_call)
Exemplo n.º 7
0
 def test_422(self):
     status_code = 422
     errors = [{
         "resource": "Issue",
         "field": "title",
         "code": "missing_field"
     }]
     body = json.dumps({"message": "it went bad", "errors": errors})
     body = body.encode("utf-8")
     headers = {"content-type": "application/json; charset=utf-8"}
     with pytest.raises(InvalidField) as exc_info:
         sansio.decipher_response(status_code, headers, body)
     assert exc_info.value.status_code == http.HTTPStatus(status_code)
     assert str(exc_info.value) == "it went bad for 'title'"
Exemplo n.º 8
0
 def test_422_no_errors_object(self):
     status_code = 422
     body = json.dumps({
         "message":
         "Reference does not exist",
         "documentation_url":
         "https://developer.github.com/v3/git/refs/#delete-a-reference",
     })
     body = body.encode("utf-8")
     headers = {"content-type": "application/json; charset=utf-8"}
     with pytest.raises(InvalidField) as exc_info:
         sansio.decipher_response(status_code, headers, body)
     assert exc_info.value.status_code == http.HTTPStatus(status_code)
     assert str(exc_info.value) == "Reference does not exist"
Exemplo n.º 9
0
 async def test__request(self):
     """Make sure that that abstract method is implemented properly."""
     request_headers = sansio.create_headers("gidgetlab")
     gl = gl_tornado.GitLabAPI("gidgetlab")
     tornado_call = await gl._request(
         "GET", "https://gitlab.com/api/v4/templates/licenses/mit",
         request_headers)
     data, rate_limit, _ = sansio.decipher_response(*tornado_call)
     assert "description" in data
Exemplo n.º 10
0
 def test_202(self):
     """Test both a 202 response and an empty response body."""
     status_code = 202
     headers, body = sample("delete_registry_repository", status_code)
     data, rate_limit, more = sansio.decipher_response(
         status_code, headers, body)
     assert more is None
     assert rate_limit.remaining == 599
     assert data is None
Exemplo n.º 11
0
 def test_204(self):
     """Test both a 204 response and an empty response body."""
     status_code = 204
     headers, body = sample("delete_tag", status_code)
     data, rate_limit, more = sansio.decipher_response(
         status_code, headers, body)
     assert more is None
     assert rate_limit.remaining == 594
     assert data is None
Exemplo n.º 12
0
 def test_200(self):
     status_code = 200
     headers, body = sample("projects_single", status_code)
     data, rate_limit, more = sansio.decipher_response(
         status_code, headers, body)
     assert more is None
     assert rate_limit.remaining == 597
     assert data[0][
         "ssh_url_to_repo"] == "[email protected]:beenje/gitlab-ce.git"
Exemplo n.º 13
0
async def test__request():
    """Make sure that that abstract method is implemented properly."""
    request_headers = sansio.create_headers("gidgetlab")
    async with httpx.AsyncClient() as client:
        gl = gl_httpx.GitLabAPI(client, "gidgetlab")
        aio_call = await gl._request(
            "GET",
            "https://gitlab.com/api/v4/templates/licenses/mit",
            request_headers,
        )
    data, rate_limit, _ = sansio.decipher_response(*aio_call)
    assert "description" in data
Exemplo n.º 14
0
 def test_no_ratelimit(self):
     """Test no ratelimit in headers."""
     status_code = 201
     headers = {
         "content-type": "application/json; charset=utf-8",
         "link": '<http://example.com>; test="unimportant"',
     }
     data = {
         "id": 208045946,
         "url":
         "https://gitlab.com/api/v4/projects/gitlab-org%2Fgitlab-ce/labels",
         "name": "bug",
         "color": "f29513",
     }
     body = json.dumps(data).encode("UTF-8")
     returned_data, rate_limit, more = sansio.decipher_response(
         status_code, headers, body)
     assert more is None
     assert rate_limit is None
     assert returned_data == data
Exemplo n.º 15
0
 def test_201(self):
     """Test a 201 response along with non-pagination Link header."""
     status_code = 201
     headers = {
         "ratelimit-limit": "60",
         "ratelimit-remaining": "50",
         "ratelimit-reset": "12345678",
         "content-type": "application/json; charset=utf-8",
         "link": '<http://example.com>; test="unimportant"',
     }
     data = {
         "id": 208045946,
         "url":
         "https://gitlab.com/api/v4/projects/gitlab-org%2Fgitlab-ce/labels",
         "name": "bug",
         "color": "f29513",
     }
     body = json.dumps(data).encode("UTF-8")
     returned_data, rate_limit, more = sansio.decipher_response(
         status_code, headers, body)
     assert more is None
     assert rate_limit.limit == 60
     assert returned_data == data
Exemplo n.º 16
0
 def test_5XX(self):
     status_code = 502
     with pytest.raises(GitLabBroken) as exc_info:
         sansio.decipher_response(status_code, {}, b"")
     assert exc_info.value.status_code == http.HTTPStatus(status_code)
Exemplo n.º 17
0
 def test_2XX_error(self):
     status_code = 205
     with pytest.raises(HTTPException) as exc_info:
         sansio.decipher_response(status_code, {}, b"")
     assert exc_info.value.status_code == http.HTTPStatus(status_code)
Exemplo n.º 18
0
 def test_4XX_no_message(self):
     status_code = 400
     with pytest.raises(BadRequest) as exc_info:
         sansio.decipher_response(status_code, {}, b"")
     assert exc_info.value.status_code == http.HTTPStatus(status_code)
Exemplo n.º 19
0
 def test_done(response):
     data, rate_limit, _ = sansio.decipher_response(*response)
     self.assertIn("description", data)
Exemplo n.º 20
0
 def test_3XX(self):
     status_code = 301
     with pytest.raises(RedirectionException) as exc_info:
         sansio.decipher_response(status_code, {}, b"")
     assert exc_info.value.status_code == http.HTTPStatus(status_code)