Skip to content

Commit

Permalink
Look for code + unit test for successsful OAuith2 authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
krak3n committed Mar 20, 2015
1 parent af6364e commit d27051d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion fm/views/oauth2.py
Expand Up @@ -53,7 +53,7 @@ def post(self):
config.GOOGLE_CLIENT_ID,
config.GOOGLE_CLIENT_SECRET,
'',
request.json['token'])
request.json['code'])

h = httplib2.Http()
h = credentials.authorize(h)
Expand Down
54 changes: 53 additions & 1 deletion tests/views/oauth2/test_google_connect.py
Expand Up @@ -12,6 +12,7 @@
import mock

from flask import url_for
from fm.models.user import User


class TestGoogleConnectPost(object):
Expand Down Expand Up @@ -40,10 +41,61 @@ def must_be_in_allowed_domains(self, google, credentials_from_code, request):

url = url_for('oauth2.google.connect')
response = self.client.post(url, data=json.dumps({
'token': 'foo'
'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.headers['Auth-Token'] == '123456.abcdefg'
assert 'Location' in response.headers

0 comments on commit d27051d

Please sign in to comment.