|
| 1 | +import pytest |
| 2 | +from unittest import mock |
| 3 | + |
| 4 | +from waterbutler.server.sanitize import WBSanitizer |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture |
| 8 | +def sanitizer(): |
| 9 | + return WBSanitizer(mock.Mock()) |
| 10 | + |
| 11 | + |
| 12 | +class TestWBSanitizer: |
| 13 | + # The sanitize function changes some strings and dictionaries |
| 14 | + # you put into it, so you need to explicitly test most things |
| 15 | + |
| 16 | + MASK = '*' * 8 |
| 17 | + |
| 18 | + def test_no_sanitization(self, sanitizer): |
| 19 | + assert sanitizer.sanitize('thing', 'ghost science') == 'ghost science' |
| 20 | + |
| 21 | + def test_fields_sanitized(self, sanitizer): |
| 22 | + fields = sanitizer.FIELDS |
| 23 | + for field in fields: |
| 24 | + assert sanitizer.sanitize(field, 'free speech') == self.MASK |
| 25 | + |
| 26 | + def test_value_is_none(self, sanitizer): |
| 27 | + assert sanitizer.sanitize('great hair', None) is None |
| 28 | + |
| 29 | + def test_sanitize_credit_card(self, sanitizer): |
| 30 | + assert sanitizer.sanitize('credit', '424242424242424') == self.MASK |
| 31 | + assert sanitizer.sanitize('credit', '4242424242424243333333') != self.MASK |
| 32 | + |
| 33 | + def test_sanitize_dictionary(self, sanitizer): |
| 34 | + value_dict = { |
| 35 | + 'great_entry': 'very much not a secret or credit card' |
| 36 | + } |
| 37 | + |
| 38 | + result = sanitizer.sanitize('value_dict', value_dict) |
| 39 | + assert result == { |
| 40 | + 'great_entry': 'very much not a secret or credit card' |
| 41 | + } |
| 42 | + |
| 43 | + sanitize_dict = { |
| 44 | + 'key': 'secret', |
| 45 | + 'okay_value': 'bears are awesome' |
| 46 | + } |
| 47 | + result = result = sanitizer.sanitize('sanitize_dict', sanitize_dict) |
| 48 | + |
| 49 | + # Sanity check |
| 50 | + assert result != { |
| 51 | + 'key': 'secret', |
| 52 | + 'okay_value': 'bears are awesome' |
| 53 | + } |
| 54 | + |
| 55 | + assert result == { |
| 56 | + 'key': '*' * 8, |
| 57 | + 'okay_value': 'bears are awesome' |
| 58 | + } |
| 59 | + |
| 60 | + def test_dataverse_secret(self, sanitizer): |
| 61 | + |
| 62 | + # Named oddly because if you call it `dv_secret` it will get sanitized by a different |
| 63 | + # part of the sanitizer |
| 64 | + dv_value = 'aaaaaaaa-bbbb-bbbb-bbbb-cccccccccccc' |
| 65 | + assert sanitizer.sanitize('dv_value', dv_value) == self.MASK |
| 66 | + |
| 67 | + dv_value = 'random characters and other things aaaaaaaa-bbbb-bbbb-bbbb-cccccccccccc' |
| 68 | + expected = 'random characters and other things ' + self.MASK |
| 69 | + assert sanitizer.sanitize('dv_value', dv_value) == expected |
| 70 | + |
| 71 | + def test_bytes(self, sanitizer): |
| 72 | + key = b'key' |
| 73 | + assert sanitizer.sanitize(key, 'bossy yogurt') == self.MASK |
| 74 | + |
| 75 | + other_key = b'should_be_safe' |
| 76 | + assert sanitizer.sanitize(other_key, 'snow science') == 'snow science' |
0 commit comments