#!/usr/share/ucs-test/runner python
## desc: ucs-school-workinggroup-module-check
## roles: [domaincontroller_master, domaincontroller_backup]
## tags: [apptest]
## exposure: dangerous
## packages: [ucs-school-umc-groups]

from essential.workgroup import Workgroup
from univention.lib.umc_connection import UMCConnection
import univention.testing.ucr as ucr_test
import univention.testing.ucsschool as utu


def main():
	ucr = ucr_test.UCSTestConfigRegistry()
	ucr.load()
	host = ucr.get('hostname')
	connection = UMCConnection(host)
	with utu.UCSTestSchool()  as schoolenv:
		schoolName, oudn = schoolenv.create_ou(displayName='')
		tea, teadn = schoolenv.create_user(schoolName, is_teacher=True)
		stu, studn = schoolenv.create_user(schoolName)
		memberListdn = [teadn, studn]
		connection.auth('Administrator' ,'univention')

		# 1 creating empty workgroup
		emptyGroup = Workgroup(schoolName, umcConnection=connection)
		emptyGroup.create()

		# 2 creating unempty workgroup
		group = Workgroup(
				schoolName,
				umcConnection=connection,
				members=memberListdn)
		group.create()

		# 3 checking the created workgroup and its file share object in ldap
		group.checkExistance(
				expected_group_result=True,
				expected_share_result=True)

		# 4 checking if the atrriputes for the group is correct in ldap
		group.checkAttr()

		# 5 should fail: creating a new working group with a duplicate name
		group2 = Workgroup(
				schoolName, 
				name = group.name,
				umcConnection=connection)
		group2.create(doubleNameAllowed=True)

		# 6 add members to group
		emptyGroup.addMembers(memberListdn)

		# 7 checking if the atrriputes for the emptygroup is correct in ldap
		emptyGroup.checkAttr()

		# 8 remove members from a group
		group.removeMembers([memberListdn[0]])

		# 9 checking if the atrriputes for the group is correct in ldap
		group.checkAttr()

		# 10 remove the group
		group.remove()

		# 11 check if the object is removed from ldap
		group.checkExistance(
				expected_group_result=False,
				expected_share_result=False)


if __name__ == '__main__':
	main()
