0001-environment-add-service-creation-api-31449.patch
hobo/environment/views.py | ||
---|---|---|
8 | 8 |
from django.shortcuts import get_object_or_404 |
9 | 9 |
from django.views.generic.base import TemplateView |
10 | 10 |
from django.views.generic.edit import CreateView, UpdateView, DeleteView |
11 |
from django.views.decorators.csrf import csrf_exempt |
|
12 |
from rest_framework.decorators import api_view, permission_classes, authentication_classes |
|
13 |
from rest_framework.authentication import SessionAuthentication, BasicAuthentication |
|
14 |
from rest_framework.permissions import IsAuthenticated |
|
11 | 15 | |
12 | 16 |
from .models import Variable, AVAILABLE_SERVICES |
13 |
from . import forms, utils |
|
17 |
from . import forms, models, utils
|
|
14 | 18 | |
15 | 19 | |
16 | 20 |
class AvailableService(object): |
... | ... | |
104 | 108 |
return reverse_lazy('environment-home') |
105 | 109 | |
106 | 110 | |
111 |
@csrf_exempt |
|
112 |
@authentication_classes((SessionAuthentication, BasicAuthentication)) |
|
113 |
@permission_classes((IsAuthenticated,)) |
|
114 |
def ServiceCreateApi(request, service): |
|
115 |
from django.http import JsonResponse |
|
116 |
if request.method != 'POST' or service not in [s.Extra.service_id for s in AVAILABLE_SERVICES]: |
|
117 |
return JsonResponse({'error': 1, 'data': {'message': ''}}) |
|
118 |
klass = getattr(models, service.title()) |
|
119 |
try: |
|
120 |
data = {k: v for k, v in request.POST.iteritems()} |
|
121 |
obj = klass(**data) |
|
122 |
obj.save() |
|
123 |
#notify_agents(None) |
|
124 |
return JsonResponse({'error': 0, 'data': {'id': obj.id}}) |
|
125 |
except Exception: |
|
126 |
return JsonResponse({'error': 1, 'data': {'message': 'failed'}}) |
|
127 | ||
128 | ||
107 | 129 |
class ServiceCreateView(CreateView): |
108 | 130 |
success_url = reverse_lazy('environment-home') |
109 | 131 |
hobo/urls.py | ||
---|---|---|
7 | 7 |
from .views import admin_required, login, login_local, logout, home, health_json, menu_json, hobo |
8 | 8 |
from .urls_utils import decorated_includes |
9 | 9 |
from .environment.urls import urlpatterns as environment_urls |
10 |
from .environment.views import ServiceCreateApi |
|
10 | 11 |
from .franceconnect.urls import urlpatterns as franceconnect_urls |
11 | 12 |
from .matomo.urls import urlpatterns as matomo_urls |
12 | 13 |
from .profile.urls import urlpatterns as profile_urls |
... | ... | |
27 | 28 |
include(theme_urls))), |
28 | 29 |
url(r'^emails/', decorated_includes(admin_required, include(emails_urls))), |
29 | 30 |
url(r'^api/health/$', health_json, name='health-json'), |
31 |
url(r'^api/sites/create/(?P<service>\w+)/$', ServiceCreateApi, name='api-create-service'), |
|
30 | 32 |
url(r'^menu.json$', menu_json, name='menu_json'), |
31 | 33 |
url(r'^hobos.json$', hobo), |
32 | 34 |
url(r'^admin/', include(admin.site.urls)), |
tests/test_environment.py | ||
---|---|---|
1 | 1 |
# -*- coding: utf-8 -*- |
2 |
import json |
|
2 | 3 |
import pytest |
3 | 4 | |
5 |
from django.contrib.auth.models import User |
|
4 | 6 |
from django.core.exceptions import ValidationError |
5 | 7 |
from django.utils import timezone |
8 | ||
6 | 9 |
from hobo.environment.models import AVAILABLE_SERVICES, Combo, Passerelle |
7 | 10 | |
8 | 11 |
pytestmark = pytest.mark.django_db |
9 | 12 | |
13 | ||
14 |
def login(app, username='admin', password='password'): |
|
15 |
login_page = app.get('/login/') |
|
16 |
login_form = login_page.forms[0] |
|
17 |
login_form['username'] = username |
|
18 |
login_form['password'] = password |
|
19 |
resp = login_form.submit() |
|
20 |
assert resp.status_int == 302 |
|
21 |
return app |
|
22 | ||
23 | ||
10 | 24 |
def test_service_id(): |
11 | 25 |
for service in AVAILABLE_SERVICES: |
12 | 26 |
assert service.Extra.service_id |
... | ... | |
58 | 72 |
combo = Combo(base_url=url, slug='wesh_'+str(cpt), **kwargs) |
59 | 73 |
combo.full_clean() |
60 | 74 |
combo.save() |
75 | ||
76 | ||
77 |
def test_service_create_api(transactional_db, app, admin_user): |
|
78 |
app = login(app) |
|
79 |
response = app.post('/api/sites/create/foobar/') |
|
80 |
content = json.loads(response.text) |
|
81 |
assert 'error' in content.keys() |
|
82 |
assert content.get('error') == 1 |
|
83 | ||
84 |
response = app.post('/api/sites/create/combo/', params={'foo': 'bar'}) |
|
85 |
content = json.loads(response.text) |
|
86 |
assert 'error' in content.keys() |
|
87 |
assert content.get('error') == 1 |
|
88 | ||
89 |
response = app.post('/api/sites/create/combo/', params={'title': 'foo', 'slug': 'foo', 'base_url': 'http://example.net/'}) |
|
90 |
content = json.loads(response.text) |
|
91 |
assert 'error' in content.keys() |
|
92 |
assert content.get('error') == 0 |
|
93 |
assert 'data' in content.keys() |
|
94 |
assert 'id' in content.get('data') |
|
95 | ||
96 |
obj = Combo.objects.get(id=content['data']['id']) |
|
97 |
assert obj.base_url == 'http://example.net/' |
|
61 |
- |