#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Copyright 2019 Univention GmbH
#
# https://www.univention.de/
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# Binary versions of this program provided by Univention to you as
# well as other copyrighted, protected or trademarked materials like
# Logos, graphics, fonts, specific documentations and configurations,
# cryptographic keys etc. are subject to a license agreement between
# you and Univention and not subject to the GNU AGPL V3.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3; if not, see
# <https://www.gnu.org/licenses/>.
#

import os.path
import importlib
import time
import sys

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait

from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys

os.environ["UCSTEST_SELENIUM_USER_AGENT"] = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
test_lib = os.environ.get('UCS_TEST_LIB', 'univention.testing.apptest')

try:
    test_lib = importlib.import_module(test_lib)
except ImportError:
    print('Could not import {}. Maybe set $UCS_TEST_LIB'.format(test_lib))
    sys.exit(1)


def test_owncloud(chrome, users):
    with chrome.capture('qd-consent'):
        chrome.goto_portal()
        chrome.click_portal_tile(u'QD Consent')
        chrome.get("/qd-consent/auth/login")
        wait(chrome.driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "input[autocomplete='username']")))
        chrome.enter_input_element("input[autocomplete='username']", 'super.admin@qua-dev.com')
        ActionChains(chrome.driver).send_keys(Keys.RETURN).perform()
        wait(chrome.driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[autocomplete='current-password']")))

        chrome.enter_input_element("input[autocomplete='current-password']", 'qua-dev.com')
        ActionChains(chrome.driver).send_keys(Keys.RETURN).perform()
        wait(chrome.driver, 10).until(EC.presence_of_element_located((By.XPATH, '//button[@id="rightMenuTrigger"]')))
        chrome.driver.find_element_by_xpath('//button[@id="rightMenuTrigger"]').click()
        wait(chrome.driver, 10).until(EC.presence_of_element_located((By.XPATH, '//div[text()="Logout"]')))
        chrome.driver.find_element_by_xpath('//div[text()="Logout"]').click()
        time.sleep(2)


if __name__ == '__main__':
    test_lib.run_test_file(__file__)