forked from lucyparsons/OpenOversight
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Edit login user functions to return the
User
(lucyparsons#1013)
lucyparsons#1010 Edit the `login_user` functions so that they return the user (removing the need for an extra database query) and create constants for the login values. - [x] This branch is up-to-date with the `develop` branch. - [x] `pytest` passes on my local development environment. - [x] `pre-commit` passes on my local development environment.
- Loading branch information
1 parent
04f00cf
commit 46c4fc6
Showing
11 changed files
with
146 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,7 +48,3 @@ | |
MEGABYTE = 1024 * KILOBYTE | ||
MINUTE = 60 | ||
HOUR = 60 * MINUTE | ||
|
||
# Test Constants | ||
ADMIN_EMAIL = "[email protected]" | ||
ADMIN_PASSWORD = "testtest" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ | |
from webdriver_manager.firefox import GeckoDriverManager | ||
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, | ||
|
@@ -42,13 +42,28 @@ | |
) | ||
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_NUM_OFFICERS, | ||
) | ||
from OpenOversight.app.utils.constants import ENCODING_UTF_8, 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() | ||
|
@@ -326,39 +341,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, | ||
) | ||
|
@@ -390,9 +411,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, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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" | ||
|
||
|
@@ -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 | ||
|
@@ -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 | ||
|
||
|
||
|
@@ -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 | ||
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.