diff --git a/modules/ciphers/atbash.py b/modules/ciphers/atbash.py index 56b56fa2..c84afd28 100644 --- a/modules/ciphers/atbash.py +++ b/modules/ciphers/atbash.py @@ -1,4 +1,5 @@ import string +import sys import click @@ -28,7 +29,7 @@ def encrypt(self, message): for char in message: if char not in self.alphabet and char != " ": click.echo("Atbash only supports ASCII characters.") - return + sys.exit(1) encrypted_message += self.key[char] return encrypted_message diff --git a/modules/ciphers/caesar.py b/modules/ciphers/caesar.py index aeb1dee9..948da800 100644 --- a/modules/ciphers/caesar.py +++ b/modules/ciphers/caesar.py @@ -1,6 +1,7 @@ import collections import itertools import string +import sys import click @@ -20,7 +21,7 @@ def encrypt(self, message): for char in message: if char not in string.ascii_uppercase and char != " ": click.echo("The Caesar Cipher only supports ASCII characters") - return + sys.exit(1) if char == " ": encrypted_text += " " @@ -41,7 +42,7 @@ def decrypt(self, message): for char in message: if char not in string.ascii_uppercase and char != " ": click.echo("The Caesar Cipher only supports ASCII characters") - return + sys.exit(1) if char == " ": decrypted_text += " " diff --git a/modules/ciphers/rot13.py b/modules/ciphers/rot13.py index c19bc3d6..7d22188c 100644 --- a/modules/ciphers/rot13.py +++ b/modules/ciphers/rot13.py @@ -1,6 +1,7 @@ import collections import itertools import string +import sys import click @@ -31,7 +32,7 @@ def encrypt(self, message): for char in message: if char not in string.ascii_uppercase and char != " ": click.echo("The ROT13 Cipher only supports ASCII characters") - return + sys.exit(1) if char == " ": encrypted_text += " " @@ -49,7 +50,7 @@ def decrypt(self, message): for char in message: if char not in string.ascii_uppercase and char != " ": click.echo("The ROT13 Cipher only supports ASCII characters") - return + sys.exit(1) if char == " ": decrypted_text += " " diff --git a/modules/dev.py b/modules/dev.py index 7b16240c..df106010 100644 --- a/modules/dev.py +++ b/modules/dev.py @@ -730,7 +730,7 @@ def ciphers(ctx, mode): mode = get_arguments(ctx, 1) if mode is None: click.echo("No mode was passed.(choose encrypt or decrypt") - return + sys.exit(1) _mode = str(mode).lower() @@ -747,7 +747,7 @@ def ciphers(ctx, mode): cipher_choice = int(click.prompt("Choose a cipher")) if cipher_choice > len(cipher_dict) - 1 or cipher_choice < 0: click.echo("Invalid cipher number was chosen.") - return + sys.exit(1) cipher = cipher_dict[list(cipher_dict.keys())[cipher_choice]]() @@ -758,7 +758,8 @@ def ciphers(ctx, mode): cipher_text = click.prompt("The text you want to decrypt") return click.echo(cipher.decrypt(cipher_text)) else: - return click.echo("Invalid mode passed.") + click.echo("Invalid mode passed.") + sys.exit(1) @dev.command() diff --git a/tests/ciphers/__init__.py b/tests/ciphers/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/ciphers/test_atbash.py b/tests/ciphers/test_atbash.py new file mode 100644 index 00000000..69dfcf18 --- /dev/null +++ b/tests/ciphers/test_atbash.py @@ -0,0 +1,45 @@ +# coding=utf-8 +import unittest +from click.testing import CliRunner + +import yoda + + +class TestAtbash(unittest.TestCase): + """ + Test for the following commands: + + | Module: ciphers + | command: ciphers + | args: encrypt + | input: 0 + """ + + def __init__(self, methodName="runTest"): + super(TestAtbash, self).__init__() + self.runner = CliRunner() + + def runTest(self): + # testing for invalid text input for Atbash encryption + result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], + input="0\n$#$#") + self.assertNotEqual(result.exit_code, 0) + + # testing for invalid text input for Atbash decryption + result = self.runner.invoke(yoda.cli, ["ciphers", "decrypt"], + input="0\n12345") + self.assertNotEqual(result.exit_code, 0) + + # testing for working Atbash encryption + result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], + input="0\nsample") + self.assertEqual(result.exit_code, 0) + + # testing for working Atbash decryption + result = self.runner.invoke(yoda.cli, ["ciphers", "decrypt"], + input="0\nsample") + self.assertEqual(result.exit_code, 0) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/ciphers/test_ceaser.py b/tests/ciphers/test_ceaser.py new file mode 100644 index 00000000..71dacb66 --- /dev/null +++ b/tests/ciphers/test_ceaser.py @@ -0,0 +1,55 @@ +# coding=utf-8 +import unittest +from click.testing import CliRunner + +import yoda + + +class TestCeaser(unittest.TestCase): + """ + Test for the following commands: + + | Module: ciphers + | command: ciphers + | args: encrypt + | input: 1 + """ + + def __init__(self, methodName="runTest"): + super(TestCeaser, self).__init__() + self.runner = CliRunner() + + def runTest(self): + # testing for invalid shift input for ceaser encryption + result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], + input="1\nsample\na") + self.assertNotEqual(result.exit_code, 0) + + # testing for invalid shift input for ceaser decryption + result = self.runner.invoke(yoda.cli, ["ciphers", "decrypt"], + input="1\nsample\na") + self.assertNotEqual(result.exit_code, 0) + + # testing for invalid text input for ceaser encryption + result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], + input="1\n$#$#\n3") + self.assertNotEqual(result.exit_code, 0) + + # testing for invalid text input for ceaser decryption + result = self.runner.invoke(yoda.cli, ["ciphers", "decrypt"], + input="1\n12345\n3") + self.assertNotEqual(result.exit_code, 0) + + # testing for working ceaser encryption + result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], + input="1\nsample\n3") + self.assertEqual(result.exit_code, 0) + + # testing for working ceaser decryption + result = self.runner.invoke(yoda.cli, ["ciphers", "decrypt"], + input="1\nsample\n3") + self.assertEqual(result.exit_code, 0) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/ciphers/test_input.py b/tests/ciphers/test_input.py new file mode 100644 index 00000000..26b18a75 --- /dev/null +++ b/tests/ciphers/test_input.py @@ -0,0 +1,44 @@ +# coding=utf-8 +import unittest +from click.testing import CliRunner + +import yoda + + +class TestInput(unittest.TestCase): + """ + Test for the following commands: + + | Module: ciphers + | command: ciphers + | args: encrypt + | input: 0 + """ + + def __init__(self, methodName="runTest"): + super(TestInput, self).__init__() + self.runner = CliRunner() + + def runTest(self): + # testing for no mode passed for ciphers + result = self.runner.invoke(yoda.cli, ["ciphers"]) + self.assertNotEqual(result.exit_code, 0) + + # testing for invalid cipher input selection in encrypt mode + result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], + input="3\nsample\na") + self.assertNotEqual(result.exit_code, 0) + + # testing for invalid cipher input selection in decrypt mode + result = self.runner.invoke(yoda.cli, ["ciphers", "decrypt"], + input="3\nsample\na") + self.assertNotEqual(result.exit_code, 0) + + # testing for invalid cipher mode + result = self.runner.invoke(yoda.cli, ["ciphers", "abc"], + input="3\nsample\na") + self.assertNotEqual(result.exit_code, 0) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/ciphers/test_rot13.py b/tests/ciphers/test_rot13.py new file mode 100644 index 00000000..b5461db9 --- /dev/null +++ b/tests/ciphers/test_rot13.py @@ -0,0 +1,45 @@ +# coding=utf-8 +import unittest +from click.testing import CliRunner + +import yoda + + +class TestRot13(unittest.TestCase): + """ + Test for the following commands: + + | Module: ciphers + | command: ciphers + | args: encrypt + | input: 2 + """ + + def __init__(self, methodName="runTest"): + super(TestRot13, self).__init__() + self.runner = CliRunner() + + def runTest(self): + # testing for invalid text input for rot13 encryption + result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], + input="2\nabc1!") + self.assertNotEqual(result.exit_code, 0) + + # testing for invalid text input for rot13 decryption + result = self.runner.invoke(yoda.cli, ["ciphers", "decrypt"], + input="2\n12345") + self.assertNotEqual(result.exit_code, 0) + + # testing for working rot13 encryption + result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], + input="2\nsample") + self.assertEqual(result.exit_code, 0) + + # testing for working rot13 decryption + result = self.runner.invoke(yoda.cli, ["ciphers", "decrypt"], + input="2\nsample") + self.assertEqual(result.exit_code, 0) + + +if __name__ == "__main__": + unittest.main()