Skip to content

Commit

Permalink
K-86: "workspace set" command doesn't validate index values properly (K…
Browse files Browse the repository at this point in the history
…I-labs#101)

* modify allowed range for validate_index()

* Version updated to: 1.1.3
  • Loading branch information
ImScientist authored Jan 20, 2020
1 parent 12a90ba commit 951c1cd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.2
1.1.3
17 changes: 16 additions & 1 deletion cli/kaos_cli/utils/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
from unittest import mock
from socket import error

from kaos_cli.utils.validators import validate_inputs, validate_unused_port
from kaos_cli.utils.validators import validate_inputs, validate_unused_port, validate_index
from kaos_cli.exceptions.exceptions import MissingArgumentError


def test_validate_input():
with pytest.raises(MissingArgumentError):
validate_inputs([None, None], ["a, b"])


@mock.patch('socket.socket')
def test_validate_unused_port_returns_true_when_port_is_unused(socket_mock):
# Arrange
Expand All @@ -26,6 +27,7 @@ def test_validate_unused_port_returns_true_when_port_is_unused(socket_mock):
assert is_available is True
mocked_socket_obj.bind.assert_called_once_with((host_arg, port_arg))


@mock.patch('socket.socket')
def test_validate_unused_port_returns_false_when_port_is_already_in_use(socket_mock):
# Arrange
Expand All @@ -41,3 +43,16 @@ def test_validate_unused_port_returns_false_when_port_is_already_in_use(socket_m
# Assert
assert is_available is False
mocked_socket_obj.bind.assert_called_once_with((host_arg, port_arg))


def test_validate_index_does_not_exist():

bad_examples = [(3, 4, 'template'), (3, 3, 'serve'), (3, -1, 'train')]
good_examples = [(3, 0, 'template'), (3, 1, 'serve'), (3, 2, 'train')]

for n, ind, command in bad_examples:
with pytest.raises(IndexError, match=f"Index {ind} does not exist... Run `kaos {command} list` again"):
validate_index(n=n, ind=ind, command=command)

for n, ind, command in good_examples:
validate_index(n=n, ind=ind, command=command)
2 changes: 1 addition & 1 deletion cli/kaos_cli/utils/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def validate_index(n: int, ind: int, command: str):
"""

# ensure index exists in indices
if -n <= ind < n:
if 0 <= ind < n:
return ind
else:
raise IndexError(f"Index {ind} does not exist... Run `kaos {command} list` again")
Expand Down

0 comments on commit 951c1cd

Please sign in to comment.