summaryrefslogtreecommitdiffstats
path: root/tests/functional/resource_func.py
blob: 36bd1bd9879672296c3f9eda4f2f0a7ed5002482 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import pykolab

from pykolab import wap_client

conf = pykolab.getConf()


def resource_add(type, cn, members=None, owner=None, **kw):
    if type is None or type == '':
        raise Exception

    if cn is None or cn == '':
        raise Exception

    resource_details = {
        'cn': cn,
        'kolabtargetfolder': "shared/Resources/" + cn + "@example.org",
        'uniquemember': members,
        'owner': owner,
        'ou': 'ou=resources,dc=example,dc=org'
    }

    resource_details.update(kw)

    bind_dn = conf.get('ldap', 'bind_dn')
    bind_pw = conf.get('ldap', 'bind_pw')
    domain = conf.get('kolab', 'primary_domain')
    result = wap_client.authenticate(bind_dn, bind_pw, domain)

    type_id = 0
    resource_types = wap_client.resource_types_list()

    for key in resource_types['list'].keys():
        if resource_types['list'][key]['key'] == type:
            type_id = key

    if type_id == 0:
        raise Exception

    resource_type_info = resource_types['list'][type_id]['attributes']

    params = {}

    for attribute in resource_type_info['form_fields'].keys():
        attr_details = resource_type_info['form_fields'][attribute]

        if isinstance(attr_details, dict):
            if 'optional' not in attr_details or attr_details['optional'] is False or attribute in resource_details:
                params[attribute] = resource_details[attribute]
        elif isinstance(attr_details, list):
            params[attribute] = resource_details[attribute]

    fvg_params = params
    fvg_params['object_type'] = 'resource'
    fvg_params['type_id'] = type_id
    fvg_params['attributes'] = [attr for attr in resource_type_info['auto_form_fields'].keys() if attr not in params]

    result = wap_client.resource_add(params)
    result['dn'] = "cn=" + result['cn'] + ",ou=Resources,dc=example,dc=org"
    return result


def purge_resources():
    bind_dn = conf.get('ldap', 'bind_dn')
    bind_pw = conf.get('ldap', 'bind_pw')
    domain = conf.get('kolab', 'primary_domain')
    result = wap_client.authenticate(bind_dn, bind_pw, domain)

    resources = wap_client.resources_list()

    for resource in resources['list']:
        wap_client.resource_delete({'id': resource})

    # from tests.functional.purge_imap import purge_imap
    # purge_imap()