Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thomascrowley committed Jan 18, 2024
1 parent dd6507f commit 5892985
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 38 deletions.
8 changes: 7 additions & 1 deletion github/Organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,13 @@ def create_secret(
"visibility": visibility,
}
if is_defined(selected_repositories):
put_parameters["selected_repository_ids"] = [element.id for element in selected_repositories]
# Dependbot and Actions endpoint expects different types
# https://docs.github.com/en/rest/dependabot/secrets?apiVersion=2022-11-28#create-or-update-an-organization-secret
# https://docs.github.com/en/rest/actions/secrets?apiVersion=2022-11-28#create-or-update-an-organization-secret
if secret_type == "actions":
put_parameters["selected_repository_ids"] = [element.id for element in selected_repositories]
if secret_type == "dependabot":
put_parameters["selected_repository_ids"] = [str(element.id) for element in selected_repositories]

self._requester.requestJsonAndCheck(
"PUT", f"{self.url}/{secret_type}/secrets/{urllib.parse.quote(secret_name)}", input=put_parameters
Expand Down
37 changes: 36 additions & 1 deletion tests/Issue2284.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Issue2284(Framework.TestCase):
def setUp(self):
super().setUp()
self.user = self.g.get_user()
self.org = self.g.get_organization("smk-org")
self.org = self.g.get_organization("pygithubtest")
self.repo = self.org.get_repo("demo-repo-1")

@mock.patch("github.PublicKey.encrypt")
Expand Down Expand Up @@ -49,3 +49,38 @@ def testOrgGetSecretAssertion(self):
self.org.get_secret(secret_name="splat", secret_type="supersecret")
except AssertionError:
assert True

@mock.patch("github.PublicKey.encrypt")
def testCreateDependabotSecretSelected(self, encrypt):
repos = [self.org.get_repo("demo-repo-1"), self.org.get_repo("demo-repo-2")]
# encrypt returns a non-deterministic value, we need to mock it so the replay data matches
encrypt.return_value = "M+5Fm/BqTfB90h3nC7F3BoZuu3nXs+/KtpXwxm9gG211tbRo0F5UiN0OIfYT83CKcx9oKES9Va4E96/b"
secret = self.org.create_secret(
secret_name="secret_dep_name",
unencrypted_value="secret-value",
visibility="selected",
secret_type="dependabot",
selected_repositories=repos,
)

self.assertIsNotNone(secret)
self.assertEqual(secret.visibility, "selected")
self.assertEqual(list(secret.selected_repositories), repos)

@mock.patch("github.PublicKey.encrypt")
def testOrgSecretEdit(self, encrypt):
repos = [self.org.get_repo("demo-repo-1"), self.org.get_repo("demo-repo-2")]
# encrypt returns a non-deterministic value, we need to mock it so the replay data matches
encrypt.return_value = "M+5Fm/BqTfB90h3nC7F3BoZuu3nXs+/KtpXwxm9gG211tbRo0F5UiN0OIfYT83CKcx9oKES9Va4E96/b"
secret = self.org.create_secret(
secret_name="secret_act_name",
unencrypted_value="secret-value",
visibility="selected",
secret_type="actions",
selected_repositories=repos,
)

try:
secret.edit(value="newvalue", secret_type="supersecret")
except AssertionError:
assert True
16 changes: 8 additions & 8 deletions tests/ReplayData/Issue2284.setUp.txt

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions tests/ReplayData/Issue2284.testCreateActionsSecret.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ https
GET
api.github.com
None
/orgs/smk-org/actions/secrets/public-key
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
/orgs/pygithubtest/actions/secrets/public-key
{'Authorization': 'basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
None
200
[('Server', 'GitHub.com'), ('Date', 'Wed, 17 Jan 2024 20:45:13 GMT'), ('Content-Type', 'application/json; charset=utf-8'), ('Transfer-Encoding', 'chunked'), ('Cache-Control', 'private, max-age=60, s-maxage=60'), ('Vary', 'Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding, Accept, X-Requested-With'), ('ETag', 'W/"3b2d45d9bbbf76c44174d65123aa44db44ecb801c36bf9a1606221f0a5f5d46f"'), ('github-authentication-token-expiration', '2024-04-16 20:21:06 +0100'), ('X-GitHub-Media-Type', 'github.v3; format=json'), ('x-accepted-github-permissions', 'organization_secrets=read'), ('x-github-api-version-selected', '2022-11-28'), ('X-RateLimit-Limit', '5000'), ('X-RateLimit-Remaining', '4955'), ('X-RateLimit-Reset', '1705526969'), ('X-RateLimit-Used', '45'), ('X-RateLimit-Resource', 'core'), ('Access-Control-Expose-Headers', 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset'), ('Access-Control-Allow-Origin', '*'), ('Strict-Transport-Security', 'max-age=31536000; includeSubdomains; preload'), ('X-Frame-Options', 'deny'), ('X-Content-Type-Options', 'nosniff'), ('X-XSS-Protection', '0'), ('Referrer-Policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('Content-Security-Policy', "default-src 'none'"), ('Content-Encoding', 'gzip'), ('X-GitHub-Request-Id', 'ECAF:1AC273:7CD4C72:7E1918F:65A83C58')]
{"key_id":"3380204578043523366","key":"lEcXo0mlVf630hnPSTSCuXmGo2CxuIAKT7RRvZ1QjB4="}
[('Server', 'GitHub.com'), ('Date', 'Thu, 18 Jan 2024 11:51:44 GMT'), ('Content-Type', 'application/json; charset=utf-8'), ('Transfer-Encoding', 'chunked'), ('Cache-Control', 'private, max-age=60, s-maxage=60'), ('Vary', 'Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding, Accept, X-Requested-With'), ('ETag', 'W/"356ce214c26e7bc245afc7364e3676a07c3ed66cc3d3bac4589e3a2204b1bb35"'), ('X-OAuth-Scopes', 'admin:enterprise, admin:gpg_key, admin:org, admin:org_hook, admin:public_key, admin:repo_hook, admin:ssh_signing_key, audit_log, codespace, copilot, delete:packages, delete_repo, gist, notifications, project, repo, user, workflow, write:discussion, write:packages'), ('X-Accepted-OAuth-Scopes', 'admin:org'), ('github-authentication-token-expiration', '2024-01-25 10:23:13 UTC'), ('X-GitHub-Media-Type', 'github.v3; format=json'), ('x-github-api-version-selected', '2022-11-28'), ('X-RateLimit-Limit', '5000'), ('X-RateLimit-Remaining', '4793'), ('X-RateLimit-Reset', '1705581949'), ('X-RateLimit-Used', '207'), ('X-RateLimit-Resource', 'core'), ('Access-Control-Expose-Headers', 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset'), ('Access-Control-Allow-Origin', '*'), ('Strict-Transport-Security', 'max-age=31536000; includeSubdomains; preload'), ('X-Frame-Options', 'deny'), ('X-Content-Type-Options', 'nosniff'), ('X-XSS-Protection', '0'), ('Referrer-Policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('Content-Security-Policy', "default-src 'none'"), ('Content-Encoding', 'gzip'), ('X-GitHub-Request-Id', '222F:2C7EC3:BAC1AF7:BC98A11:65A910D0')]
{"key_id":"3380204578043523366","key":"omE0l9TQXWY+fTPEERGoSnNkDu8NzGqyKHg4yhJyuGI="}

https
PUT
api.github.com
None
/orgs/smk-org/actions/secrets/secret_name
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python', 'Content-Type': 'application/json'}
/orgs/pygithubtest/actions/secrets/secret_name
{'Authorization': 'token private_token_removed', 'User-Agent': 'PyGithub/Python', 'Content-Type': 'application/json'}
{"key_id": "3380204578043523366", "encrypted_value": "M+5Fm/BqTfB90h3nC7F3BoZuu3nXs+/KtpXwxm9gG211tbRo0F5UiN0OIfYT83CKcx9oKES9Va4E96/b", "visibility": "all"}
204
[('Server', 'GitHub.com'), ('Date', 'Wed, 17 Jan 2024 20:45:13 GMT'), ('github-authentication-token-expiration', '2024-04-16 20:21:06 +0100'), ('X-GitHub-Media-Type', 'github.v3; format=json'), ('x-accepted-github-permissions', 'organization_secrets=write'), ('x-github-api-version-selected', '2022-11-28'), ('X-RateLimit-Limit', '5000'), ('X-RateLimit-Remaining', '4954'), ('X-RateLimit-Reset', '1705526969'), ('X-RateLimit-Used', '46'), ('X-RateLimit-Resource', 'core'), ('Access-Control-Expose-Headers', 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset'), ('Access-Control-Allow-Origin', '*'), ('Strict-Transport-Security', 'max-age=31536000; includeSubdomains; preload'), ('X-Frame-Options', 'deny'), ('X-Content-Type-Options', 'nosniff'), ('X-XSS-Protection', '0'), ('Referrer-Policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('Content-Security-Policy', "default-src 'none'"), ('Vary', 'Accept-Encoding, Accept, X-Requested-With'), ('X-GitHub-Request-Id', 'ECB0:4E5F5:7D2DFFD:7E754C0:65A83C59')]
[('Server', 'GitHub.com'), ('Date', 'Thu, 18 Jan 2024 11:51:44 GMT'), ('X-OAuth-Scopes', 'admin:enterprise, admin:gpg_key, admin:org, admin:org_hook, admin:public_key, admin:repo_hook, admin:ssh_signing_key, audit_log, codespace, copilot, delete:packages, delete_repo, gist, notifications, project, repo, user, workflow, write:discussion, write:packages'), ('X-Accepted-OAuth-Scopes', 'admin:org'), ('github-authentication-token-expiration', '2024-01-25 10:23:13 UTC'), ('X-GitHub-Media-Type', 'github.v3; format=json'), ('x-github-api-version-selected', '2022-11-28'), ('X-RateLimit-Limit', '5000'), ('X-RateLimit-Remaining', '4792'), ('X-RateLimit-Reset', '1705581949'), ('X-RateLimit-Used', '208'), ('X-RateLimit-Resource', 'core'), ('Access-Control-Expose-Headers', 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset'), ('Access-Control-Allow-Origin', '*'), ('Strict-Transport-Security', 'max-age=31536000; includeSubdomains; preload'), ('X-Frame-Options', 'deny'), ('X-Content-Type-Options', 'nosniff'), ('X-XSS-Protection', '0'), ('Referrer-Policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('Content-Security-Policy', "default-src 'none'"), ('Vary', 'Accept-Encoding, Accept, X-Requested-With'), ('X-GitHub-Request-Id', 'CC97:225771:B583BBB:B75AA83:65A910D0')]
14 changes: 7 additions & 7 deletions tests/ReplayData/Issue2284.testCreateDependabotSecret.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ https
GET
api.github.com
None
/orgs/smk-org/dependabot/secrets/public-key
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
/orgs/pygithubtest/dependabot/secrets/public-key
{'Authorization': 'basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
None
200
[('Server', 'GitHub.com'), ('Date', 'Wed, 17 Jan 2024 20:45:15 GMT'), ('Content-Type', 'application/json; charset=utf-8'), ('Transfer-Encoding', 'chunked'), ('Cache-Control', 'private, max-age=60, s-maxage=60'), ('Vary', 'Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding, Accept, X-Requested-With'), ('ETag', 'W/"c2c075d4cc3f51f6dc60e972878b8d2e5b9fa71ed8d89765c86e22d7f0b34fb4"'), ('github-authentication-token-expiration', '2024-04-16 20:21:06 +0100'), ('X-GitHub-Media-Type', 'github.v3; format=json'), ('x-accepted-github-permissions', 'organization_dependabot_secrets=read'), ('x-github-api-version-selected', '2022-11-28'), ('X-RateLimit-Limit', '5000'), ('X-RateLimit-Remaining', '4951'), ('X-RateLimit-Reset', '1705526969'), ('X-RateLimit-Used', '49'), ('X-RateLimit-Resource', 'core'), ('Access-Control-Expose-Headers', 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset'), ('Access-Control-Allow-Origin', '*'), ('Strict-Transport-Security', 'max-age=31536000; includeSubdomains; preload'), ('X-Frame-Options', 'deny'), ('X-Content-Type-Options', 'nosniff'), ('X-XSS-Protection', '0'), ('Referrer-Policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('Content-Security-Policy', "default-src 'none'"), ('Content-Encoding', 'gzip'), ('X-GitHub-Request-Id', 'ECB3:120C7D:11EC462:121A710:65A83C5A')]
{"key_id":"3380217566468950943","key":"HYk6AFuoV0iI+t+geHowOxji1OKAGW6GtngRFeETM14="}
[('Server', 'GitHub.com'), ('Date', 'Thu, 18 Jan 2024 11:51:45 GMT'), ('Content-Type', 'application/json; charset=utf-8'), ('Transfer-Encoding', 'chunked'), ('Cache-Control', 'private, max-age=60, s-maxage=60'), ('Vary', 'Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding, Accept, X-Requested-With'), ('ETag', 'W/"54dd2662902b3395d14387df1c2a369171def74124d1b9a4360f5b69bfe7d503"'), ('X-OAuth-Scopes', 'admin:enterprise, admin:gpg_key, admin:org, admin:org_hook, admin:public_key, admin:repo_hook, admin:ssh_signing_key, audit_log, codespace, copilot, delete:packages, delete_repo, gist, notifications, project, repo, user, workflow, write:discussion, write:packages'), ('X-Accepted-OAuth-Scopes', 'admin:org'), ('github-authentication-token-expiration', '2024-01-25 10:23:13 UTC'), ('X-GitHub-Media-Type', 'github.v3; format=json'), ('x-github-api-version-selected', '2022-11-28'), ('X-RateLimit-Limit', '5000'), ('X-RateLimit-Remaining', '4789'), ('X-RateLimit-Reset', '1705581949'), ('X-RateLimit-Used', '211'), ('X-RateLimit-Resource', 'core'), ('Access-Control-Expose-Headers', 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset'), ('Access-Control-Allow-Origin', '*'), ('Strict-Transport-Security', 'max-age=31536000; includeSubdomains; preload'), ('X-Frame-Options', 'deny'), ('X-Content-Type-Options', 'nosniff'), ('X-XSS-Protection', '0'), ('Referrer-Policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('Content-Security-Policy', "default-src 'none'"), ('Content-Encoding', 'gzip'), ('X-GitHub-Request-Id', 'B4C1:2C7EC3:BAC213C:BC99052:65A910D1')]
{"key_id":"3380217566468950943","key":"wiFZeO47SIh+5vVMztcnw1hCJPd8SCHUffVlKhtLMlk="}

https
PUT
api.github.com
None
/orgs/smk-org/dependabot/secrets/secret_name
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python', 'Content-Type': 'application/json'}
/orgs/pygithubtest/dependabot/secrets/secret_name
{'Authorization': 'basic login_and_password_removed', 'User-Agent': 'PyGithub/Python', 'Content-Type': 'application/json'}
{"key_id": "3380217566468950943", "encrypted_value": "M+5Fm/BqTfB90h3nC7F3BoZuu3nXs+/KtpXwxm9gG211tbRo0F5UiN0OIfYT83CKcx9oKES9Va4E96/b", "visibility": "all"}
204
[('Server', 'GitHub.com'), ('Date', 'Wed, 17 Jan 2024 20:45:15 GMT'), ('github-authentication-token-expiration', '2024-04-16 20:21:06 +0100'), ('X-GitHub-Media-Type', 'github.v3; format=json'), ('x-accepted-github-permissions', 'organization_dependabot_secrets=write'), ('x-github-api-version-selected', '2022-11-28'), ('X-RateLimit-Limit', '5000'), ('X-RateLimit-Remaining', '4950'), ('X-RateLimit-Reset', '1705526969'), ('X-RateLimit-Used', '50'), ('X-RateLimit-Resource', 'core'), ('Access-Control-Expose-Headers', 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset'), ('Access-Control-Allow-Origin', '*'), ('Strict-Transport-Security', 'max-age=31536000; includeSubdomains; preload'), ('X-Frame-Options', 'deny'), ('X-Content-Type-Options', 'nosniff'), ('X-XSS-Protection', '0'), ('Referrer-Policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('Content-Security-Policy', "default-src 'none'"), ('Vary', 'Accept-Encoding, Accept, X-Requested-With'), ('X-GitHub-Request-Id', 'ECB4:1A94FB:862249C:87696D6:65A83C5B')]
[('Server', 'GitHub.com'), ('Date', 'Thu, 18 Jan 2024 11:51:46 GMT'), ('X-OAuth-Scopes', 'admin:enterprise, admin:gpg_key, admin:org, admin:org_hook, admin:public_key, admin:repo_hook, admin:ssh_signing_key, audit_log, codespace, copilot, delete:packages, delete_repo, gist, notifications, project, repo, user, workflow, write:discussion, write:packages'), ('X-Accepted-OAuth-Scopes', 'admin:org'), ('github-authentication-token-expiration', '2024-01-25 10:23:13 UTC'), ('X-GitHub-Media-Type', 'github.v3; format=json'), ('x-github-api-version-selected', '2022-11-28'), ('X-RateLimit-Limit', '5000'), ('X-RateLimit-Remaining', '4788'), ('X-RateLimit-Reset', '1705581949'), ('X-RateLimit-Used', '212'), ('X-RateLimit-Resource', 'core'), ('Access-Control-Expose-Headers', 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset'), ('Access-Control-Allow-Origin', '*'), ('Strict-Transport-Security', 'max-age=31536000; includeSubdomains; preload'), ('X-Frame-Options', 'deny'), ('X-Content-Type-Options', 'nosniff'), ('X-XSS-Protection', '0'), ('Referrer-Policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('Content-Security-Policy', "default-src 'none'"), ('Vary', 'Accept-Encoding, Accept, X-Requested-With'), ('X-GitHub-Request-Id', 'CC9C:16832F:B39B2ED:B56E5C6:65A910D2')]

0 comments on commit 5892985

Please sign in to comment.