mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-29 23:08:06 +02:00
Finished Iframe Implementation
This commit is contained in:
52
tests/unit/filter_plugins/test_csp_hashes.py
Normal file
52
tests/unit/filter_plugins/test_csp_hashes.py
Normal file
@@ -0,0 +1,52 @@
|
||||
# tests/unit/filter_plugins/test_csp_hashes.py
|
||||
import unittest
|
||||
from ansible.errors import AnsibleFilterError
|
||||
from filter_plugins.csp_hashes import append_csp_hash
|
||||
|
||||
class TestCspHashes(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# Sample applications dict for testing
|
||||
self.applications = {
|
||||
'app1': {
|
||||
'csp': {
|
||||
'hashes': {
|
||||
'script-src-elem': ["existing-hash"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
self.code = "new-hash" # example one-liner hash
|
||||
|
||||
def test_appends_new_hash(self):
|
||||
result = append_csp_hash(self.applications, 'app1', self.code)
|
||||
# Original remains unchanged
|
||||
self.assertNotIn(self.code, self.applications['app1']['csp']['hashes']['script-src-elem'])
|
||||
# New result should contain both existing and new
|
||||
self.assertIn('existing-hash', result['app1']['csp']['hashes']['script-src-elem'])
|
||||
self.assertIn(self.code, result['app1']['csp']['hashes']['script-src-elem'])
|
||||
|
||||
def test_does_not_duplicate_existing_hash(self):
|
||||
# Append an existing hash
|
||||
result = append_csp_hash(self.applications, 'app1', 'existing-hash')
|
||||
# Should still only have one instance
|
||||
hashes = result['app1']['csp']['hashes']['script-src-elem']
|
||||
self.assertEqual(hashes.count('existing-hash'), 1)
|
||||
|
||||
def test_creates_missing_csp_structure(self):
|
||||
# Remove csp and hashes keys
|
||||
apps = {'app2': {}}
|
||||
result = append_csp_hash(apps, 'app2', self.code)
|
||||
self.assertIn('csp', result['app2'])
|
||||
self.assertIn('hashes', result['app2']['csp'])
|
||||
self.assertIn(self.code, result['app2']['csp']['hashes']['script-src-elem'])
|
||||
|
||||
def test_non_dict_applications_raises(self):
|
||||
with self.assertRaises(AnsibleFilterError):
|
||||
append_csp_hash('not-a-dict', 'app1', self.code)
|
||||
|
||||
def test_unknown_application_id_raises(self):
|
||||
with self.assertRaises(AnsibleFilterError):
|
||||
append_csp_hash(self.applications, 'unknown', self.code)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
64
tests/unit/filter_plugins/test_text_filters.py
Normal file
64
tests/unit/filter_plugins/test_text_filters.py
Normal file
@@ -0,0 +1,64 @@
|
||||
# tests/unit/filter_plugins/test_text_filters.py
|
||||
import unittest
|
||||
from ansible.errors import AnsibleFilterError
|
||||
from filter_plugins.text_filters import to_one_liner
|
||||
|
||||
class TestTextFilters(unittest.TestCase):
|
||||
def test_collapse_whitespace(self):
|
||||
s = """Line one
|
||||
|
||||
Line two
|
||||
Line three"""
|
||||
expected = "Line one Line two Line three"
|
||||
self.assertEqual(to_one_liner(s), expected)
|
||||
|
||||
def test_remove_js_line_comments(self):
|
||||
s = "var a = 1; // this is a comment\nvar b = 2;"
|
||||
expected = "var a = 1; var b = 2;"
|
||||
self.assertEqual(to_one_liner(s), expected)
|
||||
|
||||
def test_remove_js_block_comments(self):
|
||||
s = "var a = /* comment inside */ 1; var b = 2;"
|
||||
expected = "var a = 1; var b = 2;"
|
||||
self.assertEqual(to_one_liner(s), expected)
|
||||
|
||||
def test_remove_multiple_comments(self):
|
||||
s = "// first comment\nvar a = 1; /* block comment */ var b = 2; // end comment"
|
||||
expected = "var a = 1; var b = 2;"
|
||||
self.assertEqual(to_one_liner(s), expected)
|
||||
|
||||
def test_strips_leading_trailing_whitespace(self):
|
||||
s = " \n some text here \n "
|
||||
expected = "some text here"
|
||||
self.assertEqual(to_one_liner(s), expected)
|
||||
|
||||
def test_non_string_raises(self):
|
||||
with self.assertRaises(AnsibleFilterError):
|
||||
to_one_liner(123)
|
||||
|
||||
def test_preserve_urls_in_string_literals(self):
|
||||
s = 'var url = "http://example.com/path"; // comment'
|
||||
expected = 'var url = "http://example.com/path";'
|
||||
self.assertEqual(to_one_liner(s), expected)
|
||||
|
||||
def test_preserve_escaped_quotes_and_protocol(self):
|
||||
s = "var s = 'He said \\'//not comment\\''; // remove this"
|
||||
expected = "var s = 'He said \\'//not comment\\'';"
|
||||
self.assertEqual(to_one_liner(s), expected)
|
||||
|
||||
def test_preserve_templating_expressions(self):
|
||||
s = 'var tracker = "//{{ domains | get_domain(\'matomo\') }}/matomo.js"; // loader'
|
||||
expected = 'var tracker = "//{{ domains | get_domain(\'matomo\') }}/matomo.js";'
|
||||
self.assertEqual(to_one_liner(s), expected)
|
||||
|
||||
def test_mixed_string_and_comment(self):
|
||||
s = '''
|
||||
var a = "foo // still part of string";
|
||||
// top-level comment
|
||||
var b = 2; // end comment
|
||||
'''
|
||||
expected = 'var a = "foo // still part of string"; var b = 2;'
|
||||
self.assertEqual(to_one_liner(s), expected)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Reference in New Issue
Block a user