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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
# Copyright 2009 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Test EntitlementExchange and EntitlementImporter."""
__metaclass__ = type
from cStringIO import StringIO
import logging
import unittest
from zope.testing.loghandler import Handler
from lp.registry.scripts.entitlement import (
EntitlementExchange,
EntitlementImporter,
InvalidFormat,
UnsupportedVersion,
)
from lp.testing.layers import LaunchpadZopelessLayer
class EntitlementExchangeTestCase(unittest.TestCase):
"""Test EntitlementExchange methods."""
layer = LaunchpadZopelessLayer
def test_preprocessData(self):
"""The preprocessor verifies the header and removes comments."""
# Wrong header
data = ("# bad format data\n"
"more data")
in_file = StringIO(data)
self.assertRaises(
InvalidFormat, EntitlementExchange._preprocessData, in_file)
# Invalid version
data = ("# Entitlement exchange format version 0\n"
"more data")
in_file = StringIO(data)
self.assertRaises(
UnsupportedVersion, EntitlementExchange._preprocessData, in_file)
# Only one line should remain after processing
data = ("# Entitlement exchange format version 1\n"
"# comment line\n"
" # another comment\n"
"'name', 'date' #valid line")
in_file = StringIO(data)
processed = EntitlementExchange._preprocessData(in_file)
self.assertEqual(len(processed.split('\n')), 1)
class EntitlementImporterTestCase(unittest.TestCase):
"""Test EntitlementImport methods."""
layer = LaunchpadZopelessLayer
def setUp(self):
"""Setup the test environment and retrieve useful instances."""
self.log = logging.getLogger("test_entitlement")
self.log.setLevel(logging.INFO)
self.handler = Handler(self)
self.handler.add(self.log.name)
def tearDown(self):
"""Teardown the test environment."""
self.layer.txn.commit()
self.handler.close()
def _getImporterAndReader(self, data):
in_file = StringIO(data)
importer = EntitlementImporter(self.log)
return importer, EntitlementExchange.readerFactory(in_file)
def _testCreate(self, data):
importer, reader = self._getImporterAndReader(data)
return importer.createEntitlements(reader)
def _testUpdate(self, data):
importer, reader = self._getImporterAndReader(data)
return importer.updateEntitlements(reader)
def test_manipulateEntitlement(self):
"""Test creating and updating entitlements."""
def test_wrongVersion(self):
"""Wrong version."""
data = r"""# Entitlement exchange format version 0"""
in_file = StringIO(data)
importer = EntitlementImporter(self.log)
self.assertRaises(UnsupportedVersion,
EntitlementExchange.readerFactory,
in_file)
def test_successfulInsert(self):
"""Successfully insert an entitlement."""
data = ("# Entitlement exchange format version 1\n"
",A-100, lifeless, 10, 100, 0, 2007-06-14, , , "
"keybuk, keybuk, 20,")
results = self._testCreate(data)
self.assertEqual(len(results), 1)
def test_insertUsingNonExistentPerson(self):
"""Attempt to insert using a non-existent person."""
data = ("# Entitlement exchange format version 1\n"
",A-100, persona_non_grata, 10, 100, 0, 2007-06-14, , , "
"keybuk, keybuk, 20,")
results = self._testCreate(data)
self.handler.assertLogsMessage(
"[E0] Person 'persona_non_grata' "
"is not found.", level=logging.ERROR)
def test_omitQuota(self):
"""Omit the quota and get an error."""
data = ("# Entitlement exchange format version 1\n"
",A-100, lifeless, 10, , 0, 2007-06-14, , , "
"keybuk, keybuk, 20,")
results = self._testCreate(data)
self.handler.assertLogsMessage(
"[E0] A required key is missing: quota.",
level=logging.ERROR)
def test_omitPerson(self):
"""Omit the person and get an error."""
data = ("# Entitlement exchange format version 1\n"
",A-100, , 10, 100, 0, 2007-06-14, , , "
"keybuk, keybuk, 20,")
results = self._testCreate(data)
self.handler.assertLogsMessage(
"[E0] A required key is missing: person_name.",
level=logging.ERROR)
def test_omitExtId(self):
"""Omit the ext_id and get an error."""
data = ("# Entitlement exchange format version 1\n"
",, lifeless, 10, 100, 0, 2007-06-14, , , "
"keybuk, keybuk, 20,")
results = self._testCreate(data)
self.handler.assertLogsMessage(
"[E0] A required key is missing: ext_id.",
level=logging.ERROR)
def test_omitEntitlementType(self):
"""Omit the entitlement_type and get an error."""
data = ("# Entitlement exchange format version 1\n"
",A-100, lifeless, , 100, 0, 2007-06-14, , , "
"keybuk, keybuk, 20,")
results = self._testCreate(data)
self.handler.assertLogsMessage(
"[E0] A required key is missing: entitlement_type.",
level=logging.ERROR)
def test_omitState(self):
"""Omit the state and get an error."""
data = ("# Entitlement exchange format version 1\n"
",A-100, lifeless, 10, 100, 0, 2007-06-14, , , "
"keybuk, keybuk, ,")
results = self._testCreate(data)
self.handler.assertLogsMessage(
"[E0] A required key is missing: state.",
level=logging.ERROR)
def test_updateWithInvalidId(self):
"""Update using an invalid entitlement id."""
data = ("# Entitlement exchange format version 1\n"
"9999,A-100,kiko, ,1500, , , , , , , , ")
results = self._testUpdate(data)
self.handler.assertLogsMessage(
"[E0] Invalid entitlement id: 9999",
level=logging.ERROR)
def test_updateChangingPerson(self):
"""Changing the person, which isn't allowed."""
data = ("# Entitlement exchange format version 1\n"
",A-100, lifeless, 10, 100, 0, 2007-06-14, , , "
"keybuk, keybuk, 20,")
results = self._testCreate(data)
self.assertEqual(len(results), 1)
valid_id = results[0].get('id')
data = ("# Entitlement exchange format version 1\n"
" %s, A-100, kiko, , 1500, , , , , , , , " %
valid_id)
results = self._testUpdate(data)
self.handler.assertLogsMessage(
"[E0] You may not change the person for the entitlement.",
level=logging.INFO)
|