#!/usr/share/ucs-test/runner python3
## desc: Override default tab with settings/extended_attribute
## tags: [udm]
## roles: [domaincontroller_master]
## exposure: careful
## packages:
##   - univention-config
##   - univention-directory-manager-tools


from __future__ import print_function
import subprocess
import univention.testing.strings as uts
import univention.testing.utils as utils
import univention.testing.udm as udm_test


def main():
	with udm_test.UCSTestUDM() as udm:
		properties = {
			'name': uts.random_name(),
			'shortDescription': uts.random_string(),
			'CLIName': uts.random_name(),
			'module': 'users/user',
			'objectClass': 'univentionFreeAttributes',
			'ldapMapping': 'univentionFreeAttribute15',
			'tabName': 'Certificate',
			'overwriteTab': '1'
		}

		udm.create_object('settings/extended_attribute', position=udm.UNIVENTION_CONTAINER, **properties)

		module_help_text = subprocess.Popen([udm.PATH_UDM_CLI_CLIENT, properties['module']], stdout=subprocess.PIPE).communicate()[0].decode('UTF-8').splitlines()

		for i in range(0, len(module_help_text)):
			if module_help_text[i] == '  %s:' % properties['tabName']:
				if not properties['CLIName'] in module_help_text[i + 1]:
					utils.fail('Could not find attribute CLI name under tab')
				try:
					if not module_help_text[i + 2].endswith(':'):
						print('--> ', module_help_text[i + 2])
						utils.fail('Tab not overridden')
				except IndexError:
					# no more help, tab is overwritten
					pass
				return

		utils.fail('Tab not found')


if __name__ == '__main__':
	main()
