diff --git a/tests/unit/data/test_file.txt b/tests/unit/data/test_file.txt new file mode 100644 index 0000000..4fff881 --- /dev/null +++ b/tests/unit/data/test_file.txt @@ -0,0 +1 @@ +Test File diff --git a/tests/unit/test_http.py b/tests/unit/test_http.py index 7fd18d2..971dd90 100644 --- a/tests/unit/test_http.py +++ b/tests/unit/test_http.py @@ -1,4 +1,5 @@ from __future__ import unicode_literals +import os import json import httpretty try: @@ -15,17 +16,21 @@ class TestHttp(unittest.TestCase): TEST_DATA = {"message": "Test Message", "code": 200, "result": "Test Result"} + TEST_OAUTH = {"consumer_key": "dummy", + "consumer_secret": "dummy", + "token": "dummy", + "token_secret": "dummy"} + TEST_FILE = os.path.join("tests", "unit", "data", "test_file.txt") + def setUp(self): - self.client = openphoto.OpenPhoto(host=self.TEST_HOST, - consumer_key="dummy", - consumer_secret="dummy", - token="dummy", - token_secret="dummy") + self.client = openphoto.OpenPhoto(host=self.TEST_HOST, **self.TEST_OAUTH) - def _register_uri(self, method, uri=TEST_URI, data=TEST_DATA, **kwds): + def _register_uri(self, method, uri=TEST_URI, data=TEST_DATA, body=None, + **kwds): """Convenience wrapper around httpretty.register_uri""" - body = json.dumps(data) + if body is None: + body = json.dumps(data) httpretty.register_uri(method, uri=uri, body=body, **kwds) @staticmethod @@ -94,11 +99,11 @@ class TestHttp(unittest.TestCase): photo = openphoto.objects.Photo(None, {"id": "photo_id"}) album = openphoto.objects.Album(None, {"id": "album_id"}) tag = openphoto.objects.Tag(None, {"id": "tag_id"}) - response = self.client.get(self.TEST_ENDPOINT, - photo=photo, album=album, tag=tag, - list_=[photo, album, tag], - boolean=True, - unicode_="\xfcmlaut") + self.client.get(self.TEST_ENDPOINT, + photo=photo, album=album, tag=tag, + list_=[photo, album, tag], + boolean=True, + unicode_="\xfcmlaut") params=self._last_request().querystring self.assertEqual(params["photo"], ["photo_id"]) self.assertEqual(params["album"], ["album_id"]) @@ -106,3 +111,42 @@ class TestHttp(unittest.TestCase): self.assertEqual(params["list_"], ["photo_id,album_id,tag_id"]) self.assertEqual(params["boolean"], ["1"]) self.assertEqual(params["unicode_"], ["\xc3\xbcmlaut"]) + + @httpretty.activate + def test_get_with_api_version(self): + self.client = openphoto.OpenPhoto(host=self.TEST_HOST, api_version=1) + self._register_uri(httpretty.GET, + uri="http://%s/v1/%s" % (self.TEST_HOST, + self.TEST_ENDPOINT)) + self.client.get(self.TEST_ENDPOINT) + + @httpretty.activate + def test_post_with_api_version(self): + self.client = openphoto.OpenPhoto(host=self.TEST_HOST, api_version=1, + **self.TEST_OAUTH) + self._register_uri(httpretty.POST, + uri="http://%s/v1/%s" % (self.TEST_HOST, + self.TEST_ENDPOINT)) + self.client.post(self.TEST_ENDPOINT) + + @httpretty.activate + def test_post_file(self): + self._register_uri(httpretty.POST) + with open(self.TEST_FILE, 'rb') as in_file: + response = self.client.post(self.TEST_ENDPOINT, + files={"file": in_file}) + self.assertEqual(response, self.TEST_DATA) + body = self._last_request().body + self.assertIn("Content-Disposition: form-data; "+ + "name=\"file\"; filename=\"test_file.txt\"", body) + self.assertIn("Test File", body) + + + @httpretty.activate + def test_post_file_parameters_are_sent_as_querystring(self): + self._register_uri(httpretty.POST) + with open(self.TEST_FILE, 'rb') as in_file: + response = self.client.post(self.TEST_ENDPOINT, foo="bar", + files={"file": in_file}) + self.assertEqual(response, self.TEST_DATA) + self.assertEqual(self._last_request().querystring["foo"], ["bar"]) diff --git a/tests/unit/test_http_errors.py b/tests/unit/test_http_errors.py new file mode 100644 index 0000000..80eec6d --- /dev/null +++ b/tests/unit/test_http_errors.py @@ -0,0 +1,120 @@ +from __future__ import unicode_literals +import json +import httpretty +try: + import unittest2 as unittest # Python2.6 +except ImportError: + import unittest + +import openphoto +from tests.unit.test_http import TestHttp + +class TestHttpErrors(TestHttp): + def _register_uri(self, method, uri=TestHttp.TEST_URI, + data=None, body=None, status=200, **kwds): + """Convenience wrapper around httpretty.register_uri""" + if data is None: + data = self.TEST_DATA + # Set the JSON return code to match the HTTP status + data["code"] = status + if body is None: + body = json.dumps(data) + httpretty.register_uri(method, uri=uri, body=body, status=status, + **kwds) + + @httpretty.activate + def test_get_with_error_status_raises_openphoto_exception(self): + self._register_uri(httpretty.GET, status=500) + with self.assertRaises(openphoto.OpenPhotoError): + self.client.get(self.TEST_ENDPOINT) + + @httpretty.activate + def test_post_with_error_status_raises_openphoto_exception(self): + self._register_uri(httpretty.POST, status=500) + with self.assertRaises(openphoto.OpenPhotoError): + self.client.post(self.TEST_ENDPOINT) + + # TODO: 404 status should raise 404 error, even if JSON is valid + @unittest.expectedFailure + @httpretty.activate + def test_get_with_404_status_raises_404_exception(self): + self._register_uri(httpretty.GET, status=404) + with self.assertRaises(openphoto.OpenPhoto404Error): + response = self.client.get(self.TEST_ENDPOINT) + + # TODO: 404 status should raise 404 error, even if JSON is valid + @unittest.expectedFailure + @httpretty.activate + def test_post_with_404_status_raises_404_exception(self): + self._register_uri(httpretty.POST, status=404) + with self.assertRaises(openphoto.OpenPhoto404Error): + response = self.client.post(self.TEST_ENDPOINT) + + @httpretty.activate + def test_get_with_invalid_json_raises_exception(self): + self._register_uri(httpretty.GET, body="Invalid JSON") + with self.assertRaises(ValueError): + self.client.get(self.TEST_ENDPOINT) + + @httpretty.activate + def test_post_with_invalid_json_raises_exception(self): + self._register_uri(httpretty.POST, body="Invalid JSON") + with self.assertRaises(ValueError): + self.client.post(self.TEST_ENDPOINT) + + @httpretty.activate + def test_get_with_error_status_and_invalid_json_raises_openphoto_exception(self): + self._register_uri(httpretty.GET, body="Invalid JSON", status=500) + with self.assertRaises(openphoto.OpenPhotoError): + response = self.client.get(self.TEST_ENDPOINT) + + @httpretty.activate + def test_post_with_error_status_and_invalid_json_raises_openphoto_exception(self): + self._register_uri(httpretty.POST, body="Invalid JSON", status=500) + with self.assertRaises(openphoto.OpenPhotoError): + response = self.client.post(self.TEST_ENDPOINT) + + @httpretty.activate + def test_get_with_404_status_and_invalid_json_raises_404_exception(self): + self._register_uri(httpretty.GET, body="Invalid JSON", status=404) + with self.assertRaises(openphoto.OpenPhoto404Error): + response = self.client.get(self.TEST_ENDPOINT) + + @httpretty.activate + def test_post_with_404_status_and_invalid_json_raises_404_exception(self): + self._register_uri(httpretty.POST, body="Invalid JSON", status=404) + with self.assertRaises(openphoto.OpenPhoto404Error): + response = self.client.post(self.TEST_ENDPOINT) + + @httpretty.activate + def test_get_with_duplicate_status_raises_duplicate_exception(self): + data = {"message": "This photo already exists", "code": 409} + self._register_uri(httpretty.GET, data=data, status=409) + with self.assertRaises(openphoto.OpenPhotoDuplicateError): + response = self.client.get(self.TEST_ENDPOINT) + + @httpretty.activate + def test_post_with_duplicate_status_raises_duplicate_exception(self): + data = {"message": "This photo already exists", "code": 409} + self._register_uri(httpretty.POST, data=data, status=409) + with self.assertRaises(openphoto.OpenPhotoDuplicateError): + response = self.client.post(self.TEST_ENDPOINT) + + # TODO: Status code mismatch should raise an exception + @unittest.expectedFailure + @httpretty.activate + def test_get_with_status_code_mismatch_raises_openphoto_exception(self): + data = {"message": "Test Message", "code": 200} + self._register_uri(httpretty.GET, data=data, status=202) + with self.assertRaises(openphoto.OpenPhotoError): + response = self.client.get(self.TEST_ENDPOINT) + + # TODO: Status code mismatch should raise an exception + @unittest.expectedFailure + @httpretty.activate + def test_post_with_status_code_mismatch_raises_openphoto_exception(self): + data = {"message": "Test Message", "code": 200} + self._register_uri(httpretty.POST, data=data, status=202) + with self.assertRaises(openphoto.OpenPhotoError): + response = self.client.post(self.TEST_ENDPOINT) +