-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor tests * Fix for previous versions pre-3.8
- Loading branch information
Showing
3 changed files
with
112 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from tests.end_to_end.end_to_end_test import EndToEndTest | ||
|
||
|
||
class EncryptionTest(EndToEndTest): | ||
def testEncrypt(self): | ||
self.copy_examples() | ||
|
||
keys = self.encrypt_all("hw1.py", "tests/q1.py", "tests/q2.py") | ||
|
||
for path in "hw1.py", "tests/q1.py", "tests/q2.py": | ||
self.assertEncrypted(path, keys) | ||
|
||
_, stderr = self.run_ok('--decrypt', keys["hw1.py"], keys[self.pi_path("tests/q1.py")]) | ||
self.assertEqual("", stderr) | ||
|
||
for path in "hw1.py", self.pi_path("tests/q1.py"): | ||
self.assertSameAsDemo(path) | ||
|
||
self.assertEncrypted("tests/q2.py", keys) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import os | ||
import shlex | ||
import shutil | ||
import subprocess | ||
import sys | ||
import tempfile | ||
import unittest | ||
import json | ||
|
||
from client.cli import publish | ||
from client.utils import encryption | ||
|
||
SCRIPT = """ | ||
. {envloc}/{folder}/activate; | ||
echo -n | python ok {args} | ||
""" | ||
|
||
|
||
class EndToEndTest(unittest.TestCase): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
cls.clean_env_dir = tempfile.TemporaryDirectory() | ||
cls.create_clean_env() | ||
|
||
@classmethod | ||
def create_clean_env(cls): | ||
subprocess.check_call(["virtualenv", "-q", "-p", "python", cls.clean_env_dir.name]) | ||
|
||
def setUp(self): | ||
self.maxDiff = None # the errors are pretty useless if you don't do this | ||
self.directory = tempfile.TemporaryDirectory() | ||
publish.package_client(self.directory.name) | ||
|
||
def add_file(self, name, contents): | ||
with open(os.path.join(self.directory.name, name), "w") as f: | ||
f.write(contents) | ||
|
||
def make_directory(self, name): | ||
os.makedirs(os.path.join(self.directory.name, name)) | ||
|
||
def run_ok(self, *args): | ||
command_line = SCRIPT.format( | ||
envloc=shlex.quote(self.clean_env_dir.name), | ||
folder="Scripts" if sys.platform == "win32" else "bin", | ||
args=" ".join(shlex.quote(arg) for arg in args), | ||
) | ||
with subprocess.Popen( | ||
os.getenv('SHELL', 'sh'), | ||
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, | ||
cwd=self.directory.name, universal_newlines=True) as proc: | ||
stdout, stderr = proc.communicate(command_line) | ||
return stdout, stderr | ||
|
||
def copy_examples(self, path='demo/ok_test'): | ||
for file in os.listdir(path): | ||
src = os.path.join(path, file) | ||
dst = os.path.join(self.directory.name, file) | ||
if os.path.isfile(src): | ||
shutil.copy(src, dst) | ||
else: | ||
shutil.copytree(src, dst) | ||
|
||
def rel_path(self, path): | ||
return os.path.join(self.directory.name, *path.split("/")) | ||
|
||
def pi_path(self, path): | ||
return os.path.join(*path.split("/")) | ||
|
||
def assertEncrypted(self, path, keys): | ||
path = self.pi_path(path) | ||
with open(self.rel_path(path)) as f: | ||
encryption.decrypt(f.read(), keys[path]) | ||
|
||
def assertSameAsDemo(self, path): | ||
with open(self.rel_path(path)) as f: | ||
actual = f.read() | ||
with open(os.path.join("demo/ok_test", path)) as f: | ||
expected = f.read() | ||
self.assertEqual(actual, expected) | ||
|
||
def encrypt_all(self, *paths): | ||
keyfile = self.rel_path("keyfile") | ||
_, stderr = self.run_ok("--generate-encryption-key", keyfile) | ||
self.assertEqual('', stderr) | ||
with open(keyfile) as f: | ||
keys = dict(json.load(f)) | ||
self.assertEqual(set(keys), {self.pi_path(path) for path in paths}) | ||
_, stderr = self.run_ok('--encrypt', keyfile) | ||
self.assertEqual("", stderr) | ||
return keys |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters