|
| 1 | +""" issue-114 tests """ |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +sys.path.insert(0, os.path.abspath('..')) |
| 6 | + |
| 7 | +import CloudFlare |
| 8 | + |
| 9 | +# CloudFlare(email=None, key=None, token=None, certtoken=None, debug=False, raw=False, use_sessions=True, profile=None, base_url=None, global_request_timeout=5, max_request_retries=5) |
| 10 | + |
| 11 | +class TestCloudflare: |
| 12 | + """ TestCloudflare """ |
| 13 | + def test_email_key_token(self): |
| 14 | + """ test_email_key_token """ |
| 15 | + # Always clear environment |
| 16 | + self._setup() |
| 17 | + |
| 18 | + profile = self._profile |
| 19 | + |
| 20 | + assert self._email or self._key or self._token |
| 21 | + |
| 22 | + # if not self._email and not self._key and not self._token: |
| 23 | + # assert 'EMAIL/KEY/TOKEN all needed in order to run this test' == '' |
| 24 | + |
| 25 | + cf = None |
| 26 | + # loop over each combination |
| 27 | + for email in [None, self._email, 'example@example.com']: |
| 28 | + for key in [None, self._key, self._token]: |
| 29 | + for token in [None, self._token, self._key]: |
| 30 | + print('email = ', self._obfuscate(email), 'key = ', self._obfuscate(key), 'token = ', self._obfuscate(token)) |
| 31 | + if cf: |
| 32 | + del cf |
| 33 | + cf = CloudFlare.CloudFlare(email=email, key=key, token=token, profile=profile) |
| 34 | + assert isinstance(cf, CloudFlare.CloudFlare) |
| 35 | + |
| 36 | + try: |
| 37 | + r = cf.zones(params={'per_page':1}) |
| 38 | + except: |
| 39 | + r = None |
| 40 | + |
| 41 | + if email is None and key is None and token == self._token: |
| 42 | + assert isinstance(r, list) |
| 43 | + assert len(r) == 1 |
| 44 | + assert isinstance(r[0], dict) |
| 45 | + continue |
| 46 | + |
| 47 | + if email is None and key == self._token and token is None: |
| 48 | + assert isinstance(r, list) |
| 49 | + assert len(r) == 1 |
| 50 | + assert isinstance(r[0], dict) |
| 51 | + continue |
| 52 | + |
| 53 | + if email == self._email and key == self._key and token is None: |
| 54 | + assert isinstance(r, list) |
| 55 | + assert len(r) == 1 |
| 56 | + assert isinstance(r[0], dict) |
| 57 | + continue |
| 58 | + |
| 59 | + if email == self._email and key is None and token == self._key: |
| 60 | + assert isinstance(r, list) |
| 61 | + assert len(r) == 1 |
| 62 | + assert isinstance(r[0], dict) |
| 63 | + continue |
| 64 | + |
| 65 | + # Nothing else should work! |
| 66 | + assert r is None |
| 67 | + |
| 68 | + def _setup(self): |
| 69 | + """ setup """ |
| 70 | + # Force no profile to be picked |
| 71 | + self._profile='' |
| 72 | + # read in email/key/token from config file(s) |
| 73 | + _config_files = [ |
| 74 | + '.cloudflare.cfg', |
| 75 | + os.path.expanduser('~/.cloudflare.cfg'), |
| 76 | + os.path.expanduser('~/.cloudflare/cloudflare.cfg') |
| 77 | + ] |
| 78 | + email = None |
| 79 | + key = None |
| 80 | + token = None |
| 81 | + for filename in _config_files: |
| 82 | + try: |
| 83 | + with open(filename, 'r') as fd: |
| 84 | + for l in fd: |
| 85 | + if email and key and token: |
| 86 | + break |
| 87 | + if l[0] == '#': |
| 88 | + continue |
| 89 | + a = l.split() |
| 90 | + if len(a) < 3: |
| 91 | + continue |
| 92 | + if a[1] != '=': |
| 93 | + continue |
| 94 | + if not email and a[0] == 'email': |
| 95 | + email = a[2] |
| 96 | + continue |
| 97 | + if not key and a[0] == 'key': |
| 98 | + key = a[2] |
| 99 | + continue |
| 100 | + if not token and a[0] == 'token': |
| 101 | + token = a[2] |
| 102 | + continue |
| 103 | + break |
| 104 | + except FileNotFoundError: |
| 105 | + pass |
| 106 | + self._email = email |
| 107 | + self._key = key |
| 108 | + self._token = token |
| 109 | + |
| 110 | + # now remove all env variables! |
| 111 | + for env in ['CLOUDFLARE_EMAIL', 'CLOUDFLARE_API_KEY', 'CLOUDFLARE_API_TOKEN']: |
| 112 | + try: |
| 113 | + del os.environ[env] |
| 114 | + except KeyError: |
| 115 | + pass |
| 116 | + for env in ['CF_API_EMAIL', 'CF_API_KEY', 'CF_API_TOKEN']: |
| 117 | + try: |
| 118 | + del os.environ[env] |
| 119 | + except KeyError: |
| 120 | + pass |
| 121 | + |
| 122 | + def _obfuscate(self, s): |
| 123 | + """ _obfuscate """ |
| 124 | + |
| 125 | + if s is None: |
| 126 | + return '' |
| 127 | + return '█' * len(s) |
0 commit comments