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

"""
KNOWN ISSUES:
It's one of the mos common flaky tests.
In the described operations over the group, in some batches, the check for the right number of members fails after long retries.
The creation of the members in the group needs to be synced into teams.
This is an asynchronous operation, and can take as long as 15 minutes (more in our own experience).
For some reason, sometimes, some user looks to get incomplete data (no principal email is set in azure, and this looks somehow related).
Other times all goes well.
Useful links:
· https://docs.microsoft.com/en-us/graph/api/team-post?view=graph-rest-1.0&tabs=http#example-4-create-a-team-from-group
· https://docs.microsoft.com/en-us/graph/api/resources/teamsasyncoperation?view=graph-rest-1.0
· https://stackoverflow.com/questions/61607263/microsoft-team-creation-using-rest-api-taking-more-time#comment109256187_61607263
· https://sharepoint.stackexchange.com/questions/227716/team-and-groups-members-out-of-sync
· https://docs.microsoft.com/en-us/graph/known-issues

This test is used to check the team members change in Azure.
- Use o365domain connection
- Create user1, user2, user3 in UDM with UniventionMicrosoft365Team=1
- Create group in UDM with UniventionMicrosoft365Team=1 and UniventionMicrosoft365GroupMembers=[user2_dn, user3_dn] and UniventionMicrosoft365GroupOwners=user_dn
- Check that the team have 3 members (It's using the wait_for_seconds decorator to wait for the team to be created up to 500 seconds)
- Create another 3 users in UDM with UniventionMicrosoft365Team=1 and members of the group
- Check that the team have 6 members
- Remove 2 users from the UDM group
- Check that the team have 4 members
"""
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 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, _ = o365helpers.create_team_member(udm, ucr, alias)
			user2_dn, _ = o365helpers.create_team_member(udm, ucr, alias)
			user3_dn, _ = o365helpers.create_team_member(udm, ucr, alias)

			print("*** Creating Team ***")
			group_dn, group_name = o365helpers.create_team(udm, ucr, owner_dn=user_dn, users=[user2_dn, user3_dn])
			created_team = o365helpers.check_team_created(core, group_name)
			team_id = created_team.id

			print("*** Checking that team has 3 members ***")
			team_members = o365helpers.check_team_members(core, team_id, 3)

			print("*** Creating new members ***")
			user_dn, _ = o365helpers.create_team_member(udm, ucr, alias, group_dn)
			user2_dn, _ = o365helpers.create_team_member(udm, ucr, alias, group_dn)
			user3_dn, _ = o365helpers.create_team_member(udm, ucr, alias, group_dn)

			print("*** Checking that team has 6 members ***")
			_ = o365helpers.check_team_members(core, team_id, 6)

			print("*** Remove 2 users ***")
			udm.remove_object('users/user', dn=user2_dn)
			udm.remove_object('users/user', dn=user3_dn)

			print("*** Checking that team has 4 members ***")
			_ = o365helpers.check_team_members(core, team_id, 4)
			print("*** All went well.")