#!/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 importlib
import time
import sys

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_send_email(chrome, users, udm):
	domain = udm.get_ldap_base()
	domain = domain.split('dc=', 1)[1]
	domain = domain.replace(',dc=', '.')
	user1 = users('ox1', {'isOxUser': True, 'oxDisplayName': 'ox1', 'mailPrimaryAddress': '{}@{}'.format('ox1', domain)})
	user2 = users('ox2', {'isOxUser': True, 'oxDisplayName': 'ox2', 'mailPrimaryAddress': '{}@{}'.format('ox2', domain)})
	body = 'This is a test email body...\nKind regards,\nuser1'
	chrome.goto_portal()
	chrome.click_portal_tile(u'Open-Xchange WebGUI')
	time.sleep(2)
	chrome.enter_input('username', user1.properties['username'])
	chrome.enter_input('password', 'univention')
	chrome.click_element('[name=signin]')
	time.sleep(5)
	chrome.click_element('[data-action="io.ox/mail/actions/compose"]')
	time.sleep(2)
	chrome.enter_input_element('div[data-extension-id=to] input[type=email][placeholder]', user2.properties['mailPrimaryAddress'])
	chrome.enter_input_element('div[data-extension-id=subject] input', 'Test Mail')
	with chrome.switched_frame('div.editor iframe'):
		chrome.enter_input_element('#tinymce', body)
	chrome.click_element('button[data-action=send]')
	time.sleep(1)
	chrome.click_element('div.folder-icon.sent')
	time.sleep(2)
	chrome.click_element('ul.mail-item > li[data-index="0"]')
	time.sleep(1)
	assert chrome.assert_one('h1.subject').text == 'Test Mail'
	assert chrome.assert_one('a.person-to').get_property('title') == user2.properties['mailPrimaryAddress']
	with chrome.switched_frame('section.body iframe'):
		paragraphs = chrome.find_all('div.default-style')
		assert body == '\n'.join(elem.text for elem in paragraphs)
	chrome.click_element('#io-ox-topbar-dropdown-icon')
	chrome.click_element('a[data-name=logout]')
	return chrome

def test_receive_email(chrome, users):
	user1 = users('ox1')
	user2 = users('ox2')
	body = 'This is a test email body...\nKind regards,\nuser1'
	chrome.goto_portal()
	chrome.click_portal_tile(u'Open-Xchange WebGUI')
	time.sleep(2)
	chrome.enter_input('username', user2.properties['username'])
	chrome.enter_input('password', 'univention')
	chrome.click_element('[name=signin]')
	time.sleep(5)
	chrome.click_element('li[data-id="default0/INBOX"]')
	chrome.click_element('ul.mail-item > li[data-index="0"]')
	time.sleep(1)
	assert chrome.assert_one('h1.subject').text == 'Test Mail'
	assert chrome.assert_one('div.from span.address').text == '<{}>'.format(user1.properties['mailPrimaryAddress'])
	with chrome.switched_frame('section.body iframe'):
		paragraphs = chrome.find_all('div.default-style')
		assert body == '\n'.join(elem.text for elem in paragraphs)
	chrome.click_element('#io-ox-topbar-dropdown-icon')
	chrome.click_element('a[data-name=logout]')
	return chrome

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