Skip to content

Commit

Permalink
Allow testing for server versions
Browse files Browse the repository at this point in the history
  • Loading branch information
tgs committed May 22, 2014
1 parent 27ec80d commit 5be82df
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
17 changes: 17 additions & 0 deletions badgekit/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import requests
import posixpath
from . import jwt_auth
from distutils.version import StrictVersion
try:
from urlparse import urljoin
from urllib import urlencode
Expand Down Expand Up @@ -257,3 +258,19 @@ def _json_loads(self, text):
return json.loads(text)
except ValueError as e:
raise APIError("Invalid JSON in BadgeKit response")

def server_version(self):
"""Returns the server's reported version as a string."""
resp = requests.get(urljoin(self.baseurl, '/'), auth=self.auth)
resp_dict = self._json_loads(resp.text)
return resp_dict['version']

def require_server_version(self, required_version):
version = self.server_version()
server_url = self.baseurl
if StrictVersion(version) < StrictVersion(required_version):
raise ValueError(
("Version {required_version} or greater "
+ "of BadgeKit-API server required, but "
+ "{server_url} is only version {version}.")
.format(**locals()))
31 changes: 31 additions & 0 deletions test/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,37 @@ def test_query(self):
'systems/jkl/badges?archived=true')


class ServerVersionTest(unittest.TestCase):
@httpretty.activate
def test_find_version(self):
a = badgekit.BadgeKitAPI('http://example.com', 'asdf')

httpretty.register_uri(httpretty.GET,
re.compile('example.com/.*'),
body='{"app":"BadgeKit API","version":"0.2.9"}')

resp = a.server_version()
self.assertEqual(resp, "0.2.9")


@httpretty.activate
def test_require_version(self):
a = badgekit.BadgeKitAPI('http://example.com', 'asdf')

httpretty.register_uri(httpretty.GET,
re.compile('example.com/.*'),
body='{"app":"BadgeKit API","version":"0.2.9"}')

try:
a.require_server_version("0.3.0")
self.fail("Should have thrown exception")
except ValueError:
pass

a.require_server_version("0.2")
a.require_server_version("0.2.2")


class ExceptionTest(unittest.TestCase):
def test_known_str(self):
req = requests.Request("POST", "http://example.org/system/")
Expand Down

0 comments on commit 5be82df

Please sign in to comment.