~mailmanweb-django/mailmanweb/systers_django

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
# -*- coding: utf-8 -*-
# Copyright (C) 1998-2010 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman 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, either version 3 of the License, or (at your option)
# any later version.
#
# GNU Mailman 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 General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.

import unittest
from systers_django.models import MailingList
from django.conf import settings
from httplib import HTTPConnection
import json
from mailman_django.mailman_rest_client import MailmanRESTClient

class MailingListTestCaseError(Exception):
    """Error class for MailingListTestCase.
    """
    pass

class MailingListTestCase(unittest.TestCase):
    """Test class for Systers' MailingLists.
    """

    def setUp(self):
        """
        Function to create Systers mailing lists for setting up the
        test.
        """
        try:
            self.connection = HTTPConnection(settings.REST_SERVER)
        except:
            raise MailingListTestCaseError('Could not connect to server')

        self.lists = []
        # should work although not good to set _mm_listname...
        self.lists.append(MailingList.objects.create(_mm_listname="test1@example.com", 
                                                     questions_essay="This is just a test."))
        # should not work - no domain
        try:
            self.lists.append(MailingList.objects.create(_mm_listname="test2", 
                                      questions_essay="This is just a test."))
        except IndexError:
            print "" # first insert an empty line for readability
            print "Creating a list without a domain failed as it should."

    def testCreated(self):
        """
        Function testing if the created mailing list was really 
        created.
        """
        for _list in self.lists:
            try:
                self.assertNotEquals(self._test(_list._mm_listname), 'Not good')
            except AssertionError:
                msg = "Failed - list '%s' " % _list._mm_listname
                msg += "was not created properly."
                raise AssertionError(msg)

            # delete the list to clean up
            try:
                c = MailmanRESTClient('localhost:8001')
                the_list = c.get_list(_list._mm_listname)
                # get the parts of the list necessary to delete it
                parts = _list._mm_listname.split('@')
                domain = the_list.get_domain(parts[1])
                domain.delete_list(parts[0])
            except:
                print "Could not delete list '%s'." % _list._mm_listname

    def _get_url(self, path):
        """Function getting a specific URL.
        """
        headers = {
            "User-Agent": "MailmanRESTClient",
            "Accept": "text/plain", 
        }
        try:
            # sending a (connection) request to rest server
            self.connection.request('GET', path, None, headers)
        except:
            raise MailingListTestCaseError('Could not request connection.')
        try:
            response = self.connection.getresponse()
            raw_data = response.read()
            if len(raw_data) == 0:
                raise MailingListTestCaseError('raw_data is 0.')
            data = json.loads(raw_data)
        except:
            msg = 'Could not load raw_data for json object.'
            raise MailingListTestCaseError(msg)
        finally:
            self.connection.close()
        return data

    def _test(self, listname):
        """
        Function performing the test by sending a request to the rest
        server to see if the list was created.
        """
        try:
            response = self._get_url('/3.0/lists/' + listname)
        except MailingListTestCaseError, e:
            print e
            response = 'Not good'
        return response