Statistics
| Branch: | Tag: | Revision:

root / snf-app / synnefo / invitations / tests.py @ 483c9197

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
    fixtures = ['users']
41
    token = '46e427d657b20defe352804f0eb6f8a2'
42

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

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

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

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

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

    
78
        self.assertEqual(invitations.get_user_inv_level(source), -1)
79

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

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

    
90
    def test_invitation_login(self):
91
        """
92
            Basic login by invitation checks
93
        """
94
        inv = self._add_invitation()
95
        user = inv.target
96

    
97
        url = invitations.enconde_inv_url(inv)
98

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

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

    
109
    def test_relogin_with_expired_token(self):
110
        """
111
            Checks whether a user can login when his auth token has expired
112
        """
113
        inv = self._add_invitation()
114
        user = inv.target
115

    
116
        # Expire the user's token
117
        user.auth_token_expires = datetime.now()
118
        user.save()
119

    
120
        url = invitations.enconde_inv_url(inv)
121
        resp = self.client.get(url)
122
        self.assertEqual(resp.status_code, 200)
123
        self.assertEquals(resp['X-Auth-Token'], user.auth_token)
124

    
125
        user = SynnefoUser.objects.get(uniq = "test@gmail.com")
126
        self.assertTrue(user.auth_token_expires > datetime.now())
127

    
128
        # Invitation valid less than max allowed auth token expiration time
129
        valid = timedelta(days = (settings.INVITATION_VALID_DAYS - 10))
130
        inv.created = inv.created - valid
131
        inv.save()
132

    
133
        user.auth_token_expires = datetime.now()
134
        user.save()
135

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

    
143
    def test_remove_invitation(self):
144
        """
145
        Tests the remove invitation method
146
        """
147
        inv = self._add_invitation()
148

    
149
        try:
150
            invitations.remove_invitation(inv)
151
            user = SynnefoUser.objects.filter(uniq = 'test@gmail.com').count()
152
            self.assertEquals(user, 0)
153

    
154
            inv = Invitations.objects.filter(target = user).count()
155
            self.assertEquals(inv, 0)
156
        except Exception:
157
            self.assertTrue(False)
158

    
159
        try:
160
            invitation = "Invitation"
161
            invitations.remove_invitation(inv)
162
        except Exception:
163
            self.assertTrue(False)
164

    
165
    def test_resend_invitation(self):
166
        """
167
        Tests the resend invitation method
168
        """
169
        inv = self._add_invitation()
170

    
171
        resp = self.client.post("/invitations/resend",
172
                                {'invid':inv.id},
173
                                **{'HTTP_X_AUTH_TOKEN': self.token})
174
        self.assertEquals(resp.status_code, 200)
175

    
176

    
177
        resp = self.client.post("/invitations/resend",
178
                                {'invid':'1;delete from db_invitations;'},
179
                                **{'HTTP_X_AUTH_TOKEN': self.token})
180
        self.assertEquals(resp.status_code, 400)
181

    
182
        resp = self.client.post("/invitations/resend",
183
                                {'invid':inv.id},
184
                                **{'HTTP_X_AUTH_TOKEN': inv.target.auth_token})
185
        self.assertEquals(resp.status_code, 400)
186

    
187
    def _add_invitation(self):
188
        source = SynnefoUser.objects.filter(auth_token = self.token)[0]
189
        invitations.add_invitation(source, "Test", "test@gmail.com")
190

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

    
194
        self.assertNotEquals(user, None)
195
        self.assertEqual(user.uniq, 'test@gmail.com')
196

    
197
        inv = Invitations.objects.filter(target = user)[0]
198
        self.assertNotEquals(inv, None)
199

    
200
        return inv