Skip to content

Commit

Permalink
Merge pull request #10 from mapbox/test-env-fixture
Browse files Browse the repository at this point in the history
Get tokens from the environment when commands are invoked
  • Loading branch information
flippmoke authored Aug 1, 2019
2 parents 4339538 + bacf19b commit aa3ea54
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 92 deletions.
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import pytest


@pytest.fixture(scope="function")
def token_environ(monkeypatch):
monkeypatch.setenv("MAPBOX_ACCESS_TOKEN", "fake-token")
monkeypatch.setenv("MapboxAccessToken", "test-token")
17 changes: 9 additions & 8 deletions tests/test_cli_create.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from click.testing import CliRunner
from unittest import mock
import json

# have to set environment variables before importing library
# since they are used in __init__
mock_env=mock.patch.dict('os.environ', {'MAPBOX_ACCESS_TOKEN': 'fake-token', 'MapboxAccessToken': 'test-token'})
mock_env.start()
from click.testing import CliRunner
import pytest

from tilesets.cli import create


Expand All @@ -16,6 +14,7 @@ def MockResponse(self):
return self


@pytest.mark.usefixtures("token_environ")
def test_cli_create_missing_recipe():
runner = CliRunner()
# missing --recipe option
Expand All @@ -24,6 +23,7 @@ def test_cli_create_missing_recipe():
assert 'Missing option "--recipe"' in result.output


@pytest.mark.usefixtures("token_environ")
def test_cli_create_missing_name():
runner = CliRunner()
# missing --name option
Expand All @@ -32,6 +32,7 @@ def test_cli_create_missing_name():
assert 'Missing option "--name"' in result.output


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.post')
def test_cli_create_success(mock_request_post):
runner = CliRunner()
Expand All @@ -43,6 +44,7 @@ def test_cli_create_success(mock_request_post):
assert '{\n "message": "mock message"\n}\n' in result.output


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.post')
def test_cli_create_success_description(mock_request_post):
runner = CliRunner()
Expand All @@ -62,6 +64,7 @@ def test_cli_create_success_description(mock_request_post):
assert '{\n "message": "mock message with description"\n}\n' in result.output


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.post')
def test_cli_create_private_invalid(mock_request_post):
runner = CliRunner()
Expand All @@ -80,6 +83,7 @@ def test_cli_create_private_invalid(mock_request_post):
assert 'Invalid value for "--privacy" / "-p": invalid choice: invalid-privacy-value. (choose from public, private)' in result.output


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.post')
def test_cli_use_token_flag(mock_request_post):
runner = CliRunner()
Expand All @@ -89,6 +93,3 @@ def test_cli_use_token_flag(mock_request_post):
assert result.exit_code == 0
mock_request_post.assert_called_with('https://api.mapbox.com/tilesets/v1/test.id?access_token=flag-token', json={'name': 'test name', 'description': '', 'recipe': {'minzoom': 0, 'maxzoom': 10, 'layer_name': 'test_layer'}})
assert '{\n "message": "mock message"\n}\n' in result.output


mock_env.stop()
24 changes: 14 additions & 10 deletions tests/test_cli_jobs.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from click.testing import CliRunner
from unittest import mock

# have to set environment variables before importing library
# since they are used in __init__
mock_env=mock.patch.dict('os.environ', {'MAPBOX_ACCESS_TOKEN': 'fake-token', 'MapboxAccessToken': 'test-token'})
mock_env.start()
from click.testing import CliRunner
import pytest

from tilesets.cli import jobs, job


Expand All @@ -15,13 +13,16 @@ def __init__(self, mock_text):
def MockResponse(self):
return self


class MockResponseError():
def __init__(self, mock_text):
self.text = mock_text
self.status_code = 404
def MockResponse(self):
return self


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.get')
def test_cli_job(mock_request_get):
runner = CliRunner()
Expand All @@ -33,6 +34,8 @@ def test_cli_job(mock_request_get):
assert result.exit_code == 0
assert '{\n "message": "mock message"\n}\n' in result.output


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.get')
def test_cli_job_error(mock_request_get):
runner = CliRunner()
Expand All @@ -44,9 +47,11 @@ def test_cli_job_error(mock_request_get):
assert result.exit_code == 0
assert '{\n "message": "mock error message"\n}\n' in result.output

# test jobs + stage endpoint

@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.get')
def test_cli_jobs_and_stage(mock_request_get):
"""test jobs + stage endpoint"""
runner = CliRunner()

