From 45f75f26d75faffe6e96e766db9f509e0e359285 Mon Sep 17 00:00:00 2001 From: sarathsoma Date: Fri, 23 Nov 2018 15:36:17 +0530 Subject: [PATCH 1/4] Improvements to ciphers in the way yoda exits during invalid inputs --- modules/ciphers/atbash.py | 3 ++- modules/ciphers/caesar.py | 5 +++-- modules/ciphers/rot13.py | 5 +++-- modules/dev.py | 7 ++++--- 4 files changed, 12 insertions(+), 8 deletions(-) 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() From ae81c8d23ededbbb74e043972763782f990cac67 Mon Sep 17 00:00:00 2001 From: sarathsoma Date: Fri, 23 Nov 2018 15:36:46 +0530 Subject: [PATCH 2/4] Tests for ciphers --- tests/ciphers/__init__.py | 0 tests/ciphers/test_atbash.py | 45 +++++++++++++++++++++++++++++ tests/ciphers/test_ceaser.py | 55 ++++++++++++++++++++++++++++++++++++ tests/ciphers/test_input.py | 44 +++++++++++++++++++++++++++++ tests/ciphers/test_rot13.py | 45 +++++++++++++++++++++++++++++ 5 files changed, 189 insertions(+) create mode 100644 tests/ciphers/__init__.py create mode 100644 tests/ciphers/test_atbash.py create mode 100644 tests/ciphers/test_ceaser.py create mode 100644 tests/ciphers/test_input.py create mode 100644 tests/ciphers/test_rot13.py 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..bccf197a --- /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: 2 + """ + + 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="2\n$#$#") + self.assertNotEquals(result.exit_code, 0) + + # testing for invalid text input for Atbash decryption + result = self.runner.invoke(yoda.cli, ["ciphers", "decrypt"], + input="2\n12345") + self.assertNotEquals(result.exit_code, 0) + + # testing for working Atbash encryption + result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], + input="2\nsample") + self.assertEqual(result.exit_code, 0) + + # testing for working Atbash decryption + result = self.runner.invoke(yoda.cli, ["ciphers", "decrypt"], + input="2\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..7b7669c9 --- /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: 0 + """ + + 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="0\nsample\na") + self.assertNotEquals(result.exit_code, 0) + + # testing for invalid shift input for ceaser decryption + result = self.runner.invoke(yoda.cli, ["ciphers", "decrypt"], + input="0\nsample\na") + self.assertNotEquals(result.exit_code, 0) + + # testing for invalid text input for ceaser encryption + result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], + input="0\n$#$#\n3") + self.assertNotEquals(result.exit_code, 0) + + # testing for invalid text input for ceaser decryption + result = self.runner.invoke(yoda.cli, ["ciphers", "decrypt"], + input="0\n12345\n3") + self.assertNotEquals(result.exit_code, 0) + + # testing for working ceaser encryption + result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], + input="0\nsample\n3") + self.assertEqual(result.exit_code, 0) + + # testing for working ceaser decryption + result = self.runner.invoke(yoda.cli, ["ciphers", "decrypt"], + input="0\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..fdae6817 --- /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.assertNotEquals(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.assertNotEquals(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.assertNotEquals(result.exit_code, 0) + + # testing for invalid cipher mode + result = self.runner.invoke(yoda.cli, ["ciphers", "abc"], + input="3\nsample\na") + self.assertNotEquals(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..62314c84 --- /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: 1 + """ + + 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="1\n$#$#") + self.assertNotEquals(result.exit_code, 0) + + # testing for invalid text input for rot13 decryption + result = self.runner.invoke(yoda.cli, ["ciphers", "decrypt"], + input="1\n12345") + self.assertNotEquals(result.exit_code, 0) + + # testing for working rot13 encryption + result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], + input="1\nsample") + self.assertEqual(result.exit_code, 0) + + # testing for working rot13 decryption + result = self.runner.invoke(yoda.cli, ["ciphers", "decrypt"], + input="1\nsample") + self.assertEqual(result.exit_code, 0) + + +if __name__ == "__main__": + unittest.main() From a3a8371a2490bb3e828b8c6d63bf5ab72a71d717 Mon Sep 17 00:00:00 2001 From: sarathsoma Date: Tue, 27 Nov 2018 12:30:10 +0530 Subject: [PATCH 3/4] Replacing deprecated usages --- tests/ciphers/test_atbash.py | 4 ++-- tests/ciphers/test_ceaser.py | 8 ++++---- tests/ciphers/test_input.py | 8 ++++---- tests/ciphers/test_rot13.py | 6 +++--- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/ciphers/test_atbash.py b/tests/ciphers/test_atbash.py index bccf197a..1dec26aa 100644 --- a/tests/ciphers/test_atbash.py +++ b/tests/ciphers/test_atbash.py @@ -23,12 +23,12 @@ def runTest(self): # testing for invalid text input for Atbash encryption result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], input="2\n$#$#") - self.assertNotEquals(result.exit_code, 0) + self.assertNotEqual(result.exit_code, 0) # testing for invalid text input for Atbash decryption result = self.runner.invoke(yoda.cli, ["ciphers", "decrypt"], input="2\n12345") - self.assertNotEquals(result.exit_code, 0) + self.assertNotEqual(result.exit_code, 0) # testing for working Atbash encryption result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], diff --git a/tests/ciphers/test_ceaser.py b/tests/ciphers/test_ceaser.py index 7b7669c9..f353f82f 100644 --- a/tests/ciphers/test_ceaser.py +++ b/tests/ciphers/test_ceaser.py @@ -23,22 +23,22 @@ def runTest(self): # testing for invalid shift input for ceaser encryption result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], input="0\nsample\na") - self.assertNotEquals(result.exit_code, 0) + self.assertNotEqual(result.exit_code, 0) # testing for invalid shift input for ceaser decryption result = self.runner.invoke(yoda.cli, ["ciphers", "decrypt"], input="0\nsample\na") - self.assertNotEquals(result.exit_code, 0) + self.assertNotEqual(result.exit_code, 0) # testing for invalid text input for ceaser encryption result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], input="0\n$#$#\n3") - self.assertNotEquals(result.exit_code, 0) + self.assertNotEqual(result.exit_code, 0) # testing for invalid text input for ceaser decryption result = self.runner.invoke(yoda.cli, ["ciphers", "decrypt"], input="0\n12345\n3") - self.assertNotEquals(result.exit_code, 0) + self.assertNotEqual(result.exit_code, 0) # testing for working ceaser encryption result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], diff --git a/tests/ciphers/test_input.py b/tests/ciphers/test_input.py index fdae6817..26b18a75 100644 --- a/tests/ciphers/test_input.py +++ b/tests/ciphers/test_input.py @@ -22,22 +22,22 @@ def __init__(self, methodName="runTest"): def runTest(self): # testing for no mode passed for ciphers result = self.runner.invoke(yoda.cli, ["ciphers"]) - self.assertNotEquals(result.exit_code, 0) + 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.assertNotEquals(result.exit_code, 0) + 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.assertNotEquals(result.exit_code, 0) + self.assertNotEqual(result.exit_code, 0) # testing for invalid cipher mode result = self.runner.invoke(yoda.cli, ["ciphers", "abc"], input="3\nsample\na") - self.assertNotEquals(result.exit_code, 0) + self.assertNotEqual(result.exit_code, 0) if __name__ == "__main__": diff --git a/tests/ciphers/test_rot13.py b/tests/ciphers/test_rot13.py index 62314c84..fbf749b6 100644 --- a/tests/ciphers/test_rot13.py +++ b/tests/ciphers/test_rot13.py @@ -22,13 +22,13 @@ def __init__(self, methodName="runTest"): def runTest(self): # testing for invalid text input for rot13 encryption result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], - input="1\n$#$#") - self.assertNotEquals(result.exit_code, 0) + input="1\nabc1!") + self.assertNotEqual(result.exit_code, 0) # testing for invalid text input for rot13 decryption result = self.runner.invoke(yoda.cli, ["ciphers", "decrypt"], input="1\n12345") - self.assertNotEquals(result.exit_code, 0) + self.assertNotEqual(result.exit_code, 0) # testing for working rot13 encryption result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], From 8d17f4f47ccf904a508c5452caf29955b496ab40 Mon Sep 17 00:00:00 2001 From: sarathsoma Date: Tue, 27 Nov 2018 12:42:03 +0530 Subject: [PATCH 4/4] Fixes input errors in test scripts --- tests/ciphers/test_atbash.py | 10 +++++----- tests/ciphers/test_ceaser.py | 14 +++++++------- tests/ciphers/test_rot13.py | 10 +++++----- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/ciphers/test_atbash.py b/tests/ciphers/test_atbash.py index 1dec26aa..69dfcf18 100644 --- a/tests/ciphers/test_atbash.py +++ b/tests/ciphers/test_atbash.py @@ -12,7 +12,7 @@ class TestAtbash(unittest.TestCase): | Module: ciphers | command: ciphers | args: encrypt - | input: 2 + | input: 0 """ def __init__(self, methodName="runTest"): @@ -22,22 +22,22 @@ def __init__(self, methodName="runTest"): def runTest(self): # testing for invalid text input for Atbash encryption result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], - input="2\n$#$#") + 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="2\n12345") + input="0\n12345") self.assertNotEqual(result.exit_code, 0) # testing for working Atbash encryption result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], - input="2\nsample") + input="0\nsample") self.assertEqual(result.exit_code, 0) # testing for working Atbash decryption result = self.runner.invoke(yoda.cli, ["ciphers", "decrypt"], - input="2\nsample") + input="0\nsample") self.assertEqual(result.exit_code, 0) diff --git a/tests/ciphers/test_ceaser.py b/tests/ciphers/test_ceaser.py index f353f82f..71dacb66 100644 --- a/tests/ciphers/test_ceaser.py +++ b/tests/ciphers/test_ceaser.py @@ -12,7 +12,7 @@ class TestCeaser(unittest.TestCase): | Module: ciphers | command: ciphers | args: encrypt - | input: 0 + | input: 1 """ def __init__(self, methodName="runTest"): @@ -22,32 +22,32 @@ def __init__(self, methodName="runTest"): def runTest(self): # testing for invalid shift input for ceaser encryption result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], - input="0\nsample\na") + 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="0\nsample\na") + 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="0\n$#$#\n3") + 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="0\n12345\n3") + input="1\n12345\n3") self.assertNotEqual(result.exit_code, 0) # testing for working ceaser encryption result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], - input="0\nsample\n3") + input="1\nsample\n3") self.assertEqual(result.exit_code, 0) # testing for working ceaser decryption result = self.runner.invoke(yoda.cli, ["ciphers", "decrypt"], - input="0\nsample\n3") + input="1\nsample\n3") self.assertEqual(result.exit_code, 0) diff --git a/tests/ciphers/test_rot13.py b/tests/ciphers/test_rot13.py index fbf749b6..b5461db9 100644 --- a/tests/ciphers/test_rot13.py +++ b/tests/ciphers/test_rot13.py @@ -12,7 +12,7 @@ class TestRot13(unittest.TestCase): | Module: ciphers | command: ciphers | args: encrypt - | input: 1 + | input: 2 """ def __init__(self, methodName="runTest"): @@ -22,22 +22,22 @@ def __init__(self, methodName="runTest"): def runTest(self): # testing for invalid text input for rot13 encryption result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], - input="1\nabc1!") + 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="1\n12345") + input="2\n12345") self.assertNotEqual(result.exit_code, 0) # testing for working rot13 encryption result = self.runner.invoke(yoda.cli, ["ciphers", "encrypt"], - input="1\nsample") + input="2\nsample") self.assertEqual(result.exit_code, 0) # testing for working rot13 decryption result = self.runner.invoke(yoda.cli, ["ciphers", "decrypt"], - input="1\nsample") + input="2\nsample") self.assertEqual(result.exit_code, 0)