root / invitations / tests.py @ 53481544
History | View | Annotate | Download (7.1 kB)
1 | 48130e66 | Georgios Gousios | # Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 48130e66 | Georgios Gousios | #
|
3 | 48130e66 | Georgios Gousios | # Redistribution and use in source and binary forms, with or without
|
4 | 48130e66 | Georgios Gousios | # modification, are permitted provided that the following conditions
|
5 | 48130e66 | Georgios Gousios | # are met:
|
6 | 48130e66 | Georgios Gousios | #
|
7 | 48130e66 | Georgios Gousios | # 1. Redistributions of source code must retain the above copyright
|
8 | 48130e66 | Georgios Gousios | # notice, this list of conditions and the following disclaimer.
|
9 | 48130e66 | Georgios Gousios | #
|
10 | 48130e66 | Georgios Gousios | # 2. Redistributions in binary form must reproduce the above copyright
|
11 | 48130e66 | Georgios Gousios | # notice, this list of conditions and the following disclaimer in the
|
12 | 48130e66 | Georgios Gousios | # documentation and/or other materials provided with the distribution.
|
13 | 48130e66 | Georgios Gousios | #
|
14 | 48130e66 | Georgios Gousios | # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
15 | 48130e66 | Georgios Gousios | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
16 | 48130e66 | Georgios Gousios | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
17 | 48130e66 | Georgios Gousios | # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
18 | 48130e66 | Georgios Gousios | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
19 | 48130e66 | Georgios Gousios | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
20 | 48130e66 | Georgios Gousios | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
21 | 48130e66 | Georgios Gousios | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
22 | 48130e66 | Georgios Gousios | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
23 | 48130e66 | Georgios Gousios | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
24 | 48130e66 | Georgios Gousios | # SUCH DAMAGE.
|
25 | 48130e66 | Georgios Gousios | #
|
26 | 48130e66 | Georgios Gousios | # The views and conclusions contained in the software and documentation are
|
27 | 48130e66 | Georgios Gousios | # those of the authors and should not be interpreted as representing official
|
28 | 48130e66 | Georgios Gousios | # policies, either expressed or implied, of GRNET S.A.
|
29 | ada3343a | Georgios Gousios | from datetime import datetime, timedelta |
30 | 48130e66 | Georgios Gousios | |
31 | 9a5e8ebe | Georgios Gousios | from django.test import TestCase |
32 | 9a5e8ebe | Georgios Gousios | from django.test.client import Client |
33 | 9a5e8ebe | Georgios Gousios | |
34 | 9a5e8ebe | Georgios Gousios | import invitations |
35 | ada3343a | Georgios Gousios | from synnefo.db.models import SynnefoUser, Invitations |
36 | 9f8ad4c5 | Georgios Gousios | from django.conf import settings |
37 | 9a5e8ebe | Georgios Gousios | |
38 | 9a5e8ebe | Georgios Gousios | |
39 | 9a5e8ebe | Georgios Gousios | class InvitationsTestCase(TestCase): |
40 | 53481544 | Vangelis Koukis | fixtures = ['users']
|
41 | 9a5e8ebe | Georgios Gousios | token = '46e427d657b20defe352804f0eb6f8a2'
|
42 | 9a5e8ebe | Georgios Gousios | |
43 | 9a5e8ebe | Georgios Gousios | def setUp(self): |
44 | 9a5e8ebe | Georgios Gousios | self.client = Client()
|
45 | 9a5e8ebe | Georgios Gousios | |
46 | 9a5e8ebe | Georgios Gousios | def test_add_invitation(self): |
47 | 9f8ad4c5 | Georgios Gousios | """
|
48 | 9f8ad4c5 | Georgios Gousios | Tests whether invitations can be added
|
49 | 9f8ad4c5 | Georgios Gousios | """
|
50 | 747164f3 | Georgios Gousios | inv = self._add_invitation()
|
51 | 747164f3 | Georgios Gousios | source = inv.source |
52 | 566cd8b2 | Georgios Gousios | |
53 | bd1548a7 | Georgios Gousios | # Re-adding an existing invitation
|
54 | bd1548a7 | Georgios Gousios | try:
|
55 | bd1548a7 | Georgios Gousios | invitations.add_invitation(source, u'', "test@gmail.com") |
56 | bd1548a7 | Georgios Gousios | self.assertTrue(False) |
57 | bd1548a7 | Georgios Gousios | except invitations.AlreadyInvited:
|
58 | bd1548a7 | Georgios Gousios | self.assertTrue(True) |
59 | 747164f3 | Georgios Gousios | except Exception: |
60 | 747164f3 | Georgios Gousios | self.assertTrue(False) |
61 | 747164f3 | Georgios Gousios | |
62 | 747164f3 | Georgios Gousios | # Hit the invitation limit
|
63 | 747164f3 | Georgios Gousios | try:
|
64 | 747164f3 | Georgios Gousios | source.max_invitations = 0
|
65 | 747164f3 | Georgios Gousios | source.save() |
66 | 747164f3 | Georgios Gousios | self._add_invitation()
|
67 | 747164f3 | Georgios Gousios | except invitations.TooManyInvitations:
|
68 | 747164f3 | Georgios Gousios | self.assertTrue(True) |
69 | 747164f3 | Georgios Gousios | except Exception: |
70 | 747164f3 | Georgios Gousios | self.assertTrue(False) |
71 | 747164f3 | Georgios Gousios | |
72 | 9f8ad4c5 | Georgios Gousios | def test_get_invitee_level(self): |
73 | 9f8ad4c5 | Georgios Gousios | """
|
74 | 9f8ad4c5 | Georgios Gousios | Checks whether invitation levels and their limits are being respected
|
75 | 9f8ad4c5 | Georgios Gousios | """
|
76 | 9f8ad4c5 | Georgios Gousios | source = SynnefoUser.objects.filter(auth_token = self.token)[0] |
77 | 9f8ad4c5 | Georgios Gousios | |
78 | 9f8ad4c5 | Georgios Gousios | self.assertEqual(invitations.get_user_inv_level(source), -1) |
79 | 9f8ad4c5 | Georgios Gousios | |
80 | 9f8ad4c5 | Georgios Gousios | inv = invitations.add_invitation(source, "Test", "test@gmail.com") |
81 | ada3343a | Georgios Gousios | self.assertEqual(inv.target.max_invitations,
|
82 | ada3343a | Georgios Gousios | settings.INVITATIONS_PER_LEVEL[0])
|
83 | 9f8ad4c5 | Georgios Gousios | self.assertEqual(invitations.get_user_inv_level(inv.target), 0) |
84 | 9f8ad4c5 | Georgios Gousios | |
85 | 9f8ad4c5 | Georgios Gousios | inv = invitations.add_invitation(inv.target, "Test2", "test2@gmail.com") |
86 | 9f8ad4c5 | Georgios Gousios | self.assertEqual(invitations.get_user_inv_level(inv.target), 1) |
87 | ada3343a | Georgios Gousios | self.assertEqual(inv.target.max_invitations,
|
88 | ada3343a | Georgios Gousios | settings.INVITATIONS_PER_LEVEL[1])
|
89 | ada3343a | Georgios Gousios | |
90 | ada3343a | Georgios Gousios | def test_invitation_login(self): |
91 | ada3343a | Georgios Gousios | """
|
92 | ada3343a | Georgios Gousios | Basic login by invitation checks
|
93 | ada3343a | Georgios Gousios | """
|
94 | 747164f3 | Georgios Gousios | inv = self._add_invitation()
|
95 | 747164f3 | Georgios Gousios | user = inv.target |
96 | ada3343a | Georgios Gousios | |
97 | ada3343a | Georgios Gousios | url = invitations.enconde_inv_url(inv) |
98 | ada3343a | Georgios Gousios | |
99 | ada3343a | Georgios Gousios | resp = self.client.get(url)
|
100 | ada3343a | Georgios Gousios | self.assertEqual(resp.status_code, 200) |
101 | ada3343a | Georgios Gousios | self.assertEquals(resp['X-Auth-Token'], user.auth_token) |
102 | ada3343a | Georgios Gousios | |
103 | ada3343a | Georgios Gousios | # Invalid login url
|
104 | ada3343a | Georgios Gousios | url = list(url)
|
105 | ada3343a | Georgios Gousios | url[35] = '@' |
106 | ada3343a | Georgios Gousios | resp = self.client.get("".join(url)) |
107 | ada3343a | Georgios Gousios | self.assertEqual(resp.status_code, 404) |
108 | ada3343a | Georgios Gousios | |
109 | ada3343a | Georgios Gousios | def test_relogin_with_expired_token(self): |
110 | ada3343a | Georgios Gousios | """
|
111 | ada3343a | Georgios Gousios | Checks whether a user can login when his auth token has expired
|
112 | ada3343a | Georgios Gousios | """
|
113 | 747164f3 | Georgios Gousios | inv = self._add_invitation()
|
114 | 747164f3 | Georgios Gousios | user = inv.target |
115 | ada3343a | Georgios Gousios | |
116 | ada3343a | Georgios Gousios | # Expire the user's token
|
117 | ada3343a | Georgios Gousios | user.auth_token_expires = datetime.now() |
118 | ada3343a | Georgios Gousios | user.save() |
119 | ada3343a | Georgios Gousios | |
120 | ada3343a | Georgios Gousios | url = invitations.enconde_inv_url(inv) |
121 | ada3343a | Georgios Gousios | resp = self.client.get(url)
|
122 | ada3343a | Georgios Gousios | self.assertEqual(resp.status_code, 200) |
123 | ada3343a | Georgios Gousios | self.assertEquals(resp['X-Auth-Token'], user.auth_token) |
124 | ada3343a | Georgios Gousios | |
125 | ada3343a | Georgios Gousios | user = SynnefoUser.objects.get(uniq = "test@gmail.com")
|
126 | ada3343a | Georgios Gousios | self.assertTrue(user.auth_token_expires > datetime.now())
|
127 | ada3343a | Georgios Gousios | |
128 | ada3343a | Georgios Gousios | # Invitation valid less than max allowed auth token expiration time
|
129 | ada3343a | Georgios Gousios | valid = timedelta(days = (settings.INVITATION_VALID_DAYS - 10))
|
130 | ada3343a | Georgios Gousios | inv.created = inv.created - valid |
131 | ada3343a | Georgios Gousios | inv.save() |
132 | ada3343a | Georgios Gousios | |
133 | ada3343a | Georgios Gousios | user.auth_token_expires = datetime.now() |
134 | ada3343a | Georgios Gousios | user.save() |
135 | ada3343a | Georgios Gousios | |
136 | ada3343a | Georgios Gousios | url = invitations.enconde_inv_url(inv) |
137 | ada3343a | Georgios Gousios | resp = self.client.get(url)
|
138 | ada3343a | Georgios Gousios | self.assertEqual(resp.status_code, 200) |
139 | ada3343a | Georgios Gousios | inv = Invitations.objects.filter(target = user)[0]
|
140 | ada3343a | Georgios Gousios | valid = timedelta(days = settings.INVITATION_VALID_DAYS) |
141 | ada3343a | Georgios Gousios | self.assertTrue(inv.created + valid >= user.auth_token_expires)
|
142 | ada3343a | Georgios Gousios | |
143 | 747164f3 | Georgios Gousios | def test_remove_invitation(self): |
144 | 747164f3 | Georgios Gousios | """
|
145 | 747164f3 | Georgios Gousios | Tests the remove invitation method
|
146 | 747164f3 | Georgios Gousios | """
|
147 | 747164f3 | Georgios Gousios | inv = self._add_invitation()
|
148 | 747164f3 | Georgios Gousios | |
149 | 747164f3 | Georgios Gousios | try:
|
150 | 747164f3 | Georgios Gousios | invitations.remove_invitation(inv) |
151 | 747164f3 | Georgios Gousios | user = SynnefoUser.objects.filter(uniq = 'test@gmail.com').count()
|
152 | 747164f3 | Georgios Gousios | self.assertEquals(user, 0) |
153 | 747164f3 | Georgios Gousios | |
154 | 747164f3 | Georgios Gousios | inv = Invitations.objects.filter(target = user).count() |
155 | 747164f3 | Georgios Gousios | self.assertEquals(inv, 0) |
156 | 747164f3 | Georgios Gousios | except Exception: |
157 | 747164f3 | Georgios Gousios | self.assertTrue(False) |
158 | 747164f3 | Georgios Gousios | |
159 | ee60bb36 | Georgios Gousios | try:
|
160 | ee60bb36 | Georgios Gousios | invitation = "Invitation"
|
161 | ee60bb36 | Georgios Gousios | invitations.remove_invitation(inv) |
162 | ee60bb36 | Georgios Gousios | except Exception: |
163 | ee60bb36 | Georgios Gousios | self.assertTrue(False) |
164 | ee60bb36 | Georgios Gousios | |
165 | 4f8e7c6d | Georgios Gousios | def test_resend_invitation(self): |
166 | 4f8e7c6d | Georgios Gousios | """
|
167 | 4f8e7c6d | Georgios Gousios | Tests the resend invitation method
|
168 | 4f8e7c6d | Georgios Gousios | """
|
169 | 4f8e7c6d | Georgios Gousios | inv = self._add_invitation()
|
170 | 4f8e7c6d | Georgios Gousios | |
171 | 4f8e7c6d | Georgios Gousios | resp = self.client.post("/invitations/resend", |
172 | 4f8e7c6d | Georgios Gousios | {'invid':inv.id},
|
173 | 4f8e7c6d | Georgios Gousios | **{'HTTP_X_AUTH_TOKEN': self.token}) |
174 | 4f8e7c6d | Georgios Gousios | self.assertEquals(resp.status_code, 200) |
175 | 4f8e7c6d | Georgios Gousios | |
176 | 4f8e7c6d | Georgios Gousios | |
177 | 4f8e7c6d | Georgios Gousios | resp = self.client.post("/invitations/resend", |
178 | 4f8e7c6d | Georgios Gousios | {'invid':'1;delete from db_invitations;'}, |
179 | 4f8e7c6d | Georgios Gousios | **{'HTTP_X_AUTH_TOKEN': self.token}) |
180 | 4f8e7c6d | Georgios Gousios | self.assertEquals(resp.status_code, 400) |
181 | 4f8e7c6d | Georgios Gousios | |
182 | 4f8e7c6d | Georgios Gousios | resp = self.client.post("/invitations/resend", |
183 | 4f8e7c6d | Georgios Gousios | {'invid':inv.id},
|
184 | 4f8e7c6d | Georgios Gousios | **{'HTTP_X_AUTH_TOKEN': inv.target.auth_token})
|
185 | 4f8e7c6d | Georgios Gousios | self.assertEquals(resp.status_code, 400) |
186 | 4f8e7c6d | Georgios Gousios | |
187 | ada3343a | Georgios Gousios | def _add_invitation(self): |
188 | ada3343a | Georgios Gousios | source = SynnefoUser.objects.filter(auth_token = self.token)[0] |
189 | ada3343a | Georgios Gousios | invitations.add_invitation(source, "Test", "test@gmail.com") |
190 | ada3343a | Georgios Gousios | |
191 | ada3343a | Georgios Gousios | # Check whether the invited user has been added to the database
|
192 | ada3343a | Georgios Gousios | user = SynnefoUser.objects.get(uniq = "test@gmail.com")
|
193 | ada3343a | Georgios Gousios | |
194 | ada3343a | Georgios Gousios | self.assertNotEquals(user, None) |
195 | ada3343a | Georgios Gousios | self.assertEqual(user.uniq, 'test@gmail.com') |
196 | 747164f3 | Georgios Gousios | |
197 | 747164f3 | Georgios Gousios | inv = Invitations.objects.filter(target = user)[0]
|
198 | 747164f3 | Georgios Gousios | self.assertNotEquals(inv, None) |
199 | 747164f3 | Georgios Gousios | |
200 | 747164f3 | Georgios Gousios | return inv |