def test_serialize(self): ser = postfixhelper.PFTableSerializer() data = { '#': postfixhelper.TableEntry( None, ['file comments', 'even with a second line'], 0), 'key': postfixhelper.TableEntry('value', ['comments'], 4, True), 'key2': postfixhelper.TableEntry('value', [], 5), 'key3': postfixhelper.TableEntry('value2', [], 7), } expected = textwrap.dedent("""\ # file comments # even with a second line #== Entries for value 'value' # comments #-- key value key2 value #== Entries for value 'value2' key3 value2 """) out = ser.serialize(data) self.assertEqual(out, expected)
def setUp(self): load_empty_config() self.app = postfixhelper.App() self.parser = postfixhelper.create_args_parser(help.Help) self.list_alias = self.parser.parse_args('alias list'.split(' ')) self.users = postfixhelper.PostfixTable('virtual-mailbox-users') self.users['testsender'] = postfixhelper.TableEntry() self.users['testsender1'] = postfixhelper.TableEntry()
def test_get_alias(self): a = self.alias.get_alias('alias@localdomain') expected = postfixhelper.Alias('alias@localdomain', None, None) self.assertEqual(expected.alias, a.alias) self.assertEqual(expected.sender, a.sender) self.assertEqual(expected.inbox, a.inbox) a = self.alias.get_alias('#') # File comment expected = postfixhelper.Alias('#', postfixhelper.TableEntry(), postfixhelper.TableEntry()) self.assertEqual(expected.alias, a.alias) self.assertEqual(expected.sender, a.sender) self.assertEqual(expected.inbox, a.inbox)
def test_create_alias(self): self.alias.add_alias('testalias@localdomain', 'testuser@localdomain', "comment") self.assertEqual( self.alias._virtual_alias['testalias@localdomain'], postfixhelper.TableEntry('testuser@localdomain', ["comment"], sys.maxsize))
def test_singleton_pattern(self): a1 = postfixhelper.PostfixTable('virtual-alias') a2 = postfixhelper.PostfixTable('virtual-alias') b = postfixhelper.PostfixTable('sender-login-maps') # Ensure no table has the test entry self.assertRaises(KeyError, lambda: a1['testvar1']) self.assertRaises(KeyError, lambda: a2['testvar1']) self.assertRaises(KeyError, lambda: b['testvar1']) # Insert data into one table only a1['testvar1'] = postfixhelper.TableEntry('testuser1', [], 999) # Both instances of the same table must hold the same data self.assertEqual(a1['testvar1'], postfixhelper.TableEntry('testuser1', [], 999)) self.assertEqual(a2['testvar1'], postfixhelper.TableEntry('testuser1', [], 999)) # The other table must not self.assertRaises(KeyError, lambda: b['testvar1'])
def test_delete_alias(self): self.alias.add_alias('testalias@localdomain', 'testuser@localdomain', "comment") self.assertEqual( self.alias._virtual_alias['testalias@localdomain'], postfixhelper.TableEntry('testuser@localdomain', ["comment"], sys.maxsize)) self.alias.delete_alias('testalias@localdomain') self.assertRaises( KeyError, lambda: self.alias._sender_login_maps['testalias@localdomain'])
def test_read(self): data = postfixhelper.PostfixTableParser().parse(DATA) self.assertEqual(data['#'], postfixhelper.TableEntry(None, ['A file comment'], 0)) self.assertEqual( data['alias1@domain'], postfixhelper.TableEntry('user1@domain', ['alias comment'], 4)) self.assertEqual(data['Multiline'], postfixhelper.TableEntry('isAlsoPossible', [], 7)) self.assertEqual(data['alias2@domain'], postfixhelper.TableEntry('user1@domain', [], 8)) self.assertEqual( data['alias3@domain'], postfixhelper.TableEntry('user2@domain', ['A comment'], 11)) self.assertEqual( data['deleted'], postfixhelper.TableEntry('user2@domain', [], 12, True)) self.assertEqual( data[None], postfixhelper.TableEntry(None, ['comment at the end'], 13))
def setUp(self): self.alias = self.create_pf_alias() self.users = postfixhelper.PostfixTable('virtual-mailbox-users') self.users['testuser@localdomain'] = postfixhelper.TableEntry()
def test_no_comment(self): data = postfixhelper.PostfixTableParser().parse(NO_COMMENT) self.assertEqual(data['abcde'], postfixhelper.TableEntry('fghij', [], 1)) self.assertRaises(KeyError, lambda: data[None])