diff options
author | 2018-07-28 11:47:39 +0200 | |
---|---|---|
committer | 2018-07-28 11:47:39 +0200 | |
commit | c3b7a4e604a74405f822855f20705869d12ee9f8 (patch) | |
tree | 7f7bf8fff3bfa779420f39418b6e8f6842787f6b | |
parent | Fix unit test (diff) |
Add `DELETE` route to `off-topic-names` endpoint.
-rw-r--r-- | pysite/views/api/bot/off_topic_names.py | 47 | ||||
-rw-r--r-- | tests/test_api_bot_off_topic_names.py | 32 |
2 files changed, 68 insertions, 11 deletions
diff --git a/pysite/views/api/bot/off_topic_names.py b/pysite/views/api/bot/off_topic_names.py index f353ab02..1c75428e 100644 --- a/pysite/views/api/bot/off_topic_names.py +++ b/pysite/views/api/bot/off_topic_names.py @@ -9,18 +9,24 @@ from pysite.decorators import api_key, api_params from pysite.mixins import DBMixin -POST_SCHEMA = Schema({ - 'name': And( - str, - len, - lambda name: all(c.isalnum() or c == '-' for c in name), - str.islower, - lambda name: len(name) <= 96, - error=( - "The channel name must be a non-blank string consisting only of" - " lowercase regular characters and '-' with a maximum length of 96" - ) +OFF_TOPIC_NAME = And( + str, + len, + lambda name: all(c.isalnum() or c == '-' for c in name), + str.islower, + lambda name: len(name) <= 96, + error=( + "The channel name must be a non-blank string consisting only of" + " lowercase regular characters and '-' with a maximum length of 96" ) +) + +DELETE_SCHEMA = Schema({ + 'name': OFF_TOPIC_NAME +}) + +POST_SCHEMA = Schema({ + 'name': OFF_TOPIC_NAME }) @@ -30,6 +36,25 @@ class OffTopicNamesView(APIView, DBMixin): table_name = "off_topic_names" @api_key + @api_params(schema=DELETE_SCHEMA, validation_type=ValidationTypes.params) + def delete(self, params): + """ + Removes a single off-topic name from the database. + Returns the result of the deletion call. + + API key must be provided as header. + Name to delete must be provided as the `name` query argument. + """ + + result = self.db.delete( + self.table_name, + params['name'], + return_changes=True + ) + + return jsonify(result) + + @api_key def get(self): """ Fetch all known off-topic channel names from the database. diff --git a/tests/test_api_bot_off_topic_names.py b/tests/test_api_bot_off_topic_names.py index f0d0fe3e..6227ddef 100644 --- a/tests/test_api_bot_off_topic_names.py +++ b/tests/test_api_bot_off_topic_names.py @@ -100,3 +100,35 @@ class RandomSampleEndpointTests(SiteTest): ) self.assert200(response) self.assertEqual(len(response.json), 1) + + +class DeletingANameEndpointTests(SiteTest): + """Tests deleting a name from the database using DELETE.""" + + CHANNEL_NAME = 'duck-goes-meow' + + def setUp(self): + response = self.client.post( + f'/bot/off-topic-names?name={self.CHANNEL_NAME}', + app.config['API_SUBDOMAIN'], + headers=app.config['TEST_HEADER'] + ) + self.assert200(response) + + def test_deleting_random_name_returns_deleted_0(self): + response = self.client.delete( + '/bot/off-topic-names?name=my-totally-random-name', + app.config['API_SUBDOMAIN'], + headers=app.config['TEST_HEADER'] + ) + self.assert200(response) + self.assertEqual(response.json['deleted'], 0) + + def test_deleting_channel_name_returns_deleted_1(self): + response = self.client.delete( + f'/bot/off-topic-names?name={self.CHANNEL_NAME}', + app.config['API_SUBDOMAIN'], + headers=app.config['TEST_HEADER'] + ) + self.assert200(response) + self.assertEqual(response.json['deleted'], 1) |