#!/usr/share/ucs-test/runner python3
## desc: Create users/user and test "functional" object flag
## bugs: [34395]
## tags: [udm]
## roles: [domaincontroller_master]
## exposure: careful
## packages:
##   - univention-config
##   - univention-directory-manager-tools


import univention.testing.utils as utils
import univention.testing.udm as udm_test
import atexit
import subprocess

if __name__ == '__main__':
	license_before = subprocess.Popen(['univention-license-check'], stdout=subprocess.PIPE).communicate()[0]

	# Since the S4 connector uses a object based synchronization,
	# it is a problem to change the same object in short intervals,
	# see https://forge.univention.org/bugzilla/show_bug.cgi?id=35336
	if utils.s4connector_present():
		atexit.register(utils.start_s4connector)
		utils.stop_s4connector()

	with udm_test.UCSTestUDM() as udm:
		# create user and check its existence
		user_dn = udm.create_user(check_for_drs_replication=False, wait_for=False)[0]
		utils.verify_ldap_object(user_dn)
		stdout = subprocess.Popen([udm_test.UCSTestUDM.PATH_UDM_CLI_CLIENT, 'users/user', 'list'], stdout=subprocess.PIPE).communicate()[0]
		if not user_dn.lower().encode('UTF-8') in stdout.lower():
			utils.fail('Cannot find user DN %s in output of "udm users/user list":\n%s' % (user_dn, stdout))

		# perform a license check
		license_after = subprocess.Popen(['univention-license-check'], stdout=subprocess.PIPE).communicate()[0]
		if license_before == license_after:
			utils.fail('License check failed to detect normal user')

		# add 'functional' flag to user
		lo = utils.get_ldap_connection()
		lo.modify(user_dn, (('univentionObjectFlag', b'', b'functional'),))
		utils.wait_for_replication()
		stdout = subprocess.Popen([udm_test.UCSTestUDM.PATH_UDM_CLI_CLIENT, 'users/user', 'list'], stdout=subprocess.PIPE).communicate()[0]
		if user_dn.lower().encode('UTF-8') in stdout.lower():
			utils.fail('"udm users/user list" still finds user object with functional flag')

		# perform a license check
		license_after = subprocess.Popen(['univention-license-check'], stdout=subprocess.PIPE).communicate()[0]
		if license_before != license_after:
			utils.fail('License check detected to "functional" user')

		# remove 'functional' flag to user
		lo.modify(user_dn, (('univentionObjectFlag', b'functional', b''),))
		utils.wait_for_replication()
		stdout = subprocess.Popen([udm_test.UCSTestUDM.PATH_UDM_CLI_CLIENT, 'users/user', 'list'], stdout=subprocess.PIPE).communicate()[0]
		if not user_dn.lower().encode('UTF-8') in stdout.lower():
			utils.fail('Cannot find user DN %s in output of "udm users/user list" after removing flag:\n%s' % (user_dn, stdout))

		# perform a license check
		license_after = subprocess.Popen(['univention-license-check'], stdout=subprocess.PIPE).communicate()[0]
		if license_before == license_after:
			utils.fail('License check failed to detect normal user')
