~rajeevs1992/mailman.client/mailmancli

« back to all changes in this revision

Viewing changes to src/mailmanclient/cli/tests/test_user.py

  • Committer: Rajeev S
  • Date: 2014-06-16 17:37:41 UTC
  • Revision ID: rajeevs1992@gmail.com-20140616173741-kffpv988jp6p6pj5
- adds describe feature that describes the
  instance of scope, made by extending the
  `show` functionality
- adds subscribe and unsubscribe feature to the
  user scope
- adds unittests for the users scope

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
# Copyright (C) 2010-2014 by the Free Software Foundation, Inc.
 
4
#
 
5
# This file is part of mailman.client.
 
6
#
 
7
# mailman.client is free software: you can redistribute it and/or modify it
 
8
# under the terms of the GNU Lesser General Public License as published by the
 
9
# Free Software Foundation, version 3 of the License.
 
10
#
 
11
# mailman.client is distributed in the hope that it will be useful, but
 
12
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
13
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
14
# License for more details.
 
15
#
 
16
# You should have received a copy of the GNU Lesser General Public License
 
17
# along with mailman.client.  If not, see <http://www.gnu.org/licenses/>.
 
18
 
 
19
import os
 
20
import sys
 
21
import unittest
 
22
try:
 
23
    from core.users import Users, UserException
 
24
except:
 
25
    sys.path = [os.path.abspath(os.path.dirname(__file__)) +
 
26
                '/../../cli/'] + sys.path
 
27
    from core.users import Users, UserException
 
28
from lib.mailman_utils import MailmanUtils
 
29
 
 
30
 
 
31
class TestCreateUser(unittest.TestCase):
 
32
 
 
33
    utils = MailmanUtils()
 
34
    new_users = []
 
35
 
 
36
    def setUp(self):
 
37
        self.client = self.utils.connect()
 
38
        self.users = Users(self.client)
 
39
        self.test_user = 'a@' + self.utils.get_random_string(5) + '.org'
 
40
        self.new_users.append(self.test_user)
 
41
        self.client.create_user(email=self.test_user,
 
42
                                password='abcdefgh',
 
43
                                display_name='abc')
 
44
        self.test_list = self.client.lists[0]
 
45
        self.test_list.subscribe(self.test_user)
 
46
 
 
47
        # Test if user created else setup fails.
 
48
        self.client.get_user(self.test_user)
 
49
 
 
50
    def test_normal_create(self):
 
51
        args = {}
 
52
        new_user = 'a@' + self.utils.get_random_string(5) + '.org'
 
53
        self.new_users.append(new_user)
 
54
        args['email'] = new_user
 
55
        args['password'] = 'abc'
 
56
        args['name'] = 'abc'
 
57
        self.users.create(args)
 
58
        self.assertRaises(UserException, self.users.create, args)
 
59
 
 
60
    def test_create_existent_user(self):
 
61
        args = {}
 
62
        args['email'] = self.test_user
 
63
        args['password'] = 'abc'
 
64
        args['name'] = 'abc'
 
65
        self.assertRaises(UserException, self.users.create, args)
 
66
 
 
67
    def tearDown(self):
 
68
        for user in self.new_users:
 
69
            try:
 
70
                u = self.client.get_user(user)
 
71
                u.delete()
 
72
            except Exception as e:
 
73
                print e
 
74
 
 
75
 
 
76
class TestSubscription(unittest.TestCase):
 
77
 
 
78
    utils = MailmanUtils()
 
79
    new_users = []
 
80
    domain_list = []
 
81
 
 
82
    def setUp(self):
 
83
        self.client = self.utils.connect()
 
84
        self.users = Users(self.client)
 
85
 
 
86
        self.test_domain = self.utils.get_new_domain_name()
 
87
        self.domain_list.append(self.test_domain)
 
88
        self.client.create_domain(self.test_domain)
 
89
 
 
90
        self.test_user = self.utils.get_random_string(8) + '@domain.org'
 
91
        self.new_users.append(self.test_user)
 
92
        self.client.create_user(email=self.test_user,
 
93
                                password='abcdefgh',
 
94
                                display_name='abc')
 
95
        self.client.get_user(self.test_user)
 
96
        domain = self.client.get_domain(self.test_domain)
 
97
        self.test_list = (self.utils.get_random_string(8) +
 
98
                          '@' + self.test_domain)
 
99
        domain.create_list(self.test_list.split('@')[0])
 
100
 
 
101
        # Test if user created else setup fails.
 
102
        self.client.get_user(self.test_user)
 
103
 
 
104
    def test_subscribe_to_list(self):
 
105
        print self.test_user
 
106
        _list = self.client.get_list(self.test_list)
 
107
        nmembers = len(_list.members)
 
108
        args = {}
 
109
        args['users'] = ['a@b.com', 'b@c.com', 'd@e.com']
 
110
        self.new_users.extend(args['users'])
 
111
        args['list_name'] = self.test_list
 
112
        args['quiet'] = False
 
113
        self.users.subscribe(args)
 
114
        self.assertEqual(nmembers + 3, len(_list.members))
 
115
 
 
116
    def tearDown(self):
 
117
        for i in self.new_users:
 
118
            try:
 
119
                user = self.client.get_user(i)
 
120
                user.delete()
 
121
            except Exception:
 
122
                pass
 
123
        for i in self.domain_list:
 
124
            try:
 
125
                self.client.delete_domain(i)
 
126
            except Exception:
 
127
                pass