File: test_associate.py

package info (click to toggle)
python-social-auth 0.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,828 kB
  • ctags: 3,245
  • sloc: python: 12,867; makefile: 119; sh: 3
file content (89 lines) | stat: -rw-r--r-- 2,935 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import json
from sure import expect

from social.exceptions import AuthAlreadyAssociated

from social.tests.models import User
from social.tests.actions.actions import BaseActionTest


class AssociateActionTest(BaseActionTest):
    expected_username = 'foobar'

    def setUp(self):
        super(AssociateActionTest, self).setUp()
        self.user = User(username='foobar', email='foo@bar.com')
        self.backend.strategy.session_set('username', self.user.username)

    def test_associate(self):
        self.do_login()
        expect(len(self.user.social)).to.equal(1)
        expect(self.user.social[0].provider).to.equal('github')

    def test_associate_with_partial_pipeline(self):
        self.do_login_with_partial_pipeline()
        expect(len(self.user.social)).to.equal(1)
        expect(self.user.social[0].provider).to.equal('github')


class MultipleAccountsTest(AssociateActionTest):
    alternative_user_data_body = json.dumps({
        'login': 'foobar2',
        'id': 2,
        'avatar_url': 'https://github.com/images/error/foobar2_happy.gif',
        'gravatar_id': 'somehexcode',
        'url': 'https://api.github.com/users/foobar2',
        'name': 'monalisa foobar2',
        'company': 'GitHub',
        'blog': 'https://github.com/blog',
        'location': 'San Francisco',
        'email': 'foo@bar.com',
        'hireable': False,
        'bio': 'There once was...',
        'public_repos': 2,
        'public_gists': 1,
        'followers': 20,
        'following': 0,
        'html_url': 'https://github.com/foobar2',
        'created_at': '2008-01-14T04:33:35Z',
        'type': 'User',
        'total_private_repos': 100,
        'owned_private_repos': 100,
        'private_gists': 81,
        'disk_usage': 10000,
        'collaborators': 8,
        'plan': {
            'name': 'Medium',
            'space': 400,
            'collaborators': 10,
            'private_repos': 20
        }
    })

    def test_multiple_social_accounts(self):
        self.do_login()
        self.do_login(user_data_body=self.alternative_user_data_body)
        expect(len(self.user.social)).to.equal(2)
        expect(self.user.social[0].provider).to.equal('github')
        expect(self.user.social[1].provider).to.equal('github')


class AlreadyAssociatedErrorTest(BaseActionTest):
    def setUp(self):
        super(AlreadyAssociatedErrorTest, self).setUp()
        self.user1 = User(username='foobar', email='foo@bar.com')
        self.user = None

    def tearDown(self):
        super(AlreadyAssociatedErrorTest, self).tearDown()
        self.user1 = None
        self.user = None

    def test_already_associated_error(self):
        self.user = self.user1
        self.do_login()
        self.user = User(username='foobar2', email='foo2@bar2.com')
        self.do_login.when.called_with().should.throw(
            AuthAlreadyAssociated,
            'This github account is already in use.'
        )