|
1 | | -import json |
2 | 1 | import os |
3 | 2 | import unittest |
4 | | -import jc.parsers.ipconfig |
5 | | -import jc.parsers.net_localgroup |
6 | | -import jc.parsers.net_user |
| 3 | +import json |
| 4 | +from typing import Dict |
| 5 | +from jc.parsers.net_user import parse |
7 | 6 |
|
8 | 7 | THIS_DIR = os.path.dirname(os.path.abspath(__file__)) |
9 | 8 |
|
10 | 9 |
|
11 | 10 | class MyTests(unittest.TestCase): |
12 | | - test_files = [ |
13 | | - "tests/fixtures/windows/windows-xp/net_user", |
14 | | - "tests/fixtures/windows/windows-xp/net_user.administrator", |
15 | | - "tests/fixtures/windows/windows-7/net_user", |
16 | | - "tests/fixtures/windows/windows-7/net_user.administrator", |
17 | | - "tests/fixtures/windows/windows-2008/net_user", |
18 | | - "tests/fixtures/windows/windows-2008/net_user.administrator", |
19 | | - "tests/fixtures/windows/windows-2016/net_user.administrators", |
20 | | - "tests/fixtures/windows/windows-2016/net_user", |
21 | | - "tests/fixtures/windows/windows-10/net_user", |
22 | | - "tests/fixtures/windows/windows-10/net_user.administrator", |
23 | | - "tests/fixtures/windows/windows-11/net_user", |
24 | | - "tests/fixtures/windows/windows-11/net_user.administrator" |
25 | | - ] |
26 | | - |
27 | | - def setUp(self): |
28 | | - for tf in MyTests.test_files: |
29 | | - in_file = os.path.join(THIS_DIR, os.pardir, f"{tf}.out") |
30 | | - out_file = os.path.join(THIS_DIR, os.pardir, f"{tf}.json") |
31 | | - |
32 | | - with open(in_file, "r", encoding="utf-8") as f: |
33 | | - setattr(self, self.varName(tf), f.read()) |
34 | | - with open(out_file, "r", encoding="utf-8") as f: |
35 | | - setattr(self, self.varName(tf) + "_json", json.loads(f.read())) |
36 | | - |
37 | | - def varName(self, path): |
38 | | - return ( |
39 | | - path.replace("tests/fixtures/windows", "") |
40 | | - .replace("-", "_") |
41 | | - .replace("/", "_") |
| 11 | + f_in: Dict = {} |
| 12 | + f_json: Dict = {} |
| 13 | + |
| 14 | + @classmethod |
| 15 | + def setUpClass(cls): |
| 16 | + fixtures = { |
| 17 | + 'windows_xp_net_user': ( |
| 18 | + 'fixtures/windows/windows-xp/net_user.out', |
| 19 | + 'fixtures/windows/windows-xp/net_user.json'), |
| 20 | + 'windows_7_net_user': ( |
| 21 | + 'fixtures/windows/windows-7/net_user.out', |
| 22 | + 'fixtures/windows/windows-7/net_user.json'), |
| 23 | + 'windows_2008_net_user': ( |
| 24 | + 'fixtures/windows/windows-2008/net_user.out', |
| 25 | + 'fixtures/windows/windows-2008/net_user.json'), |
| 26 | + 'windows_2016_net_user': ( |
| 27 | + 'fixtures/windows/windows-2016/net_user.out', |
| 28 | + 'fixtures/windows/windows-2016/net_user.json'), |
| 29 | + 'windows_10_net_user': ( |
| 30 | + 'fixtures/windows/windows-10/net_user.out', |
| 31 | + 'fixtures/windows/windows-10/net_user.json'), |
| 32 | + 'windows_11_net_user': ( |
| 33 | + 'fixtures/windows/windows-11/net_user.out', |
| 34 | + 'fixtures/windows/windows-11/net_user.json'), |
| 35 | + } |
| 36 | + |
| 37 | + for file, filepaths in fixtures.items(): |
| 38 | + with open(os.path.join(THIS_DIR, filepaths[0]), 'r', encoding='utf-8') as a, \ |
| 39 | + open(os.path.join(THIS_DIR, filepaths[1]), 'r', encoding='utf-8') as b: |
| 40 | + cls.f_in[file] = a.read() |
| 41 | + cls.f_json[file] = json.loads(b.read()) |
| 42 | + |
| 43 | + |
| 44 | + def test_net_user_nodata(self): |
| 45 | + """ |
| 46 | + Test 'net_user' with no data |
| 47 | + """ |
| 48 | + self.assertEqual(parse('', quiet=True), {}) |
| 49 | + |
| 50 | + |
| 51 | + def test_net_user_windows_xp(self): |
| 52 | + """ |
| 53 | + Test 'net_user' on Windows XP |
| 54 | + """ |
| 55 | + self.assertEqual( |
| 56 | + parse(self.f_in['windows_xp_net_user'], quiet=True), |
| 57 | + self.f_json['windows_xp_net_user'] |
42 | 58 | ) |
43 | 59 |
|
44 | | - def test_windows_net_localgroup(self): |
| 60 | + |
| 61 | + def test_net_user_windows_7(self): |
45 | 62 | """ |
46 | | - Test a sample Windows "net localgroup" command output |
| 63 | + Test 'net_user' on Windows 7 |
47 | 64 | """ |
48 | | - for tf in MyTests.test_files: |
49 | | - in_var = getattr(self, self.varName(tf)) |
50 | | - out_var = getattr(self, self.varName(tf) + "_json") |
| 65 | + self.assertEqual( |
| 66 | + parse(self.f_in['windows_7_net_user'], quiet=True), |
| 67 | + self.f_json['windows_7_net_user'] |
| 68 | + ) |
| 69 | + |
51 | 70 |
|
52 | | - self.assertEqual(jc.parsers.net_user.parse(in_var, quiet=True), out_var) |
| 71 | + def test_net_user_windows_2008(self): |
| 72 | + """ |
| 73 | + Test 'net_user' on Windows 2008 |
| 74 | + """ |
| 75 | + self.assertEqual( |
| 76 | + parse(self.f_in['windows_2008_net_user'], quiet=True), |
| 77 | + self.f_json['windows_2008_net_user'] |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | + def test_net_user_windows_2016(self): |
| 82 | + """ |
| 83 | + Test 'net_user' on Windows 2016 |
| 84 | + """ |
| 85 | + self.assertEqual( |
| 86 | + parse(self.f_in['windows_2016_net_user'], quiet=True), |
| 87 | + self.f_json['windows_2016_net_user'] |
| 88 | + ) |
| 89 | + |
| 90 | + |
| 91 | + def test_net_user_windows_10(self): |
| 92 | + """ |
| 93 | + Test 'net_user' on Windows 10 |
| 94 | + """ |
| 95 | + self.assertEqual( |
| 96 | + parse(self.f_in['windows_10_net_user'], quiet=True), |
| 97 | + self.f_json['windows_10_net_user'] |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | + def test_net_user_windows_11(self): |
| 102 | + """ |
| 103 | + Test 'net_user' on Windows 11 |
| 104 | + """ |
| 105 | + self.assertEqual( |
| 106 | + parse(self.f_in['windows_11_net_user'], quiet=True), |
| 107 | + self.f_json['windows_11_net_user'] |
| 108 | + ) |
53 | 109 |
|
54 | 110 |
|
55 | | -if __name__ == "__main__": |
| 111 | +if __name__ == '__main__': |
56 | 112 | unittest.main() |
0 commit comments