new userclient from flask instead of teal
This commit is contained in:
parent
ec99ad22b5
commit
f2ab02804b
|
@ -1,10 +1,11 @@
|
||||||
from inspect import isclass
|
from inspect import isclass
|
||||||
from typing import Dict, Iterable, Type, Union
|
from typing import Dict, Iterable, Type, Union
|
||||||
from flask.testing import FlaskClient
|
|
||||||
from flask_wtf.csrf import generate_csrf
|
|
||||||
|
|
||||||
from ereuse_utils.test import JSON, Res
|
from ereuse_utils.test import JSON, Res
|
||||||
from teal.client import Client as TealClient, Query, Status
|
from flask.testing import FlaskClient
|
||||||
|
from flask_wtf.csrf import generate_csrf
|
||||||
|
from teal.client import Client as TealClient
|
||||||
|
from teal.client import Query, Status
|
||||||
from werkzeug.exceptions import HTTPException
|
from werkzeug.exceptions import HTTPException
|
||||||
|
|
||||||
from ereuse_devicehub.resources import models, schemas
|
from ereuse_devicehub.resources import models, schemas
|
||||||
|
@ -15,13 +16,19 @@ ResourceLike = Union[Type[Union[models.Thing, schemas.Thing]], str]
|
||||||
class Client(TealClient):
|
class Client(TealClient):
|
||||||
"""A client suited for Devicehub main usage."""
|
"""A client suited for Devicehub main usage."""
|
||||||
|
|
||||||
def __init__(self, application,
|
def __init__(
|
||||||
|
self,
|
||||||
|
application,
|
||||||
response_wrapper=None,
|
response_wrapper=None,
|
||||||
use_cookies=False,
|
use_cookies=False,
|
||||||
allow_subdomain_redirects=False):
|
allow_subdomain_redirects=False,
|
||||||
super().__init__(application, response_wrapper, use_cookies, allow_subdomain_redirects)
|
):
|
||||||
|
super().__init__(
|
||||||
|
application, response_wrapper, use_cookies, allow_subdomain_redirects
|
||||||
|
)
|
||||||
|
|
||||||
def open(self,
|
def open(
|
||||||
|
self,
|
||||||
uri: str,
|
uri: str,
|
||||||
res: ResourceLike = None,
|
res: ResourceLike = None,
|
||||||
status: Status = 200,
|
status: Status = 200,
|
||||||
|
@ -31,13 +38,16 @@ class Client(TealClient):
|
||||||
item=None,
|
item=None,
|
||||||
headers: dict = None,
|
headers: dict = None,
|
||||||
token: str = None,
|
token: str = None,
|
||||||
**kw) -> Res:
|
**kw,
|
||||||
|
) -> Res:
|
||||||
if isclass(res) and issubclass(res, (models.Thing, schemas.Thing)):
|
if isclass(res) and issubclass(res, (models.Thing, schemas.Thing)):
|
||||||
res = res.t
|
res = res.t
|
||||||
return super().open(uri, res, status, query, accept, content_type, item, headers, token,
|
return super().open(
|
||||||
**kw)
|
uri, res, status, query, accept, content_type, item, headers, token, **kw
|
||||||
|
)
|
||||||
|
|
||||||
def get(self,
|
def get(
|
||||||
|
self,
|
||||||
uri: str = '',
|
uri: str = '',
|
||||||
res: ResourceLike = None,
|
res: ResourceLike = None,
|
||||||
query: Query = tuple(),
|
query: Query = tuple(),
|
||||||
|
@ -46,10 +56,12 @@ class Client(TealClient):
|
||||||
accept: str = JSON,
|
accept: str = JSON,
|
||||||
headers: dict = None,
|
headers: dict = None,
|
||||||
token: str = None,
|
token: str = None,
|
||||||
**kw) -> Res:
|
**kw,
|
||||||
|
) -> Res:
|
||||||
return super().get(uri, res, query, status, item, accept, headers, token, **kw)
|
return super().get(uri, res, query, status, item, accept, headers, token, **kw)
|
||||||
|
|
||||||
def post(self,
|
def post(
|
||||||
|
self,
|
||||||
data: str or dict,
|
data: str or dict,
|
||||||
uri: str = '',
|
uri: str = '',
|
||||||
res: ResourceLike = None,
|
res: ResourceLike = None,
|
||||||
|
@ -59,11 +71,14 @@ class Client(TealClient):
|
||||||
accept: str = JSON,
|
accept: str = JSON,
|
||||||
headers: dict = None,
|
headers: dict = None,
|
||||||
token: str = None,
|
token: str = None,
|
||||||
**kw) -> Res:
|
**kw,
|
||||||
return super().post(data, uri, res, query, status, content_type, accept, headers, token,
|
) -> Res:
|
||||||
**kw)
|
return super().post(
|
||||||
|
data, uri, res, query, status, content_type, accept, headers, token, **kw
|
||||||
|
)
|
||||||
|
|
||||||
def patch(self,
|
def patch(
|
||||||
|
self,
|
||||||
data: str or dict,
|
data: str or dict,
|
||||||
uri: str = '',
|
uri: str = '',
|
||||||
res: ResourceLike = None,
|
res: ResourceLike = None,
|
||||||
|
@ -74,11 +89,24 @@ class Client(TealClient):
|
||||||
accept: str = JSON,
|
accept: str = JSON,
|
||||||
headers: dict = None,
|
headers: dict = None,
|
||||||
token: str = None,
|
token: str = None,
|
||||||
**kw) -> Res:
|
**kw,
|
||||||
return super().patch(data, uri, res, query, item, status, content_type, accept, token,
|
) -> Res:
|
||||||
headers, **kw)
|
return super().patch(
|
||||||
|
data,
|
||||||
|
uri,
|
||||||
|
res,
|
||||||
|
query,
|
||||||
|
item,
|
||||||
|
status,
|
||||||
|
content_type,
|
||||||
|
accept,
|
||||||
|
token,
|
||||||
|
headers,
|
||||||
|
**kw,
|
||||||
|
)
|
||||||
|
|
||||||
def put(self,
|
def put(
|
||||||
|
self,
|
||||||
data: str or dict,
|
data: str or dict,
|
||||||
uri: str = '',
|
uri: str = '',
|
||||||
res: ResourceLike = None,
|
res: ResourceLike = None,
|
||||||
|
@ -89,11 +117,24 @@ class Client(TealClient):
|
||||||
accept: str = JSON,
|
accept: str = JSON,
|
||||||
headers: dict = None,
|
headers: dict = None,
|
||||||
token: str = None,
|
token: str = None,
|
||||||
**kw) -> Res:
|
**kw,
|
||||||
return super().put(data, uri, res, query, item, status, content_type, accept, token,
|
) -> Res:
|
||||||
headers, **kw)
|
return super().put(
|
||||||
|
data,
|
||||||
|
uri,
|
||||||
|
res,
|
||||||
|
query,
|
||||||
|
item,
|
||||||
|
status,
|
||||||
|
content_type,
|
||||||
|
accept,
|
||||||
|
token,
|
||||||
|
headers,
|
||||||
|
**kw,
|
||||||
|
)
|
||||||
|
|
||||||
def delete(self,
|
def delete(
|
||||||
|
self,
|
||||||
uri: str = '',
|
uri: str = '',
|
||||||
res: ResourceLike = None,
|
res: ResourceLike = None,
|
||||||
query: Query = tuple(),
|
query: Query = tuple(),
|
||||||
|
@ -102,23 +143,29 @@ class Client(TealClient):
|
||||||
accept: str = JSON,
|
accept: str = JSON,
|
||||||
headers: dict = None,
|
headers: dict = None,
|
||||||
token: str = None,
|
token: str = None,
|
||||||
**kw) -> Res:
|
**kw,
|
||||||
return super().delete(uri, res, query, status, item, accept, headers, token, **kw)
|
) -> Res:
|
||||||
|
return super().delete(
|
||||||
|
uri, res, query, status, item, accept, headers, token, **kw
|
||||||
|
)
|
||||||
|
|
||||||
def login(self, email: str, password: str):
|
def login(self, email: str, password: str):
|
||||||
assert isinstance(email, str)
|
assert isinstance(email, str)
|
||||||
assert isinstance(password, str)
|
assert isinstance(password, str)
|
||||||
return self.post({'email': email, 'password': password}, '/users/login/', status=200)
|
return self.post(
|
||||||
|
{'email': email, 'password': password}, '/users/login/', status=200
|
||||||
|
)
|
||||||
|
|
||||||
def get_many(self,
|
def get_many(
|
||||||
|
self,
|
||||||
res: ResourceLike,
|
res: ResourceLike,
|
||||||
resources: Iterable[Union[dict, int]],
|
resources: Iterable[Union[dict, int]],
|
||||||
key: str = None,
|
key: str = None,
|
||||||
**kw) -> Iterable[Union[Dict[str, object], str]]:
|
**kw,
|
||||||
|
) -> Iterable[Union[Dict[str, object], str]]:
|
||||||
"""Like :meth:`.get` but with many resources."""
|
"""Like :meth:`.get` but with many resources."""
|
||||||
return (
|
return (
|
||||||
self.get(res=res, item=r[key] if key else r, **kw)[0]
|
self.get(res=res, item=r[key] if key else r, **kw)[0] for r in resources
|
||||||
for r in resources
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -128,18 +175,24 @@ class UserClient(Client):
|
||||||
It will automatically perform login on the first request.
|
It will automatically perform login on the first request.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, application,
|
def __init__(
|
||||||
|
self,
|
||||||
|
application,
|
||||||
email: str,
|
email: str,
|
||||||
password: str,
|
password: str,
|
||||||
response_wrapper=None,
|
response_wrapper=None,
|
||||||
use_cookies=False,
|
use_cookies=False,
|
||||||
allow_subdomain_redirects=False):
|
allow_subdomain_redirects=False,
|
||||||
super().__init__(application, response_wrapper, use_cookies, allow_subdomain_redirects)
|
):
|
||||||
|
super().__init__(
|
||||||
|
application, response_wrapper, use_cookies, allow_subdomain_redirects
|
||||||
|
)
|
||||||
self.email = email # type: str
|
self.email = email # type: str
|
||||||
self.password = password # type: str
|
self.password = password # type: str
|
||||||
self.user = None # type: dict
|
self.user = None # type: dict
|
||||||
|
|
||||||
def open(self,
|
def open(
|
||||||
|
self,
|
||||||
uri: str,
|
uri: str,
|
||||||
res: ResourceLike = None,
|
res: ResourceLike = None,
|
||||||
status: int or HTTPException = 200,
|
status: int or HTTPException = 200,
|
||||||
|
@ -149,9 +202,20 @@ class UserClient(Client):
|
||||||
item=None,
|
item=None,
|
||||||
headers: dict = None,
|
headers: dict = None,
|
||||||
token: str = None,
|
token: str = None,
|
||||||
**kw) -> Res:
|
**kw,
|
||||||
return super().open(uri, res, status, query, accept, content_type, item, headers,
|
) -> Res:
|
||||||
self.user['token'] if self.user else token, **kw)
|
return super().open(
|
||||||
|
uri,
|
||||||
|
res,
|
||||||
|
status,
|
||||||
|
query,
|
||||||
|
accept,
|
||||||
|
content_type,
|
||||||
|
item,
|
||||||
|
headers,
|
||||||
|
self.user['token'] if self.user else token,
|
||||||
|
**kw,
|
||||||
|
)
|
||||||
|
|
||||||
# noinspection PyMethodOverriding
|
# noinspection PyMethodOverriding
|
||||||
def login(self):
|
def login(self):
|
||||||
|
@ -161,13 +225,15 @@ class UserClient(Client):
|
||||||
|
|
||||||
|
|
||||||
class UserClientFlask:
|
class UserClientFlask:
|
||||||
|
def __init__(
|
||||||
def __init__(self, application,
|
self,
|
||||||
|
application,
|
||||||
email: str,
|
email: str,
|
||||||
password: str,
|
password: str,
|
||||||
response_wrapper=None,
|
response_wrapper=None,
|
||||||
use_cookies=True,
|
use_cookies=True,
|
||||||
follow_redirects=True):
|
follow_redirects=True,
|
||||||
|
):
|
||||||
self.email = email
|
self.email = email
|
||||||
self.password = password
|
self.password = password
|
||||||
self.follow_redirects = follow_redirects
|
self.follow_redirects = follow_redirects
|
||||||
|
@ -181,38 +247,25 @@ class UserClientFlask:
|
||||||
'password': password,
|
'password': password,
|
||||||
'csrf_token': generate_csrf(),
|
'csrf_token': generate_csrf(),
|
||||||
}
|
}
|
||||||
body, status, headers = self.client.post('/login/', data=data, follow_redirects=True)
|
body, status, headers = self.client.post(
|
||||||
|
'/login/', data=data, follow_redirects=True
|
||||||
|
)
|
||||||
self.headers = headers
|
self.headers = headers
|
||||||
body = next(body).decode("utf-8")
|
body = next(body).decode("utf-8")
|
||||||
assert "Unassgined" in body
|
assert "Unassgined" in body
|
||||||
|
|
||||||
def get(self,
|
def get(self, uri='', data=None, follow_redirects=True, **kw):
|
||||||
uri='',
|
|
||||||
data=None,
|
|
||||||
follow_redirects=True,
|
|
||||||
**kw):
|
|
||||||
|
|
||||||
body, status, headers = self.client.get(
|
body, status, headers = self.client.get(
|
||||||
uri,
|
uri, data=data, follow_redirects=follow_redirects, headers=self.headers
|
||||||
data=data,
|
|
||||||
follow_redirects=follow_redirects,
|
|
||||||
headers=self.headers
|
|
||||||
)
|
)
|
||||||
body = next(body).decode("utf-8")
|
body = next(body).decode("utf-8")
|
||||||
return (body, status)
|
return (body, status)
|
||||||
|
|
||||||
def post(self,
|
def post(self, uri='', data=None, follow_redirects=True, **kw):
|
||||||
uri='',
|
|
||||||
data=None,
|
|
||||||
follow_redirects=True,
|
|
||||||
**kw):
|
|
||||||
|
|
||||||
import pdb; pdb.set_trace()
|
|
||||||
body, status, headers = self.client.post(
|
body, status, headers = self.client.post(
|
||||||
uri,
|
uri, data=data, follow_redirects=follow_redirects, headers=self.headers
|
||||||
data=data,
|
|
||||||
follow_redirects=follow_redirects,
|
|
||||||
headers=self.headers
|
|
||||||
)
|
)
|
||||||
body = next(body).decode("utf-8")
|
body = next(body).decode("utf-8")
|
||||||
return (body, status)
|
return (body, status)
|
||||||
|
|
|
@ -142,9 +142,7 @@ def user3(app: Devicehub) -> UserClientFlask:
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
password = 'foo'
|
password = 'foo'
|
||||||
user = create_user(password=password)
|
user = create_user(password=password)
|
||||||
client = UserClientFlask(
|
client = UserClientFlask(app, user.email, password)
|
||||||
app, user.email, password
|
|
||||||
)
|
|
||||||
return client
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
@ -155,9 +153,7 @@ def user4(app: Devicehub) -> UserClient:
|
||||||
password = 'foo'
|
password = 'foo'
|
||||||
email = 'foo2@foo.com'
|
email = 'foo2@foo.com'
|
||||||
user = create_user(email=email, password=password)
|
user = create_user(email=email, password=password)
|
||||||
client = UserClientFlask(
|
client = UserClientFlask(app, user.email, password)
|
||||||
app, user.email, password
|
|
||||||
)
|
|
||||||
return client
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in a new issue