-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cover get token, refresh token processes. Cover invalid credentials or invalid data tries to get token `view.py:authorize()` is left uncovered and revoke token is uncovered because of bug in `flask-oauthlib`
- Loading branch information
Showing
3 changed files
with
188 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# encoding: utf-8 | ||
import datetime | ||
import pytest | ||
|
||
|
||
@pytest.yield_fixture() | ||
def regular_user_oauth2_client(regular_user ,db): | ||
# pylint: disable=invalid-name,unused-argument | ||
from app.modules.auth.models import OAuth2Client | ||
|
||
admin_oauth2_client_instance = OAuth2Client( | ||
user=regular_user, | ||
client_id='regular_user_client', | ||
client_secret='regular_user_secret', | ||
redirect_uris=[], | ||
default_scopes=[] | ||
) | ||
|
||
db.session.add(admin_oauth2_client_instance) | ||
db.session.commit() | ||
yield admin_oauth2_client_instance | ||
db.session.delete(admin_oauth2_client_instance) | ||
db.session.commit() | ||
|
||
|
||
@pytest.yield_fixture() | ||
def regular_user_oauth2_token(regular_user_oauth2_client, db): | ||
from app.modules.auth.models import OAuth2Token | ||
|
||
regular_user_token = OAuth2Token( | ||
client=regular_user_oauth2_client, | ||
user=regular_user_oauth2_client.user, | ||
access_token='test_token', | ||
refresh_token='test_refresh_token', | ||
expires=datetime.datetime.now() + datetime.timedelta(seconds=3600), | ||
token_type=OAuth2Token.TokenTypes.Bearer, | ||
scopes=[] | ||
) | ||
|
||
db.session.add(regular_user_token) | ||
db.session.commit() | ||
yield regular_user_token | ||
db.session.delete(regular_user_token) | ||
db.session.commit() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
from base64 import b64encode | ||
|
||
import pytest | ||
|
||
|
||
def test_regular_user_can_retrieve_token( | ||
flask_app_client, | ||
regular_user, | ||
regular_user_oauth2_client | ||
): | ||
response = flask_app_client.post( | ||
'/auth/oauth2/token', | ||
content_type='application/x-www-form-urlencoded', | ||
data={ | ||
'username': regular_user.username, | ||
'password': 'regular_user_password', | ||
'client_id': regular_user_oauth2_client.client_id, | ||
'client_secret': regular_user_oauth2_client.client_secret, | ||
'grant_type': 'password', | ||
}, | ||
) | ||
|
||
assert response.status_code == 200 | ||
assert set(response.json.keys()) >= {'access_token', 'refresh_token'} | ||
|
||
|
||
def test_regular_user_cant_retrieve_token_without_credentials( | ||
flask_app_client, | ||
regular_user, | ||
): | ||
response = flask_app_client.post( | ||
'/auth/oauth2/token', | ||
content_type='application/x-www-form-urlencoded', | ||
data={ | ||
'username': regular_user.username, | ||
'password': 'regular_user_password', | ||
'grant_type': 'password', | ||
}, | ||
) | ||
|
||
assert response.status_code == 401 | ||
|
||
|
||
def test_regular_user_cant_retrieve_token_with_invalid_credentials( | ||
flask_app_client, | ||
regular_user, | ||
): | ||
response = flask_app_client.post( | ||
'/auth/oauth2/token', | ||
content_type='application/x-www-form-urlencoded', | ||
data={ | ||
'username': regular_user.username, | ||
'password': 'wrong_password', | ||
'client_id': 'wrong_client_id', | ||
'client_secret': 'wrong_client_secret', | ||
'grant_type': 'password', | ||
}, | ||
) | ||
|
||
assert response.status_code == 401 | ||
|
||
|
||
def test_regular_user_cant_retrieve_token_without_any_data( | ||
flask_app_client, | ||
): | ||
response = flask_app_client.post( | ||
'/auth/oauth2/token', | ||
content_type='application/x-www-form-urlencoded', | ||
data={}, | ||
) | ||
|
||
assert response.status_code == 400 | ||
|
||
|
||
def test_regular_user_can_refresh_token( | ||
flask_app_client, | ||
regular_user_oauth2_token, | ||
): | ||
refresh_token_response = flask_app_client.post( | ||
'/auth/oauth2/token', | ||
content_type='application/x-www-form-urlencoded', | ||
data={ | ||
'refresh_token': regular_user_oauth2_token.refresh_token, | ||
'client_id': regular_user_oauth2_token.client.client_id, | ||
'client_secret': regular_user_oauth2_token.client.client_secret, | ||
'grant_type': 'refresh_token', | ||
}, | ||
) | ||
|
||
assert refresh_token_response.status_code == 200 | ||
assert set(refresh_token_response.json.keys()) >= {'access_token'} | ||
|
||
|
||
def test_regular_user_cant_refresh_token_with_invalid_refresh_token( | ||
flask_app_client, | ||
regular_user_oauth2_token, | ||
): | ||
refresh_token_response = flask_app_client.post( | ||
'/auth/oauth2/token', | ||
content_type='application/x-www-form-urlencoded', | ||
data={ | ||
'refresh_token': 'wrong_refresh_token', | ||
'client_id': regular_user_oauth2_token.client.client_id, | ||
'client_secret': regular_user_oauth2_token.client.client_secret, | ||
'grant_type': 'refresh_token', | ||
}, | ||
) | ||
|
||
assert refresh_token_response.status_code == 401 | ||
|
||
|
||
def test_user_cant_refresh_token_without_any_data( | ||
flask_app_client, | ||
): | ||
refresh_token_response = flask_app_client.post( | ||
'/auth/oauth2/token', | ||
content_type='application/x-www-form-urlencoded', | ||
data={}, | ||
) | ||
|
||
assert refresh_token_response.status_code == 400 | ||
|
||
|
||
# There is a bug in flask-oauthlib: https://github.com/lepture/flask-oauthlib/issues/233 | ||
@pytest.mark.xfail | ||
def test_regular_user_can_revoke_token( | ||
flask_app_client, | ||
regular_user_oauth2_token, | ||
): | ||
data = { | ||
'token': regular_user_oauth2_token.refresh_token, | ||
'client_id': regular_user_oauth2_token.client.client_id, | ||
'client_secret': regular_user_oauth2_token.client.client_secret, | ||
} | ||
revoke_token_response = flask_app_client.post( | ||
'/auth/oauth2/revoke', | ||
content_type='application/x-www-form-urlencoded', | ||
headers={ | ||
'Authorization': 'Basic %s' % b64encode(('%s:%s' % (regular_user_oauth2_token.client.client_id, regular_user_oauth2_token.client.client_secret)).encode('utf-8')), | ||
}, | ||
data=data, | ||
) | ||
|
||
assert revoke_token_response.status_code == 200 |