Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit login user functions to return the User #1013

Merged
merged 57 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from 54 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
609efd7
Update route_helpers.py
michplunkett Aug 4, 2023
b5f909a
Update test_auth.py
michplunkett Aug 4, 2023
9211a46
Update constants.py
michplunkett Aug 4, 2023
2bf8e98
Stuff
michplunkett Aug 4, 2023
65472c6
Update constants.py
michplunkett Aug 4, 2023
a107d38
Create constants.py
michplunkett Aug 4, 2023
eee40cd
Update route_helpers.py
michplunkett Aug 4, 2023
ce1fcad
Update conftest.py
michplunkett Aug 4, 2023
5e59f25
Update test_functional.py
michplunkett Aug 4, 2023
df8686e
Update constants.py
michplunkett Aug 4, 2023
68a2233
Update conftest.py
michplunkett Aug 4, 2023
bcd248e
Update test_auth.py
michplunkett Aug 4, 2023
81fb690
Update constants.py
michplunkett Aug 4, 2023
3e77d3a
Update test_user_api.py
michplunkett Aug 4, 2023
e3c2888
Revert "Update test_user_api.py"
michplunkett Aug 4, 2023
80010ed
Update test_user_api.py
michplunkett Aug 4, 2023
e43e2d0
Update test_user_api.py
michplunkett Aug 4, 2023
74ff6d7
Update test_user_api.py
michplunkett Aug 4, 2023
6341933
Update conftest.py
michplunkett Aug 4, 2023
634144d
Update test_user_api.py
michplunkett Aug 4, 2023
eee741a
Update sort.html
michplunkett Aug 4, 2023
caf40a4
Update test_functional.py
michplunkett Aug 4, 2023
8afeb80
Simpsons
michplunkett Aug 4, 2023
c1f95e9
Update test_functional.py
michplunkett Aug 4, 2023
afb805a
Update sort.html
michplunkett Aug 4, 2023
383525d
Create cache_client.py
michplunkett Aug 4, 2023
4660f78
Update cache_client.py
michplunkett Aug 4, 2023
70b9bf9
Update database.py
michplunkett Aug 4, 2023
3227fc7
Update db_cache.py
michplunkett Aug 4, 2023
364b5d2
Update database.py
michplunkett Aug 4, 2023
1892b3e
Update db_cache.py
michplunkett Aug 4, 2023
6dfe278
Update database.py
michplunkett Aug 4, 2023
70fae44
Update constants.py
michplunkett Aug 4, 2023
7b98372
Update database.py
michplunkett Aug 4, 2023
c662150
Update db_cache.py
michplunkett Aug 4, 2023
dd85fdb
Update database.py
michplunkett Aug 4, 2023
3dc0747
Update views.py
michplunkett Aug 4, 2023
dfd7a75
Update model_view.py
michplunkett Aug 4, 2023
97d9431
Delete db_cache.py
michplunkett Aug 4, 2023
f93fd98
Create database_cache.py
michplunkett Aug 4, 2023
f218ea3
Update database.py
michplunkett Aug 4, 2023
c8e31b0
Update database_cache.py
michplunkett Aug 4, 2023
d5a938c
Create test_cache.py
michplunkett Aug 4, 2023
d7571c7
Update test_cache.py
michplunkett Aug 4, 2023
64bb6c4
Update test_cache.py
michplunkett Aug 4, 2023
b9cac0e
Update test_cache.py
michplunkett Aug 5, 2023
7ee9038
Update test_cache.py
michplunkett Aug 5, 2023
e8d7aad
Merge branch 'cache_singleton' into edit_login_user_funcs
michplunkett Aug 6, 2023
ebc29a1
Update route_helpers.py
michplunkett Aug 6, 2023
5b85f6b
Update test_cache.py
michplunkett Aug 6, 2023
b69a5fd
Merge branch 'develop' into edit_login_user_funcs
michplunkett Aug 7, 2023
cb2c4ac
Update constants.py
michplunkett Aug 7, 2023
1fddde1
Update conftest.py
michplunkett Aug 7, 2023
5f9a5bd
Update test_other.py
michplunkett Aug 7, 2023
723abab
Update test_cache.py
michplunkett Aug 7, 2023
47ae58c
Update database.py
michplunkett Aug 7, 2023
1e68467
Update test_models.py
michplunkett Aug 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions OpenOversight/app/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,3 @@
MEGABYTE = 1024 * KILOBYTE
MINUTE = 60
HOUR = 60 * MINUTE

