| 1 | """Constants for the prawcore test suite.""" |
| 2 | |
| 3 | import os |
| 4 | from base64 import b64encode |
| 5 | |
| 6 | from betamax import Betamax |
| 7 | from betamax_matchers.json_body import JSONBodyMatcher |
| 8 | from betamax_serializers import pretty_json |
| 9 | |
| 10 | from prawcore import Requestor |
| 11 | |
| 12 | CLIENT_ID = os.environ.get("PRAWCORE_CLIENT_ID", "fake_client_id") |
| 13 | CLIENT_SECRET = os.environ.get("PRAWCORE_CLIENT_SECRET", "fake_client_secret") |
| 14 | PASSWORD = os.environ.get("PRAWCORE_PASSWORD", "fake_password") |
| 15 | PERMANENT_GRANT_CODE = os.environ.get( |
| 16 | "PRAWCORE_PERMANENT_GRANT_CODE", "fake_perm_code" |
| 17 | ) |
| 18 | REDIRECT_URI = os.environ.get("PRAWCORE_REDIRECT_URI", "http://localhost:8080") |
| 19 | REFRESH_TOKEN = os.environ.get("PRAWCORE_REFRESH_TOKEN", "fake_refresh_token") |
| 20 | TEMPORARY_GRANT_CODE = os.environ.get( |
| 21 | "PRAWCORE_TEMPORARY_GRANT_CODE", "fake_temp_code" |
| 22 | ) |
| 23 | USERNAME = os.environ.get("PRAWCORE_USERNAME", "fake_username") |
| 24 | |
| 25 | |
| 26 | REQUESTOR = Requestor("prawcore:test (by /u/bboe)") |
| 27 | |
| 28 | |
| 29 | def b64_string(input_string): |
| 30 | """Return a base64 encoded string (not bytes) from input_string.""" |
| 31 | return b64encode(input_string.encode("utf-8")).decode("utf-8") |
| 32 | |
| 33 | |
| 34 | def two_factor_callback(): |
| 35 | """Return an OTP code.""" |
| 36 | return None |
| 37 | |
| 38 | |
| 39 | Betamax.register_request_matcher(JSONBodyMatcher) |
| 40 | Betamax.register_serializer(pretty_json.PrettyJSONSerializer) |
| 41 | |
| 42 | with Betamax.configure() as config: |
| 43 | if os.getenv("TRAVIS"): |
| 44 | config.default_cassette_options["record_mode"] = "none" |
| 45 | config.cassette_library_dir = "tests/cassettes" |
| 46 | config.default_cassette_options["serialize_with"] = "prettyjson" |
| 47 | config.default_cassette_options["match_requests_on"].append("body") |
| 48 | config.define_cassette_placeholder( |
| 49 | "<BASIC_AUTH>", b64_string(f"{CLIENT_ID}:{CLIENT_SECRET}") |
| 50 | ) |
| 51 | config.define_cassette_placeholder("<CLIENT_ID>", CLIENT_ID) |
| 52 | config.define_cassette_placeholder("<CLIENT_SECRET>", CLIENT_SECRET) |
| 53 | config.define_cassette_placeholder("<PASSWORD>", PASSWORD) |
| 54 | config.define_cassette_placeholder("<PERM_CODE>", PERMANENT_GRANT_CODE) |
| 55 | config.define_cassette_placeholder("<REFRESH_TOKEN>", REFRESH_TOKEN) |
| 56 | config.define_cassette_placeholder("<TEMP_CODE>", TEMPORARY_GRANT_CODE) |
| 57 | config.define_cassette_placeholder("<USERNAME>", USERNAME) |