.github
1 year ago
.gitignore
1 year ago
.travis.yml
2 years ago
LICENSE
3 years ago
MANIFEST.in
2 years ago
README.md
1 year ago
__init__.py
1 year ago
composer.json
3 years ago
crawler-user-agents.json
1 year ago
format.js
1 year ago
go.mod
2 years ago
go.sum
2 years ago
index.d.ts
3 years ago
main.php
3 years ago
package-lock.json
2 years ago
package.json
2 years ago
pyproject.toml
1 year ago
test_harness.py
1 year ago
test_validation.py
3 years ago
validate.go
1 year ago
validate.php
3 years ago
validate.py
2 years ago
validate_test.go
1 year ago
test_validation.py
189 lines
| 1 | """ |
| 2 | Break crawler-user-agents.json in ways that validate.py should detect |
| 3 | |
| 4 | Usage: |
| 5 | $ pytest test_validation.py |
| 6 | |
| 7 | """ |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import json |
| 11 | import subprocess |
| 12 | from copy import deepcopy |
| 13 | |
| 14 | import pytest |
| 15 | |
| 16 | |
| 17 | def update_json_file(data): |
| 18 | with open('crawler-user-agents.json', 'w') as f: |
| 19 | json.dump(data, f, indent=2) |
| 20 | |
| 21 | @pytest.fixture |
| 22 | def restore_original_json(): |
| 23 | # Load original version of crawler-user-agents.json |
| 24 | with open('crawler-user-agents.json') as f: |
| 25 | original_json = json.load(f) |
| 26 | |
| 27 | # By using a yield statement instead of return, all the code after |
| 28 | # the yield statement serves as the teardown code: |
| 29 | yield None |
| 30 | |
| 31 | # tear down code: restore original version of crawler-user-agents.json |
| 32 | update_json_file(original_json) |
| 33 | |
| 34 | |
| 35 | def assert_validate_failed(): |
| 36 | assert subprocess.call(['python', 'validate.py']) != 0 |
| 37 | |
| 38 | |
| 39 | def assert_validate_passed(): |
| 40 | assert subprocess.call(['python', 'validate.py']) == 0 |
| 41 | |
| 42 | |
| 43 | def test_simple_pass(restore_original_json): |
| 44 | # the json must be an array of objects containing "pattern" |
| 45 | # there must be more than 10 instances to pass |
| 46 | user_agent_list = [{'pattern': 'foo', |
| 47 | 'instances': ['foo', |
| 48 | 'afoo', |
| 49 | 'foob', |
| 50 | 'cfood', |
| 51 | '/foo/', |
| 52 | '\\foo\\', |
| 53 | ':foo:', |
| 54 | 'foo.', |
| 55 | '!foo', |
| 56 | '/foo', |
| 57 | 'foo\\', |
| 58 | 'FoofooFoo', |
| 59 | 'foot']}] |
| 60 | update_json_file(user_agent_list) |
| 61 | assert_validate_passed() |
| 62 | |
| 63 | def test_simplest_pass(restore_original_json): |
| 64 | # the simplest crawler file passes |
| 65 | user_agent_list = [{'pattern': 'foo', 'instances': []}] |
| 66 | update_json_file(user_agent_list) |
| 67 | assert_validate_passed() |
| 68 | |
| 69 | def test_schema_violation_dict1(restore_original_json): |
| 70 | # contract: the json must be an array |
| 71 | user_agent_list = {'foo':None} |
| 72 | update_json_file(user_agent_list) |
| 73 | assert_validate_failed() |
| 74 | |
| 75 | def test_schema_violation_dict2(restore_original_json): |
| 76 | # contract: the json must be an array of objects containing "pattern" |
| 77 | user_agent_list = [{'foo':None}] |
| 78 | update_json_file(user_agent_list) |
| 79 | assert_validate_failed() |
| 80 | |
| 81 | def test_schema_violation_dict3(restore_original_json): |
| 82 | # contract: the json must be an array of objects containing "pattern" and valid properties |
| 83 | user_agent_list = [{'pattern':'foo', 'foo':3}] |
| 84 | update_json_file(user_agent_list) |
| 85 | assert_validate_failed() |
| 86 | |
| 87 | def test_simple_duplicate_detection(restore_original_json): |
| 88 | # contract: if we have the same pattern twice, it fails |
| 89 | user_agent_list = [{'pattern': 'foo', |
| 90 | 'instances': ['foo', |
| 91 | 'afoo', |
| 92 | 'foob', |
| 93 | 'cfood', |
| 94 | '/foo/', |
| 95 | '\\foo\\', |
| 96 | ':foO:', |
| 97 | 'foo.', |
| 98 | '!foo', |
| 99 | '/foo', |
| 100 | 'foo\\', |
| 101 | 'FoofooFoo', |
| 102 | 'foot']}, |
| 103 | {'pattern': 'foo'}] |
| 104 | update_json_file(user_agent_list) |
| 105 | assert_validate_failed() |
| 106 | |
| 107 | |
| 108 | def test_simple_duplicate_detection2(restore_original_json): |
| 109 | # contract: if we have the same pattern twice, it fails (even w/o instances) |
| 110 | user_agent_list = [{'pattern': 'foo', |
| 111 | 'instances': ['foo', |
| 112 | 'afoo', |
| 113 | 'foob', |
| 114 | 'cfood', |
| 115 | '/foo/', |
| 116 | '\\foo\\', |
| 117 | ':foo:', |
| 118 | 'foo.', |
| 119 | '!foo', |
| 120 | '/foo', |
| 121 | 'foo\\', |
| 122 | 'FoofooFoo', |
| 123 | 'foot']}, |
| 124 | {'pattern': 'bar'}, |
| 125 | {'pattern': 'bar'}] |
| 126 | update_json_file(user_agent_list) |
| 127 | assert_validate_failed() |
| 128 | |
| 129 | |
| 130 | def test_subset_duplicate_detection(restore_original_json): |
| 131 | # contract: if a pattern matches another pattern, it fails |
| 132 | user_agent_list = [{'pattern': 'foo', |
| 133 | 'instances': ['foo', |
| 134 | 'afoo', |
| 135 | 'foob', |
| 136 | 'cfood', |
| 137 | '/foo/', |
| 138 | '\\foo\\', |
| 139 | ':foo:', |
| 140 | 'foo.', |
| 141 | '!foo', |
| 142 | '/foo', |
| 143 | 'foo\\', |
| 144 | 'FoofooFoo', |
| 145 | 'foot']}, |
| 146 | {'pattern': 'afoot'}] |
| 147 | update_json_file(user_agent_list) |
| 148 | assert_validate_failed() |
| 149 | |
| 150 | |
| 151 | def test_case_sensitivity(restore_original_json): |
| 152 | # contract: the patterns are case sensitive |
| 153 | user_agent_list = [{'pattern': 'foo', |
| 154 | 'instances': ['FOO', |
| 155 | 'aFoo', |
| 156 | 'foob', |
| 157 | 'cfood', |
| 158 | '/foo/', |
| 159 | '\\foo\\', |
| 160 | ':foo:', |
| 161 | 'fOo.', |
| 162 | '!FOO', |
| 163 | '/foo', |
| 164 | 'foo\\', |
| 165 | 'FoofooFoo', |
| 166 | 'foot']}] |
| 167 | update_json_file(user_agent_list) |
| 168 | assert_validate_failed() |
| 169 | |
| 170 | def test_duplicate_case_insensitive_detection(restore_original_json): |
| 171 | # contract: fail if we have patterns that differ only in capitailization |
| 172 | user_agent_list = [{'pattern': 'foo', |
| 173 | 'instances': ['foo', |
| 174 | 'afoo', |
| 175 | 'foob', |
| 176 | 'cfood', |
| 177 | '/foo/', |
| 178 | '\\foo\\', |
| 179 | ':foo:', |
| 180 | 'foo.', |
| 181 | '!foo', |
| 182 | '/foo', |
| 183 | 'foo\\', |
| 184 | 'FoofooFoo', |
| 185 | 'foot']}, |
| 186 | {'pattern': 'fOo'}] |
| 187 | update_json_file(user_agent_list) |
| 188 | assert_validate_failed() |
| 189 |