# Test Constants
ADMIN_EMAIL = "[email protected]"
ADMIN_PASSWORD = "testtest"
63 changes: 46 additions & 17 deletions OpenOversight/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from sqlalchemy.orm import scoped_session, sessionmaker
from xvfbwrapper import Xvfb

from OpenOversight.app import create_app
from OpenOversight.app import EmailClient, create_app
from OpenOversight.app.models.database import (
Assignment,
Department,
Expand All @@ -40,13 +40,31 @@
from OpenOversight.app.models.database import db as _db
from OpenOversight.app.utils.choices import DEPARTMENT_STATE_CHOICES
from OpenOversight.app.utils.constants import (
ADMIN_EMAIL,
ADMIN_PASSWORD,
ENCODING_UTF_8,
KEY_ENV_TESTING,
KEY_NUM_OFFICERS,
)
from OpenOversight.app.utils.general import merge_dicts
from OpenOversight.tests.constants import (
AC_USER_EMAIL,
AC_USER_PASSWORD,
AC_USER_USERNAME,
ADMIN_USER_EMAIL,
ADMIN_USER_PASSWORD,
ADMIN_USER_USER_NAME,
DISABLED_USER_EMAIL,
DISABLED_USER_PASSWORD,
DISABLED_USER_USERNAME,
GENERAL_USER_EMAIL,
GENERAL_USER_PASSWORD,
GENERAL_USER_USERNAME,
MOD_DISABLED_USER_EMAIL,
MOD_DISABLED_USER_PASSWORD,
MOD_DISABLED_USER_USERNAME,
UNCONFIRMED_USER_EMAIL,
UNCONFIRMED_USER_PASSWORD,
UNCONFIRMED_USER_USERNAME,
)


factory = Faker()
Expand Down Expand Up @@ -263,6 +281,11 @@ def app(request):
yield app


@pytest.fixture(autouse=True)
def email_client():
EmailClient(testing=True)


@pytest.fixture(autouse=True)
def ctx(app):
with app.app_context():
Expand Down Expand Up @@ -337,39 +360,45 @@ def add_mockdata(session):
assert current_app.config[KEY_NUM_OFFICERS] >= 5

test_user = User(
email="[email protected]", username="test_user", password="dog", confirmed=True
email=GENERAL_USER_EMAIL,
username=GENERAL_USER_USERNAME,
password=GENERAL_USER_PASSWORD,
confirmed=True,
)
session.add(test_user)

test_admin = User(
email=ADMIN_EMAIL,
username="test_admin",
password=ADMIN_PASSWORD,
email=ADMIN_USER_EMAIL,
username=ADMIN_USER_USER_NAME,
password=ADMIN_USER_PASSWORD,
confirmed=True,
is_administrator=True,
)
session.add(test_admin)

test_unconfirmed_user = User(
email="[email protected]", username="b_meson", password="dog", confirmed=False
email=UNCONFIRMED_USER_EMAIL,
username=UNCONFIRMED_USER_USERNAME,
password=UNCONFIRMED_USER_PASSWORD,
confirmed=False,
)
session.add(test_unconfirmed_user)
session.commit()

test_disabled_user = User(
email="[email protected]",
username="may",
password="yam",
email=DISABLED_USER_EMAIL,
username=DISABLED_USER_USERNAME,
password=DISABLED_USER_PASSWORD,
confirmed=True,
is_disabled=True,
)
session.add(test_disabled_user)
session.commit()

test_modified_disabled_user = User(
email="[email protected]",
username="sam",
password="the yam",
email=MOD_DISABLED_USER_EMAIL,
username=MOD_DISABLED_USER_USERNAME,
password=MOD_DISABLED_USER_PASSWORD,
confirmed=True,
is_disabled=True,
)
Expand Down Expand Up @@ -401,9 +430,9 @@ def add_mockdata(session):
session.commit()

test_area_coordinator = User(
email="[email protected]",
username="test_ac",
password="horse",
email=AC_USER_EMAIL,
username=AC_USER_USERNAME,
password=AC_USER_PASSWORD,
confirmed=True,
is_area_coordinator=True,
ac_department_id=AC_DEPT,
Expand Down
19 changes: 19 additions & 0 deletions OpenOversight/tests/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# User Constants
AC_USER_EMAIL = "[email protected]"
AC_USER_PASSWORD = "horse"
AC_USER_USERNAME = "test_ac"
ADMIN_USER_EMAIL = "[email protected]"
ADMIN_USER_PASSWORD = "testtest"
ADMIN_USER_USER_NAME = "test_admin"
GENERAL_USER_EMAIL = "[email protected]"
GENERAL_USER_PASSWORD = "dog"
GENERAL_USER_USERNAME = "test_user"
DISABLED_USER_EMAIL = "[email protected]"
DISABLED_USER_PASSWORD = "yam"
DISABLED_USER_USERNAME = "may"
MOD_DISABLED_USER_EMAIL = "[email protected]"
MOD_DISABLED_USER_PASSWORD = "the yam"
MOD_DISABLED_USER_USERNAME = "sam"
UNCONFIRMED_USER_EMAIL = "[email protected]"
UNCONFIRMED_USER_PASSWORD = "dog"
UNCONFIRMED_USER_USERNAME = "b_meson"
56 changes: 38 additions & 18 deletions OpenOversight/tests/routes/route_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,69 @@

from OpenOversight.app.auth.forms import LoginForm
from OpenOversight.app.models.database import User
from OpenOversight.app.utils.constants import ADMIN_PASSWORD
from OpenOversight.tests.conftest import AC_DEPT
from OpenOversight.tests.constants import (
AC_USER_EMAIL,
AC_USER_PASSWORD,
ADMIN_USER_EMAIL,
ADMIN_USER_PASSWORD,
DISABLED_USER_EMAIL,
DISABLED_USER_PASSWORD,
GENERAL_USER_EMAIL,
GENERAL_USER_PASSWORD,
MOD_DISABLED_USER_EMAIL,
MOD_DISABLED_USER_PASSWORD,
UNCONFIRMED_USER_EMAIL,
UNCONFIRMED_USER_PASSWORD,
)


def login_user(client):
user = User.query.filter_by(id=1).first()
form = LoginForm(email=user.email, password="dog", remember_me=True)
user = User.query.filter_by(email=GENERAL_USER_EMAIL).first()
form = LoginForm(email=user.email, password=GENERAL_USER_PASSWORD, remember_me=True)
rv = client.post(url_for("auth.login"), data=form.data, follow_redirects=False)
return rv
return rv, user


def login_unconfirmed_user(client):
user = User.query.filter_by(confirmed=False).first()
form = LoginForm(email=user.email, password="dog", remember_me=True)
user = User.query.filter_by(email=UNCONFIRMED_USER_EMAIL).first()
form = LoginForm(
email=user.email, password=UNCONFIRMED_USER_PASSWORD, remember_me=True
)
rv = client.post(url_for("auth.login"), data=form.data, follow_redirects=False)
assert b"Invalid username or password" not in rv.data
return rv
return rv, user


def login_disabled_user(client):
form = LoginForm(email="[email protected]", password="yam", remember_me=True)
user = User.query.filter_by(email=DISABLED_USER_EMAIL).first()
form = LoginForm(
email=user.email, password=DISABLED_USER_PASSWORD, remember_me=True
)
rv = client.post(url_for("auth.login"), data=form.data, follow_redirects=True)
return rv
return rv, user


def login_modified_disabled_user(client):
form = LoginForm(email="[email protected]", password="the yam", remember_me=True)
user = User.query.filter_by(email=MOD_DISABLED_USER_EMAIL).first()
form = LoginForm(
email=user.email, password=MOD_DISABLED_USER_PASSWORD, remember_me=True
)
rv = client.post(url_for("auth.login"), data=form.data, follow_redirects=True)
return rv
return rv, user


def login_admin(client):
user = User.query.filter_by(is_administrator=True).first()
form = LoginForm(email=user.email, password=ADMIN_PASSWORD, remember_me=True)
user = User.query.filter_by(email=ADMIN_USER_EMAIL).first()
form = LoginForm(email=user.email, password=ADMIN_USER_PASSWORD, remember_me=True)
rv = client.post(url_for("auth.login"), data=form.data, follow_redirects=False)
return rv
return rv, user


def login_ac(client):
user = User.query.filter_by(ac_department_id=AC_DEPT).first()
form = LoginForm(email=user.email, password="horse", remember_me=True)
user = User.query.filter_by(email=AC_USER_EMAIL).first()
form = LoginForm(email=user.email, password=AC_USER_PASSWORD, remember_me=True)
rv = client.post(url_for("auth.login"), data=form.data, follow_redirects=False)
return rv
return rv, user


def process_form_data(form_dict):
Expand Down
17 changes: 11 additions & 6 deletions OpenOversight/tests/routes/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
from OpenOversight.app.models.database import User
from OpenOversight.app.utils.constants import KEY_OO_MAIL_SUBJECT_PREFIX
from OpenOversight.tests.conftest import AC_DEPT
from OpenOversight.tests.constants import (
GENERAL_USER_EMAIL,
MOD_DISABLED_USER_EMAIL,
UNCONFIRMED_USER_EMAIL,
)
from OpenOversight.tests.routes.route_helpers import (
login_disabled_user,
login_modified_disabled_user,
Expand Down Expand Up @@ -58,7 +63,7 @@ def test_route_login_required(route, client, mockdata):

def test_valid_user_can_login(mockdata, client, session):
with current_app.test_request_context():
rv = login_user(client)
rv, _ = login_user(client)
assert rv.status_code == HTTPStatus.FOUND
assert urlparse(rv.location).path == "/index"

Expand All @@ -74,7 +79,7 @@ def test_valid_user_can_login_with_email_differently_cased(mockdata, client, ses
def test_invalid_user_cannot_login(mockdata, client, session):
with current_app.test_request_context():
form = LoginForm(
email="[email protected]", password="bruteforce", remember_me=True
email=UNCONFIRMED_USER_EMAIL, password="bruteforce", remember_me=True
)
rv = client.post(url_for("auth.login"), data=form.data)
assert b"Invalid username or password." in rv.data
Expand Down Expand Up @@ -428,7 +433,7 @@ def test_unconfirmed_user_redirected_to_confirm_account(mockdata, client, sessio

def test_disabled_user_cannot_login(mockdata, client, session):
with current_app.test_request_context():
rv = login_disabled_user(client)
rv, _ = login_disabled_user(client)
assert b"User has been disabled" in rv.data


Expand All @@ -438,11 +443,11 @@ def test_disabled_user_cannot_visit_pages_requiring_auth(mockdata, client, sessi
# you'll get unexpected results if both tests run simultaneously.
with current_app.test_request_context():
# Temporarily enable account for login
user = User.query.filter_by(email="[email protected]").one()
user = User.query.filter_by(email=MOD_DISABLED_USER_EMAIL).one()
user.is_disabled = False
session.add(user)

rv = login_modified_disabled_user(client)
rv, _ = login_modified_disabled_user(client)
assert b"/user/sam" in rv.data

# Disable account again and check that login_required redirects user correctly
Expand Down Expand Up @@ -477,5 +482,5 @@ def test_user_can_change_dept_pref(mockdata, client, session):

assert b"Updated!" in rv.data

user = User.query.filter_by(email="[email protected]").one()
user = User.query.filter_by(email=GENERAL_USER_EMAIL).one()
assert user.dept_pref == AC_DEPT
8 changes: 5 additions & 3 deletions OpenOversight/tests/routes/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from flask import current_app, url_for

from OpenOversight.app.utils.constants import ENCODING_UTF_8, KEY_TIMEZONE
from OpenOversight.tests.constants import GENERAL_USER_USERNAME
from OpenOversight.tests.routes.route_helpers import login_user


Expand Down Expand Up @@ -32,9 +33,10 @@ def test_user_can_access_profile(mockdata, client, session):
login_user(client)

rv = client.get(
url_for("main.profile", username="test_user"), follow_redirects=True
url_for("main.profile", username=GENERAL_USER_USERNAME),
follow_redirects=True,
)
assert "test_user" in rv.data.decode(ENCODING_UTF_8)
assert GENERAL_USER_USERNAME in rv.data.decode(ENCODING_UTF_8)
# User email should not appear
assert "User Email" not in rv.data.decode(ENCODING_UTF_8)
# Toggle button should not appear for this non-admin user
Expand All @@ -48,7 +50,7 @@ def test_user_can_access_profile_differently_cased(mockdata, client, session):
rv = client.get(
url_for("main.profile", username="TEST_USER"), follow_redirects=True
)
assert "test_user" in rv.data.decode(ENCODING_UTF_8)
assert GENERAL_USER_USERNAME in rv.data.decode(ENCODING_UTF_8)
assert "User Email" not in rv.data.decode(ENCODING_UTF_8)
assert "Edit User" not in rv.data.decode(ENCODING_UTF_8)

Expand Down
Loading