| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- """Guard against cross-SKU contamination and fabricated numeric specifications."""
- import hashlib
- import importlib.util
- import json
- from pathlib import Path
- import unittest
- spec = importlib.util.spec_from_file_location('completion', Path(__file__).with_name('recover-product-completion.py'))
- m = importlib.util.module_from_spec(spec)
- spec.loader.exec_module(m)
- class ExactSkuAttributes(unittest.TestCase):
- def page(self, *, selected='123', variants=None, suffix=';', props=None):
- item = {'skuId': selected, 'saleProp': props or {'1': '规格'},
- 'skuName': '不要把标题 100g*5 当已核重量',
- 'newColorSize': variants if variants is not None else [
- {'skuId': '999', '1': '其他口味100袋'}, {'skuId': '123', '1': '当前口味五连包'}]}
- return ('window._itemOnly = ' + json.dumps({'item': item}, ensure_ascii=False) + suffix).encode()
- def parse(self, raw):
- return m.select_spec(raw, '123', hashlib.sha256(raw).hexdigest())
- def test_only_selected_variant_and_no_title_weights(self):
- actual = self.parse(self.page())
- self.assertEqual(actual['specText'], '规格:当前口味五连包')
- self.assertEqual(actual['attributes'][0]['valuePointer'], '/item/newColorSize/1/1')
- self.assertNotIn('netWeightG', actual)
- self.assertNotIn('其他', actual['specText'])
- def test_wrong_product_rejected(self):
- with self.assertRaisesRegex(ValueError, 'PAGE_PRODUCT_MISMATCH'):
- self.parse(self.page(selected='999'))
- def test_missing_selected_variant_rejected(self):
- with self.assertRaisesRegex(ValueError, 'SELECTED_SKU_NOT_UNIQUE'):
- self.parse(self.page(variants=[{'skuId': '999', '1': '其他口味'}]))
- def test_duplicate_selected_variant_rejected(self):
- with self.assertRaisesRegex(ValueError, 'SELECTED_SKU_NOT_UNIQUE'):
- self.parse(self.page(variants=[{'skuId': '123', '1': 'a'}, {'skuId': '123', '1': 'b'}]))
- def test_unlabelled_display_color_is_not_attribute(self):
- with self.assertRaisesRegex(ValueError, 'NO_EXPLICIT_SELECTED_ATTRIBUTE'):
- self.parse(self.page(variants=[{'skuId': '123', 'color': 'unproven display text'}]))
- def test_changed_original_rejected(self):
- with self.assertRaisesRegex(ValueError, 'PAGE_HASH_MISMATCH'):
- m.select_spec(self.page() + b'changed', '123', hashlib.sha256(self.page()).hexdigest())
- def test_javascript_expression_never_executed_or_accepted(self):
- with self.assertRaisesRegex(ValueError, 'NON_LITERAL_ASSIGNMENT'):
- self.parse(self.page(suffix='+ maliciousCall();'))
- if __name__ == '__main__':
- unittest.main()
|