Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Fixes lint issues in test files
Browse files Browse the repository at this point in the history
  • Loading branch information
kushaldas committed Jan 3, 2020
1 parent e2e9bf3 commit 8cb672b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 37 deletions.
29 changes: 15 additions & 14 deletions tests/test_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io
import json
import os
import sys
import tempfile
import unittest.mock

Expand Down Expand Up @@ -33,7 +32,9 @@ def test_missing_config(self):
output = None
with unittest.mock.patch(
"sys.argv", new_callable=lambda: ["sd-proxy", config_path]
) as mock_argv, unittest.mock.patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
) as mock_argv, unittest.mock.patch( # noqa: F841
"sys.stdout", new_callable=io.StringIO
) as mock_stdout:
with self.assertRaises(SystemExit), sdhome():
entrypoint.start()
output = mock_stdout.getvalue()
Expand All @@ -52,7 +53,9 @@ def test_unwritable_log_folder(self):
output = None
with sdhome() as home:
os.chmod(home, 0o0444)
with unittest.mock.patch("sys.stdout", new_callable=io.StringIO) as mock_stdout:
with unittest.mock.patch(
"sys.stdout", new_callable=io.StringIO
) as mock_stdout:
with self.assertRaises(SystemExit):
entrypoint.start()
output = mock_stdout.getvalue()
Expand All @@ -64,10 +67,10 @@ def test_unwritable_log_folder(self):
self.assertIn("Permission denied: ", body["error"])

def test_wrong_number_of_arguments(self):
with sdhome() as home:
with sdhome() as home: # noqa: F841
with unittest.mock.patch(
"sys.argv", new_callable=lambda: ["sd-proxy"]
) as mock_argv, unittest.mock.patch(
) as mock_argv, unittest.mock.patch( # noqa: F841
"sys.stdout", new_callable=io.StringIO
) as mock_stdout:
with self.assertRaises(SystemExit):
Expand All @@ -85,23 +88,21 @@ def test_empty_input(self):
config_path = "tests/files/valid-config.yaml"
self.assertTrue(os.path.exists(config_path))

with sdhome() as home:
with sdhome() as home: # noqa: F841
with unittest.mock.patch(
"sys.stdin", new_callable=lambda: io.StringIO("")
) as mock_stdin, unittest.mock.patch(
) as mock_stdin, unittest.mock.patch( # noqa: F841
"sys.stdout", new_callable=io.StringIO
) as mock_stdout, unittest.mock.patch(
"sys.argv", new_callable=lambda: ["sd-proxy", config_path]
) as mock_argv:
) as mock_argv: # noqa: F841
entrypoint.start()
output = mock_stdout.getvalue()

response = json.loads(output)
self.assertEqual(response["status"], http.HTTPStatus.BAD_REQUEST)
body = json.loads(response["body"])
self.assertEqual(
body["error"], "Invalid JSON in request"
)
self.assertEqual(body["error"], "Invalid JSON in request")

@vcr.use_cassette("fixtures/main_json_response.yaml")
def test_json_response(self):
Expand All @@ -114,13 +115,13 @@ def test_json_response(self):
}

output = None
with sdhome() as home, unittest.mock.patch(
with sdhome() as home, unittest.mock.patch( # noqa: F841
"sys.stdin", new_callable=lambda: io.StringIO(json.dumps(test_input))
) as mock_stding, unittest.mock.patch(
) as mock_stding, unittest.mock.patch( # noqa: F841
"sys.stdout", new_callable=io.StringIO
) as mock_stdout, unittest.mock.patch(
"sys.argv", new_callable=lambda: ["sd-proxy", config_path]
) as mock_argv:
) as mock_argv: # noqa: F841
entrypoint.start()
output = mock_stdout.getvalue()

Expand Down
50 changes: 27 additions & 23 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
class TestMain(unittest.TestCase):
def setUp(self):
self.conf = config.Conf()
self.conf.host = 'jsonplaceholder.typicode.com'
self.conf.scheme = 'https'
self.conf.host = "jsonplaceholder.typicode.com"
self.conf.scheme = "https"
self.conf.port = 443
self.conf.dev = True

@vcr.use_cassette('fixtures/main_json_response.yaml')
@vcr.use_cassette("fixtures/main_json_response.yaml")
def test_json_response(self):
test_input_json = """{ "method": "GET",
"path_query": "/posts?userId=1" }"""

req = proxy.Req()
req.method = 'GET'
req.path_query = ''
req.headers = {'Accept': 'application/json'}
req.method = "GET"
req.path_query = ""
req.headers = {"Accept": "application/json"}

