From 67d1b3719e9b309a6210af37a798813cc6102bfa Mon Sep 17 00:00:00 2001 From: Katie Smith Date: Mon, 28 Feb 2022 18:18:02 +0000 Subject: [PATCH] Stop blank strings being inserted as inbound numbers We had an inbound number in the database with a value of ''. This could happen if there are blank lines in the inbound numbers file we use for the `insert-inbound-numbers` command. To avoid this happening again, the command now calls `.strip()` on each line of the file and only inserts a row if the result is truthy (i.e. not ''). --- app/commands.py | 15 ++++++++------- tests/app/test_commands.py | 17 ++++++++++++++++- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/app/commands.py b/app/commands.py index e44f96d04..3f27237a9 100644 --- a/app/commands.py +++ b/app/commands.py @@ -246,14 +246,15 @@ def fix_notification_statuses_not_in_sync(): one number per line. The number must have the format of 07... not 447....""") def insert_inbound_numbers_from_file(file_name): print("Inserting inbound numbers from {}".format(file_name)) - file = open(file_name) - sql = "insert into inbound_numbers values('{}', '{}', 'mmg', null, True, now(), null);" + with open(file_name) as file: + sql = "insert into inbound_numbers values('{}', '{}', 'mmg', null, True, now(), null);" - for line in file: - print(line) - db.session.execute(sql.format(uuid.uuid4(), line.strip())) - db.session.commit() - file.close() + for line in file: + line = line.strip() + if line: + print(line) + db.session.execute(sql.format(uuid.uuid4(), line)) + db.session.commit() @notify_command(name='replay-create-pdf-for-templated-letter') diff --git a/tests/app/test_commands.py b/tests/app/test_commands.py index dd8f1c15b..4d3f991c8 100644 --- a/tests/app/test_commands.py +++ b/tests/app/test_commands.py @@ -1,8 +1,23 @@ -from app.commands import local_dev_broadcast_permissions +from app.commands import ( + insert_inbound_numbers_from_file, + local_dev_broadcast_permissions, +) +from app.dao.inbound_numbers_dao import dao_get_available_inbound_numbers from app.dao.services_dao import dao_add_user_to_service from tests.app.db import create_user +def test_insert_inbound_numbers_from_file(notify_db_session, notify_api, tmpdir): + numbers_file = tmpdir.join("numbers.txt") + numbers_file.write("07700900373\n07700900473\n07700900375\n\n\n\n") + + notify_api.test_cli_runner().invoke(insert_inbound_numbers_from_file, ['-f', numbers_file]) + + inbound_numbers = dao_get_available_inbound_numbers() + assert len(inbound_numbers) == 3 + assert set(x.number for x in inbound_numbers) == {'07700900373', '07700900473', '07700900375'} + + def test_local_dev_broadcast_permissions( sample_service, sample_broadcast_service,