Repository: mapbox/mapbox-sdk-py
Pull request #87 (2015-12-04 23:49:44)

Add upload test and boto3 session monkeypatching
TODO in another PR: convert mock responses to test fixtures.


Patch:
diff --git a/mapbox/services/uploads.py b/mapbox/services/uploads.py
index b103d8a..5a4d2c0 100644
--- a/mapbox/services/uploads.py
+++ b/mapbox/services/uploads.py
@@ -1,6 +1,8 @@
 # mapbox
-import boto3
+
+from boto3.session import Session as boto3_session
 from uritemplate import URITemplate
+
 from .base import Service
 
 
@@ -39,7 +41,7 @@ class Uploader(Service):
             custom_messages={401: "Token does not have upload scope"})
         return resp
 
-    def stage(self, filepath, creds=None, session_class=boto3.session.Session):
+    def stage(self, filepath, creds=None):
         """Stages the user's file on S3
         If creds are not provided, temporary credientials will be generated
         Returns the URL to the staged resource.
@@ -48,7 +50,7 @@ class Uploader(Service):
             res = self._get_credentials()
             creds = res.json()
 
-        session = session_class(
+        session = boto3_session(
             aws_access_key_id=creds['accessKeyId'],
             aws_secret_access_key=creds['secretAccessKey'],
             aws_session_token=creds['sessionToken'],


Test patch:
diff --git a/tests/test_upload.py b/tests/test_upload.py
index 3dd4815..d3b5ff3 100644
--- a/tests/test_upload.py
+++ b/tests/test_upload.py
@@ -1,9 +1,10 @@
-import json
 import base64
+import json
 
 import responses
 
 import mapbox
+import mapbox.services.uploads
 
 
 username = 'testuser'
@@ -179,7 +180,10 @@ class MockSession(object):
 
 
 @responses.activate
-def test_stage():
+def test_stage(monkeypatch):
+
+    monkeypatch.setattr(mapbox.services.uploads, 'boto3_session', MockSession)
+
     # Credentials
     query_body = """
        {{"key": "_pending/{username}/key.test",
@@ -195,8 +199,41 @@ def test_stage():
         body=query_body, status=200,
         content_type='application/json')
 
-    stage_url = mapbox.Uploader(access_token=access_token).stage(
-        'tests/moors.json',
-        session_class=MockSession)
-
+    stage_url = mapbox.Uploader(access_token=access_token).stage('tests/moors.json')
     assert stage_url.startswith("https://tilestream-tilesets-production.s3.amazonaws.com/_pending")
+
+
+@responses.activate
+def test_upload(monkeypatch):
+    """Upload a file and create a tileset"""
+
+    monkeypatch.setattr(mapbox.services.uploads, 'boto3_session', MockSession)
+
+    # Credentials
+    query_body = """
+       {{"key": "_pending/{username}/key.test",
+         "accessKeyId": "ak.test",
+         "bucket": "tilestream-tilesets-production",
+         "url": "https://tilestream-tilesets-production.s3.amazonaws.com/_pending/{username}/key.test",
+         "secretAccessKey": "sak.test",
+         "sessionToken": "st.test"}}""".format(username=username)
+
+    responses.add(
+        responses.GET,
+        'https://api.mapbox.com/uploads/v1/{0}/credentials?access_token={1}'.format(username, access_token),
+        match_querystring=True,
+        body=query_body, status=200,
+        content_type='application/json')
+
+    responses.add(
+        responses.POST,
+        'https://api.mapbox.com/uploads/v1/{0}?access_token={1}'.format(username, access_token),
+        match_querystring=True,
+        body=upload_response_body, status=201,
+        content_type='application/json')
+
+    res = mapbox.Uploader(access_token=access_token).upload(
+        'tests/moors.json', 'test1')
+    assert res.status_code == 201
+    job = res.json()
+    assert job['tileset'] == "{0}.test1".format(username)