|
1 | 1 | # -*- coding: utf-8 -*-
|
| 2 | +import base64 |
| 3 | +import os |
| 4 | +import pickle |
2 | 5 | import pytest
|
3 | 6 | import re
|
| 7 | +import zlib |
4 | 8 |
|
5 | 9 | from scrapinghub.hubstorage.serialization import MSGPACK_AVAILABLE
|
| 10 | +from scrapinghub import HubstorageClient |
| 11 | +from scrapinghub.legacy import Connection |
| 12 | + |
| 13 | + |
| 14 | +DEFAULT_PROJECT_ID = "2222222" |
| 15 | +DEFAULT_ENDPOINT = 'http://storage.vm.scrapinghub.com' |
| 16 | +DEFAULT_DASH_ENDPOINT = 'http://33.33.33.51:8080/api/' |
| 17 | +DEFAULT_ADMIN_AUTH = 'f' * 32 |
| 18 | +DEFAULT_USER_AUTH = 'e' * 32 |
| 19 | + |
| 20 | + |
| 21 | +TEST_PROJECT_ID = os.getenv('HS_PROJECT_ID', DEFAULT_PROJECT_ID) |
| 22 | +TEST_SPIDER_NAME = 'hs-test-spider' |
| 23 | +TEST_FRONTIER_SLOT = 'site.com' |
| 24 | +TEST_BOTGROUP = 'python-hubstorage-test' |
| 25 | +TEST_COLLECTION_NAME = "test_collection_123" |
| 26 | +TEST_AUTH = os.getenv('HS_AUTH', DEFAULT_ADMIN_AUTH) |
| 27 | +TEST_ENDPOINT = os.getenv('HS_ENDPOINT', DEFAULT_ENDPOINT) |
| 28 | +TEST_COLLECTION_NAME = "test_collection_123" |
| 29 | +TEST_ADMIN_AUTH = os.getenv('AUTH', DEFAULT_ADMIN_AUTH) |
| 30 | +TEST_USER_AUTH = os.getenv('USER_AUTH', DEFAULT_USER_AUTH) |
| 31 | +TEST_DASH_ENDPOINT = os.getenv('DASH_ENDPOINT', DEFAULT_DASH_ENDPOINT) |
| 32 | + |
| 33 | + |
| 34 | +class VCRGzipSerializer(object): |
| 35 | + """Custom ZIP serializer for VCR.py.""" |
| 36 | + |
| 37 | + def serialize(self, cassette_dict): |
| 38 | + # receives a dict, must return a string |
| 39 | + # there can be binary data inside some of the requests, |
| 40 | + # so it's impossible to use json for serialization to string |
| 41 | + cassette_dict = normalize_cassette(cassette_dict) |
| 42 | + compressed = zlib.compress(pickle.dumps(cassette_dict, protocol=2)) |
| 43 | + return base64.b64encode(compressed).decode('utf8') |
| 44 | + |
| 45 | + def deserialize(self, cassette_string): |
| 46 | + # receives a string, must return a dict |
| 47 | + decoded = base64.b64decode(cassette_string.encode('utf8')) |
| 48 | + return pickle.loads(zlib.decompress(decoded)) |
| 49 | + |
| 50 | + |
| 51 | +def normalize_endpoint(uri, endpoint, default_endpoint): |
| 52 | + return uri.replace(endpoint.rstrip('/'), default_endpoint.rstrip('/')) |
| 53 | + |
| 54 | + |
| 55 | +def normalize_cassette(cassette_dict): |
| 56 | + """ |
| 57 | + This function normalizes the cassette dict trying to make sure |
| 58 | + we are always making API requests with the same variables: |
| 59 | + - project id |
| 60 | + - endpoint |
| 61 | + - authentication header |
| 62 | + """ |
| 63 | + interactions = [] |
| 64 | + for interaction in cassette_dict['interactions']: |
| 65 | + uri = interaction['request']['uri'] |
| 66 | + uri = uri.replace(TEST_PROJECT_ID, DEFAULT_PROJECT_ID) |
| 67 | + |
| 68 | + hs_endpoint = TEST_ENDPOINT or HubstorageClient.DEFAULT_ENDPOINT |
| 69 | + uri = normalize_endpoint(uri, hs_endpoint, DEFAULT_ENDPOINT) |
| 70 | + |
| 71 | + dash_endpoint = TEST_DASH_ENDPOINT or Connection.DEFAULT_ENDPOINT |
| 72 | + uri = normalize_endpoint(uri, dash_endpoint, DEFAULT_DASH_ENDPOINT) |
| 73 | + |
| 74 | + interaction['request']['uri'] = uri |
| 75 | + |
| 76 | + if 'Authorization' in interaction['request']['headers']: |
| 77 | + del interaction['request']['headers']['Authorization'] |
| 78 | + interaction['request']['headers']['Authorization'] = ( |
| 79 | + 'Basic {}'.format( |
| 80 | + base64.b64encode( |
| 81 | + '{}:'.format(DEFAULT_ADMIN_AUTH).encode('utf-8') |
| 82 | + ).decode('utf-8') |
| 83 | + ) |
| 84 | + ) |
| 85 | + |
| 86 | + interactions.append(interaction) |
| 87 | + |
| 88 | + cassette_dict['interactions'] = interactions |
| 89 | + return cassette_dict |
6 | 90 |
|
7 | 91 |
|
8 | 92 | def pytest_addoption(parser):
|
|
0 commit comments