#!/usr/share/ucs-test/runner python
## -*- coding: utf-8 -*-
## desc: list OX contexts and check CSV output
## tags: [apptest]
## exposure: dangerous
## packages:
##   - univention-ox
## bugs: [43199]

import univention.testing.strings as uts
import univention.testing.utils as utils
from univention.ox.oxlistener import OxListener
from univention.ox.listener_tools import ox_list_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(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}
	default_context = OxListener.get_default_context_id()
	print('Found contexts in OX: {}'.format(context_ids))
	if {context_id, default_context} - context_ids:
		utils.fail('Could not find both default context ({}) and new context ({}) in OX.'.format(
			default_context, 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')
