#!/usr/share/ucs-test/runner python
## -*- coding: utf-8 -*-
## desc: Set ISO birthday (2016-06-23) for new users (Bug #41642)
## tags: [apptest,ucsschool,skip_in_multiserver,ucsschool_import]
## roles: [domaincontroller_master]
## exposure: dangerous
## packages:
##   - ucs-school-import
## bugs: [41642]

import time
import copy
import pprint
import univention.testing.strings as uts
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_iso_birthday()
		"""
		Bug #41642: Create/modify a new user for each role:
        - set ISO birthday (2016-06-23) for each user type
		"""
		for role in ('student', 'teacher', 'staff', 'teacher_and_staff'):
			self.log.info('*** Importing a new single user with role %r with multivalue attributes', role)

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

			config = copy.deepcopy(self.default_config)
			config.update_entry('sourceUID', source_uid)
			config.update_entry('csv:incell-delimiter:default', ';')
			config.update_entry('csv:mapping:Benutzername', 'username')
			config.update_entry('csv:mapping:recordUID', 'record_uid')
			config.update_entry('csv:mapping:birthday', 'birthday')
			config.update_entry('scheme:recordUID', '<username>')
			config.update_entry('sourceUID', source_uid)
			config.update_entry('user_role', role)

			class BirthdayPerson(Person):

				def __init__(self, school, role):
					Person.__init__(self, school, role)
					self.birthday = time.strftime('%Y-%m-%d')

				def map_to_dict(self, value_map):
					result = Person.map_to_dict(self, value_map)
					result[value_map.get('birthday', '__EMPTY__')] = self.birthday
					return result

				def expected_attributes(self):
					result = Person.expected_attributes(self)
					result['birthday'] = [self.birthday]

			# create person with ISO birthday (today)
			person = BirthdayPerson(self.ou_A.name, role)
			person.update(record_uid=person.username, source_uid=source_uid)
			fn_csv = self.create_csv_file(person_list=[person], mapping=config['csv']['mapping'])
			fn_config = self.create_config_json(config=config)
			# start import
			self.run_import(['-c', fn_config, '-i', fn_csv])

			self.log.debug('User object %r:\n%s', person.dn, pprint.PrettyPrinter(indent=2).pformat(self.lo.get(person.dn)))
			person.verify()

			# modify person and set birthday to new year's day
			person.update(birthday='2016-01-01')
			fn_csv = self.create_csv_file(person_list=[person], mapping=config['csv']['mapping'])
			fn_config = self.create_config_json(config=config)
			# start import
			self.run_import(['-c', fn_config, '-i', fn_csv])

			self.log.debug('User object %r:\n%s', person.dn, pprint.PrettyPrinter(indent=2).pformat(self.lo.get(person.dn)))
			person.verify()


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


if __name__ == '__main__':
	main()
