diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..73a3403 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,18 @@ +language: python +sudo: false +cache: pip + +matrix: + include: + - python: 2.7 + - python: 3.5 + - python: 3.6 + - python: 3.7 + dist: xenial + sudo: true + +script: + - python setup.py test + +notifications: + email: false diff --git a/Makefile b/Makefile index 8db3ced..8d58560 100644 --- a/Makefile +++ b/Makefile @@ -23,3 +23,7 @@ clean-tox: docs: @$(MAKE) -C docs html + +coverage: + @coverage run --source=loginpass setup.py -q nosetests + @coverage html diff --git a/setup.py b/setup.py index fd4099f..bf3e4e0 100755 --- a/setup.py +++ b/setup.py @@ -24,6 +24,8 @@ packages=['loginpass'], install_requires=['Authlib'], include_package_data=True, + tests_require=['nose'], + test_suite='nose.collector', classifiers=[ 'Development Status :: 4 - Beta', 'Environment :: Web Environment', diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/data/github_response.json b/tests/data/github_response.json new file mode 100644 index 0000000..5060919 --- /dev/null +++ b/tests/data/github_response.json @@ -0,0 +1,45 @@ +{ + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false, + "name": "monalisa octocat", + "company": "GitHub", + "blog": "https://github.com/blog", + "location": "San Francisco", + "email": "octocat@github.com", + "hireable": false, + "bio": "There once was...", + "public_repos": 2, + "public_gists": 1, + "followers": 20, + "following": 0, + "created_at": "2008-01-14T04:33:35Z", + "updated_at": "2008-01-14T04:33:35Z", + "total_private_repos": 100, + "owned_private_repos": 100, + "private_gists": 81, + "disk_usage": 10000, + "collaborators": 8, + "two_factor_authentication": true, + "plan": { + "name": "Medium", + "space": 400, + "private_repos": 20, + "collaborators": 0 + } +} diff --git a/tests/data/github_result.json b/tests/data/github_result.json new file mode 100644 index 0000000..bd092a8 --- /dev/null +++ b/tests/data/github_result.json @@ -0,0 +1,10 @@ +{ + "sub": "1", + "name": "monalisa octocat", + "email": "octocat@github.com", + "picture": "https://github.com/images/error/octocat_happy.gif", + "preferred_username": "octocat", + "profile": "https://github.com/octocat", + "website": "https://github.com/blog", + "updated_at": 1200252815 +} diff --git a/tests/data/twitter_response.json b/tests/data/twitter_response.json new file mode 100644 index 0000000..ea5d124 --- /dev/null +++ b/tests/data/twitter_response.json @@ -0,0 +1,104 @@ +{ + "contributors_enabled": true, + "created_at": "Sat May 09 17:58:22 +0000 2009", + "default_profile": false, + "default_profile_image": false, + "description": "I taught your phone that thing you like. The Mobile Partner Engineer @Twitter. ", + "favourites_count": 588, + "follow_request_sent": null, + "followers_count": 10625, + "following": null, + "friends_count": 1181, + "geo_enabled": true, + "id": 38895958, + "id_str": "38895958", + "is_translator": false, + "lang": "en", + "listed_count": 190, + "location": "San Francisco", + "name": "Sean Cook", + "notifications": null, + "profile_background_color": "1A1B1F", + "profile_background_image_url": "http://a0.twimg.com/profile_background_images/495742332/purty_wood.png", + "profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/495742332/purty_wood.png", + "profile_background_tile": true, + "profile_image_url": "http://a0.twimg.com/profile_images/1751506047/dead_sexy_normal.JPG", + "profile_image_url_https": "https://si0.twimg.com/profile_images/1751506047/dead_sexy_normal.JPG", + "profile_link_color": "2FC2EF", + "profile_sidebar_border_color": "181A1E", + "profile_sidebar_fill_color": "252429", + "profile_text_color": "666666", + "profile_use_background_image": true, + "protected": false, + "screen_name": "theSeanCook", + "show_all_inline_media": true, + "status": { + "contributors": null, + "coordinates": { + "coordinates": [ + -122.45037293, + 37.76484123 + ], + "type": "Point" + }, + "created_at": "Tue Aug 28 05:44:24 +0000 2012", + "favorited": false, + "geo": { + "coordinates": [ + 37.76484123, + -122.45037293 + ], + "type": "Point" + }, + "id": 240323931419062272, + "id_str": "240323931419062272", + "in_reply_to_screen_name": "messl", + "in_reply_to_status_id": 240316959173009410, + "in_reply_to_status_id_str": "240316959173009410", + "in_reply_to_user_id": 18707866, + "in_reply_to_user_id_str": "18707866", + "place": { + "attributes": {}, + "bounding_box": { + "coordinates": [ + [ + [ + -122.45778216, + 37.75932999 + ], + [ + -122.44248216, + 37.75932999 + ], + [ + -122.44248216, + 37.76752899 + ], + [ + -122.45778216, + 37.76752899 + ] + ] + ], + "type": "Polygon" + }, + "country": "United States", + "country_code": "US", + "full_name": "Ashbury Heights, San Francisco", + "id": "866269c983527d5a", + "name": "Ashbury Heights", + "place_type": "neighborhood", + "url": "http://api.twitter.com/1/geo/id/866269c983527d5a.json" + }, + "retweet_count": 0, + "retweeted": false, + "source": "Twitter for iPhone", + "text": "@messl congrats! So happy for all 3 of you.", + "truncated": false + }, + "statuses_count": 2609, + "time_zone": "Pacific Time (US & Canada)", + "url": null, + "utc_offset": -28800, + "verified": false +} diff --git a/tests/data/twitter_result.json b/tests/data/twitter_result.json new file mode 100644 index 0000000..128860c --- /dev/null +++ b/tests/data/twitter_result.json @@ -0,0 +1,9 @@ +{ + "sub": "38895958", + "name": "Sean Cook", + "email": null, + "locale": "en", + "picture": "https://si0.twimg.com/profile_images/1751506047/dead_sexy_normal.JPG", + "preferred_username": "theSeanCook", + "profile": "https://twitter.com/theSeanCook" +} diff --git a/tests/data/yandex_response.json b/tests/data/yandex_response.json new file mode 100644 index 0000000..b872630 --- /dev/null +++ b/tests/data/yandex_response.json @@ -0,0 +1,22 @@ +{ + "first_name": "\u0412\u0430\u0441\u044F", + "last_name": "\u041F\u0443\u043F\u043A\u0438\u043D", + "display_name": "Vasya", + "emails": [ + "test@yandex.ru", + "other-test@yandex.ru" + ], + "default_email": "test@yandex.ru", + "real_name": "\u0412\u0430\u0441\u044F \u041F\u0443\u043F\u043A\u0438\u043D", + "is_avatar_empty": false, + "birthday": "1987-03-12", + "default_avatar_id": "131652443", + "openid_identities": [ + "http://openid.yandex.ru/vasya/", + "http://vasya.ya.ru/" + ], + "login": "vasya", + "old_social_login": "uid-mmzxrnry", + "sex": "male", + "id": "1000034426" +} diff --git a/tests/data/yandex_result.json b/tests/data/yandex_result.json new file mode 100644 index 0000000..98db196 --- /dev/null +++ b/tests/data/yandex_result.json @@ -0,0 +1,11 @@ +{ + "sub": "1000034426", + "name": "\u0412\u0430\u0441\u044F \u041F\u0443\u043F\u043A\u0438\u043D", + "given_name": "\u0412\u0430\u0441\u044F", + "family_name": "\u041F\u0443\u043F\u043A\u0438\u043D", + "preferred_username": "vasya", + "email": "test@yandex.ru", + "picture": "https://avatars.yandex.net/get-yapic/131652443/islands-200", + "gender": "male", + "birthdate": "1987-03-12" +} diff --git a/tests/test_oauth_backends.py b/tests/test_oauth_backends.py new file mode 100644 index 0000000..138940e --- /dev/null +++ b/tests/test_oauth_backends.py @@ -0,0 +1,63 @@ +import os +import json +import mock +import requests +from loginpass._core import UserInfo +from loginpass import ( + Twitter, + GitHub, + Yandex, +) + + +TEST_DIR = os.path.dirname(os.path.abspath(__file__)) + + +def run_profile(backend, **kwargs): + filename = '{}_response.json'.format(backend.OAUTH_NAME) + with open(os.path.join(TEST_DIR, 'data', filename), 'r') as f: + resp = mock.MagicMock(spec=requests.Response) + data = json.load(f) + resp.json = lambda: data + resp.status_code = 200 + + with mock.patch('requests.sessions.Session.send') as send: + send.return_value = resp + profile = backend.profile(**kwargs) + assert isinstance(profile, UserInfo) + + filename = '{}_result.json'.format(backend.OAUTH_NAME) + with open(os.path.join(TEST_DIR, 'data', filename), 'r') as f: + result = json.load(f) + assert dict(profile) == result + + +def run_oauth_profile(backend_cls): + c = backend_cls( + client_id='a', + client_secret='b', + **backend_cls.OAUTH_CONFIG + ) + if c.OAUTH_TYPE == '1.0': + token = { + 'oauth_token': 'a', + 'oauth_token_secret': 'b' + } + else: + token = { + 'token_type': 'bearer', + 'access_token': 'a' + } + run_profile(c, token=token) + + +def test_twitter(): + run_oauth_profile(Twitter) + + +def test_github(): + run_oauth_profile(GitHub) + + +def test_yandex(): + run_oauth_profile(Yandex)