#!/usr/share/ucs-test/runner python
## -*- coding: utf-8 -*-
## desc: check group renaming
## tags: [apptest]
## exposure: dangerous
## packages:
##   - univention-ox

from ldap import explode_dn
import univention.testing.udm as testing_udm
import univention.testing.ucr as testing_ucr
import univention.testing.strings as uts
import univention.testing.utils as utils
from univention.ox.api.http.http_api_client import OXHttpApi, LoginError
from essential.common import OXTestEmptyQueue, OXTestEnv, ox_default_context, CSV_GROUP_ROW_ID
from univention.ox.wait_for_ox_server import wait_for_ox_server


def get_group_id_with_http_api(group_name):
	groups = api.list_groups(group_name)
	for grp in groups:
		if grp.get('name') == group_name:
			print('*** Found group %r (ID %r) using the HTTP API.' % (group_name, grp['id']))
			return grp['id']
	utils.fail('Could not find group %r with HTTP API.' % group_name)


wait_for_ox_server()
with testing_udm.UCSTestUDM() as udm, testing_ucr.UCSTestConfigRegistry() as ucr, OXTestEmptyQueue(), OXTestEnv() as oxenv:
	mail_domain = ucr['mail/hosteddomains'].split()[0]

	print('*** Creating OX user...')
	ox_user_password = uts.random_string()
	ox_user_dn, ox_username = udm.create_user(
		mailPrimaryAddress='{}@{}'.format(uts.random_username(), mail_domain),
		password=ox_user_password,
		isOxUser='OK')

	try:
		api = OXHttpApi(ox_username, ox_user_password)
	except LoginError as exc:
		utils.fail('Could not login with correct password.')
	print('*** OK: logged in with correct password.')

	print('*** Creating OX group...')
	group_dn, groupname = udm.create_group(
		users=[ox_user_dn],
		isOxGroup='OK')

	oxenv.check_if_group_exists_in_context(groupname, ox_default_context, [ox_username])
	ox_group_id_api = str(get_group_id_with_http_api(groupname))
	ox_group_id = oxenv.get_ox_group(groupname, ox_default_context)[CSV_GROUP_ROW_ID]
	if ox_group_id != ox_group_id_api:
		utils.fail('ID from CSV (%r) different to ID from HTTP (%r).' % (ox_group_id, ox_group_id_api))
	new_group_name = uts.random_username()

	print('*** Renaming group %r to %r...' % (groupname, new_group_name))
	udm.modify_object('groups/group', dn=group_dn, name=new_group_name)
	new_group_dn = 'cn={},{}'.format(new_group_name, ','.join(explode_dn(group_dn)[1:]))
	udm._cleanup['groups/group'].append(new_group_dn)

	oxenv.check_if_group_exists_in_context(new_group_name, ox_default_context, [ox_username])
	ox_group_id_new_api = str(get_group_id_with_http_api(new_group_name))
	new_ox_group_id = oxenv.get_ox_group(new_group_name, ox_default_context)[CSV_GROUP_ROW_ID]
	if new_ox_group_id != ox_group_id_new_api:
		utils.fail('ID from CSV (%r) different to ID from HTTP (%r).' % (new_ox_group_id, ox_group_id_new_api))

	if ox_group_id != new_ox_group_id:
		utils.fail('ID of OX group changed: ox group was removed ({}, {}) and created ({}, {}), not renamed.'.format(
			groupname, ox_group_id, new_group_name, new_ox_group_id))
	print('*** OK: ID of OX group did not changed: old ox group ({}) ID: {} and new group ({}) ID: {}.'.format(
			groupname, ox_group_id, new_group_name, new_ox_group_id))
