summaryrefslogtreecommitdiffstats
path: root/pykolab/tests/contacts.py
blob: bc60bbd3b5a0ca2f515d26077d3fcb548c601172 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# -*- coding: utf-8 -*-
# Copyright 2010-2011 Kolab Systems AG (http://www.kolabsys.com)
#
# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3 or, at your option, any later version
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

import calendar
import datetime
import imaplib
import os
import random
import time

import pykolab

from pykolab.constants import *
from pykolab.tests.constants import *
from pykolab.translate import _

log = pykolab.getLogger('pykolab.tests.contacts')

conf = pykolab.getConf()

imap = pykolab.imap

class ContactsItem(object):
    def __init__(self, item_num=0, total_num=1, folder=None, user=None):
        """
            A contact item is created from a template.

            The attributes that can be modified are set to defaults first.
        """

        if user == None:
            user = TEST_USERS[random.randint(0,(len(TEST_USERS)-1))]

        # Used for some randomization
        self.item_num = item_num
        self.total_num = total_num

        # Initial event data
        self.kolab_contact_given_name = "John"
        self.kolab_contact_last_name = "von Test"
        self.kolab_contact_email_str = "john@von.test"
        self.kolab_contact_mobile_number = "+31612345678"

        from_user = TEST_USERS[random.randint(0,(len(TEST_USERS)-1))]

        self.from_name_str = "%s %s" %(from_user['givenname'].capitalize(),from_user['sn'].capitalize())
        self.from_email_str = "%(givenname)s@%(domain)s" %(from_user)

        self.kolab_creation_date = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())

        self.rfc_2822_sent_date = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime())

        self.to_name_str = "%s %s" %(user['givenname'].capitalize(),user['sn'].capitalize())
        self.to_email_str = "%(givenname)s@%(domain)s" %(user)

        self.uid = "%s.%s" %(str(random.randint(1000000000,9999999999)),str(random.randint(0,999)).zfill(3))

        if folder:
            self.mailbox = folder
        else:
            self.mailbox = "Contacts"

        self.randomize_contact()

    def randomize_contact(self):
        """
            Randomize the contact's information.

        """
        pass

    def __str__(self):
        for tpl_file_location in [ '/usr/share/kolab/tests/kaddress-contact.tpl', './share/tests/kaddress-contact.tpl', '../share/tests/kaddress-contact.tpl' ]:
            if os.path.isfile(tpl_file_location):
                tpl_file = open(tpl_file_location, 'r')
                tpl_orig = tpl_file.read()
                tpl_file.close()
                break
        return tpl_orig % self.__dict__

def create_items(conf, num=None, folder=None):
    for item in TEST_ITEMS:
        if item['name'] == 'contacts':
            info = item

    if num:
        info['number'] = int(num)

    log.debug(_("Creating %d Contacts") %(info['number']), level=3)

    alloc_uids = []

    for user in eval(conf.get('testing','users')):
        if conf.use_mail:
            pass
        elif conf.use_lmtp:
            pass
        elif conf.use_imap:
            imap.connect(login=False)
            imap.login("%(givenname)s.%(sn)s@%(domain)s" %(user), user['password'])
        else:
            pass

        #print "Running for user %(givenname)s@%(domain)s" %(user)
        item_num = 0

        while item_num < int(info['number']):
            log.debug(_("Creating Contact item number %d") %(item_num+1), level=5)

            item = ContactsItem(item_num=(item_num+1), total_num=num, folder=folder, user=user)

            if not item.uid in alloc_uids:
                alloc_uids.append(item.uid)
            else:
                continue

            msg = str(item)

            if conf.use_mail:
                log.debug(_("Sending UID message %s through SMTP targeting user %s@%s") %(item.uid,user['givenname'],user['domain']), level=9)

            elif conf.use_lmtp:
                log.debug(_("Sending UID message %s through LMTP targeting user %s@%s") %(item.uid,user['givenname'],user['domain']), level=9)

            elif conf.use_imap:
                log.debug(_("Saving UID message %s to IMAP (user %s, folder %s)") %(item.uid,user['givenname'],item.mailbox), level=9)
                imap.imap.m.append(item.mailbox, '', imaplib.Time2Internaldate(time.time()), msg)
            else:
                log.debug(_("Somehow ended up NOT sending these messages"), level=9)

            item_num +=1