Repository: alexis-mignon/python-flickr-api

Issue:
Exception UnboundLocalError if server response not 200 when uploading photo
```
Traceback (most recent call last):
 
  File "/home/w/projects/panoedit/flickr_upload.py", line 60, in main
    path = os.path.join(conf.photos_dir, file_name)
  File "/home/w/projects/panoedit/google_venv/local/lib/python2.7/site-packages/flickr_api/upload.py", line 107, in upload
    r = post(UPLOAD_URL, auth.AUTH_HANDLER, args, photo_file, photo_file_data)
  File "/home/w/projects/panoedit/google_venv/local/lib/python2.7/site-packages/flickr_api/upload.py", line 61, in post
    raise FlickrError("HTTP Error %i: %s" % (r.status, data))
UnboundLocalError: local variable 'r' referenced before assignment
```

Discussion:

Patch:
diff --git a/flickr_api/upload.py b/flickr_api/upload.py
index 1595b7f..65de043 100644
--- a/flickr_api/upload.py
+++ b/flickr_api/upload.py
@@ -58,7 +58,7 @@ def post(url, auth_handler, args, photo_file, photo_file_data=None):
     data = resp.content
 
     if resp.status_code != 200:
-        raise FlickrError("HTTP Error %i: %s" % (r.status, data))
+        raise FlickrError("HTTP Error %i: %s" % (resp.status_code, resp.text))
 
     r = ET.fromstring(data)
     if r.get("stat") != 'ok':


Test patch:
diff --git a/test/test_upload.py b/test/test_upload.py
new file mode 100644
index 0000000..90dffba
--- /dev/null
+++ b/test/test_upload.py
@@ -0,0 +1,37 @@
+import unittest
+from unittest.mock import MagicMock
+
+from flickr_api import upload
+from flickr_api.auth import AuthHandler
+from flickr_api.flickrerrors import FlickrError
+
+from requests import Response
+
+from io import StringIO
+from io import BytesIO
+
+import inspect
+
+class TestUpload(unittest.TestCase):
+    def test_upload_not_200(self):
+        from flickr_api import set_auth_handler
+        auth_handler = AuthHandler(key="test", secret="test", 
+            access_token_key="test",
+            access_token_secret="test")
+        set_auth_handler(auth_handler)
+        args = dict(
+            photo_file = '/tmp/test_file',
+            photo_file_data = StringIO("000000")
+        )
+
+        module = inspect.getmodule(upload)
+        resp = Response()
+        resp.status_code = 404
+        resp.raw = BytesIO("Not Found".encode("utf-8"))
+        module.requests.post = MagicMock(return_value=resp)
+
+        with self.assertRaises(FlickrError) as context:
+            upload(**args)
+        
+        print(context.exception)
+        self.assertEquals("HTTP Error 404: Not Found", str(context.exception))
\ No newline at end of file