Skip to content

Commit

Permalink
Fixes #4
Browse files Browse the repository at this point in the history
Make passing a bad value as an argument to certain functions more
recoverable.
  • Loading branch information
mschwager committed Jan 19, 2017
1 parent 5ca2679 commit 72da801
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
22 changes: 17 additions & 5 deletions lib/gitem/api.py
Expand Up @@ -169,9 +169,17 @@ def get_users_public_repositories(self, username, type=None, sort=None, directio
https://developer.github.com/v3/repos/#list-user-repositories
"""
assert type in ["all", "owner", "member", None]
assert sort in ["created", "updated", "pushed", "full_name", None]
assert direction in ["asc", "desc", None]
type_values = ["all", "owner", "member"]
if type not in type_values and type is not None:
raise ValueError("type must be one of {}".format(type_values))

sort_values = ["created", "updated", "pushed", "full_name"]
if sort not in sort_values and sort is not None:
raise ValueError("sort must be one of {}".format(sort_values))

direction_values = ["asc", "desc"]
if direction not in direction_values and direction is not None:
raise ValueError("direction must be one of {}".format(direction_values))

method = "GET"
endpoint = "/users/{}/repos".format(username)
Expand Down Expand Up @@ -218,7 +226,9 @@ def get_organizations_public_repositories(self, organization, type=None):
https://developer.github.com/v3/repos/#list-organization-repositories
"""
assert type in ["all", "public", "private", "forks", "sources", "member", None]
type_values = ["all", "public", "private", "forks", "sources", "member"]
if type not in type_values and type is not None:
raise ValueError("type must be one of {}".format(type_values))

method = "GET"
endpoint = "/orgs/{}/repos".format(organization)
Expand Down Expand Up @@ -265,7 +275,9 @@ def get_repository_contributors(self, owner, repository, anon=None):
https://developer.github.com/v3/repos/#list-contributors
"""
assert anon in [1, "true", None]
anon_values = [1, "true"]
if anon not in anon_values and type is not None:
raise ValueError("anon must be one of {}".format(anon_values))

method = "GET"
endpoint = "/repos/{}/{}/contributors".format(owner, repository)
Expand Down
35 changes: 35 additions & 0 deletions tests/test_api.py
Expand Up @@ -202,6 +202,41 @@ def test_paged_ok(self):
self.assertOk(status_code)
self.assertEqual(result, expected)

def test_get_users_public_repositories_bad_type(self):
type = ""
ghapi = api.Api()

with self.assertRaises(ValueError):
ghapi.get_users_public_repositories("UNUSED", type=type)

def test_get_users_public_repositories_bad_sort(self):
sort = ""
ghapi = api.Api()

with self.assertRaises(ValueError):
ghapi.get_users_public_repositories("UNUSED", sort=sort)

def test_get_users_public_repositories_bad_direction(self):
direction = ""
ghapi = api.Api()

with self.assertRaises(ValueError):
ghapi.get_users_public_repositories("UNUSED", direction=direction)

def test_get_organizations_public_repositories_bad_type(self):
type = ""
ghapi = api.Api()

with self.assertRaises(ValueError):
ghapi.get_organizations_public_repositories("UNUSED", type=type)

def test_get_repository_contributors_bad_anon(self):
anon = ""
ghapi = api.Api()

with self.assertRaises(ValueError):
ghapi.get_repository_contributors("UNUSED", "UNUSED", anon=anon)


if __name__ == "__main__":
unittest.main()

0 comments on commit 72da801

Please sign in to comment.