~ubuntuone-pqm-team/canonical-identity-provider/trunk

« back to all changes in this revision

Viewing changes to identityprovider/tests/test_views_account.py

  • Committer: Danny Tamez
  • Date: 2010-04-21 15:29:24 UTC
  • Revision ID: danny.tamez@canonical.com-20100421152924-lq1m92tstk2iz75a
Canonical SSO Provider (Open Source) - Initial Commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
import logging
 
5
 
 
6
from django.core import mail
 
7
 
 
8
from identityprovider.models import Account, EmailAddress
 
9
from identityprovider.models.const import AccountStatus, EmailStatus
 
10
from identityprovider.utils import validate_launchpad_password
 
11
 
 
12
from utils import BasicAccountTestCase, AuthenticatedTestCase
 
13
 
 
14
 
 
15
class AccountViewsUnauthenticatedTestCase(BasicAccountTestCase):
 
16
 
 
17
    def test_index_unauthenticated(self):
 
18
        r = self.client.get('/+edit')
 
19
        self.assertEqual(r.status_code, 200)
 
20
 
 
21
 
 
22
class AccountViewsTest(AuthenticatedTestCase):
 
23
 
 
24
    def setUp(self):
 
25
        super(AccountViewsTest, self).setUp(disableCSRF=True)
 
26
        logging.disable(logging.WARNING)
 
27
 
 
28
    def test_index_authenticated(self):
 
29
        r = self.client.get('/+edit')
 
30
        self.assertTemplateUsed(r, 'account/edit.html')
 
31
 
 
32
    def test_index_edit_displayname(self):
 
33
        r = self.client.post('/+edit', {'displayname': "New Display Name",
 
34
                                        'preferred_email': '16'})
 
35
        self.assertEqual(r.status_code, 302)
 
36
 
 
37
    def test_index_edit_password(self):
 
38
        r = self.client.post('/+edit', {'displayname': "New Display Name",
 
39
                                        'preferred_email': '16',
 
40
                                        'newpassword': 'new-Password',
 
41
                                        'newpasswordconfirm': 'new-Password'})
 
42
        account = Account.objects.get(displayname='New Display Name')
 
43
        self.assertEqual(r.status_code, 302)
 
44
        self.assert_(validate_launchpad_password(
 
45
            'new-Password', account.accountpassword.password))
 
46
 
 
47
    def test_index_clears_up_message_from_session(self):
 
48
        self.client.session['message'] = 'test message'
 
49
        self.client.get('/+edit')
 
50
 
 
51
        self.assert_('message' not in self.client.session)
 
52
 
 
53
    def test_verify_email_already_verified(self):
 
54
        r = self.client.get('/+verify-email', {'id': 1})
 
55
        self.assertEqual(r.status_code, 404)
 
56
 
 
57
    def test_verify_email_other_users_email(self):
 
58
        other_account = Account.objects.get(pk=1)
 
59
        other_email = EmailAddress.objects.create(
 
60
            email='foo@bar.com',
 
61
            account=other_account,
 
62
            status=EmailStatus.NEW)
 
63
        r = self.client.get('/+verify-email', {'id': other_email.id})
 
64
        self.assertEqual(r.status_code, 404)
 
65
 
 
66
    def test_verify_email_success(self):
 
67
        account = Account.objects.get_by_email('test@canonical.com')
 
68
        email = EmailAddress.objects.create(
 
69
            email='footest@bar.com',
 
70
            account=account,
 
71
            status=EmailStatus.NEW)
 
72
        r = self.client.get('/+verify-email', {'id': email.id})
 
73
        self.assertEqual(r.status_code, 302)
 
74
        self.assertEqual(len(mail.outbox), 1)
 
75
        # Erasing emails. Necessary to make other tests pass.
 
76
        mail.outbox = []
 
77
 
 
78
    def test_new_email_get(self):
 
79
        r = self.client.get('/+new-email')
 
80
        self.assertTemplateUsed(r, 'account/new_email.html')
 
81
 
 
82
    def test_new_email_post(self):
 
83
        r = self.client.post('/+new-email',
 
84
                             {'newemail': "very-new-email@example.com"})
 
85
        self.assertRedirects(r, '/+email-sent')
 
86
        mail.outbox = []
 
87
 
 
88
    def test_new_email_post_with_token(self):
 
89
        r = self.client.post('/thisissuperrando/+new-email',
 
90
                            {'newemail': "very-new-email@example.com"})
 
91
        self.assertEquals(len(mail.outbox), 1)
 
92
        mail.outbox = []
 
93
 
 
94
 
 
95
class AccountDeletionViewTestCase(AuthenticatedTestCase):
 
96
 
 
97
    def setUp(self):
 
98
        super(AccountDeletionViewTestCase, self).setUp(disableCSRF=True)
 
99
 
 
100
        email = EmailAddress.objects.get(email__iexact=self.login_email)
 
101
        self.account = email.account
 
102
 
 
103
    def test_delete_email_get_form(self):
 
104
        email = self.account.emailaddress_set.create(
 
105
            email='test@test.com', status=EmailStatus.NEW)
 
106
        r = self.client.get('/+remove-email', {'id': email.id})
 
107
        self.assertTemplateUsed(r, 'account/delete_email.html')
 
108
 
 
109
    def test_delete_email_when_submiting_form(self):
 
110
        email = self.account.emailaddress_set.create(
 
111
            email='test@test.com', status=EmailStatus.NEW)
 
112
        # Due to limitation in Django 1.0 you can't pass directly GET arguments
 
113
        # to post method, that's why you need to use QUERY_STRING, this is
 
114
        # unnecessary in Django 1.1
 
115
        r = self.client.post('/+remove-email', QUERY_STRING='id=%s' % email.id)
 
116
        self.assertRedirects(r, '/')
 
117
 
 
118
 
 
119
class AccountDeactivationViewTestCase(AuthenticatedTestCase):
 
120
    def setUp(self):
 
121
        super(AccountDeactivationViewTestCase, self).setUp(disableCSRF=True)
 
122
        self._refresh_account()
 
123
 
 
124
    def _refresh_account(self):
 
125
        email = EmailAddress.objects.get(email__iexact=self.login_email)
 
126
        self.account = email.account
 
127
 
 
128
    def test_deactivate_account(self):
 
129
        # deactivate account
 
130
        r = self.client.post('/+deactivate')
 
131
        self.assertRedirects(r, '/+deactivated')
 
132
 
 
133
        # re-request account object from db
 
134
        self._refresh_account()
 
135
 
 
136
        # test account has been deactivated
 
137
        self.assertEqual(AccountStatus.DEACTIVATED, self.account.status)
 
138
        self.assertEqual(None, self.account.preferredemail)
 
139
        for email in self.account.emailaddress_set.all():
 
140
            self.assertEqual(EmailStatus.NEW, email.status)