13
13
# You should have received a copy of the GNU Affero General Public License
14
14
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15
15
from __future__ import unicode_literals
18
from django.contrib.auth.models import User
19
from django.test import TestCase
17
21
from ci_utils.tastypie.test import TastypieTestCase
18
from django.contrib.auth.models import User
21
24
class UserInfoTest(TastypieTestCase):
54
57
'superuser': False,
61
class UserInfoAuthTest(TestCase):
64
""" Creates an `User` for tests."""
65
self.test_user = User.objects.create_user(
66
username='test', password='1234', first_name='Test')
67
self.client.login(username='test', password='1234')
69
def _get_current_username(self):
70
"""Return the current logged-in username.
72
By the lack of response 'context' (tastypie thing) we recover the
73
logged-in username from the 'userinfo' resource.
75
response = self.client.get('/api/v1/userinfo/')
76
self.assertEqual(200, response.status_code)
77
return json.loads(response.content)['username']
79
def assertLoggedIn(self, username):
80
"""Assert the given username matches the current user."""
81
self.assertEqual(username, self._get_current_username())
83
def assertNotLoggedIn(self):
84
"""Assert there is no user currently logged-in."""
85
self.assertEqual('', self._get_current_username())
87
def test_logout_works(self):
88
#'logout' inconditionally logs the user out.
89
self.assertLoggedIn('test')
91
# By default the 'logout' page redirects to the (webui) site root
92
# which is not available in the isolated TS deployment.
93
response = self.client.get('/logout/')
94
self.assertEqual(302, response.status_code)
95
self.assertEqual('http://testserver/', response['location'])
97
self.assertNotLoggedIn()
99
def test_logout_supports_next(self):
100
#'logout' can optionally redirect users to another page, normally
101
# where they came from, via 'next' GET parameter.
102
self.assertLoggedIn('test')
104
response = self.client.get('/logout/?next=/ticket/whatever/')
105
self.assertEqual(302, response.status_code)
107
'http://testserver/ticket/whatever/', response['location'])
109
self.assertNotLoggedIn()