|
1 |
import re
|
|
2 |
import sys
|
|
3 |
import mock
|
|
4 |
|
|
5 |
from django.contrib.auth.models import User
|
|
6 |
from django.core.wsgi import get_wsgi_application
|
|
7 |
import pytest
|
|
8 |
from webtest import TestApp
|
|
9 |
|
|
10 |
from passerelle.base import signature
|
|
11 |
|
|
12 |
pytestmark = pytest.mark.django_db
|
|
13 |
|
|
14 |
@pytest.fixture
|
|
15 |
def admin_user():
|
|
16 |
try:
|
|
17 |
user = User.objects.get(username='admin')
|
|
18 |
except User.DoesNotExist:
|
|
19 |
user = User.objects.create_superuser('admin', email=None, password='admin')
|
|
20 |
return user
|
|
21 |
|
|
22 |
def login(app, username='admin', password='admin'):
|
|
23 |
login_page = app.get('/login/')
|
|
24 |
login_form = login_page.forms[0]
|
|
25 |
login_form['username'] = username
|
|
26 |
login_form['password'] = password
|
|
27 |
resp = login_form.submit()
|
|
28 |
assert resp.status_int == 302
|
|
29 |
return app
|
|
30 |
|
|
31 |
def create_api_user(app):
|
|
32 |
# go to "Access Management" page
|
|
33 |
resp = app.get('/manage/access/', status=200)
|
|
34 |
assert 'Add API User' in resp.body
|
|
35 |
resp = resp.click('Add API User')
|
|
36 |
resp.forms[0]['username'] = 'eservices'
|
|
37 |
resp.forms[0]['fullname'] = 'Eservices User'
|
|
38 |
resp.forms[0]['keytype'] = 'SIGN'
|
|
39 |
resp.forms[0]['key'] = '12345'
|
|
40 |
resp = resp.forms[0].submit()
|
|
41 |
assert resp.status_int == 302
|
|
42 |
assert resp.location == 'http://localhost:80/manage/access/'
|
|
43 |
resp = resp.follow()
|
|
44 |
assert 'Eservices User' in resp.body
|
|
45 |
assert 'SIGN' in resp.body
|
|
46 |
return app
|
|
47 |
|
|
48 |
@mock.patch('bdp.models.requests.get')
|
|
49 |
def test_connector_access(mock_get, admin_user):
|
|
50 |
app = login(TestApp(get_wsgi_application()))
|
|
51 |
app = create_api_user(app)
|
|
52 |
resp = app.get('/manage/', status=200)
|
|
53 |
resp = resp.click('Add Connector')
|
|
54 |
assert 'Business Process Connectors' in resp.body
|
|
55 |
assert 'BDP Web Service' in resp.body
|
|
56 |
resp = resp.click('BDP Web Service')
|
|
57 |
resp.forms[0]['title'] = 'Test Connector'
|
|
58 |
resp.forms[0]['description'] = 'Connector for a simple test'
|
|
59 |
resp.forms[0]['service_url'] = 'http://service.url'
|
|
60 |
resp.forms[0]['username'] = 'username'
|
|
61 |
resp.forms[0]['password'] = 'password'
|
|
62 |
resp = resp.forms[0].submit()
|
|
63 |
assert resp.status_int == 302
|
|
64 |
assert resp.location == 'http://localhost:80/bdp/test-connector/'
|
|
65 |
resp = resp.follow()
|
|
66 |
assert 'BDP - Test Connector' in resp.body
|
|
67 |
resp = app.get('/manage/', status=200)
|
|
68 |
assert 'Test Connector' in resp.body
|
|
69 |
resp = resp.click('Test Connector')
|
|
70 |
assert 'Add' in resp.body
|
|
71 |
resp = resp.click('Add')
|
|
72 |
resp.forms[0]['apiuser'] = 1
|
|
73 |
resp.forms[0].submit()
|
|
74 |
# call with no signature
|
|
75 |
resp = app.get('/bdp/test-connector/resources/', status=403)
|
|
76 |
# sign url
|
|
77 |
url = signature.sign_url('/bdp/test-connector/resources/?orig=eservices', '12345')
|
|
78 |
response = mock.Mock()
|
|
79 |
response.json.return_value = {}
|
|
80 |
mock_get.return_value = response
|
|
81 |
resp = app.get(url, status=200)
|