~ev/uci-engine/pyflakes-the-world

« back to all changes in this revision

Viewing changes to ticket_system/ticket/tests/test_user_info.py

  • Committer: Ubuntu CI Bot
  • Author(s): Celso Providelo
  • Date: 2014-11-04 20:35:35 UTC
  • mfrom: (873.2.1 webui-logout)
  • Revision ID: ubuntu_ci_bot-20141104203535-jsgmd9lc3f5121gx
[r=Joe Talbott, PS Jenkins bot] Implements a top-level logout/ view to unconditionally log users out.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
16
import json
 
17
 
 
18
from django.contrib.auth.models import User
 
19
from django.test import TestCase
16
20
 
17
21
from ci_utils.tastypie.test import TastypieTestCase
18
 
from django.contrib.auth.models import User
19
22
 
20
23
 
21
24
class UserInfoTest(TastypieTestCase):
53
56
            'staff': False,
54
57
            'superuser': False,
55
58
        }, user_info)
 
59
 
 
60
 
 
61
class UserInfoAuthTest(TestCase):
 
62
 
 
63
    def setUp(self):
 
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')
 
68
 
 
69
    def _get_current_username(self):
 
70
        """Return the current logged-in username.
 
71
 
 
72
        By the lack of response 'context' (tastypie thing) we recover the
 
73
        logged-in username from the 'userinfo' resource.
 
74
        """
 
75
        response = self.client.get('/api/v1/userinfo/')
 
76
        self.assertEqual(200, response.status_code)
 
77
        return json.loads(response.content)['username']
 
78
 
 
79
    def assertLoggedIn(self, username):
 
80
        """Assert the given username matches the current user."""
 
81
        self.assertEqual(username, self._get_current_username())
 
82
 
 
83
    def assertNotLoggedIn(self):
 
84
        """Assert there is no user currently logged-in."""
 
85
        self.assertEqual('', self._get_current_username())
 
86
 
 
87
    def test_logout_works(self):
 
88
        #'logout' inconditionally logs the user out.
 
89
        self.assertLoggedIn('test')
 
90
 
 
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'])
 
96
 
 
97
        self.assertNotLoggedIn()
 
98
 
 
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')
 
103
 
 
104
        response = self.client.get('/logout/?next=/ticket/whatever/')
 
105
        self.assertEqual(302, response.status_code)
 
106
        self.assertEqual(
 
107
            'http://testserver/ticket/whatever/', response['location'])
 
108
 
 
109
        self.assertNotLoggedIn()