#!/usr/share/ucs-test/runner python
## desc: ucs-school-helpdesk-check
## roles: [domaincontroller_master]
## tags: [apptest]
## exposure: safe
## packages:
##    - ucs-school-umc-helpdesk

from univention.config_registry import handler_set
from univention.lib.umc_connection import UMCConnection
import sys, time
import univention.testing.ucr as ucr_test
import univention.testing.ucsschool as utu
import univention.testing.utils as utils

def main():
	# initializing mail data
	message = time.time() * 1000
	message = '%.30f' % message
	category = 'Sonstige'
	passwd = 'univention'
	with utu.UCSTestSchool()  as schoolenv:
		# creating ou & a teacher
		schoolName, oudn = schoolenv.create_ou(displayName='')
		userName, userdn = schoolenv.create_user(schoolName, is_teacher=True, password=passwd)
		with ucr_test.UCSTestConfigRegistry() as ucr:
			handler_set(['ucsschool/helpdesk/recipient=root@localhost'])
			host = ucr.get('hostname')
			connection = UMCConnection(host)
			connection.auth(userName, passwd)
			params = {
				'username': userName,
				'school': schoolName,
				'category': category,
				'message': message,
				}
			result = connection.request('helpdesk/send', params)
			if not result:
				utils.fail('Message was not sent successfully')
	# time out for waiting for the message to be delivered  in seconds
	timeOut = 10
	# periodically checking the receipt of the same sent mail
	for i in range(0, timeOut):
		if message in open('/var/mail/systemmail').read():
			returnValue = 0
			break
		else:
			continue
		returnValue = 1
		time.sleep(1)
	return returnValue


if __name__ == '__main__':
		sys.exit(main())
