Project

General

Profile

0001-include-roles-in-users-api-25645.patch

Paul Marillonnet, 20 Sep 2018 03:41 PM

Download (4.15 KB)

View differences:

Subject: [PATCH] include roles in users api (#25645)

 src/authentic2/api_views.py |  7 +++++
 tests/test_api.py           | 55 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 61 insertions(+), 1 deletion(-)
src/authentic2/api_views.py
326 326
    def __init__(self, *args, **kwargs):
327 327
        super(BaseUserSerializer, self).__init__(*args, **kwargs)
328 328

  
329
        self.fields['roles'] = RoleCustomField(many=True, read_only=True)
330

  
329 331
        for at in Attribute.objects.all():
330 332
            if at.name in self.fields:
331 333
                self.fields[at.name].required = at.required
......
494 496
        extra_kwargs = {'uuid': {'read_only': True}}
495 497

  
496 498

  
499
class RoleCustomField(RoleSerializer):
500
    class Meta(RoleSerializer.Meta):
501
        fields = ('uuid', 'name', 'slug',)
502

  
503

  
497 504
class UsersFilter(FilterSet):
498 505
    class Meta:
499 506
        model = get_user_model()
tests/test_api.py
241 241
    if api_user.is_superuser or api_user.roles.exists():
242 242
        assert set(['ou', 'id', 'uuid', 'is_staff', 'is_superuser', 'first_name', 'last_name',
243 243
                    'date_joined', 'last_login', 'username', 'password', 'email', 'is_active',
244
                    'title', 'modified', 'email_verified']) == set(resp.json.keys())
244
                    'title', 'modified', 'email_verified', 'roles']) == set(resp.json.keys())
245 245
        assert resp.json['first_name'] == payload['first_name']
246 246
        assert resp.json['last_name'] == payload['last_name']
247 247
        assert resp.json['email'] == payload['email']
......
934 934
    assert response.json['checks'][3]['result'] is True
935 935
    assert response.json['checks'][4]['label'] == 'must contain "ok"'
936 936
    assert response.json['checks'][4]['result'] is True
937

  
938

  
939
def test_roles_in_users_api(app, admin):
940

  
941
    User = get_user_model()
942
    user1 = User(username='john.doe', email='john.doe@example.com')
943
    user1.set_password('password')
944
    user1.save()
945
    user2 = User(username='bob.smith', email='bob.smith@example.com')
946
    user2.set_password('password')
947
    user2.save()
948

  
949
    Role = get_role_model()
950
    role1 = Role.objects.create(name='Role1')
951
    role1.members.add(user1)
952
    role2 = Role.objects.create(name='Role2')
953
    role2.members.add(user1)
954
    role2.members.add(user2)
955
    role3 = Role.objects.create(name='Role3')
956
    role3.members.add(user2)
957

  
958
    app.authorization = ('Basic', (admin.username, admin.username))
959
    response = app.get(u'/api/users/', status=200)
960
    assert len(response.json['results']) == 3
961
    for user in response.json['results']:
962
        assert user['roles']
963
        for role in user['roles']:
964
            keys = ['slug', 'name', 'uuid']
965
            for key, value in role.items():
966
                assert key in keys
967
                assert value
968
                keys.remove(key)
969

  
970
    url = u'/api/users/%s/' % admin.uuid
971
    response = app.get(url, status=200)
972
    assert len(response.json['roles']) == 1
973
    assert response.json['roles'][0]['slug'] == '_a2-manager'
974

  
975
    url = u'/api/users/%s/' % user1.uuid
976
    response = app.get(url, status=200)
977
    assert len(response.json['roles']) == 2
978
    user_roles = ['Role1', 'Role2']
979
    for role in response.json['roles']:
980
        assert role['name'] in user_roles
981
        user_roles.remove(role['name'])
982

  
983
    url = u'/api/users/%s/' % user2.uuid
984
    response = app.get(url, status=200)
985
    assert len(response.json['roles']) == 2
986
    user_roles = ['Role2', 'Role3']
987
    for role in response.json['roles']:
988
        assert role['name'] in user_roles
989
        user_roles.remove(role['name'])
937
-