Skip to content

Commit 82ea751

Browse files
committed
mock google auth
1 parent 6581533 commit 82ea751

File tree

6 files changed

+58
-11
lines changed

6 files changed

+58
-11
lines changed

abnosql/plugins/table/cosmos.py

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def strip_cosmos_attrs(item):
119119

120120
class Table(TableBase):
121121

122+
@cosmos_ex_handler()
122123
def __init__(
123124
self, pm: PM, name: str, config: t.Optional[dict] = None
124125
) -> None:

abnosql/plugins/table/dynamodb.py

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ def get_dynamodb_kwargs(
151151

152152
class Table(TableBase):
153153

154+
@dynamodb_ex_handler()
154155
def __init__(
155156
self, pm: PM, name: str, config: t.Optional[dict] = None
156157
) -> None:

abnosql/plugins/table/firestore.py

+21-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import logging
66
import os
77
from os.path import expanduser
8+
import sys
89

910
import typing as t
1011

@@ -32,7 +33,8 @@
3233
hookimpl = pluggy.HookimplMarker('abnosql.table')
3334

3435
try:
35-
from google.api_core import exceptions as gex # type: ignore
36+
from google.api_core.exceptions import ClientError # type: ignore
37+
from google.auth.exceptions import GoogleAuthError # type: ignore
3638
from google.cloud import firestore # type: ignore
3739
except ImportError:
3840
MISSING_DEPS = True
@@ -53,20 +55,23 @@ def decorator(func):
5355
def wrapper(*args, **kwargs):
5456
try:
5557
return func(*args, **kwargs)
56-
except gex.ClientError as e:
58+
except ClientError as e:
5759
if raise_not_found and e.code in [404]:
5860
raise ex.NotFoundException() from None
5961
raise ex.ValidationException(detail=e) from None
62+
except GoogleAuthError as e:
63+
raise ex.ConfigException(detail=e) from None
6064
except ex.NoSQLException:
6165
raise
6266
except Exception as e:
63-
raise ex.PluginException(detail=e)
67+
raise ex.PluginException(detail=e) from None
6468
return wrapper
6569
return decorator
6670

6771

6872
class Table(TableBase):
6973

74+
@firestore_ex_handler()
7075
def __init__(
7176
self, pm: PM, name: str, config: t.Optional[dict] = None
7277
) -> None:
@@ -97,14 +102,19 @@ def _get_client(self):
97102
if val is not None:
98103
kwargs[attr] = val
99104
break
100-
cred_file = os.path.join(
101-
expanduser('~'),
102-
'.config',
103-
'gcloud',
104-
'application_default_credentials.json'
105-
)
106-
if os.path.isfile(cred_file):
107-
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = cred_file
105+
cred_file = os.path.join(*(
106+
(
107+
tuple(os.environ.get('APPDATA', ''))
108+
if sys.platform == 'win32'
109+
else (expanduser('~'), '.config')
110+
) + (
111+
'gcloud',
112+
'application_default_credentials.json'
113+
)
114+
))
115+
gac = 'GOOGLE_APPLICATION_CREDENTIALS'
116+
if gac not in os.environ and os.path.isfile(cred_file):
117+
os.environ[gac] = cred_file
108118
return firestore.Client(**kwargs)
109119

110120
def _docid(self, **kwargs):

abnosql/plugins/table/memory.py

+2
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,13 @@ def wrapper(*args, **kwargs):
159159

160160
class Table(TableBase):
161161

162+
@memory_ex_handler()
162163
def __init__(
163164
self, pm: PM, name: str, config: t.Optional[dict] = None
164165
) -> None:
165166
self.pm = pm
166167
self.name = name
168+
self.database = 'memory'
167169
self.set_config(config)
168170
self.key_attrs = get_key_attrs(self.config)
169171
self.items = self.config.get('items', {})

tests/data/google_mocked_creds.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"account": "",
3+
"client_id": "foobar.apps.googleusercontent.com",
4+
"client_secret": "foobar",
5+
"refresh_token": "foobar",
6+
"type": "authorized_user",
7+
"universe_domain": "googleapis.com"
8+
}
9+

tests/test_firestore.py

+24
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
import os
22

33
from mockfirestore import MockFirestore # type: ignore
4+
import pytest
45
from tests import common as cmn
56

7+
from abnosql import exceptions as ex
8+
from abnosql import table
9+
10+
ROOT_DIR = os.path.dirname(os.path.dirname(__file__))
11+
DATA_DIR = os.path.join(ROOT_DIR, 'tests', 'data')
12+
613

714
def config(table='hash_range', extra={}):
815
os.environ.update({
16+
'GOOGLE_APPLICATION_CREDENTIALS': os.path.join(
17+
DATA_DIR, 'google_mocked_creds.json'
18+
),
919
'ABNOSQL_DB': 'firestore',
1020
'GOOGLE_CLOUD_PROJECT': 'foo', # this is to just stop warnings on CLI
1121
'ABNOSQL_FIRESTORE_DATABASE': 'bar'
@@ -19,6 +29,20 @@ def config(table='hash_range', extra={}):
1929
return config
2030

2131

32+
def test_exceptions():
33+
_config = config()
34+
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'foobar'
35+
with pytest.raises(ex.ConfigException) as e:
36+
table('foobar', _config)
37+
assert 'invalid config' in str(e.value)
38+
assert e.value.to_problem() == {
39+
'title': 'invalid config',
40+
'detail': 'File foobar was not found.',
41+
'status': 500,
42+
'type': None
43+
}
44+
45+
2246
def test_get_item():
2347
cmn.test_get_item(config('hash_range'), 'hash_range')
2448
cmn.test_get_item(config('hash_only'), 'hash_only')

0 commit comments

Comments
 (0)