|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch, MagicMock |
1 | 3 | from click.testing import CliRunner |
2 | | -from simple_notifications import cli |
| 4 | +from simple_notifications.cli import pushover, pushbullet, email |
| 5 | +import os |
3 | 6 |
|
| 7 | +class TestNotifications(unittest.TestCase): |
4 | 8 |
|
5 | | -def test_help(): |
6 | | - runner = CliRunner() |
7 | | - result = runner.invoke(cli.notification) |
8 | | - assert result.exit_code == 0 |
9 | | - assert "Usage: " in result.output |
| 9 | + def setUp(self): |
| 10 | + self.runner = CliRunner() |
| 11 | + os.environ['LC_ALL'] = 'en_US.UTF-8' |
| 12 | + os.environ['LANG'] = 'en_US.UTF-8' |
| 13 | + |
| 14 | + |
| 15 | + def test_help(): |
| 16 | + runner = CliRunner() |
| 17 | + result = runner.invoke(cli.notification) |
| 18 | + assert result.exit_code == 0 |
| 19 | + assert "Usage: " in result.output |
| 20 | + |
| 21 | + |
| 22 | + @patch('simple_notifications.cli.simple_notifications_config.PUSHOVER_APP_TOKEN', 'fake_token') |
| 23 | + @patch('simple_notifications.cli.simple_notifications_config.USER_KEY', 'fake_user_key') |
| 24 | + @patch('requests.post') |
| 25 | + def test_pushover(self, mock_post): |
| 26 | + mock_post.return_value = MagicMock(status_code=200) |
| 27 | + result = self.runner.invoke(pushover, ['--subject', 'Test Subject', '--body', 'Test Body', '--image', 'no']) |
| 28 | + print("Output:", result.output) # Debugging output |
| 29 | + print("Exception:", result.exception) # Print exception if any |
| 30 | + print("Exit Code:", result.exit_code) # Print exit code |
| 31 | + self.assertEqual(result.exit_code, 0) |
| 32 | + self.assertIn('Sending out Pushover notification...', result.output) |
| 33 | + self.assertIn('Sending complete.', result.output) |
| 34 | + |
| 35 | + |
| 36 | + @patch('simple_notifications.cli.simple_notifications_config.PUSHBULLET_APP_TOKEN', 'fake_token') |
| 37 | + @patch('requests.post') |
| 38 | + def test_pushbullet(self, mock_post): |
| 39 | + mock_post.return_value = MagicMock(status_code=200) |
| 40 | + result = self.runner.invoke(pushbullet, ['--subject', 'Test Subject', '--body', 'Test Body']) |
| 41 | + print("Output:", result.output) # Debugging output |
| 42 | + print("Exception:", result.exception) # Print exception if any |
| 43 | + print("Exit Code:", result.exit_code) # Print exit code |
| 44 | + self.assertEqual(result.exit_code, 0) |
| 45 | + self.assertIn('Sending out Pushbullet notification...', result.output) |
| 46 | + self.assertIn('Sending complete.', result.output) |
| 47 | + |
| 48 | + |
| 49 | + @patch('simple_notifications.cli.simple_notifications_config.EMAIL_SENDER', 'test@example.com') |
| 50 | + @patch('simple_notifications.cli.simple_notifications_config.EMAIL_SERVER', 'smtp.example.com') |
| 51 | + @patch('simple_notifications.cli.simple_notifications_config.EMAIL_SERVER_PORT', '587') |
| 52 | + @patch('simple_notifications.cli.simple_notifications_config.EMAIL_PASSWORD', 'fake_password') |
| 53 | + @patch('smtplib.SMTP') |
| 54 | + def test_email(self, mock_smtp): |
| 55 | + mock_smtp_instance = mock_smtp.return_value |
| 56 | + result = self.runner.invoke(email, ['--subject', 'Test Subject', '--body', 'Test Body', '--recipients', 'test@example.com']) |
| 57 | + print("Output:", result.output) # Debugging output |
| 58 | + print("Exception:", result.exception) # Print exception if any |
| 59 | + print("Exit Code:", result.exit_code) # Print exit code |
| 60 | + self.assertEqual(result.exit_code, 0) |
| 61 | + self.assertIn('Sending out Email notification...', result.output) |
| 62 | + self.assertIn('Sending complete.', result.output) |
| 63 | + mock_smtp_instance.sendmail.assert_called_once() |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == '__main__': |
| 67 | + unittest.main() |
0 commit comments