#!/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
import os.path
import importlib
import time
import sys
import random
import 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 _login(chrome, user, password=None):
	chrome.goto_portal()
	chrome.click_portal_tile(u'Wekan')
	time.sleep(2)
	if isinstance(user, str):
		chrome.enter_input('at-field-username_and_email', user)
	else:
		chrome.enter_input('at-field-username_and_email', user.properties['username'])
	chrome.enter_input('at-field-password', password if password else 'univention')
	chrome.click_element('#at-btn')
	time.sleep(10)


def _logout(chrome):
	chrome.click_element('#header-user-bar')
	chrome.click_element('.js-logout')
	time.sleep(1)


def test_enabled_user(chrome, users):
	with chrome.capture('wekan_enabled_user'):
		# test login with enabled user
		enabled_user = users('enabled_user', {'wekanActivated': True, 'mailPrimaryAddress': 'enabled@user.de'})
		_login(chrome, enabled_user)
		_logout(chrome)

		# test login with wrong password
		_login(chrome, enabled_user, password='wrongpw')
		try:
			# should not work
			_logout(chrome)
			raise AssertionError
		except AssertionError:
			pass

		# test login after password change
		new_password = 'Univention.1'
		enabled_user = users('enabled_user', {'wekanActivated': True, 'mailPrimaryAddress': 'enabled@user.de', 'password': new_password})
		_login(chrome, enabled_user, password=new_password)
		_logout(chrome)


def test_disabled_user(chrome, users):
	with chrome.capture('wekan_disabled_user'):
		# test login with disabled user; should not work
		disabled_user = users('disabled_user', {'wekanActivated': False, 'mailPrimaryAddress': 'disabled@user.de'})
		_login(chrome, disabled_user)
		try:
			# should not work
			_logout(chrome)
			raise AssertionError
		except AssertionError:
			pass


def test_admin_user(chrome):
	with chrome.capture('wekan_admin_user'):
		# test login with wekan admin account
		password_file = '/etc/wekan-admin.secret'
		password = ''
		with open(password_file) as f:
			password = f.read()
		_login(chrome, 'wekan-admin', password=password)
		_logout(chrome)


def test_features(chrome, users):
	with chrome.capture('wekan_features'):
		enabled_user = users('feature_user', {'wekanActivated': True, 'mailPrimaryAddress': 'feature@user.de'})
		_login(chrome, enabled_user)

		# create board
		board_name = ''.join(random.choice(string.ascii_uppercase) for _ in range(10))
		chrome.click_element('#content > div > ul > li.js-add-board > a')
		chrome.enter_input_element('input.js-new-board-title', board_name)
		chrome.click_element('input[value="Create"]')
		time.sleep(1)

		# create list
		list_name = ''.join(random.choice(string.ascii_uppercase) for _ in range(10))
		chrome.click_element('.open-list-composer')
		chrome.enter_input_element('input.list-name-input', list_name)
		chrome.click_element('div.edit-controls > button.confirm')

		# create card
		card_content = ''.join(random.choice(string.ascii_uppercase) for _ in range(10))
		chrome.click_element('a.js-card-composer')
		chrome.enter_input_element('textarea.minicard-composer-textarea', card_content)
		chrome.click_element('div.add-controls > button.confirm')

		# confirm card exists
		assert chrome.find_first('div.minicards').find_elements_by_xpath('//p[contains(text(), "%s")]' % card_content)

		_logout(chrome)


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