9 |
9 |
# This program is distributed in the hope that it will be useful,
|
10 |
10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
12 |
# GNU Affero General Public License for more details.
|
13 |
13 |
#
|
14 |
14 |
# You should have received a copy of the GNU Affero General Public License
|
15 |
15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16 |
16 |
|
|
17 |
import base64
|
17 |
18 |
import json
|
18 |
19 |
import httmock
|
19 |
20 |
import pytest
|
20 |
21 |
|
|
22 |
from Cryptodome.Cipher import DES
|
|
23 |
from Cryptodome.Util.Padding import pad, unpad
|
|
24 |
|
|
25 |
from django.utils.encoding import force_bytes
|
|
26 |
|
21 |
27 |
from passerelle.apps.esirius.models import ESirius
|
22 |
28 |
from passerelle.utils.jsonresponse import APIError
|
23 |
29 |
|
24 |
30 |
from test_manager import login
|
25 |
31 |
import utils
|
26 |
32 |
|
27 |
33 |
|
28 |
34 |
CREATE_APPOINTMENT_PAYLOAD = {
|
... | ... | |
101 |
107 |
== b'yM4zYAxT67Qvjd20riG3j0eu0t0Ku+HLlttj17Gul7zkruFaXX1J8BJ6sV2Ldgw40axfWh+ESAY='
|
102 |
108 |
)
|
103 |
109 |
return httmock.response(200)
|
104 |
110 |
|
105 |
111 |
with httmock.HTTMock(esirius_mock):
|
106 |
112 |
connector.request('an/uri/', method='get', params="somes")
|
107 |
113 |
|
108 |
114 |
|
109 |
|
@pytest.mark.parametrize('secret_key', ['xxx', ''])
|
|
115 |
@pytest.mark.parametrize('secret_key', ['yyy', ''])
|
110 |
116 |
def test_pre_request(connector, secret_key):
|
111 |
117 |
@httmock.urlmatch(netloc='esirius.example.net', path='/an/uri/', method='GET')
|
112 |
118 |
def esirius_mock(url, request):
|
113 |
119 |
assert request.headers['Accept'] == 'application/json; charset=utf-8'
|
114 |
120 |
assert bool(request.headers.get('token_info_caller')) == bool(secret_key)
|
115 |
121 |
if secret_key:
|
116 |
|
assert request.headers['token_info_caller'][:42] == b'f3G6sjRZETBam6vcdrAxmvJQTX5hh6OjZ8XlUO6SMo'
|
|
122 |
des_key = pad(force_bytes(secret_key), 8)[:8]
|
|
123 |
cipher = DES.new(des_key, DES.MODE_ECB)
|
|
124 |
msg = cipher.decrypt(base64.b64decode(request.headers['token_info_caller']))
|
|
125 |
token = json.loads(unpad(msg, 8))
|
|
126 |
assert set(token) == {'caller', 'createInfo'}
|
|
127 |
assert token['caller'] == connector.secret_id
|
117 |
128 |
return httmock.response(200)
|
118 |
129 |
|
119 |
130 |
connector.secret_key = secret_key
|
120 |
131 |
connector.save()
|
121 |
132 |
with httmock.HTTMock(esirius_mock):
|
122 |
133 |
connector.request('an/uri/', method='get', params="somes")
|
123 |
134 |
|
124 |
135 |
|
125 |
|
-
|