From 90ead45abd168112599f4a258019b733622659a0 Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Fri, 29 Nov 2019 13:40:22 -0500 Subject: [PATCH] 100% test coverage --- apprise_api/.coveragerc | 2 +- apprise_api/api/tests/test_add.py | 21 ++++++++++ apprise_api/api/tests/test_del.py | 66 +++++++++++++++++++++++++++++++ apprise_api/api/tests/test_get.py | 31 +++++++++++++++ 4 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 apprise_api/api/tests/test_del.py diff --git a/apprise_api/.coveragerc b/apprise_api/.coveragerc index a2cb161..4e436a7 100644 --- a/apprise_api/.coveragerc +++ b/apprise_api/.coveragerc @@ -5,6 +5,6 @@ omit = *settings*, *tests/*, *urls.py, - *wsgi/*, + *core/wsgi.py, gunicorn.conf.py, manage.py diff --git a/apprise_api/api/tests/test_add.py b/apprise_api/api/tests/test_add.py index 76dfe51..7490df1 100644 --- a/apprise_api/api/tests/test_add.py +++ b/apprise_api/api/tests/test_add.py @@ -63,6 +63,27 @@ class AddTests(SimpleTestCase): '/add/{}'.format(key), {'urls': 'mailto://user:pass@yahoo.ca'}) assert response.status_code == 200 + # No URLs loaded + response = self.client.post( + '/add/{}'.format(key), + {'config': 'invalid content', 'format': 'text'}) + assert response.status_code == 400 + + # Test a case where we fail to load a valid configuration file + with patch('apprise.AppriseConfig.add', return_value=False): + response = self.client.post( + '/add/{}'.format(key), + {'config': 'garbage://', 'format': 'text'}) + assert response.status_code == 400 + + with patch('os.remove', side_effect=OSError): + # We will fail to remove the device first prior to placing a new + # one; This will result in a 500 error + response = self.client.post( + '/add/{}'.format(key), { + 'urls': 'mailto://user:newpass@gmail.com'}) + assert response.status_code == 500 + # URL is actually not a valid one (invalid Slack tokens specified # below) response = self.client.post( diff --git a/apprise_api/api/tests/test_del.py b/apprise_api/api/tests/test_del.py new file mode 100644 index 0000000..864b6c6 --- /dev/null +++ b/apprise_api/api/tests/test_del.py @@ -0,0 +1,66 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2019 Chris Caron +# All rights reserved. +# +# This code is licensed under the MIT License. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files(the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions : +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +from django.test import SimpleTestCase +from unittest.mock import patch + + +class DelTests(SimpleTestCase): + + def test_del_get_invalid_key_status_code(self): + """ + Test GET requests to invalid key + """ + response = self.client.get('/del/**invalid-key**') + assert response.status_code == 404 + + def test_del_post(self): + """ + Test DEL POST + """ + # our key to use + key = 'test_delete' + + # Invalid Key + response = self.client.post('/del/**invalid-key**') + assert response.status_code == 404 + + # A key that just simply isn't present + response = self.client.post('/del/{}'.format(key)) + assert response.status_code == 204 + + # Add our key + response = self.client.post( + '/add/{}'.format(key), {'urls': 'mailto://user:pass@yahoo.ca'}) + assert response.status_code == 200 + + # Test removing key when the OS just can't do it: + with patch('os.remove', side_effect=OSError): + # We can now remove the key + response = self.client.post('/del/{}'.format(key)) + assert response.status_code == 500 + + # We can now remove the key + response = self.client.post('/del/{}'.format(key)) + assert response.status_code == 200 diff --git a/apprise_api/api/tests/test_get.py b/apprise_api/api/tests/test_get.py index c5b0493..c7b431a 100644 --- a/apprise_api/api/tests/test_get.py +++ b/apprise_api/api/tests/test_get.py @@ -23,6 +23,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. from django.test import SimpleTestCase +from unittest.mock import patch class GetTests(SimpleTestCase): @@ -56,6 +57,36 @@ class GetTests(SimpleTestCase): {'urls': 'mailto://user:pass@yahoo.ca'}) assert response.status_code == 200 + # Handle case when we try to retrieve our content but we have no idea + # what the format is in. Essentialy there had to have been disk + # corruption here or someone meddling with the backend. + with patch('gzip.open', side_effect=OSError): + response = self.client.post('/get/{}'.format(key)) + assert response.status_code == 500 + # Now we should be able to see our content response = self.client.post('/get/{}'.format(key)) assert response.status_code == 200 + + # Add a YAML file + response = self.client.post( + '/add/{}'.format(key), { + 'format': 'yaml', + 'config': """ + urls: + - dbus://"""}) + assert response.status_code == 200 + + # Verify that the correct Content-Type is set in the header of the + # response + assert 'Content-Type' in response + assert response['Content-Type'].startswith('text/plain') + + # Now retrieve our YAML configuration + response = self.client.post('/get/{}'.format(key)) + assert response.status_code == 200 + + # Verify that the correct Content-Type is set in the header of the + # response + assert 'Content-Type' in response + assert response['Content-Type'].startswith('text/yaml')