#!/usr/share/ucs-test/runner python3
## -*- coding: utf-8 -*-
## desc: list, create, retrieve, modify and delete group in Azure AD with multi connection support
## tags: [apptest]
## exposure: dangerous
## packages:
##   - univention-office365

"""
For Groups
No UDM involved currently.
This test is used to list, create, retrieve, modify and delete group in Azure AD with multi connection support.
The next operations are performed on the two preconfigured connections to Azure:
- list groups and store
- create group
- retrieve group by id
- check the group's attributes
- modify group
- retrieve group again and check the attributes
- list groups again and store
- check the difference between the two lists > 0 and the id of the last one added must be the same as the last retrieved.
- delete group
- try to retrieve the group again and check that it is not found, or it's disabled.
"""

import time

import univention.testing.ucr as ucr_test
import univention.testing.utils as utils
from typing import Dict, Any

from helpers.office365_test_helpers import AzureDirectoryTestGroups, azure_group_args, setup_logging
# from univention.office365.azure_handler import AzureHandler, ResourceNotFoundError
# from univention.office365.azure_auth import AzureAuth, AzureADConnectionHandler
from univention.office365.microsoft.account import AzureAccount
from univention.office365.microsoft.core import MSGraphApiCore
from univention.office365.microsoft.exceptions.core_exceptions import MSGraphError
from univention.office365.microsoft.objects.azureobjects import GroupAzure
from univention.office365.ucr_helper import UCRHelper

# TODO: too similar to the operation done with the users, join both.
def test_grp(grp, args):
	# type: (GroupAzure, Dict[str, Any]) -> None
	for k, v in args.items():
		if v != getattr(grp, k):
			utils.fail("Property '{}' is '{}', but should be '{}'.".format(k, getattr(grp, k), v))


logger = setup_logging()
adconnection_aliases = UCRHelper.get_adconnection_aliases()
initialized_adconnections = [adconnection_alias for adconnection_alias in adconnection_aliases if AzureAccount(adconnection_alias).is_initialized()]
assert initialized_adconnections, "run ./01_initialize_external_testenv first!"

print("*** adconnection_aliases={!r}.".format(adconnection_aliases))
print("*** initialized_adconnections={!r}.".format(initialized_adconnections))

for adconnection_alias in initialized_adconnections:
	print("*** Running for adconnection_alias={!r}.".format(adconnection_alias))

	core = MSGraphApiCore(AzureAccount(adconnection_alias))

	groups1 = GroupAzure.list(core)

	print("*** list_groups() returned:")
	for g in groups1:
		print("id: '{objectId}' mailNickname: '{mailNickname}' mail: '{mail}' description: '{description}'".format(objectId=g.id, mailNickname=g.mailNickname, mail=g.mail, description=g.description))

	args = azure_group_args()
	print("*** create_group(), args={}".format(args))
	new_group1 = GroupAzure(**args)
	new_group1.set_core(core)
	new_group1.create()
	time.sleep(10)

	with AzureDirectoryTestGroups(core, [new_group1]):
		new_group2 = GroupAzure.get(core, new_group1.id)
		test_grp(new_group2, args)

		args = azure_group_args()
		print("*** modify_group(), args={}".format(args))
		new_group2.update(GroupAzure(**args))
		time.sleep(10)
		new_group3 = GroupAzure.get(core, new_group2.id)
		test_grp(new_group3, args)

		groups2 = GroupAzure.list(core)
		old_ids = set([group.id for group in groups1])
		new_ids = set([group.id for group in groups2])
		diff = new_ids - old_ids
		if len(diff) == 0:
			utils.fail("Test group disappeared. groups1={}, groups2={}".format(groups1, groups2))
		elif len(diff) > 1 or diff.pop() != new_group3.id:
			utils.fail("Unexpected group(s) found. groups1={}, groups2={}".format(groups1, groups2))

		print("*** delete_group({})".format(new_group3.id))
		new_group3.deactivate(rename=True)
		time.sleep(10)

		print("*** list_groups(objectid={})".format(new_group3.id))
		try:
			groups3 = GroupAzure.get(core, new_group3.id)
			if (groups3.description == "deleted group" and
				groups3.displayName.startswith("ZZZ_deleted_") and
				groups3.mailNickname.startswith("ZZZ_deleted_")):
				print("OK: group was deactivated.")
			else:
				utils.fail("Group was not deleted: id: '{objectId}' mailNickname: '{mailNickname}' mail: '{mail}'"
					"description: '{description}'".format(objectId=groups3.id, mailNickname=groups3.mailNickname, mail=groups3.mail, description=groups3.description))
		except MSGraphError:
			print("OK: group was deleted (404).")

	print("*** All went well for Azure AD connection alias {!r}.".format(adconnection_alias))
print("*** All went well for all in {!r}.".format(initialized_adconnections))
