#!/usr/bin/env python3
"""SSO login test on Sentry"""

import logging

from eewebtests import EEWebTest

webtest = EEWebTest()

webtest.parser.add_argument("--sentry-url", help="Sentry URL")
webtest.sso_login()

# Get Sentry homepage
sentry_url = webtest.get_option("sentry", "url", default="https://sentry.example.tld/")
logging.info("Get Sentry homepage (%s)", sentry_url)
webtest.get(sentry_url)

# Locate and click on "Login with SAML2" button
webtest.locate("button.btn-login-saml2", label="Login with SAML2 button", click=True)

# Check authenticated
logging.info("Check authenticated by checking user name in user menu")
webtest.locate("button[data-test-id='sidebar-dropdown']", label="user menu button", click=True)
auth_user_name = webtest.locate(
    "a[href='/settings/account/details/']",
    label="user name in menu",
    non_empty=True,
)

if webtest.sso_name not in auth_user_name.text:
    webtest.clean_exit(
        f"WARNING - User name retrieved from Sentry ({auth_user_name.text}) is not the expected "
        f"one ({webtest.sso_name})",
    )

webtest.clean_exit(f"Successfully authenticated as {webtest.sso_name} on Sentry", status="OK")
