#!/usr/share/ucs-test/runner /usr/share/ucs-test/selenium
# -*- coding: utf-8 -*-
## desc: Test the password reset module
## roles: [domaincontroller_master]
## tags: [apptest,ucsschool,ucsschool_selenium]
## exposure: dangerous
## packages:
##   - ucs-school-multiserver | ucs-school-singleserver

import univention.testing.ucsschool.ucs_test_school as utu
import univention.testing.utils as utils
from univention.testing import selenium
from univention.admin import localization
import time

translator = localization.translation('ucs-test-selenium')
_ = translator.translate


class UMCTester(object):

	def test_umc(self):
		with utu.UCSTestSchool() as schoolenv:
			reset_password = 'uni1vention1'
			schoolname, schooldn = schoolenv.create_ou()
			username, userdn = schoolenv.create_user(schoolname)

			# Reset password for user
			self.selenium.do_login()
			self.selenium.open_module(_('Passwords (students)'), wait_for_standby=False)
			self.selenium.wait_until_standby_animation_appears_and_disappears(appear_timeout=15, disappear_timeout=60)
			self.selenium.enter_input_combobox('school', schoolname)
			self.selenium.submit_input('pattern')
			self.selenium.wait_for_text(username, timeout=60)
			self.selenium.click_checkbox_of_grid_entry(username)
			self.selenium.click_button(_('Reset password'))
			self.selenium.click_text(_('User has to change password on next login'))
			self.selenium.enter_input('newPassword', reset_password)
			lid = utils.get_lid()
			self.selenium.click_button('Reset')
			# wait until there is a new LDAP transaction, otherwise the UMCP command will run
			# in the background and the following login attempt will come too fast
			for i in range(4*30):
				if lid != utils.get_lid():
					break
				time.sleep(0.25)
			self.selenium.end_umc_session()
			self.selenium.do_login(username=username, password=reset_password)
			self.selenium.end_umc_session()

			# Reset password for user and require password reset
			reset_password = 'uni2vention2'
			self.selenium.do_login()
			self.selenium.open_module(_('Passwords (students)'), wait_for_standby=False)
			self.selenium.wait_until_standby_animation_appears_and_disappears(appear_timeout=15, disappear_timeout=60)
			self.selenium.enter_input_combobox('school', schoolname)
			self.selenium.submit_input('pattern')
			self.selenium.wait_for_text(username, timeout=60)
			self.selenium.click_checkbox_of_grid_entry(username)
			self.selenium.click_button(_('Reset password'))
			self.selenium.enter_input('newPassword', reset_password)
			lid = utils.get_lid()
			self.selenium.click_button('Reset')
			# wait until there is a new LDAP transaction, otherwise the UMCP command will run
			# in the background and the following login attempt will come too fast
			for i in range(4*30):
				if lid != utils.get_lid():
					break
				time.sleep(0.25)
			self.selenium.end_umc_session()
			self.selenium.do_login(username, reset_password, check_successful_login=False)
			self.selenium.wait_for_text('The password has expired and must be renewed.', timeout=10)
			self.selenium.end_umc_session()

			# TODO: Test for the proper separation of users regarding schools and working groups?



if __name__ == '__main__':
	with selenium.UMCSeleniumTest() as s:
		umc_tester = UMCTester()
		umc_tester.selenium = s

		umc_tester.test_umc()
