| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a3eafab commit 51573dc
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -95,6 +95,7 @@ def __init__( | |||
| 95 | 95 | self.http_password = http_password | |
| 96 | 96 | self.oauth_token = oauth_token | |
| 97 | 97 | self.job_token = job_token | |
| 98 | + self.auth_type = "" | ||
| 98 | 99 | self._set_auth_info() | |
| 99 | 100 | ||
| 100 | 101 | #: Create a session object for requests | |
@@ -405,21 +406,25 @@ def _set_auth_info(self) -> None: | |||
| 405 | 406 | self.headers.pop("Authorization", None) | |
| 406 | 407 | self.headers["PRIVATE-TOKEN"] = self.private_token | |
| 407 | 408 | self.headers.pop("JOB-TOKEN", None) | |
| 409 | + self.auth_type = "private_token" | ||
| 408 | 410 | ||
| 409 | 411 | if self.oauth_token: | |
| 410 | 412 | self.headers["Authorization"] = f"Bearer {self.oauth_token}" | |
| 411 | 413 | self.headers.pop("PRIVATE-TOKEN", None) | |
| 412 | 414 | self.headers.pop("JOB-TOKEN", None) | |
| 415 | + self.auth_type = "oauth_token" | ||
| 413 | 416 | ||
| 414 | 417 | if self.job_token: | |
| 415 | 418 | self.headers.pop("Authorization", None) | |
| 416 | 419 | self.headers.pop("PRIVATE-TOKEN", None) | |
| 417 | 420 | self.headers["JOB-TOKEN"] = self.job_token | |
| 421 | + self.auth_type = "job_token" | ||
| 418 | 422 | ||
| 419 | 423 | if self.http_username: | |
| 420 | 424 | self._http_auth = requests.auth.HTTPBasicAuth( | |
| 421 | 425 | self.http_username, self.http_password | |
| 422 | 426 | ) | |
| 427 | + self.auth_type = "password" | ||
| 423 | 428 | ||
| 424 | 429 | def enable_debug(self) -> None: | |
| 425 | 430 | import logging | |
@@ -640,6 +645,7 @@ def http_request( | |||
| 640 | 645 | response_code=result.status_code, | |
| 641 | 646 | error_message=error_message, | |
| 642 | 647 | response_body=result.content, | |
| 648 | + auth_type=self.auth_type, | ||
| 643 | 649 | ) | |
| 644 | 650 | ||
| 645 | 651 | raise gitlab.exceptions.GitlabHttpError( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -52,7 +52,24 @@ def __str__(self) -> str: | |||
| 52 | 52 | ||
| 53 | 53 | ||
| 54 | 54 | class GitlabAuthenticationError(GitlabError): | |
| 55 | - pass | ||
| 55 | + def __init__( | ||
| 56 | + self, | ||
| 57 | + error_message: Union[str, bytes] = "", | ||
| 58 | + response_code: Optional[int] = None, | ||
| 59 | + response_body: Optional[bytes] = None, | ||
| 60 | + auth_type: str = "", | ||
| 61 | + ) -> None: | ||
| 62 | + super().__init__( | ||
| 63 | + error_message=error_message, | ||
| 64 | + response_code=response_code, | ||
| 65 | + response_body=response_body, | ||
| 66 | + ) | ||
| 67 | + self.auth_type = auth_type | ||
| 68 | + | ||
| 69 | + def __str__(self) -> str: | ||
| 70 | + if self.auth_type: | ||
| 71 | + return f"{super().__str__()}: authentication_type: {self.auth_type}" | ||
| 72 | + return super().__str__() | ||
| 56 | 73 | ||
| 57 | 74 | ||
| 58 | 75 | class RedirectError(GitlabError): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,3 +16,27 @@ def raise_error_from_http_error(): | |||
| 16 | 16 | with pytest.raises(TestError) as context: | |
| 17 | 17 | raise_error_from_http_error() | |
| 18 | 18 | assert isinstance(context.value.__cause__, exceptions.GitlabHttpError) | |
| 19 | + | ||
| 20 | + | ||
| 21 | + def test_gitlabauthenticationerror_with_auth_type(): | ||
| 22 | + with pytest.raises(exceptions.GitlabAuthenticationError) as context: | ||
| 23 | + raise exceptions.GitlabAuthenticationError( | ||
| 24 | + error_message="401 Unauthorized", | ||
| 25 | + response_code=401, | ||
| 26 | + response_body=b"bad user", | ||
| 27 | + auth_type="job_token", | ||
| 28 | + ) | ||
| 29 | + assert "authentication_type" in str(context.value) | ||
| 30 | + assert "job_token" in str(context.value) | ||
| 31 | + assert "401 Unauthorized" in str(context.value) | ||
| 32 | + | ||
| 33 | + | ||
| 34 | + def test_gitlabauthenticationerror_no_auth_type(): | ||
| 35 | + with pytest.raises(exceptions.GitlabAuthenticationError) as context: | ||
| 36 | + raise exceptions.GitlabAuthenticationError( | ||
| 37 | + error_message="401 Unauthorized", | ||
| 38 | + response_code=401, | ||
| 39 | + response_body=b"bad user", | ||
| 40 | + ) | ||
| 41 | + assert "authentication_type" not in str(context.value) | ||
| 42 | + assert "401 Unauthorized" in str(context.value) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments