From d756e1f56f235ec1941474a36a565ffa4cfe047d Mon Sep 17 00:00:00 2001 From: Maria Mafalda Fernando <30693435+mariaFernando@users.noreply.github.com> Date: Thu, 20 Jun 2019 13:31:33 +0100 Subject: [PATCH] fix: added input control for special characters + test (#970) * added input control for special characters + test * refactored test * syntax fix --- .../static/portal/js/join_create_game_toggle.js | 17 +++++++++++------ portal/tests/test_preview_users.py | 10 +++++++++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/portal/static/portal/js/join_create_game_toggle.js b/portal/static/portal/js/join_create_game_toggle.js index 35df8f42b..8befcb830 100644 --- a/portal/static/portal/js/join_create_game_toggle.js +++ b/portal/static/portal/js/join_create_game_toggle.js @@ -36,7 +36,7 @@ program; modified versions of the program must be marked as such and not identified as the original program. */ -$(document).ready(function() { +$(document).ready(function () { var game_name_input = $('#id_name'); var create_game_form = $('#create-game-form'); var join_game_section = $('#join_game'); @@ -45,24 +45,28 @@ $(document).ready(function() { var create_new_game_button = $('#create_new_game_button'); var back_button = $('#back_button'); - if(game_name_input.val()) { + if (game_name_input.val()) { showCreateGameForm(); game_name_input.val(""); showInputError("Sorry, a game with this name already exists..."); } - create_new_game_button.click(function() { + create_new_game_button.click(function () { showCreateGameForm(); }); - back_button.click(function() { + back_button.click(function () { showJoinGameForm(); }); - create_game_button.click(function() { - if(!game_name_input.val() || game_name_input.val() === "") { + create_game_button.click(function () { + if (!game_name_input.val() || game_name_input.val() === "") { showInputError("Give your new game a name..."); } + var exp = /^[\w-]+$/; + if (!exp.test(game_name_input.val())) { + showInputError("Name cannot contain special characters."); + } else { create_game_form.submit(); } @@ -84,6 +88,7 @@ $(document).ready(function() { } function showInputError(error_message) { + game_name_input.val(""); game_name_input.attr("placeholder", error_message); game_name_input.addClass('input-invalid'); } diff --git a/portal/tests/test_preview_users.py b/portal/tests/test_preview_users.py index af9005525..c62d63ee7 100644 --- a/portal/tests/test_preview_users.py +++ b/portal/tests/test_preview_users.py @@ -152,7 +152,7 @@ def test_preview_user_can_create_game(self): self.assertIn("/aimmo/play/1/", self.selenium.driver.current_url) - def test_preview_user_cannot_create_empty_game(self): + def test_preview_user_cannot_create_invalid_game_name(self): email, password = signup_teacher_directly_as_preview_user() create_organisation_directly(email, True) _, _, access_code = create_class_directly(email) @@ -170,6 +170,14 @@ def test_preview_user_cannot_create_empty_game(self): page.get_input_game_name_placeholder(), "Give your new game a name..." ) + page.input_new_game_name("it's an invalid name") + page.click_create_game_button() + + self.assertEqual( + page.get_input_game_name_placeholder(), + "Name cannot contain special characters.", + ) + def test_preview_user_cannot_create_duplicate_game(self): email, password = signup_teacher_directly_as_preview_user() create_organisation_directly(email, True)