#!/usr/share/ucs-test/runner python3
## -*- coding: utf-8 -*-
## desc: recreate team in azure
## tags: [apptest]
## exposure: dangerous
## packages:
##   - univention-office365


"""
This test is used to check the team recreation in Azure.
- use the o365domain connection
- set the UCR variable office365/groups/sync=yes
- create user in UDM
- create group in UDM with UniventionMicrosoft365Team=1 and UniventionMicrosoft365GroupOwners=user_dn
- Check the recreation of the team in Azure (It's using the wait_for_seconds decorator to wait for the team to be created up to 500 seconds)
"""
from __future__ import print_function

import time

import univention.testing.ucr as ucr_test
import univention.testing.udm as udm_test
import univention.testing.utils as utils
from univention.config_registry import handler_set

import helpers.office365_test_helpers as o365helpers
from univention.office365.microsoft.account import AzureAccount
from univention.office365.microsoft.core import MSGraphApiCore
from univention.office365.microsoft.objects.azureobjects import GroupAzure

with utils.AutomaticListenerRestart():
	with udm_test.UCSTestUDM() as udm:
		with ucr_test.UCSTestConfigRegistry() as ucr:
			ucr.load()

			alias = 'o365domain'

			core = MSGraphApiCore(AzureAccount(alias))

			handler_set(["office365/groups/sync=yes"])
			utils.restart_listener()

			print("*** Creating members ***")
			user_dn, username = o365helpers.create_team_member(udm, ucr, alias)
			print("*** Creating Team ***")
			team_dn, team_name = o365helpers.create_team(udm, ucr, owner_dn=user_dn, users=[user_dn])

			team = o365helpers.check_team_created(core, team_name)
			if not team:
				utils.fail("Team {!r} was not created in Azure.".format(team_name))
			group_azure = GroupAzure.get_by_name(core, team_name)

			group_azure.delete()

			udm.modify_object(
				modulename="groups/group",
				dn=team_dn,
				set=dict(
					description="Modified description"
				),
				check_for_drs_replication=True
			)
			team = o365helpers.check_team_created(core, team_name)



			print("*** All went well.")
