#!/usr/share/ucs-test/runner python
## -*- coding: utf-8 -*-
## desc: Test username scheme with <:lower> and [COUNTER2] (Bug 41645)
## tags: [apptest,ucsschool,skip_in_multiserver,ucsschool_import]
## roles: [domaincontroller_master]
## exposure: dangerous
## packages:
##   - ucs-school-import
## bugs: [41645]

import copy
import pprint
import univention.testing.strings as uts
import univention.testing.utils as utils
from essential.importusers_cli_v2 import CLI_Import_v2_Tester
from essential.importusers import Person


class Test(CLI_Import_v2_Tester):

	def __init__(self):
		super(Test, self).__init__()
		self.ou_B = None
		self.ou_C = None

	def test(self):  # formally test_lowercase_username_with_counter()
		"""
		Bug #41645: a username scheme with <:lower> and [COUNTER2]
		"""
		for scheme in ["ALWAYSCOUNTER", "COUNTER2"]:
			for role in ('student', 'teacher', 'staff', 'teacher_and_staff'):
				self.log.info('*** Importing a new single user with role %r from templates with scheme %r', role, scheme)

				source_uid = 'sourceUID-%s' % (uts.random_string(),)
				record_uid = uts.random_name()

				config = copy.deepcopy(self.default_config)
				config.update_entry('sourceUID', source_uid)
				config.update_entry('csv:mapping:employeeNumber', 'employeeNumber')
				config.update_entry('csv:mapping:DBID', 'record_uid')
				config.update_entry('scheme:recordUID', '<record_uid>')
				config.update_entry('user_role', role)
				config.update_entry('scheme:username:default', "<:lower><:umlauts><firstname>[0:6].<lastname>[0:3]a[{}]".format(scheme))
				config.update_entry('scheme:email', "<:umlauts><firstname:lower>.<lastname:lower>@example.com")
				config.update_entry('scheme:employeeNumber', "The user's name is <firstname:upper> <lastname> <description>")

				person = Person(self.ou_A.name, role)
				person.update(record_uid=record_uid, source_uid=source_uid, username=None, mail=None, description=uts.random_name())
				fn_csv = self.create_csv_file(person_list=[person], mapping=config['csv']['mapping'])
				fn_config = self.create_config_json(config=config)
				# save ldap state for later comparison
				self.save_ldap_status()

				# start import
				self.run_import(['-c', fn_config, '-i', fn_csv])

				# check for new users in LDAP
				new_users = [x for x in self.diff_ldap_status().new if x.startswith('uid=')]
				if len(new_users) != 1:
					self.log.error('Invalid number of new users! Found new objects: %r', new_users)
					self.fail('Stopping here')

				# update dn+username of person and verify LDAP attributes
				mail = '%s.%s@example.com' % (person.firstname.lower(), person.lastname.lower())
				person.update(dn=new_users[0], record_uid=record_uid, source_uid=source_uid, mail=mail)
				person.verify()

				username_prefix = '%s.%sa' % (person.firstname[0:6], person.lastname[0:3])
				if person.username != "{}{}".format(username_prefix, "1" if scheme == "ALWAYSCOUNTER" else ""):
					self.fail('username %r is not expected string %r.' % (person.username, "{}{}".format(username_prefix, "1" if scheme == "ALWAYSCOUNTER" else "")))
				self.log.info('Username %r is not expected with string %r.', person.username, "{}{}".format(username_prefix, "1" if scheme == "ALWAYSCOUNTER" else ""))

				self.log.info('Testing mailPrimaryAddress and description...')
				values = {
					'employeeNumber': ["The user's name is %s %s %s" % (person.firstname.upper(), person.lastname, person.description)],
				}
				utils.verify_ldap_object(person.dn, expected_attr=values, strict=True, should_exist=True)
				self.log.debug('User object %r:\n%s', person.dn, pprint.PrettyPrinter(indent=2).pformat(self.lo.get(person.dn)))


def main():
	tester = Test()
	try:
		tester.run()
	finally:
		tester.cleanup()


if __name__ == '__main__':
	main()
