forked from Github-Mirrors/canaille
Password flow unit test
This commit is contained in:
parent
531c34a689
commit
ff8bd12aaf
5 changed files with 44 additions and 5 deletions
|
@ -4,6 +4,5 @@ flask-babel
|
||||||
flask-wtf
|
flask-wtf
|
||||||
python-ldap
|
python-ldap
|
||||||
pytest
|
pytest
|
||||||
pytest-flask
|
|
||||||
toml
|
toml
|
||||||
pdbpp
|
pdbpp
|
||||||
|
|
|
@ -7,5 +7,5 @@ skipsdist=True
|
||||||
|
|
||||||
[testenv]
|
[testenv]
|
||||||
install_command = pip install {packages}
|
install_command = pip install {packages}
|
||||||
commands = {envbindir}/pytest --showlocals {posargs}
|
commands = {envbindir}/pytest --showlocals --full-trace {posargs}
|
||||||
deps = --requirement requirements.txt
|
deps = --requirement requirements.txt
|
||||||
|
|
|
@ -66,6 +66,8 @@ def slapd_connection(slapd_server):
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def app(slapd_server, slapd_connection):
|
def app(slapd_server, slapd_connection):
|
||||||
|
os.environ["AUTHLIB_INSECURE_TRANSPORT"] = "true"
|
||||||
|
|
||||||
LDAPObjectHelper.root_dn = slapd_server.suffix
|
LDAPObjectHelper.root_dn = slapd_server.suffix
|
||||||
Client.initialize(slapd_connection)
|
Client.initialize(slapd_connection)
|
||||||
User.initialize(slapd_connection)
|
User.initialize(slapd_connection)
|
||||||
|
@ -75,6 +77,7 @@ def app(slapd_server, slapd_connection):
|
||||||
app = create_app(
|
app = create_app(
|
||||||
{
|
{
|
||||||
"LDAP": {
|
"LDAP": {
|
||||||
|
"ROOT_DN": slapd_server.suffix,
|
||||||
"URI": slapd_server.ldap_uri,
|
"URI": slapd_server.ldap_uri,
|
||||||
"BIND_DN": slapd_server.root_dn,
|
"BIND_DN": slapd_server.root_dn,
|
||||||
"BIND_PW": slapd_server.root_pw,
|
"BIND_PW": slapd_server.root_pw,
|
||||||
|
@ -84,6 +87,14 @@ def app(slapd_server, slapd_connection):
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def testclient(app):
|
||||||
|
app.config["TESTING"] = True
|
||||||
|
|
||||||
|
with app.test_client() as client:
|
||||||
|
yield client
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def client(app, slapd_connection):
|
def client(app, slapd_connection):
|
||||||
c = Client(
|
c = Client(
|
||||||
|
|
|
@ -1,2 +1,31 @@
|
||||||
def test_foobar(slapd_connection, user, client):
|
import base64
|
||||||
assert True
|
|
||||||
|
|
||||||
|
def test_success(testclient, user, client):
|
||||||
|
client_credentials = base64.b64encode(
|
||||||
|
client.oauthClientID.encode("utf-8")
|
||||||
|
+ b":"
|
||||||
|
+ client.oauthClientSecret.encode("utf-8")
|
||||||
|
).decode("utf-8")
|
||||||
|
|
||||||
|
res = testclient.post(
|
||||||
|
"/oauth/token",
|
||||||
|
data=dict(
|
||||||
|
grant_type="password",
|
||||||
|
username=user.name,
|
||||||
|
password="valid",
|
||||||
|
scope="profile",
|
||||||
|
),
|
||||||
|
headers={"Authorization": f"Basic {client_credentials}"},
|
||||||
|
)
|
||||||
|
assert 200 == res.status_code
|
||||||
|
|
||||||
|
assert res.json["scope"] == ["openid", "profile"]
|
||||||
|
assert res.json["token_type"] == "Bearer"
|
||||||
|
access_token = res.json["access_token"]
|
||||||
|
|
||||||
|
res = testclient.get(
|
||||||
|
"/api/me", headers={"Authorization": f"Bearer {access_token}"}
|
||||||
|
)
|
||||||
|
assert 200 == res.status_code
|
||||||
|
assert {"foo": "bar"} == res.json
|
||||||
|
|
|
@ -141,7 +141,7 @@ def save_token(token, request):
|
||||||
oauthClientID=request.client.oauthClientID[0],
|
oauthClientID=request.client.oauthClientID[0],
|
||||||
)
|
)
|
||||||
if "refresh_token" in token:
|
if "refresh_token" in token:
|
||||||
t.oauthRefreshToken = (token["refresh_token"],)
|
t.oauthRefreshToken = token["refresh_token"]
|
||||||
t.save()
|
t.save()
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue