astre_rest: add auth parameter (#62924)
This commit is contained in:
parent
55810f7755
commit
dcb996cfdf
|
@ -0,0 +1,23 @@
|
|||
# Generated by Django 2.2.24 on 2022-03-21 17:23
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('astre_rest', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='astrerest',
|
||||
name='auth',
|
||||
field=models.CharField(
|
||||
blank=True,
|
||||
choices=[('AD', 'AD'), ('EXTERNE', 'EXTERNE'), ('LDAP', 'LDAP'), ('NORMAL', 'NORMAL')],
|
||||
max_length=32,
|
||||
verbose_name='Authentication mode',
|
||||
),
|
||||
),
|
||||
]
|
|
@ -164,6 +164,13 @@ ASTRE_ERRORS = (
|
|||
'AstreException',
|
||||
)
|
||||
|
||||
AUTH_CHOICES = [
|
||||
('AD', 'AD'),
|
||||
('EXTERNE', 'EXTERNE'),
|
||||
('LDAP', 'LDAP'),
|
||||
('NORMAL', 'NORMAL'),
|
||||
]
|
||||
|
||||
|
||||
class AstreREST(BaseResource):
|
||||
|
||||
|
@ -171,6 +178,7 @@ class AstreREST(BaseResource):
|
|||
username = models.CharField(_('Username'), max_length=32)
|
||||
password = models.CharField(_('Password'), max_length=32)
|
||||
database = models.CharField('Database', max_length=32)
|
||||
auth = models.CharField(_('Authentication mode'), max_length=32, choices=AUTH_CHOICES, blank=True)
|
||||
organism = models.CharField('Organisme', max_length=32)
|
||||
budget = models.CharField('Budget', max_length=32)
|
||||
exercice = models.CharField('Exercice', max_length=32)
|
||||
|
@ -194,19 +202,22 @@ class AstreREST(BaseResource):
|
|||
|
||||
def _astre_call(self, path, method='get', params={}, data=None, files=None, expect_json=True):
|
||||
url = urllib.parse.urljoin(self.base_url, path)
|
||||
authentication_params = {
|
||||
'login': self.username,
|
||||
'password': self.encrypted_password,
|
||||
'database': self.database,
|
||||
}
|
||||
if self.auth:
|
||||
authentication_params['auth'] = self.auth
|
||||
|
||||
kwargs = {}
|
||||
if method == 'get':
|
||||
kwargs['headers'] = {
|
||||
'login': self.username,
|
||||
'password': self.encrypted_password,
|
||||
'database': self.database,
|
||||
}
|
||||
kwargs['headers'] = authentication_params
|
||||
|
||||
elif method == 'post':
|
||||
data['login'] = self.username
|
||||
data['password'] = self.encrypted_password
|
||||
data['database'] = self.database
|
||||
if not data:
|
||||
data = {}
|
||||
data.update(authentication_params)
|
||||
kwargs['data'] = data
|
||||
|
||||
resp = getattr(self.requests, method)(url, params=params, **kwargs)
|
||||
|
|
|
@ -3,6 +3,7 @@ import json
|
|||
|
||||
import httmock
|
||||
import pytest
|
||||
import responses
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
|
||||
from passerelle.apps.astre_rest.models import AstreREST
|
||||
|
@ -30,6 +31,31 @@ def connector(db):
|
|||
return connector
|
||||
|
||||
|
||||
@responses.activate
|
||||
def test_authentication(app, connector):
|
||||
assert not connector.auth
|
||||
responses.add(
|
||||
responses.GET,
|
||||
'http://example.astre.net/astre/webservices/gf/documents/entites/getRefEntite/somebigcity-2022-01/who-what',
|
||||
body='$55101$',
|
||||
status=200,
|
||||
)
|
||||
|
||||
resp = app.get('/astre-rest/slug-astre-rest/gf-documents-entites-getref?entity_type=who&entity_code=what')
|
||||
assert len(responses.calls) == 1
|
||||
assert responses.calls[0].request.headers['login'] == 'admin'
|
||||
assert responses.calls[0].request.headers['database'] == 'db'
|
||||
assert 'auth' not in responses.calls[0].request.headers
|
||||
|
||||
connector.auth = 'AD'
|
||||
connector.save()
|
||||
resp = app.get('/astre-rest/slug-astre-rest/gf-documents-entites-getref?entity_type=who&entity_code=what')
|
||||
assert len(responses.calls) == 2
|
||||
assert responses.calls[1].request.headers['login'] == 'admin'
|
||||
assert responses.calls[1].request.headers['database'] == 'db'
|
||||
assert responses.calls[1].request.headers['auth'] == 'AD'
|
||||
|
||||
|
||||
def test_gf_documents_entites_getref(app, connector):
|
||||
@httmock.urlmatch(
|
||||
netloc='example.astre.net',
|
||||
|
|
Loading…
Reference in New Issue