def test_admin_view_access_and_render(init_app): """Make sure route '/cabinet' works and template with cabinet form is rendered""" app = init_app with captured_templates(app) as templates: with app.test_client() as client: # User is not authenticated response_admin_view = client.get("/admin/") assert response_admin_view.status_code == 302 assert current_user.is_anonymous # Authenticate user response_login_user = login_user(client, "admin", "test") assert response_login_user.status_code == 200 assert current_user.is_authenticated # Template 'admin' was rendered template, context = templates[-1] assert len(templates) == 1 assert template.name == "admin/admin.html" # Logout user response_logout_user = logout_user(client) assert response_logout_user.status_code == 200 assert current_user.is_anonymous # Authenticate usual user and try to access /admin response_login_admin = login_user(client, "john", "test") assert response_login_admin.status_code == 200 assert current_user.is_authenticated and current_user.username != "admin" response_admin_view_access_for_admin = client.get("/admin/") assert response_admin_view_access_for_admin.status_code == 302
def test_payment_card_form(init_app): """Check if user can use the payment card""" app = init_app with app.test_client() as client: # login user to access cabinet response_login_user = login_user(client, "john", "test") assert response_login_user.status_code == 200 assert current_user.is_authenticated assert current_user.balance == 0 # Use card with code 000001 response_use_card = client.post("/cabinet", data=dict(code="000001"), follow_redirects=True) prev_user_balance = current_user.balance assert response_use_card.status_code == 200 assert current_user.balance != 0 # Use card with code 000001 again (balance shouldn't change) response_use_card = client.post("/cabinet", data=dict(code="000001"), follow_redirects=True) assert response_use_card.status_code == 200 assert current_user.balance == prev_user_balance
def test_login_view_access_and_render(init_app): """Make sure route '/' works and template with login form is rendered""" app = init_app with app.test_client() as client: with captured_templates(app) as templates: # GET request to the route response = client.get("/") assert response.status_code == 200 assert len(templates) == 1 # ['auth/login.html'] # Template 'login' was rendered template, context = templates[-1] assert template.name == "auth/login.html" assert isinstance(context["form"], FlaskForm) # Login admin and try to access login view response_login_admin = login_user(client, "admin", "test") assert response_login_admin.status_code == 200 response_get_login_view = client.get("/") assert len( templates) == 2 # ['auth/login.html', 'admin/admin.html'] template, context = templates[-1] assert response_get_login_view.status_code == 302 assert template.name == "admin/admin.html" responce_logout_admin = logout_user(client) assert len( templates ) == 3 # ['auth/login.html', 'admin/admin.html', 'auth/login.html'] assert responce_logout_admin.status_code == 200 assert current_user.is_anonymous # Login user and try to access login view response_login_admin = login_user(client, "john", "test") assert response_login_admin.status_code == 200 response_get_login_view = client.get("/") assert len(templates) == 4 # [..., 'cabinet/cabinet.html'] template, context = templates[-1] assert response_get_login_view.status_code == 302 assert template.name == "cabinet/cabinet.html"
def test_admin_form_buttons(init_app): """Make sure all buttons work""" app = init_app with captured_templates(app) as templates: with app.test_client() as client: response_login_user = login_user(client, "admin", "test") assert response_login_user.status_code == 200 assert current_user.is_authenticated # Search response_press_search_button = client.post( "/admin/", data=dict(username="******", search_button=True), follow_redirects=True, ) assert response_press_search_button.status_code == 200 assert session["_username_search_form"] == "john" # Register response_press_register_button = client.post( "/admin/", data=dict(username="******", register_button=True), follow_redirects=True, ) template, context = templates[-1] assert response_press_register_button.status_code == 200 assert template.name == "auth/register.html" # Activate state response_press_activate_button = client.post( "/admin/", data=dict(username="******", activate_button=True), follow_redirects=True, ) usr = User.query.filter_by(username=session.get("_username_search_form")).first() assert response_press_activate_button.status_code == 200 assert usr.state == State.activated_state.value # Deactivate state response_press_deactivate_button = client.post( "/admin/", data=dict(username="******", deactivate_button=True), follow_redirects=True, ) usr = User.query.filter_by(username=session.get("_username_search_form")).first() assert response_press_deactivate_button.status_code == 200 assert usr.state == State.deactivated_state.value # Delete response_press_delete_button = client.post( "/admin/", data=dict(username="******", delete_button=True), follow_redirects=True, ) assert response_press_delete_button.status_code == 200 assert not User.query.filter_by(username=session.get("_username_search_form")).first()
def test_login_view(init_app): """Make sure login works""" app = init_app with app.test_client() as client: with captured_templates(app) as templates: # Login admin responce_login_admin = login_user(client, "admin", "test") assert responce_login_admin.status_code == 200 assert current_user.username == "admin" assert current_user.is_authenticated # Redirect to admin interface template, context = templates[0] assert template.name == "admin/admin.html" # Logout admin to continue testing responce_logout_admin = logout_user(client) assert responce_logout_admin.status_code == 200 assert current_user.is_anonymous # Wrong login response_login_bad_login = login_user(client, "john1", "test") assert response_login_bad_login.status_code == 200 assert current_user.is_anonymous # Wrong password response_login_bad_password = login_user(client, "john", "test1") assert response_login_bad_password.status_code == 200 assert current_user.is_anonymous # Successful login response_login = login_user(client, "john", "test") assert response_login.status_code == 200 assert current_user.is_authenticated # User is authenticated response_login_view = client.get("/") assert response_login_view.status_code == 302
def test_register_new_user(init_app): """Make sure registration form is worked""" app = init_app with captured_templates(app) as templates: with app.test_client() as client: # Login admin response_login_user = login_user(client, "admin", "test") assert response_login_user.status_code == 200 assert current_user.is_authenticated and current_user.username == "admin" # Username 'test' doesn't exists assert not User.query.filter_by(username="******").first() # Register new user response_register_view = client.post( "/admin/register", data=dict( name="Test Surname", phone="+380989898988", email="*****@*****.**", username="******", password="******", tariff=Tariffs.tariff_50m.value["tariff_name"], address="St. Fairy, 23", ), follow_redirects=True, ) template, context = templates[-1] assert response_register_view.status_code == 200 assert User.query.filter_by(username="******").all() assert template.name == "admin/admin.html" # Try to register new NON UNIQUE user response_register_view = client.post( "/admin/register", data=dict( name="Test Surname", phone="+380989898988", email="*****@*****.**", username="******", password="******", tariff=Tariffs.tariff_50m.value["tariff_name"], address="St. Fairy, 23", ), follow_redirects=True, ) template, context = templates[-1] assert response_register_view.status_code == 200 assert len(User.query.filter_by(username="******").all()) == 1 assert template.name == "auth/register.html"
def test_logout(init_app): """Tests logout current user""" app = init_app with app.test_client() as client: # Login user 'john' response_login = login_user(client, "john", "test") assert response_login.status_code == 200 assert current_user.is_authenticated assert current_user.username == "john" # Logout user 'john' response_logout = logout_user(client) assert response_logout.status_code == 200 assert current_user.is_anonymous
def test_register_view(init_app): """Tests for the '/admin/register route""" app = init_app with captured_templates(app) as templates: with app.test_client() as client: # User is not authenticated response_register_view = client.get("/admin/register") assert response_register_view.status_code == 302 assert current_user.is_anonymous # Authenticate user response_login_user = login_user(client, "admin", "test") assert response_login_user.status_code == 200 assert current_user.is_authenticated # Get register view response_register_view_user_authenticated = client.get("/admin/register") assert response_register_view_user_authenticated.status_code == 200 # Template 'admin' was rendered template, context = templates[-1] assert template.name == "auth/register.html" # Logout user response_logout_user = logout_user(client) assert response_logout_user.status_code == 200 assert current_user.is_anonymous # Authenticate usual user and try to access /admin/register response_login_admin = login_user(client, "john", "test") assert response_login_admin.status_code == 200 assert current_user.is_authenticated and current_user.username != "admin" response_cabinet_view_access_for_admin = client.get("/admin/register") assert response_cabinet_view_access_for_admin.status_code == 302