#!/usr/share/ucs-test/runner python
## desc: computerroom module base checks
## roles: [domaincontroller_master, domaincontroller_slave]
## tags: [apptest, ucsschool]
## exposure: dangerous
## packages: [ucs-school-umc-computerroom]

from essential.computerroom import Room, Computers
from univention.lib.umc_connection import UMCConnection
import univention.testing.ucr as ucr_test
import univention.testing.ucsschool as utu


def main():
	with utu.UCSTestSchool() as schoolenv:
		with ucr_test.UCSTestConfigRegistry() as ucr:
			school, oudn = schoolenv.create_ou(name_edudc=ucr.get('hostname'))
			tea1, tea1_dn = schoolenv.create_user(school, is_teacher=True)
			tea2, tea2_dn = schoolenv.create_user(school, is_teacher=True)
			open_ldap_co = schoolenv.open_ldap_connection()

			# importing random 9 computers
			computers = Computers(open_ldap_co, school, 3, 3, 3)
			created_computers = computers.create()
			created_computers_dn = computers.get_dns(created_computers)

			# setting an empty computer room
			room1 = Room(school)
			# setting 2 computer rooms contain the created computers
			room2 = Room(school, host_members=created_computers_dn[0:4])
			room3 = Room(school, host_members=created_computers_dn[4:9])

			# Creating the rooms
			for room in [room1, room2, room3]:
				schoolenv.create_computerroom(
						school,
						name=room.name,
						description=room.description,
						host_members=room.host_members
						)

			# Checking empty room properties
			umc_connection1 = UMCConnection(ucr.get('hostname'))
			umc_connection1.auth(tea1, 'univention')
			room1.checK_room_aquire(umc_connection1, 'EMPTY_ROOM')
			room1.check_room_user(umc_connection1, None)

			# Checking non-empty room properties
			umc_connection2 = UMCConnection(ucr.get('hostname'))
			umc_connection2.auth(tea2, 'univention')
			room2.checK_room_aquire(umc_connection2, 'OK')
			room2.check_room_user(umc_connection1, tea2)
			room2.check_room_computers(umc_connection2,	created_computers_dn[0:4])


			# switching room for tea2
			room3.checK_room_aquire(umc_connection2, 'OK')
			room3.check_room_user(umc_connection1, tea2)
			room3.check_room_computers(umc_connection2,	created_computers_dn[4:9])

if __name__ == '__main__':
	main()
