Projet

Général

Profil

0001-api-use-formdata-user-on-formdata.get_json_export_di.patch

Lauréline Guérin, 15 novembre 2022 11:51

Télécharger (5,51 ko)

Voir les différences:

Subject: [PATCH 1/3] api: use formdata user on formdata.get_json_export_dict
 (#71303)

and not user calling the api
 tests/api/test_carddata.py | 71 ++++++++++++++++++++++++++++++++++++++
 wcs/formdata.py            | 25 ++++++++------
 2 files changed, 85 insertions(+), 11 deletions(-)
tests/api/test_carddata.py
94 94
    )
95 95

  
96 96

  
97
@pytest.mark.parametrize('auth', ['signature', 'http-basic', 'api-admin-user'])
98
def test_carddata_include_params(pub, local_user, auth):
99
    # signature: SqlUser
100
    # http-basic: RestrictedApiUser
101
    # api-admin-user: ApiAdminUser
102
    pub.role_class.wipe()
103
    role = pub.role_class(name='test')
104
    role.id = '123'
105
    role.store()
106
    local_user.roles = [role.id]
107
    local_user.store()
108

  
109
    CardDef.wipe()
110
    carddef = CardDef()
111
    carddef.name = 'test'
112
    carddef.workflow_roles = {'_viewer': role.id}
113
    carddef.fields = [
114
        fields.StringField(id='1', label='foobar', varname='foobar'),
115
    ]
116
    carddef.store()
117

  
118
    carddef.data_class().wipe()
119
    carddata = carddef.data_class()()
120
    carddata.data = {'1': 'FOO BAR'}
121
    carddata.user_id = local_user.id
122
    carddata.just_created()
123
    carddata.store()
124

  
125
    ApiAccess.wipe()
126
    access = ApiAccess()
127
    access.name = 'test'
128
    access.access_identifier = 'test'
129
    access.access_key = '12345'
130
    access.store()
131

  
132
    app = get_app(pub)
133

  
134
    if auth == 'http-basic':
135
        # there's not "defaults to admin" permissions in case of basic authentication.
136
        access.roles = [role]
137
        access.store()
138

  
139
        def get_url(url, **kwargs):
140
            app.set_authorization(('Basic', ('test', '12345')))
141
            return app.get(url, **kwargs)
142

  
143
    else:
144

  
145
        def get_url(url, **kwargs):
146
            return app.get(
147
                sign_uri(
148
                    url,
149
                    user=local_user if auth == 'signature' else None,
150
                    orig=access.access_identifier,
151
                    key=access.access_key,
152
                ),
153
                **kwargs,
154
            )
155

  
156
    resp = get_url('/api/cards/test/list?include-fields=on')
157
    assert 'fields' in resp.json['data'][0]
158
    resp = get_url('/api/cards/test/list?include-evolution=on')
159
    assert 'evolution' in resp.json['data'][0]
160
    resp = get_url('/api/cards/test/list?include-roles=on')
161
    assert 'roles' in resp.json['data'][0]
162
    resp = get_url('/api/cards/test/list?include-submission=on')
163
    assert 'submission' in resp.json['data'][0]
164
    resp = get_url('/api/cards/test/list?include-workflow=on')
165
    assert 'workflow' in resp.json['data'][0]
166

  
167

  
97 168
def test_carddata_user_fields(pub, local_user):
98 169
    pub.role_class.wipe()
99 170
    role = pub.role_class(name='test')
wcs/formdata.py
1373 1373
        data['receipt_time'] = datetime.datetime(*self.receipt_time[:6])
1374 1374
        data['last_update_time'] = datetime.datetime(*self.last_update_time[:6])
1375 1375

  
1376
        formdata_user = None
1377
        if include_fields or include_workflow or include_evolution:
1378
            try:
1379
                if prefetched_users is not None:
1380
                    formdata_user = prefetched_users.get(str(self.user_id))
1381
                else:
1382
                    formdata_user = get_publisher().user_class.get(self.user_id)
1383
            except KeyError:
1384
                pass
1385

  
1376 1386
        if include_fields:
1377 1387
            data['criticality_level'] = self.criticality_level
1378 1388
            data['api_url'] = self.get_api_url()
1379 1389
            data['backoffice_url'] = self.get_backoffice_url()
1380 1390

  
1381
            try:
1382
                if prefetched_users is not None:
1383
                    user = prefetched_users.get(str(self.user_id))
1384
                else:
1385
                    user = get_publisher().user_class.get(self.user_id)
1386
            except KeyError:
1387
                user = None
1388
            if not anonymise and user:
1391
            if not anonymise and formdata_user:
1389 1392
                from .carddef import CardDef
1390 1393

  
1391
                data['user'] = user.get_json_export_dict(full=isinstance(self.formdef, CardDef))
1394
                data['user'] = formdata_user.get_json_export_dict(full=isinstance(self.formdef, CardDef))
1392 1395

  
1393 1396
            data['fields'] = self.get_json_dict(
1394 1397
                self.formdef.fields,
......
1399 1402

  
1400 1403
        if include_workflow:
1401 1404
            data['workflow'] = {}
1402
            wf_status = self.get_visible_status(user)
1405
            wf_status = self.get_visible_status(formdata_user)
1403 1406
            if wf_status:
1404 1407
                data['workflow']['status'] = {'id': wf_status.id, 'name': wf_status.name}
1405 1408
            wf_real_status = self.get_status()
......
1472 1475
            for evo in self.evolution:
1473 1476
                evolution.append(
1474 1477
                    evo.get_json_export_dict(
1475
                        user=None if anonymise else user,
1478
                        user=None if anonymise else formdata_user,
1476 1479
                        anonymise=anonymise,
1477 1480
                        include_files=include_files,
1478 1481
                    )
1479
-