-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_google_connect.py
115 lines (90 loc) · 3.51 KB
/
test_google_connect.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python
# encoding: utf-8
"""
tests.views.oauth2.test_google_connect
======================================
Unit tests for the ``fm.views.oauth2.GoogleConnectView`` class.
"""
import json
import mock
from flask import url_for
from fm.models.user import User
from oauth2client.client import FlowExchangeError
class TestGoogleConnectPost(object):
def setup(self):
self.app.config['GOOGLE_CLIENT_ID'] = 'foo'
self.app.config['GOOGLE_CLIENT_SECRET'] = 'bar'
@mock.patch('fm.views.oauth2.httplib2.Http.request')
@mock.patch('fm.views.oauth2.credentials_from_code')
@mock.patch('fm.views.oauth2.google')
def must_be_in_allowed_domains(self, google, credentials_from_code, request):
credentials_from_code.return_value = mock.MagicMock(access_token='foo')
service = mock.MagicMock()
service.execute.return_value = {
'domain': 'foo.com'
}
people = mock.MagicMock()
people.get.return_value = service
build = mock.MagicMock()
build.people.return_value = people
google.discovery.build.return_value = build
url = url_for('oauth2.google.connect')
response = self.client.post(url, data=json.dumps({
'code': 'foo'
}))
assert response.status_code == 422
request.assert_called_once_with(
'https://accounts.google.com/o/oauth2/revoke?token=foo',
'GET')
@mock.patch('fm.views.oauth2.make_session')
@mock.patch('fm.views.oauth2.httplib2.Http.request')
@mock.patch('fm.views.oauth2.credentials_from_code')
@mock.patch('fm.views.oauth2.google')
def should_create_user(
self,
google,
credentials_from_code,
request,
make_session):
make_session.return_value = '123456.abcdefg'
credentials_from_code.return_value = mock.MagicMock(
access_token='foo',
to_json=mock.Mock(return_value=json.dumps({'foo': 'bar'})))
service = mock.MagicMock()
service.execute.return_value = {
'id': u'123456',
'domain': 'thisissoon.com',
'emails': [{'value': 'foo@thisissoon.com'}],
'name': {
'givenName': 'Foo',
'familyName': 'Bar',
},
'displayName': 'FooBar',
'image': {
'url': 'http://foo.com/foo.jpg?sz=60x60'
}
}
people = mock.MagicMock()
people.get.return_value = service
build = mock.MagicMock()
build.people.return_value = people
google.discovery.build.return_value = build
assert User.query.count() == 0
url = url_for('oauth2.google.connect')
response = self.client.post(url, data=json.dumps({
'code': 'foo'
}))
assert response.status_code == 201
assert User.query.count() == 1
assert response.json['access_token'] == '123456.abcdefg'
assert 'Location' in response.headers
@mock.patch('fm.views.oauth2.credentials_from_code')
@mock.patch('fm.views.oauth2.google')
def should_catch_flow_exceptions(self, google, credentials_from_code):
credentials_from_code.return_value = mock.MagicMock(access_token='foo')
credentials_from_code.side_effect = FlowExchangeError('some_error')
url = url_for('oauth2.google.connect')
response = self.client.post(url, data=json.dumps({
'code': 'foo'
}))
assert response.status_code == 422