# Use custom callbacks
def on_save(res, fh, conf):
Expand All @@ -51,10 +51,10 @@ def on_done(res):
sys.stdout = saved_stdout

response = json.loads(output)
for item in json.loads(response['body']):
self.assertEqual(item['userId'], 1)
for item in json.loads(response["body"]):
self.assertEqual(item["userId"], 1)

@vcr.use_cassette('fixtures/main_non_json_response.yaml')
@vcr.use_cassette("fixtures/main_non_json_response.yaml")
def test_non_json_response(self):
test_input_json = """{ "method": "GET",
"path_query": "" }"""
Expand All @@ -64,9 +64,9 @@ def on_save(fh, res, conf):

subprocess.run(["cp", fh.name, "/tmp/{}".format(self.fn)])

res.headers['X-Origin-Content-Type'] = res.headers['Content-Type']
res.headers['Content-Type'] = 'application/json'
res.body = json.dumps({'filename': self.fn})
res.headers["X-Origin-Content-Type"] = res.headers["Content-Type"]
res.headers["Content-Type"] = "application/json"
res.body = json.dumps({"filename": self.fn})

self.p = proxy.Proxy(self.conf, proxy.Req(), on_save)

Expand All @@ -80,10 +80,10 @@ def on_save(fh, res, conf):
sys.stdout = saved_stdout

response = json.loads(output)
self.assertEqual(response['status'], 200)
self.assertEqual(response["status"], 200)

# The proxy should have created a filename in the response body
self.assertIn('filename', response['body'])
self.assertIn("filename", response["body"])

# The file should not be empty
with open("/tmp/{}".format(self.fn)) as f:
Expand All @@ -100,7 +100,7 @@ def on_save(fh, res, conf):

def on_done(res):
res = res.__dict__
self.assertEqual(res['status'], 400)
self.assertEqual(res["status"], 400)
sys.exit(1)

p = proxy.Proxy(self.conf, proxy.Req(), on_save, on_done)
Expand All @@ -116,20 +116,20 @@ def on_save(fh, res, conf):

def on_done(res):
res = res.__dict__
self.assertEqual(res['status'], 400)
self.assertEqual(res['body'], '{"error": "Missing keys in request"}')
self.assertEqual(res["status"], 400)
self.assertEqual(res["body"], '{"error": "Missing keys in request"}')
sys.exit(1)

p = proxy.Proxy(self.conf, proxy.Req(), on_save, on_done)
with self.assertRaises(SystemExit):
main.__main__(test_input_json, p)

@vcr.use_cassette('fixtures/main_input_headers.yaml')
@vcr.use_cassette("fixtures/main_input_headers.yaml")
def test_input_headers(self):
test_input = {
"method": "GET",
"path_query": "/posts?userId=1",
"headers": { "X-Test-Header": "th" }
"headers": {"X-Test-Header": "th"},
}

def on_save(fh, res, conf):
Expand All @@ -139,12 +139,12 @@ def on_save(fh, res, conf):
main.__main__(json.dumps(test_input), p)
self.assertEqual(p.req.headers, test_input["headers"])

@vcr.use_cassette('fixtures/main_input_body.yaml')
@vcr.use_cassette("fixtures/main_input_body.yaml")
def test_input_body(self):
test_input = {
"method": "POST",
"path_query": "/posts",
"body": { "id": 42, "title": "test" }
"body": {"id": 42, "title": "test"},
}

def on_save(fh, res, conf):
Expand All @@ -154,15 +154,19 @@ def on_save(fh, res, conf):
main.__main__(json.dumps(test_input), p)
self.assertEqual(p.req.body, test_input["body"])

@vcr.use_cassette('fixtures/main_non_json_response.yaml')
@vcr.use_cassette("fixtures/main_non_json_response.yaml")
def test_default_callbacks(self):
test_input = {
"method": "GET",
"path_query": "",
}

p = proxy.Proxy(self.conf, proxy.Req())
with unittest.mock.patch("securedrop_proxy.callbacks.on_done") as on_done, unittest.mock.patch("securedrop_proxy.callbacks.on_save") as on_save:
with unittest.mock.patch(
"securedrop_proxy.callbacks.on_done"
) as on_done, unittest.mock.patch(
"securedrop_proxy.callbacks.on_save"
) as on_save:
main.__main__(json.dumps(test_input), p)
self.assertEqual(on_save.call_count, 1)
self.assertEqual(on_done.call_count, 1)

0 comments on commit 8cb672b

Please sign in to comment.