Projet

Général

Profil

0001-WIP-add-role-creation-api-20706.patch

Paul Marillonnet, 12 janvier 2018 12:12

Télécharger (4,54 ko)

Voir les différences:

Subject: [PATCH] WIP add role creation api (#20706)

 src/authentic2/api_urls.py  |  2 ++
 src/authentic2/api_views.py | 47 +++++++++++++++++++++++++++++++++++++++++++++
 tests/test_api.py           | 20 +++++++++++++++++++
 3 files changed, 69 insertions(+)
src/authentic2/api_urls.py
13 13
                           api_views.role_memberships, name='a2-api-role-member'),
14 14
                       url(r'^check-password/$', api_views.check_password,
15 15
                           name='a2-api-check-password'),
16
                       url(r'^ous/(?P<ou_id_or_slug>[\w+]*)/roles/',
17
                           api_views.roles, name='a2-api-role'),
16 18
)
17 19
urlpatterns += api_views.router.urls
src/authentic2/api_views.py
576 576
        return Response({'result': 1})
577 577

  
578 578

  
579
class RolesAPI(ExceptionHandlerMixin, APIView):
580
    queryset = get_role_model()
581
    permission_classes = (permissions.IsAuthenticated,)
582
    _api_fetched_fields = ['uuid', 'name', 'slug', 'admin_scope_ct',
583
            'admin_scope_id', 'service']
584

  
585
    def post(self, request, *args, **kwargs):
586
        logger = logging.getLogger(__name__)
587

  
588
        try:
589
            Role = get_role_model()
590
            ou_id_or_slug = kwargs['ou_id_or_slug']
591
            role_data = request.body['role_data']
592
            missing_fields = app_settings.A2_ROLES_REQUIRED_FIELDS - role_data.keys()
593
            if missing_fields:
594
                return Response({'result': 0, 'detail': 'Missing fields in API request: %r' % missing_fields},
595
                                status=status.HTTP_400_BAD_REQUEST)
596
            if count(Role.objects.filter(slug=role_data['slug'])):
597
                return Response({'result': 0, 'detail': 'Error: role already existing in the A2 database'},
598
                                status=status.HTTP_400_BAD_REQUEST)
599
            if 'ou_id_or_slug' not in role_data:
600
                return Response({'result': 0, 'detail': 'Error: no OU identifier provided, role can\'t be created'},
601
                                status=status.HTTP_400_BAD_REQUEST)
602
            role = Role.objects.create()
603
            for api_field in self._api_fetched_fields:
604
                if api_field in role_data:
605
                    setattr(self, api_field, role_data[api_field])
606
            OU = get_ou_model()
607

  
608
            # try first on the OU uuid then on its slug
609
            try:
610
                ou = OU.objects.get(uuid=role_data.get('ou_id_or_slug'))
611
            except MultipleObjectsReturned:
612
                ou = OU.objects.get(slug=role_data.get('ou_id_or_slug'))
613
            role.ou = ou
614
            role.save()
615

  
616
        except Exception as e:
617
            logger.error('Couldn\'t create role')
618
            return Response({'result': 0, 'detail': e},
619
                            status=status.HTTP_400_BAD_REQUEST)
620

  
621
        return Response({'result': 1, 'detail': _('Role created')},
622
                        status=status.HTTP_201_CREATED)
623

  
624
roles = RolesAPI.as_view()
625

  
579 626
class RoleMembershipsAPI(ExceptionHandlerMixin, APIView):
580 627
    permission_classes = (permissions.IsAuthenticated,)
581 628

  
tests/test_api.py
30 30
    assert 'username' in resp.json
31 31

  
32 32

  
33
def test_api_role_simple(logged_app):
34
    resp = logged_app.post_json(
35
            '/api/ous/epsilon/roles',
36
            dict('role_data'={
37
                'slug': 'soma-officer',
38
                'uuid': 'H1GH',
39
                'name': 'Soma Officer',
40
                'service': 'default',
41
                'admin_scope_ct': 'foo',
42
                'admin_scope_id': 'bar'
43
                })
44
            )
45
    assert isinstance(resp.json, dict)
46
    assert 'role_data' in resp.json
47

  
48
    Role = get_role_model()
49
    posted_role = Role.objects.get(slug='soma-officer')
50
    assert posted_role.uuid == 'H1GH'
51

  
52

  
33 53
def test_api_user(client):
34 54
    # create an user, an ou role, a service and a service role
35 55
    ou = get_default_ou()
36
-