#!/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 univention.testing.strings import random_string

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 _auth(chrome, user, password):
        chrome.reload()
        chrome.goto_portal()
        chrome.click_portal_tile(u'Relution')
        time.sleep(2)
        chrome.enter_input('username', user.properties['username'])
        chrome.enter_input('password', password)
        chrome.click_element('button[id="BTN_LOGIN"]')
        time.sleep(5)


def _login_ok(chrome, user, password='univention'):
        _auth(chrome, user, password)
        chrome.click_element('button[id="MENU_USER"]')
        chrome.click_element('div[id="MENU_LOGOUT"]')
        print("LOGIN OK")


def _login_fail(chrome, user, password='univention'):
        _auth(chrome, user, password)
        el = chrome.driver.find_element_by_id('BTN_LOGIN')
        assert el is not None
        print("LOGIN FAIL")


def test_enabled_user(chrome, users):
        with chrome.capture('relution_enabled_user'):
                user_name = random_string()
                enabled_user = users(user_name, {'relutionActivated': True, 'mailPrimaryAddress': f'{user_name}@example.com'})
                _login_ok(chrome, enabled_user)


def test_enabled_user_wrong_password(chrome, users):
        with chrome.capture('relution_enabled_user_wrong_password'):
                user_name = random_string()
                enabled_user = users(user_name, {'relutionActivated': True, 'mailPrimaryAddress': f'{user_name}@example.com'})
                _login_fail(chrome, enabled_user, password='wrongpw')


def test_disabled_user(chrome, users):
        with chrome.capture('relution_disabled_user'):
                user_name = random_string()
                disabled_user = users(user_name, {'relutionActivated': False, 'mailPrimaryAddress': f'{user_name}@example.com'})
                _login_fail(chrome, disabled_user)


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