From da814db193897da1c765ba3a690abb10b38eeee8 Mon Sep 17 00:00:00 2001 From: index-git Date: Sat, 5 Dec 2020 17:54:27 +0100 Subject: [PATCH] Preferred_username is part of rest_users.get and rest_current-user.get response as screen_name. Closes #178 --- doc/rest.md | 1 + src/layman/authn/oauth2_test.py | 13 +++++++------ src/layman/user/rest_users.py | 1 + src/layman/user/util.py | 2 ++ 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/doc/rest.md b/doc/rest.md index 766185d91..f9e2118e6 100644 --- a/doc/rest.md +++ b/doc/rest.md @@ -576,6 +576,7 @@ Content-Type: `application/json` JSON array of objects representing users of Layman with following structure: - **username**: String. Username of the user. +- **screen_name**: String. Screen name of the user. - **given_name**: String. Given name of the user. - **family_name**: String. Family name of the user - **middle_name**: String. Middle name of the user diff --git a/src/layman/authn/oauth2_test.py b/src/layman/authn/oauth2_test.py index 6b0160f5e..5dde0c036 100644 --- a/src/layman/authn/oauth2_test.py +++ b/src/layman/authn/oauth2_test.py @@ -242,7 +242,7 @@ def test_authn_get_current_user_without_username(client): claims = resp_json['claims'] assert { 'email', 'email_verified', 'family_name', 'given_name', 'iss', 'middle_name', 'name', - 'preferred_username', 'sub', 'updated_at' + 'preferred_username', 'sub', 'updated_at', 'screen_name' } == set(claims.keys()) assert claims['email'] == 'test@liferay.com' assert claims['email_verified'] is True @@ -251,6 +251,7 @@ def test_authn_get_current_user_without_username(client): assert claims['middle_name'] == '' assert claims['name'] == 'Test Test' assert claims['preferred_username'] == 'test' + assert claims['screen_name'] == 'test' assert claims['sub'] == '20139' @@ -260,14 +261,14 @@ def test_get_current_user_anonymous(client): rv = client.get(rest_path) assert rv.status_code == 200 resp_json = rv.get_json() - assert resp_json['authenticated'] is False - assert {'authenticated', 'claims'} == set(resp_json.keys()) + assert resp_json['authenticated'] is False, resp_json + assert {'authenticated', 'claims'} == set(resp_json.keys()), resp_json claims = resp_json['claims'] assert { 'iss', 'name', 'nickname' - } == set(claims.keys()) - assert claims['name'] == 'Anonymous' - assert claims['nickname'] == 'Anonymous' + } == set(claims.keys()), claims + assert claims['name'] == 'Anonymous', claims + assert claims['nickname'] == 'Anonymous', claims @pytest.mark.usefixtures('app_context') diff --git a/src/layman/user/rest_users.py b/src/layman/user/rest_users.py index 8fa16b1ef..99ef0459b 100644 --- a/src/layman/user/rest_users.py +++ b/src/layman/user/rest_users.py @@ -21,6 +21,7 @@ def get(): infos = [ { "username": username, + "screen_name": info.get("preferred_username"), "given_name": info.get("given_name"), "family_name": info.get("family_name"), "middle_name": info.get("middle_name"), diff --git a/src/layman/user/util.py b/src/layman/user/util.py index 2ee39f95b..8c5b45444 100644 --- a/src/layman/user/util.py +++ b/src/layman/user/util.py @@ -19,6 +19,8 @@ def get_user_profile(user_obj): result = {k: v for k, v in result.items() if v is not None} claims = get_open_id_claims().copy() result['claims'] = claims + if result['claims'].get('preferred_username'): + result['claims']['screen_name'] = result['claims']['preferred_username'] return result