|
1 |
| -from django.conf import settings |
2 |
| -from django.test import TestCase |
3 |
| - |
4 | 1 | from main.templatetags.pgp import pgp_key_link, format_key, pgp_fingerprint
|
5 | 2 |
|
6 | 3 |
|
7 |
| -class PGPTemplateTest(TestCase): |
| 4 | +def test_format_key(): |
| 5 | + # 40 len case |
| 6 | + pgp_key = '423423fD9004FB063E2C81117BFB1108D234DAFZ' |
| 7 | + pgp_key_len = len(pgp_key) |
| 8 | + |
| 9 | + output = format_key(pgp_key) |
| 10 | + spaces = output.count(' ') + output.count('\xa0') # nbsp |
| 11 | + assert pgp_key_len + spaces == len(output) |
8 | 12 |
|
| 13 | + # 21 - 39 len case |
| 14 | + pgp_key = '3E2C81117BFB1108D234DAFZ' |
| 15 | + pgp_key_len = len(pgp_key) + len('0x') |
| 16 | + assert pgp_key_len == len(format_key(pgp_key)) |
9 | 17 |
|
10 |
| - def test_format_key(self): |
11 |
| - # 40 len case |
12 |
| - pgp_key = '423423fD9004FB063E2C81117BFB1108D234DAFZ' |
13 |
| - pgp_key_len = len(pgp_key) |
| 18 | + # 8, 20 len case |
| 19 | + pgp_key = '3E2C81117BFB1108DEFF' |
| 20 | + pgp_key_len = len(pgp_key) + len('0x') |
| 21 | + assert pgp_key_len == len(format_key(pgp_key)) |
14 | 22 |
|
15 |
| - output = format_key(pgp_key) |
16 |
| - spaces = output.count(' ') + output.count('\xa0') # nbsp |
17 |
| - self.assertEqual(pgp_key_len + spaces, len(output)) |
| 23 | + # 0 - 7 len case |
| 24 | + pgp_key = 'B1108D' |
| 25 | + pgp_key_len = len(pgp_key) + len('0x') |
| 26 | + assert pgp_key_len == len(format_key(pgp_key)) |
18 | 27 |
|
19 |
| - # 21 - 39 len case |
20 |
| - pgp_key = '3E2C81117BFB1108D234DAFZ' |
21 |
| - pgp_key_len = len(pgp_key) + len('0x') |
22 |
| - self.assertEqual(pgp_key_len, len(format_key(pgp_key))) |
23 | 28 |
|
24 |
| - # 8, 20 len case |
25 |
| - pgp_key = '3E2C81117BFB1108DEFF' |
26 |
| - pgp_key_len = len(pgp_key) + len('0x') |
27 |
| - self.assertEqual(pgp_key_len, len(format_key(pgp_key))) |
| 29 | +def assert_pgp_key_link(pgp_key): |
| 30 | + output = pgp_key_link(int(pgp_key, 16)) |
| 31 | + assert pgp_key[2:] in output |
| 32 | + assert "https" in output |
28 | 33 |
|
29 |
| - # 0 - 7 len case |
30 |
| - pgp_key = 'B1108D' |
31 |
| - pgp_key_len = len(pgp_key) + len('0x') |
32 |
| - self.assertEqual(pgp_key_len, len(format_key(pgp_key))) |
33 | 34 |
|
34 |
| - def assert_pgp_key_link(self, pgp_key): |
35 |
| - output = pgp_key_link(int(pgp_key, 16)) |
36 |
| - self.assertIn(pgp_key[2:], output) |
37 |
| - self.assertIn("https", output) |
| 35 | +def test_pgp_key_link(settings): |
| 36 | + assert pgp_key_link("") == "Unknown" |
38 | 37 |
|
39 |
| - def test_pgp_key_link(self): |
40 |
| - self.assertEqual(pgp_key_link(""), "Unknown") |
| 38 | + pgp_key = '423423fD9004FB063E2C81117BFB1108D234DAFZ' |
| 39 | + output = pgp_key_link(pgp_key) |
| 40 | + assert pgp_key in output |
| 41 | + assert "https" in output |
41 | 42 |
|
42 |
| - pgp_key = '423423fD9004FB063E2C81117BFB1108D234DAFZ' |
43 |
| - output = pgp_key_link(pgp_key) |
44 |
| - self.assertIn(pgp_key, output) |
45 |
| - self.assertIn("https", output) |
| 43 | + output = pgp_key_link(pgp_key, "test") |
| 44 | + assert "test" in output |
| 45 | + assert "https" in output |
46 | 46 |
|
47 |
| - output = pgp_key_link(pgp_key, "test") |
48 |
| - self.assertIn("test", output) |
49 |
| - self.assertIn("https", output) |
| 47 | + # Numeric key_id <= 8 |
| 48 | + assert_pgp_key_link('0x0023BDC7') |
50 | 49 |
|
51 |
| - # Numeric key_id <= 8 |
52 |
| - self.assert_pgp_key_link('0x0023BDC7') |
| 50 | + # Numeric key_id <= 16 |
| 51 | + assert_pgp_key_link('0xBDC7FF5E34A12F') |
53 | 52 |
|
54 |
| - # Numeric key_id <= 16 |
55 |
| - self.assert_pgp_key_link('0xBDC7FF5E34A12F') |
| 53 | + # Numeric key_id <= 40 |
| 54 | + assert_pgp_key_link('0xA10E234343EA8BDC7FF5E34A12F') |
56 | 55 |
|
57 |
| - # Numeric key_id <= 40 |
58 |
| - self.assert_pgp_key_link('0xA10E234343EA8BDC7FF5E34A12F') |
| 56 | + pgp_key = '423423fD9004FB063E2C81117BFB1108D234DAFZ' |
| 57 | + server = settings.PGP_SERVER |
59 | 58 |
|
60 |
| - pgp_key = '423423fD9004FB063E2C81117BFB1108D234DAFZ' |
61 |
| - server = getattr(settings, 'PGP_SERVER') |
| 59 | + settings.PGP_SERVER = '' |
| 60 | + assert server not in pgp_key_link(pgp_key) |
62 | 61 |
|
63 |
| - with self.settings(PGP_SERVER=''): |
64 |
| - self.assertNotIn(server, pgp_key_link(pgp_key)) |
65 | 62 |
|
66 |
| - with self.settings(PGP_SERVER_SECURE=False): |
67 |
| - pgp_key = '423423fD9004FB063E2C81117BFB1108D234DAFZ' |
68 |
| - self.assertNotIn("https", pgp_key_link(pgp_key)) |
| 63 | + settings.PGP_SERVER_SECURE = False |
| 64 | + settings.PGP_SERVER = server |
| 65 | + pgp_key = '423423fD9004FB063E2C81117BFB1108D234DAFZ' |
| 66 | + assert not "https" in pgp_key_link(pgp_key) |
69 | 67 |
|
70 |
| - def test_pgp_fingerprint(self): |
71 |
| - self.assertEqual(pgp_fingerprint(None), "") |
72 |
| - keyid = '423423fD9004FB063E2C81117BFB1108D234DAFZ' |
73 |
| - fingerprint = pgp_fingerprint(keyid) |
74 |
| - self.assertTrue(len(fingerprint) > len(keyid)) |
| 68 | +def test_pgp_fingerprint(): |
| 69 | + assert pgp_fingerprint(None) == "" |
| 70 | + keyid = '423423fD9004FB063E2C81117BFB1108D234DAFZ' |
| 71 | + fingerprint = pgp_fingerprint(keyid) |
| 72 | + assert len(fingerprint) > len(keyid) |
0 commit comments