# sends expected request
Expand All @@ -56,9 +61,11 @@ def test_cli_jobs_and_stage(mock_request_get):
assert result.exit_code == 0
assert '{\n "message": "mock message"\n}\n' in result.output

# test job endpoint

@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.get')
def test_cli_single_job(mock_request_get):
"""test job endpoint"""
runner = CliRunner()

# sends expected request
Expand All @@ -67,6 +74,3 @@ def test_cli_single_job(mock_request_get):
mock_request_get.assert_called_with('https://api.mapbox.com/tilesets/v1/test.id/jobs/job_id?access_token=fake-token')
assert result.exit_code == 0
assert '{\n "message": "mock message"\n}\n' in result.output


mock_env.stop()
11 changes: 4 additions & 7 deletions tests/test_cli_publish.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from click.testing import CliRunner
from unittest import mock

# have to set environment variables before importing library
# since they are used in __init__
mock_env=mock.patch.dict('os.environ', {'MAPBOX_ACCESS_TOKEN': 'fake-token', 'MapboxAccessToken': 'test-token'})
mock_env.start()
import pytest

from tilesets.cli import publish


Expand All @@ -16,6 +14,7 @@ def MockResponse(self):
return self


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.post')
def test_cli_publish(mock_request_post):
runner = CliRunner()
Expand All @@ -28,6 +27,7 @@ def test_cli_publish(mock_request_post):
assert 'You can view the status of your tileset with the `tilesets status test.id` command.' in result.output


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.post')
def test_cli_publish_use_token_flag(mock_request_post):
runner = CliRunner()
Expand All @@ -37,6 +37,3 @@ def test_cli_publish_use_token_flag(mock_request_post):
mock_request_post.assert_called_with('https://api.mapbox.com/tilesets/v1/test.id/publish?access_token=flag-token')
assert result.exit_code == 0
assert 'You can view the status of your tileset with the `tilesets status test.id` command.' in result.output


mock_env.stop()
12 changes: 8 additions & 4 deletions tests/test_cli_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
from unittest import mock
import json

# have to set environment variables before importing library
# since they are used in __init__
mock_env=mock.patch.dict('os.environ', {'MAPBOX_ACCESS_TOKEN': 'fake-token', 'MapboxAccessToken': 'test-token'})
mock_env.start()
import pytest

