|
3 | 3 | """ Unit tests for Token plugins"""
|
4 | 4 |
|
5 | 5 | import unittest
|
6 |
| -from unittest.mock import patch |
| 6 | +from unittest.mock import patch, mock_open, MagicMock |
7 | 7 | from jwcrypto import jwt
|
8 | 8 |
|
9 |
| -from websockify.token_plugins import JWTTokenApi |
| 9 | +from websockify.token_plugins import ReadOnlyTokenFile, JWTTokenApi |
| 10 | + |
| 11 | +class ReadOnlyTokenFileTestCase(unittest.TestCase): |
| 12 | + patch('os.path.isdir', MagicMock(return_value=False)) |
| 13 | + def test_empty(self): |
| 14 | + plugin = ReadOnlyTokenFile('configfile') |
| 15 | + |
| 16 | + config = "" |
| 17 | + pyopen = mock_open(read_data=config) |
| 18 | + |
| 19 | + with patch("websockify.token_plugins.open", pyopen): |
| 20 | + result = plugin.lookup('testhost') |
| 21 | + |
| 22 | + pyopen.assert_called_once_with('configfile') |
| 23 | + self.assertIsNone(result) |
| 24 | + |
| 25 | + patch('os.path.isdir', MagicMock(return_value=False)) |
| 26 | + def test_simple(self): |
| 27 | + plugin = ReadOnlyTokenFile('configfile') |
| 28 | + |
| 29 | + config = "testhost: remote_host:remote_port" |
| 30 | + pyopen = mock_open(read_data=config) |
| 31 | + |
| 32 | + with patch("websockify.token_plugins.open", pyopen): |
| 33 | + result = plugin.lookup('testhost') |
| 34 | + |
| 35 | + pyopen.assert_called_once_with('configfile') |
| 36 | + self.assertIsNotNone(result) |
| 37 | + self.assertEqual(result[0], "remote_host") |
| 38 | + self.assertEqual(result[1], "remote_port") |
10 | 39 |
|
11 | 40 | class JWSTokenTestCase(unittest.TestCase):
|
12 | 41 | def test_asymmetric_jws_token_plugin(self):
|
|
0 commit comments