#!/usr/share/ucs-test/runner python3
## -*- coding: utf-8 -*-
## desc: list OX contexts and check CSV output
## tags: [apptest]
## roles: [domaincontroller_master,domaincontroller_backup]
## exposure: dangerous
## packages:
##   - univention-ox
##   - python3-univention-ox-soap-api-client
## bugs: [43199]

import univention.testing.strings as uts
import univention.testing.utils as utils
from univention.ox.listener_tools import ox_list_context, default_context
import essential.common

with essential.common.OXTestEnv() as oxenv, essential.common.OXTestEmptyQueue():
	context_id = uts.random_int(500, 5000)

	oxenv.create_context(context_id)
	oxenv.check_if_context_exists_in_ldap(context_id)
	oxenv.check_if_context_exists_in_ox_via_csv(context_id)
	oxenv.check_if_context_exists_in_ox_via_soap(context_id)

	context_csv = list(ox_list_context(oxenv.ox_master_admin, oxenv.ox_master_admin_password))
	context_ids = {row['id'] for row in context_csv}
	print('Found contexts in OX: {!r}'.format(context_ids))
	if {str(context_id), str(default_context)} - context_ids:
		utils.fail('Could not find both default context ({!r}) and new context ({!r}) in OX.'.format(
			str(default_context), str(context_id)))

	print('Checking CSV output for expected values...')
	expected = dict()
	for cid in context_ids:
		expected[cid] = {
			'id': cid,
			'filestore_id': r'[0-9]+',
			'filestore_name': '{}_ctx_store'.format(cid),
			'enabled': 'true',
			'max_quota': r'[1-9][0-9]*',
			'used_quota': r'[0-9]+',
			'name': 'context{}'.format(cid),
			'lmappings': r'(.+,)?context{}(,.+)?'.format(cid),
			'attributes': '',
		}
	oxenv.check_csv(context_csv, expected, 'id')
