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


"""
This test is used to check the team deletion in Azure.
- use the o365domain connection
- set the UCR variable office365/groups/sync=yes
- create user in UDM and set UniventionMicrosoft365Team=1
- create group in UDM with UniventionMicrosoft365Team=1 and UniventionMicrosoft365GroupOwners=user_dn
- check the team creation in Azure
- check the team deletion in Azure after the UDM context manager deletes the created team
"""
import univention.testing.utils as utils
import univention.testing.ucr as ucr_test
import univention.testing.udm as udm_test
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

with utils.AutomaticListenerRestart():
	with ucr_test.UCSTestConfigRegistry() as ucr:
		with udm_test.UCSTestUDM() as udm:
			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 ***")
			group_dn, group_name = o365helpers.create_team(udm, ucr, owner_dn=user_dn, users=[user_dn])

			print("*** Checking Team ***")
			created_team = o365helpers.check_team_created(core, group_name)

		print("*** Checking Team Archived***")
		created_team = o365helpers.check_team_archived(core, group_name)

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