-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #347 from Wirecloud/feature/keyrock-v7-remove-v5
Improve KeyRock v7 support and remove v5
- Loading branch information
Showing
3 changed files
with
80 additions
and
40 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
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
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 |
---|---|---|
|
@@ -35,6 +35,20 @@ class BasicClass(object): | |
def __init__(self): | ||
pass | ||
|
||
def extra_data(self, user, uid, response, details=None, *args, **kwargs): | ||
return { | ||
"access_token": "access_token", | ||
"refresh_token": "refresh_token", | ||
"expires_in": 3600 | ||
} | ||
|
||
def refresh_token(self, token, *args, **kwargs): | ||
return { | ||
"access_token": "new_access_token", | ||
"refresh_token": "new_refresh_token", | ||
"expires_in": 3600 | ||
} | ||
|
||
@classmethod | ||
def get_key_and_secret(cls): | ||
return ('client', 'secret') | ||
|
@@ -46,25 +60,24 @@ class TestSocialAuthBackend(WirecloudTestCase, TestCase): | |
populate = False | ||
use_search_indexes = False | ||
|
||
# KeyRock v6 | ||
OLD_RESPONSE = { | ||
"schemas": ["urn:scim:schemas:core:2.0:User"], | ||
"id": 1, | ||
"actorId": 1, | ||
"nickName": "demo", | ||
"id": "demo", | ||
"username": "demo", | ||
"displayName": "Demo user", | ||
"email": "[email protected]", | ||
"roles": [{"id": 1, "name": "Manager"}, {"id": 7, "name": "Ticket manager"}], | ||
"roles": [{"id": "1", "name": "Manager"}, {"id": "7", "name": "Ticket manager"}], | ||
"organizations": [{ | ||
"id": 1, | ||
"actorId": 2, | ||
"id": "00000000000000000000000000000001", | ||
"displayName": "Universidad Politecnica de Madrid", | ||
"roles": [{"id": 14, "name": "Admin"}] | ||
}] | ||
} | ||
|
||
NEW_RESPONSE = { | ||
OLD_RESPONSE_NO_LAST_NAME = { | ||
"id": "demo", | ||
"displayName": "Demo user", | ||
"username": "demo", | ||
"displayName": "Demo", | ||
"email": "[email protected]", | ||
"roles": [{"id": "1", "name": "Manager"}, {"id": "7", "name": "Ticket manager"}], | ||
"organizations": [{ | ||
|
@@ -74,21 +87,26 @@ class TestSocialAuthBackend(WirecloudTestCase, TestCase): | |
}] | ||
} | ||
|
||
RESPONSE_NO_LAST_NAME = { | ||
"id": "demo", | ||
# KeyRock v7 | ||
NEW_RESPONSE = { | ||
"id": "8b0127d8-38f7-4428-b22d-31bd80bba510", | ||
"displayName": "", | ||
"username": "demo", | ||
"displayName": "Demo", | ||
"email": "[email protected]", | ||
"roles": [{"id": "1", "name": "Manager"}, {"id": "7", "name": "Ticket manager"}], | ||
"roles": [{"id": "4a923351-b767-4fef-bc92-4a4fa996e88e", "name": "Manager"}, {"id": "4a92as51-b54d-4fef-bc92-4a4fa996e88e", "name": "Ticket manager"}], | ||
"organizations": [{ | ||
"id": "00000000000000000000000000000001", | ||
"displayName": "Universidad Politecnica de Madrid", | ||
"roles": [{"id": 14, "name": "Admin"}] | ||
"id": "04ac28b2-54c7-46a7-a606-c62fdc4f1513", | ||
"name": "Mi organization", | ||
"description":"dafsdf", | ||
"website": None, | ||
"roles":[{"id": "4a923351-b767-4fef-bc92-4a4fa996e88e", "name":"one_role"}] | ||
}] | ||
} | ||
|
||
USER_DATA = {"username": "demo", "email": "[email protected]", "fullname": "Demo user", "first_name": "Demo", "last_name": "user", "is_superuser": False, "is_staff": False} | ||
NEW_USER_DATA = {"username": "demo", "email": "[email protected]", "fullname": "", "first_name": "", "last_name": "", "is_superuser": False, "is_staff": False} | ||
USER_DATA_ADMIN = {"username": "demo", "email": "[email protected]", "fullname": "Demo user", "first_name": "Demo", "last_name": "user", "is_superuser": True, "is_staff": True} | ||
NEW_USER_DATA_ADMIN = {"username": "demo", "email": "[email protected]", "fullname": "", "first_name": "", "last_name": "", "is_superuser": True, "is_staff": True} | ||
USER_DATA_NO_LAST_NAME = {"username": "demo", "email": "[email protected]", "fullname": "Demo", "first_name": "Demo", "last_name": "", "is_superuser": False, "is_staff": False} | ||
|
||
def setUp(self): | ||
|
@@ -129,7 +147,7 @@ def test_get_user_data_old_version(self): | |
self.assertIn('username', data) | ||
self.assertEqual(data['username'], 'demo') | ||
self.assertIn('id', data['organizations'][0]) | ||
self.assertEqual(data['organizations'][0]['id'], 2) | ||
self.assertEqual(data['organizations'][0]['id'], "00000000000000000000000000000001") | ||
|
||
def test_get_user_data_new_version(self): | ||
|
||
|
@@ -139,7 +157,7 @@ def test_get_user_data_new_version(self): | |
self.assertIn('username', data) | ||
self.assertEqual(data['username'], 'demo') | ||
self.assertIn('id', data['organizations'][0]) | ||
self.assertEqual(data['organizations'][0]['id'], "00000000000000000000000000000001") | ||
self.assertEqual(data['organizations'][0]['id'], "04ac28b2-54c7-46a7-a606-c62fdc4f1513") | ||
|
||
def test_get_user_data_invalid_response(self): | ||
|
||
|
@@ -154,6 +172,20 @@ def test_auth_headers(self): | |
self.assertIn('Basic ', headers['Authorization']) | ||
self.assertEqual(headers['Authorization'], 'Basic Y2xpZW50OnNlY3JldA==') | ||
|
||
@patch("wirecloud.fiware.social_auth_backend.time.time") | ||
def test_extra_data(self, time_mock): | ||
|
||
time_mock.return_value = 10000 | ||
|
||
data = self.instance.extra_data("user", "uid", "response") | ||
|
||
self.assertEqual(data, { | ||
"access_token": "access_token", | ||
"refresh_token": "refresh_token", | ||
"expires_in": 3600, | ||
"expires_on": 13600 | ||
}) | ||
|
||
def test_get_user_details_old_version(self): | ||
|
||
response = deepcopy(self.OLD_RESPONSE) | ||
|
@@ -165,33 +197,48 @@ def test_get_user_details_old_version(self): | |
def test_get_user_details_old_version_admin(self): | ||
|
||
response = deepcopy(self.OLD_RESPONSE) | ||
response['roles'][0]['name'] = 'admin' | ||
response['roles'][0]['name'] = 'Admin' | ||
data = self.instance.get_user_details(response) | ||
|
||
self.assertEqual(data, self.USER_DATA_ADMIN) | ||
|
||
def test_get_user_details_old_version_no_last_name(self): | ||
|
||
response = deepcopy(self.OLD_RESPONSE_NO_LAST_NAME) | ||
data = self.instance.get_user_details(response) | ||
|
||
self.assertEqual(data, self.USER_DATA_NO_LAST_NAME) | ||
|
||
def test_get_user_details_new_version(self): | ||
|
||
response = deepcopy(self.NEW_RESPONSE) | ||
response['username'] = 'demo' | ||
data = self.instance.get_user_details(response) | ||
|
||
self.assertEqual(data, self.USER_DATA) | ||
self.assertEqual(data, self.NEW_USER_DATA) | ||
|
||
def test_get_user_details_new_version_admin(self): | ||
|
||
response = deepcopy(self.NEW_RESPONSE) | ||
response['roles'][0]['name'] = 'Admin' | ||
data = self.instance.get_user_details(response) | ||
|
||
self.assertEqual(data, self.USER_DATA_ADMIN) | ||
self.assertEqual(data, self.NEW_USER_DATA_ADMIN) | ||
|
||
def test_get_user_details_no_last_name(self): | ||
@patch("wirecloud.fiware.social_auth_backend.time.time") | ||
def test_refresh_token_normalizes_token_expiration_time(self, time_mock): | ||
|
||
response = deepcopy(self.RESPONSE_NO_LAST_NAME) | ||
data = self.instance.get_user_details(response) | ||
time_mock.return_value = 10000 | ||
|
||
self.assertEqual(data, self.USER_DATA_NO_LAST_NAME) | ||
data = self.instance.refresh_token("old_access_token") | ||
|
||
self.assertEqual(data, { | ||
"access_token": "new_access_token", | ||
"refresh_token": "new_refresh_token", | ||
"expires_in": 3600, | ||
"expires_on": 13600, | ||
"openstack_token": None | ||
}) | ||
|
||
def test_request_user_info(self): | ||
|
||
|