This commit is contained in:
parent
ccc28435db
commit
ec99ad22b5
|
@ -1,5 +1,7 @@
|
||||||
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 teal.client import Client as TealClient, Query, Status
|
||||||
|
@ -156,3 +158,61 @@ class UserClient(Client):
|
||||||
response = super().login(self.email, self.password)
|
response = super().login(self.email, self.password)
|
||||||
self.user = response[0]
|
self.user = response[0]
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
class UserClientFlask:
|
||||||
|
|
||||||
|
def __init__(self, application,
|
||||||
|
email: str,
|
||||||
|
password: str,
|
||||||
|
response_wrapper=None,
|
||||||
|
use_cookies=True,
|
||||||
|
follow_redirects=True):
|
||||||
|
self.email = email
|
||||||
|
self.password = password
|
||||||
|
self.follow_redirects = follow_redirects
|
||||||
|
self.user = None
|
||||||
|
|
||||||
|
self.client = FlaskClient(application, use_cookies=use_cookies)
|
||||||
|
self.client.get('/login/')
|
||||||
|
|
||||||
|
data = {
|
||||||
|
'email': email,
|
||||||
|
'password': password,
|
||||||
|
'csrf_token': generate_csrf(),
|
||||||
|
}
|
||||||
|
body, status, headers = self.client.post('/login/', data=data, follow_redirects=True)
|
||||||
|
self.headers = headers
|
||||||
|
body = next(body).decode("utf-8")
|
||||||
|
assert "Unassgined" in body
|
||||||
|
|
||||||
|
def get(self,
|
||||||
|
uri='',
|
||||||
|
data=None,
|
||||||
|
follow_redirects=True,
|
||||||
|
**kw):
|
||||||
|
|
||||||
|
body, status, headers = self.client.get(
|
||||||
|
uri,
|
||||||
|
data=data,
|
||||||
|
follow_redirects=follow_redirects,
|
||||||
|
headers=self.headers
|
||||||
|
)
|
||||||
|
body = next(body).decode("utf-8")
|
||||||
|
return (body, status)
|
||||||
|
|
||||||
|
def post(self,
|
||||||
|
uri='',
|
||||||
|
data=None,
|
||||||
|
follow_redirects=True,
|
||||||
|
**kw):
|
||||||
|
|
||||||
|
import pdb; pdb.set_trace()
|
||||||
|
body, status, headers = self.client.post(
|
||||||
|
uri,
|
||||||
|
data=data,
|
||||||
|
follow_redirects=follow_redirects,
|
||||||
|
headers=self.headers
|
||||||
|
)
|
||||||
|
body = next(body).decode("utf-8")
|
||||||
|
return (body, status)
|
||||||
|
|
|
@ -13,7 +13,7 @@ from decouple import config
|
||||||
from psycopg2 import IntegrityError
|
from psycopg2 import IntegrityError
|
||||||
from sqlalchemy.exc import ProgrammingError
|
from sqlalchemy.exc import ProgrammingError
|
||||||
|
|
||||||
from ereuse_devicehub.client import Client, UserClient
|
from ereuse_devicehub.client import Client, UserClient, UserClientFlask
|
||||||
from ereuse_devicehub.config import DevicehubConfig
|
from ereuse_devicehub.config import DevicehubConfig
|
||||||
from ereuse_devicehub.db import db
|
from ereuse_devicehub.db import db
|
||||||
from ereuse_devicehub.devicehub import Devicehub
|
from ereuse_devicehub.devicehub import Devicehub
|
||||||
|
@ -136,6 +136,31 @@ def user2(app: Devicehub) -> UserClient:
|
||||||
return client
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def user3(app: Devicehub) -> UserClientFlask:
|
||||||
|
"""Gets a client with a logged-in dummy user."""
|
||||||
|
with app.app_context():
|
||||||
|
password = 'foo'
|
||||||
|
user = create_user(password=password)
|
||||||
|
client = UserClientFlask(
|
||||||
|
app, user.email, password
|
||||||
|
)
|
||||||
|
return client
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def user4(app: Devicehub) -> UserClient:
|
||||||
|
"""Gets a client with a logged-in dummy user."""
|
||||||
|
with app.app_context():
|
||||||
|
password = 'foo'
|
||||||
|
email = 'foo2@foo.com'
|
||||||
|
user = create_user(email=email, password=password)
|
||||||
|
client = UserClientFlask(
|
||||||
|
app, user.email, password
|
||||||
|
)
|
||||||
|
return client
|
||||||
|
|
||||||
|
|
||||||
def create_user(email='foo@foo.com', password='foo') -> User:
|
def create_user(email='foo@foo.com', password='foo') -> User:
|
||||||
user = User(email=email, password=password)
|
user = User(email=email, password=password)
|
||||||
user.individuals.add(Person(name='Timmy'))
|
user.individuals.add(Person(name='Timmy'))
|
||||||
|
|
|
@ -2,7 +2,7 @@ import pytest
|
||||||
from flask.testing import FlaskClient
|
from flask.testing import FlaskClient
|
||||||
from flask_wtf.csrf import generate_csrf
|
from flask_wtf.csrf import generate_csrf
|
||||||
|
|
||||||
from ereuse_devicehub.client import UserClient
|
from ereuse_devicehub.client import UserClient, UserClientFlask
|
||||||
from ereuse_devicehub.devicehub import Devicehub
|
from ereuse_devicehub.devicehub import Devicehub
|
||||||
from tests import conftest
|
from tests import conftest
|
||||||
|
|
||||||
|
@ -35,19 +35,27 @@ def test_login(user: UserClient, app: Devicehub):
|
||||||
|
|
||||||
@pytest.mark.mvp
|
@pytest.mark.mvp
|
||||||
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
||||||
def test_inventory(user: UserClient, app: Devicehub):
|
def test_inventory(user3: UserClientFlask):
|
||||||
client = FlaskClient(app, use_cookies=True)
|
body, status = user3.get('/inventory/device/')
|
||||||
client.get('/login/')
|
|
||||||
|
assert status == '200 OK'
|
||||||
|
assert "Unassgined" in body
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.mvp
|
||||||
|
@pytest.mark.usefixtures(conftest.app_context.__name__)
|
||||||
|
def test_add_lot(user3: UserClientFlask):
|
||||||
|
body, status = user3.get('/inventory/lot/add/')
|
||||||
|
|
||||||
|
assert status == '200 OK'
|
||||||
|
assert "Add a new lot" in body
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
'email': user.email,
|
'name': 'lot1',
|
||||||
'password': 'foo',
|
|
||||||
'remember': False,
|
|
||||||
'csrf_token': generate_csrf(),
|
'csrf_token': generate_csrf(),
|
||||||
}
|
}
|
||||||
body, status, headers = client.post('/login/', data=data, follow_redirects=True)
|
|
||||||
body, status, headers = client.get('/inventory/device/', headers=headers)
|
|
||||||
|
|
||||||
body = next(body).decode("utf-8")
|
|
||||||
assert status == '200 OK'
|
|
||||||
# import pdb; pdb.set_trace()
|
# import pdb; pdb.set_trace()
|
||||||
assert "Unassgined" in body
|
body, status = user3.post('/inventory/lot/add/', data=data)
|
||||||
|
|
||||||
|
assert status == '200 OK'
|
||||||
|
assert "lot1" in body
|
||||||
|
|
Reference in New Issue