Statistics
| Branch: | Tag: | Revision:

root / invitations / tests.py @ 4f8e7c6d

History | View | Annotate | Download (7.1 kB)

1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29
from datetime import datetime, timedelta
30

    
31
from django.test import TestCase
32
from django.test.client import Client
33

    
34
import invitations
35
from synnefo.db.models import SynnefoUser, Invitations
36
from django.conf import settings
37

    
38

    
39
class InvitationsTestCase(TestCase):
40

    
41
    token = '46e427d657b20defe352804f0eb6f8a2'
42

    
43
    def setUp(self):
44
        self.client = Client()
45

    
46

    
47
    def test_add_invitation(self):
48
        """
49
            Tests whether invitations can be added
50
        """
51
        inv = self._add_invitation()
52
        source = inv.source
53

    
54
        # Re-adding an existing invitation
55
        try:
56
            invitations.add_invitation(source, u'', "test@gmail.com")
57
            self.assertTrue(False)
58
        except invitations.AlreadyInvited:
59
            self.assertTrue(True)
60
        except Exception:
61
            self.assertTrue(False)
62

    
63
        # Hit the invitation limit
64
        try:
65
            source.max_invitations = 0
66
            source.save()
67
            self._add_invitation()
68
        except invitations.TooManyInvitations:
69
            self.assertTrue(True)
70
        except Exception:
71
            self.assertTrue(False)
72

    
73

    
74
    def test_get_invitee_level(self):
75
        """
76
            Checks whether invitation levels and their limits are being respected
77
        """
78
        source = SynnefoUser.objects.filter(auth_token = self.token)[0]
79

    
80
        self.assertEqual(invitations.get_user_inv_level(source), -1)
81

    
82
        inv = invitations.add_invitation(source, "Test", "test@gmail.com")
83
        self.assertEqual(inv.target.max_invitations,
84
                         settings.INVITATIONS_PER_LEVEL[0])
85
        self.assertEqual(invitations.get_user_inv_level(inv.target), 0)
86

    
87
        inv = invitations.add_invitation(inv.target, "Test2", "test2@gmail.com")
88
        self.assertEqual(invitations.get_user_inv_level(inv.target), 1)
89
        self.assertEqual(inv.target.max_invitations,
90
                         settings.INVITATIONS_PER_LEVEL[1])
91

    
92

    
93
    def test_invitation_login(self):
94
        """
95
            Basic login by invitation checks
96
        """
97
        inv = self._add_invitation()
98
        user = inv.target
99

    
100
        url = invitations.enconde_inv_url(inv)
101

    
102
        resp = self.client.get(url)
103
        self.assertEqual(resp.status_code, 200)
104
        self.assertEquals(resp['X-Auth-Token'], user.auth_token)
105

    
106
        # Invalid login url
107
        url = list(url)
108
        url[35] = '@'
109
        resp = self.client.get("".join(url))
110
        self.assertEqual(resp.status_code, 404)
111

    
112

    
113
    def test_relogin_with_expired_token(self):
114
        """
115
            Checks whether a user can login when his auth token has expired
116
        """
117
        inv = self._add_invitation()
118
        user = inv.target
119

    
120
        # Expire the user's token
121
        user.auth_token_expires = datetime.now()
122
        user.save()
123

    
124
        url = invitations.enconde_inv_url(inv)
125
        resp = self.client.get(url)
126
        self.assertEqual(resp.status_code, 200)
127
        self.assertEquals(resp['X-Auth-Token'], user.auth_token)
128

    
129
        user = SynnefoUser.objects.get(uniq = "test@gmail.com")
130
        self.assertTrue(user.auth_token_expires > datetime.now())
131

    
132
        # Invitation valid less than max allowed auth token expiration time
133
        valid = timedelta(days = (settings.INVITATION_VALID_DAYS - 10))
134
        inv.created = inv.created - valid
135
        inv.save()
136

    
137
        user.auth_token_expires = datetime.now()
138
        user.save()
139

    
140
        url = invitations.enconde_inv_url(inv)
141
        resp = self.client.get(url)
142
        self.assertEqual(resp.status_code, 200)
143
        inv = Invitations.objects.filter(target = user)[0]
144
        valid = timedelta(days = settings.INVITATION_VALID_DAYS)
145
        self.assertTrue(inv.created + valid >= user.auth_token_expires)
146

    
147

    
148
    def test_remove_invitation(self):
149
        """
150
        Tests the remove invitation method
151
        """
152
        inv = self._add_invitation()
153

    
154
        try:
155
            invitations.remove_invitation(inv)
156
            user = SynnefoUser.objects.filter(uniq = 'test@gmail.com').count()
157
            self.assertEquals(user, 0)
158

    
159
            inv = Invitations.objects.filter(target = user).count()
160
            self.assertEquals(inv, 0)
161
        except Exception:
162
            self.assertTrue(False)
163

    
164
        try:
165
            invitation = "Invitation"
166
            invitations.remove_invitation(inv)
167
        except Exception:
168
            self.assertTrue(False)
169

    
170

    
171
    def test_resend_invitation(self):
172
        """
173
        Tests the resend invitation method
174
        """
175
        inv = self._add_invitation()
176

    
177
        resp = self.client.post("/invitations/resend",
178
                                {'invid':inv.id},
179
                                **{'HTTP_X_AUTH_TOKEN': self.token})
180
        self.assertEquals(resp.status_code, 200)
181

    
182

    
183
        resp = self.client.post("/invitations/resend",
184
                                {'invid':'1;delete from db_invitations;'},
185
                                **{'HTTP_X_AUTH_TOKEN': self.token})
186
        self.assertEquals(resp.status_code, 400)
187

    
188
        resp = self.client.post("/invitations/resend",
189
                                {'invid':inv.id},
190
                                **{'HTTP_X_AUTH_TOKEN': inv.target.auth_token})
191
        self.assertEquals(resp.status_code, 400)
192

    
193

    
194
    def _add_invitation(self):
195
        source = SynnefoUser.objects.filter(auth_token = self.token)[0]
196
        invitations.add_invitation(source, "Test", "test@gmail.com")
197

    
198
        # Check whether the invited user has been added to the database
199
        user = SynnefoUser.objects.get(uniq = "test@gmail.com")
200

    
201
        self.assertNotEquals(user, None)
202
        self.assertEqual(user.uniq, 'test@gmail.com')
203

    
204
        inv = Invitations.objects.filter(target = user)[0]
205
        self.assertNotEquals(inv, None)
206

    
207
        return inv