Skip to content

Commit 100a776

Browse files
committed
Add unit tests for ReadOnlyTokenFile
1 parent 984dcc6 commit 100a776

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

tests/test_token_plugins.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,39 @@
33
""" Unit tests for Token plugins"""
44

55
import unittest
6-
from unittest.mock import patch
6+
from unittest.mock import patch, mock_open, MagicMock
77
from jwcrypto import jwt
88

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")
1039

1140
class JWSTokenTestCase(unittest.TestCase):
1241
def test_asymmetric_jws_token_plugin(self):

0 commit comments

Comments
 (0)