Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests and minor improvements to ciphers module #235

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion modules/ciphers/atbash.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import string
import sys

import click

Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions modules/ciphers/caesar.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import collections
import itertools
import string
import sys

import click

Expand All @@ -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 += " "
Expand All @@ -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 += " "
Expand Down
5 changes: 3 additions & 2 deletions modules/ciphers/rot13.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import collections
import itertools
import string
import sys

import click

Expand Down Expand Up @@ -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 += " "
Expand All @@ -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 += " "
Expand Down
7 changes: 4 additions & 3 deletions modules/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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]]()

Expand All @@ -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()
Expand Down
Empty file added tests/ciphers/__init__.py
Empty file.
45 changes: 45 additions & 0 deletions tests/ciphers/test_atbash.py
Original file line number Diff line number Diff line change
@@ -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 <text>
"""

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()
55 changes: 55 additions & 0 deletions tests/ciphers/test_ceaser.py
Original file line number Diff line number Diff line change
@@ -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 <text> <shift>
"""

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()
44 changes: 44 additions & 0 deletions tests/ciphers/test_input.py
Original file line number Diff line number Diff line change
@@ -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 <text> <shift>
"""

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()
45 changes: 45 additions & 0 deletions tests/ciphers/test_rot13.py
Original file line number Diff line number Diff line change
@@ -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 <text>
"""

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()