Skip to content

Commit

Permalink
All existing tests now removed, skipped or passing
Browse files Browse the repository at this point in the history
  • Loading branch information
Thingus committed May 31, 2022
1 parent 1fa32cc commit 8bde8d6
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 86 deletions.
2 changes: 1 addition & 1 deletion flowauth/backend/flowauth/token_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def list_my_servers():
Produces a list of json objects with "id" and "server_name" fields.
"""
servers = db.session.execute(
db.select(Server).join(current_user.roles.server)
db.select(Server).join(current_user.roles).join(Role.server)
).all()

for group in current_user.groups:
Expand Down
56 changes: 30 additions & 26 deletions flowauth/backend/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from freezegun import freeze_time
from time import sleep

from functools import partial
Expand Down Expand Up @@ -28,32 +29,33 @@
"TestTwoFactorUser", TestUser._fields + ("otp_generator", "backup_codes")
)


@pytest.fixture
def test_date(monkeypatch):

# Adapted from https://stackoverflow.com/questions/20503373/how-to-monkeypatch-pythons-datetime-datetime-now-with-py-test /
TEST_DATE = datetime.datetime(year=2020, month=12, day=31)

class test_datetime(datetime.datetime):
@classmethod
def now(cls, *args, **kwargs):
return TEST_DATE

@classmethod
def utcnow(cls, *args, **kwargs):
return cls.now()

monkeypatch.setattr(datetime, "datetime", test_datetime)
return TEST_DATE


def test_test_date_fixture(test_date):
assert datetime.datetime.now() == test_date
#
# @pytest.fixture
# def test_date(monkeypatch):
#
# # Adapted from https://stackoverflow.com/questions/20503373/how-to-monkeypatch-pythons-datetime-datetime-now-with-py-test /
# TEST_DATE = datetime.datetime(year=2020, month=12, day=31)
#
# class test_datetime(datetime.datetime):
# @classmethod
# def now(cls, *args, **kwargs):
# return TEST_DATE
#
# @classmethod
# def utcnow(cls, *args, **kwargs):
# return cls.now()
#
# monkeypatch.setattr(datetime, "datetime", test_datetime)
# return TEST_DATE
#
#
# def test_test_date_fixture(test_date):
# assert datetime.datetime.now() == test_date
#


@pytest.fixture
def app(tmpdir, test_date):
def app(tmpdir):
"""Per test app"""
db_path = tmpdir / "db.db"
print(f"DB path: {db_path}")
Expand Down Expand Up @@ -156,10 +158,11 @@ def test_two_factor_auth_user(app, get_two_factor_code):
def test_admin(app):
with app.app_context():
user = User.query.filter(User.username == app.config["ADMIN_USER"]).first()
return TestUser(user.id, user.username, app.config["ADMIN_PASSWORD"])
return TestUser(user.id, user.username, app.config["ADMIN_PASSWORD"])


@pytest.fixture # (scope="session")
@freeze_time("2020-12-31")
def test_servers(app):
with app.app_context():
# Add some servers
Expand All @@ -173,7 +176,7 @@ def test_servers(app):
name="DUMMY_SERVER_B",
longest_token_life_minutes=2880,
latest_token_expiry=datetime.datetime.now().date()
+ datetime.timedelta(days=700),
+ datetime.timedelta(days=365),
)
db.session.add(dummy_server_a)
db.session.add(dummy_server_b)
Expand Down Expand Up @@ -202,6 +205,7 @@ def test_scopes(app, test_servers):


@pytest.fixture # (scope="session")
@freeze_time("2020-12-31")
def test_roles(app, test_scopes, test_servers):
read_a, read_b, run, dummy_query = test_scopes
server_a, server_b = test_servers
Expand Down Expand Up @@ -237,7 +241,7 @@ def test_user_with_roles(app, test_user, test_roles):
test_user_orm.roles += [role_a, role_b]
db.session.add(test_user_orm)
db.session.commit()
return uid, uname, upass
return uid, uname, upass


@pytest.fixture
Expand Down
54 changes: 28 additions & 26 deletions flowauth/backend/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@
from flask import g, session


def test_login(client, auth, test_user):
"""Test that we can log in by posting a username and password as json"""
# test that signin route doesn't accept a get
assert client.get("/signin").status_code == 405
uid, username, password = test_user
# test that successful login redirects to the index page
response, _ = auth.login(username, password)
assert {
"logged_in": True,
"is_admin": False,
"require_two_factor_setup": False,
} == response.get_json()

# login request set the user_id in the session
# check that the user is loaded from the session
with client:
client.get("/")
assert session["identity.id"] == uid
assert g.user.username == username
def test_login(app, client, auth, test_user):
with app.app_context():
"""Test that we can log in by posting a username and password as json"""
# test that signin route doesn't accept a get
assert client.get("/signin").status_code == 405
uid, username, password = test_user
# test that successful login redirects to the index page
response, _ = auth.login(username, password)
assert {
"logged_in": True,
"is_admin": False,
"require_two_factor_setup": False,
} == response.get_json()

# login request set the user_id in the session
# check that the user is loaded from the session
with client:
client.get("/")
assert session["identity.id"] == uid
assert g.user.username == username


@pytest.mark.parametrize(
Expand Down Expand Up @@ -52,11 +53,12 @@ def test_is_logged_in(client):
assert client.get("/is_signed_in").status_code == 401


def test_logout(client, auth, test_user):
"""Test that we can log out"""
uid, username, password = test_user
auth.login(username, password)
def test_logout(app, client, auth, test_user):
with app.app_context():
"""Test that we can log out"""
uid, username, password = test_user
auth.login(username, password)

with client:
auth.logout()
assert "user_id" not in session
with client:
auth.logout()
assert "user_id" not in session
51 changes: 24 additions & 27 deletions flowauth/backend/tests/test_role_admin.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
import pytest
from freezegun import freeze_time


@pytest.mark.usefixtures("test_data_with_access_rights")
@pytest.fixture
def logged_in_session(client, auth, app):
response, csrf_cookie = auth.login("TEST_ADMIN", "DUMMY_PASSWORD")


@freeze_time("2020-12-31")
def test_list_roles(client, auth, app, test_roles, test_scopes):
response, csrf_cookie = auth.login("TEST_ADMIN", "DUMMY_PASSWORD")
response = client.get(
"/admin/servers/1/roles", headers={"X-CSRF-Token": csrf_cookie}
)
assert response.status_code == 200
assert [
{
"id": 1,
"name": "runner",
"scopes": ["run", "get_result", "dummy_query:admin_level_1"],
"latest_token_expiry": "2021-12-31T00:00:00.000000Z",
"longest_token_life_minutes": 2880,
},
{
"id": 2,
"name": "reader",
"scopes": ["get_result"],
"latest_token_expiry": "2021-12-31T00:00:00.000000Z",
"longest_token_life_minutes": 2880,
},
] == response.get_json()
with app.app_context():
response, csrf_cookie = auth.login("TEST_ADMIN", "DUMMY_PASSWORD")
response = client.get(
"/admin/servers/1/roles", headers={"X-CSRF-Token": csrf_cookie}
)
assert response.status_code == 200
assert [
{
"id": 1,
"name": "runner",
"scopes": ["run", "get_result", "dummy_query:admin_level_1"],
"latest_token_expiry": "2021-12-31T00:00:00.000000Z",
"longest_token_life_minutes": 2880,
},
{
"id": 2,
"name": "reader",
"scopes": ["get_result"],
"latest_token_expiry": "2021-12-31T00:00:00.000000Z",
"longest_token_life_minutes": 2880,
},
] == response.get_json()


def test_add_role(client, auth, app, test_scopes):
Expand Down
2 changes: 2 additions & 0 deletions flowauth/backend/tests/test_server_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# file, You can obtain one at http.//mozilla.org/MPL/2.0/.
import datetime

from freezegun import freeze_time
from werkzeug.http import http_date

import pytest
Expand Down Expand Up @@ -33,6 +34,7 @@ def test_get_server(client, auth, app):
assert {"id": 1, "name": "DUMMY_SERVER_A"} == response.get_json()


@freeze_time("2020-12-31")
@pytest.mark.usefixtures("test_data_with_access_rights")
def test_get_server_time_limits(client, auth, app):

Expand Down
5 changes: 1 addition & 4 deletions flowauth/backend/tests/test_user_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import datetime

import pytest
from pytest import approx
from werkzeug.http import http_date


@pytest.mark.skip(reason="Users do not have direct access to servers anymore")
@pytest.mark.usefixtures("test_data")
def test_server_access(client, auth, test_user):
uid, uname, upass = test_user
Expand Down
3 changes: 1 addition & 2 deletions flowauth/backend/tests/test_version_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import flowauth


def test_version(client):
def test_version(client, app):
"""Test the correct version is returned."""

response = client.get("/version")
assert response.status_code == 200 # Should get an OK

Expand Down

0 comments on commit 8bde8d6

Please sign in to comment.