Skip to content

Commit

Permalink
add more tests to increase code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
thomascrowley committed Jan 18, 2024
1 parent 3e0e36a commit 68b6495
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 40 deletions.
11 changes: 10 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,16 @@ app_private_key = "my_app_private_key" # Can be left empty if not used
```

If you use 2 factor authentication on your Github account, tests that require a login/password authentication will fail.
You can use `pytest Issue139.testCompletion --record --auth_with_token` to use the `oauth_token` field specified in `GithubCredentials.py` when recording a unit test interaction. Note that the `password = ""` (empty string is ok) must still be present in `GithubCredentials.py` to run the tests even when the `--auth_with_token` arg is used. (Also note that if you record your test data with `--auth_with_token` then you also need to be in token authentication mode when running the test. A simple alternative is to replace `token private_token_removed` with `Basic login_and_password_removed` in all your newly generated ReplayData files.)
You can use `pytest Issue139.testCompletion --record --auth_with_token` to use the `oauth_token` field specified in `GithubCredentials.py` when recording a unit test interaction. Note that the `password = ""` (empty string is ok) must still be present in `GithubCredentials.py` to run the tests even when the `--auth_with_token` arg is used.

Also note that if you record your test data with `--auth_with_token` then you also need to be in token authentication mode when running the test. You can do this by setting `tokenAuthMode` to be true like so:

```python
def setUp(self):
self.tokenAuthMode = True
```

A simple alternative is to replace `token private_token_removed` with `Basic login_and_password_removed` in all your newly generated ReplayData files.

Similarly, you can use `pytest Issue139.testCompletion --record --auth_with_jwt` to use the `jwt` field specified in `GithubCredentials.py` to access endpoints that require JWT.

Expand Down
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
50 changes: 49 additions & 1 deletion tests/Issue2284.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

class Issue2284(Framework.TestCase):
def setUp(self):
self.tokenAuthMode = True
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 @@ -37,3 +38,50 @@ def testCreateRepoDependabotSecret(self, encrypt):
encrypt.return_value = "M+5Fm/BqTfB90h3nC7F3BoZuu3nXs+/KtpXwxm9gG211tbRo0F5UiN0OIfYT83CKcx9oKES9Va4E96/b"
secret = self.repo.create_secret("secret_name", "secret-value", "dependabot")
self.assertIsNotNone(secret)

def testRepoGetSecretAssertion(self):
try:
self.repo.get_secret(secret_name="splat", secret_type="supersecret")
except AssertionError:
assert True

def testOrgGetSecretAssertion(self):
try:
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
9 changes: 8 additions & 1 deletion tests/Organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,14 @@ def testCreateSecretSelected(self, encrypt):
repos = [self.org.get_repo("TestPyGithub"), self.org.get_repo("FatherBeaver")]
# 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-value", "selected", "actions", repos)
secret = self.org.create_secret(
secret_name="secret-name",
unencrypted_value="secret-value",
visibility="selected",
secret_type="actions",
selected_repositories=repos,
)

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

0 comments on commit 68b6495

Please sign in to comment.