from tilesets.cli import (
add_source,
view_source,
Expand All @@ -24,6 +22,8 @@ def MockResponse(self):
def json(self):
return json.loads(self.text)


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.post')
def test_cli_add_source(mock_request_post):
mock_request_post.return_value = MockResponse('{"id":"mapbox://tileset-source/test-user/hello-world"}', 200)
Expand All @@ -35,6 +35,7 @@ def test_cli_add_source(mock_request_post):
assert '{\n "id": "mapbox://tileset-source/test-user/hello-world"\n}\n' in result.output


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.get')
def test_cli_view_source(mock_request_get):
mock_request_get.return_value = MockResponse('{"id":"mapbox://tileset-source/test-user/hello-world"}', 200)
Expand All @@ -44,6 +45,7 @@ def test_cli_view_source(mock_request_get):
assert result.output == '{\n "id": "mapbox://tileset-source/test-user/hello-world"\n}\n'


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.delete')
def test_cli_delete_source(mock_request_delete):
mock_request_delete.return_value = MockResponse('', 201)
Expand All @@ -53,6 +55,7 @@ def test_cli_delete_source(mock_request_delete):
assert result.output == 'Source deleted.\n'


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.get')
def test_cli_view_source(mock_request_get):
mock_request_get.return_value = MockResponse(
Expand All @@ -67,6 +70,7 @@ def test_cli_view_source(mock_request_get):
assert "mapbox://tileset-source/test-user/hola-mundo" in result.output


@pytest.mark.usefixtures("token_environ")
def test_cli_validate_source():
runner = CliRunner()
result = runner.invoke(validate_source, ['tests/fixtures/valid.ldgeojson'])
Expand Down
11 changes: 4 additions & 7 deletions tests/test_cli_status.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
from click.testing import CliRunner
from unittest import mock

# have to set environment variables before importing library
# since they are used in __init__
mock_env=mock.patch.dict('os.environ', {'MAPBOX_ACCESS_TOKEN': 'fake-token', 'MapboxAccessToken': 'test-token'})
mock_env.start()
import pytest

from tilesets.cli import status


Expand All @@ -16,6 +14,7 @@ def MockResponse(self):
return self


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.get')
def test_cli_status(mock_request_get):
runner = CliRunner()
Expand All @@ -28,6 +27,7 @@ def test_cli_status(mock_request_get):
assert '{\n "message": "mock message"\n}\n' in result.output


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.get')
def test_cli_status_use_token_flag(mock_request_get):
runner = CliRunner()
Expand All @@ -37,6 +37,3 @@ def test_cli_status_use_token_flag(mock_request_get):
mock_request_get.assert_called_with('https://api.mapbox.com/tilesets/v1/test.id/status?access_token=flag-token')
assert result.exit_code == 0
assert '{\n "message": "mock message"\n}\n' in result.output


mock_env.stop()
11 changes: 4 additions & 7 deletions tests/test_cli_update_recipe.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from click.testing import CliRunner
from unittest import mock
import pytest

# have to set environment variables before importing library
# since they are used in __init__
mock_env=mock.patch.dict('os.environ', {'MAPBOX_ACCESS_TOKEN': 'fake-token', 'MapboxAccessToken': 'test-token'})
mock_env.start()
from tilesets.cli import update_recipe


Expand All @@ -15,13 +12,15 @@ def MockResponse(self):
return self


@pytest.mark.usefixtures("token_environ")
def test_cli_update_recipe_no_recipe():
runner = CliRunner()
result = runner.invoke(update_recipe, ['test.id', 'does/not/exist/recipe.json'])
assert result.exit_code == 2
assert 'Path "does/not/exist/recipe.json" does not exist' in result.output


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.patch')
def test_cli_update_recipe(mock_request_patch):
runner = CliRunner()
Expand All @@ -37,6 +36,7 @@ def test_cli_update_recipe(mock_request_patch):
assert 'Updated recipe.' in result.output


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.patch')
def test_cli_update_recipe(mock_request_patch):
runner = CliRunner()
Expand All @@ -49,6 +49,3 @@ def test_cli_update_recipe(mock_request_patch):
)
assert result.exit_code == 0
assert 'Updated recipe.' in result.output


mock_env.stop()
11 changes: 4 additions & 7 deletions tests/test_cli_validate_recipe.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from click.testing import CliRunner
from unittest import mock
import pytest

# have to set environment variables before importing library
# since they are used in __init__
mock_env=mock.patch.dict('os.environ', {'MAPBOX_ACCESS_TOKEN': 'fake-token', 'MapboxAccessToken': 'test-token'})
mock_env.start()
from tilesets.cli import validate_recipe


Expand All @@ -16,13 +13,15 @@ def MockResponse(self):
return self


@pytest.mark.usefixtures("token_environ")
def test_cli_validate_recipe_no_recipe():
runner = CliRunner()
result = runner.invoke(validate_recipe, ['does/not/exist/recipe.json'])
assert result.exit_code == 2
assert 'Path "does/not/exist/recipe.json" does not exist' in result.output


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.put')
def test_cli_validate_recipe(mock_request_put):
runner = CliRunner()
Expand All @@ -38,6 +37,7 @@ def test_cli_validate_recipe(mock_request_put):
assert '{\n "message": "mock message"\n}\n' in result.output


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.put')
def test_cli_validate_recipe_use_token_flag(mock_request_put):
runner = CliRunner()
Expand All @@ -50,6 +50,3 @@ def test_cli_validate_recipe_use_token_flag(mock_request_put):
)
assert result.exit_code == 0
assert '{\n "message": "mock message"\n}\n' in result.output


mock_env.stop()
10 changes: 3 additions & 7 deletions tests/test_cli_view_recipe.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from click.testing import CliRunner
from unittest import mock
import pytest

# have to set environment variables before importing library
# since they are used in __init__
mock_env=mock.patch.dict('os.environ', {'MAPBOX_ACCESS_TOKEN': 'fake-token', 'MapboxAccessToken': 'test-token'})
mock_env.start()
from tilesets.cli import view_recipe


Expand All @@ -16,6 +13,7 @@ def MockResponse(self):
return self


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.get')
def test_cli_view_recipe(mock_request_get):
runner = CliRunner()
Expand All @@ -30,6 +28,7 @@ def test_cli_view_recipe(mock_request_get):
assert '{\n "fake": "recipe_data"\n}\n' in result.output


@pytest.mark.usefixtures("token_environ")
@mock.patch('requests.get')
def test_cli_view_recipe_use_token_flag(mock_request_get):
runner = CliRunner()
Expand All @@ -41,6 +40,3 @@ def test_cli_view_recipe_use_token_flag(mock_request_get):
)
assert result.exit_code == 0
assert '{\n "fake": "recipe_data"\n}\n' in result.output


mock_env.stop()
Loading

0 comments on commit aa3ea54

Please sign in to comment.