From b572523a336dd6e1d4994c7be30c964680aadf91 Mon Sep 17 00:00:00 2001 From: Christian Cueni Date: Tue, 25 Feb 2020 14:42:42 +0100 Subject: [PATCH] Add proxy for registration call --- client/src/hep-client/index.js | 2 +- client/src/layouts/PublicLayout.vue | 9 +- server/api/urls.py | 4 + server/core/hep_client.py | 13 +-- server/core/urls.py | 2 +- .../tests/test_registration_proxy.py | 86 +++++++++++++++++++ server/registration/urls.py | 18 ++++ server/registration/view.py | 37 ++++++++ 8 files changed, 155 insertions(+), 16 deletions(-) create mode 100644 server/registration/tests/test_registration_proxy.py create mode 100644 server/registration/urls.py create mode 100644 server/registration/view.py diff --git a/client/src/hep-client/index.js b/client/src/hep-client/index.js index fe863fee..956e8d3e 100644 --- a/client/src/hep-client/index.js +++ b/client/src/hep-client/index.js @@ -3,7 +3,7 @@ import * as axios from 'axios' const hepBaseUrl = 'https://stage.hep-verlag.ch'; export function register(registrationData) { - return axios.post(`${hepBaseUrl}/rest/deutsch/V1/customers`, registrationData); + return axios.post('/api/proxy/registration/', registrationData); } export function login(username, password) { diff --git a/client/src/layouts/PublicLayout.vue b/client/src/layouts/PublicLayout.vue index 410309cb..4503f6f3 100644 --- a/client/src/layouts/PublicLayout.vue +++ b/client/src/layouts/PublicLayout.vue @@ -34,7 +34,6 @@ import Logo from '@/components/icons/Logo'; margin: 0 auto; } - .public { grid-template-areas: "h" "c" "f"; @@ -44,7 +43,13 @@ import Logo from '@/components/icons/Logo'; width: 100%; } - &__content, &__logo { + &__content { + @include content-block(); + margin-bottom: $large-spacing; + } + + + &__logo { @include content-block(); } diff --git a/server/api/urls.py b/server/api/urls.py index 8667c1fe..000ed89e 100644 --- a/server/api/urls.py +++ b/server/api/urls.py @@ -1,5 +1,6 @@ from django.conf import settings from django.conf.urls import url +from django.urls import include from django.views.decorators.csrf import csrf_exempt from graphene_django.views import GraphQLView @@ -11,6 +12,9 @@ app_name = 'api' urlpatterns = [ url(r'^graphql-public', csrf_exempt(GraphQLView.as_view(schema=schema))), url(r'^graphql', csrf_exempt(PrivateGraphQLView.as_view())), + + # hep proxy + url(r'^proxy/', include('registration.urls', namespace="registration")), ] if settings.DEBUG: diff --git a/server/core/hep_client.py b/server/core/hep_client.py index 2edf27ab..348fe0db 100644 --- a/server/core/hep_client.py +++ b/server/core/hep_client.py @@ -82,18 +82,7 @@ class HepClient: response = self._call('/rest/V1/customers/me', method='put', data={'confirmationKey': confirmation_key}) return response.json() - def customer_create(self, customer_data, address): - - if customer_data['prefix'] == 'Herr': - customer_data['gender'] = 1 - else: - customer_data['gender'] = 2 - - address['country_id'] = 'CH' - address['default_billing'] = True - address['default_shipping'] = True - - customer_data['addresses'] = [address] + def customer_create(self, customer_data): response = self._call('/rest/deutsch/V1/customers', method='post', data=customer_data) return response.json() diff --git a/server/core/urls.py b/server/core/urls.py index a1ae1a84..0ef47e28 100644 --- a/server/core/urls.py +++ b/server/core/urls.py @@ -2,7 +2,7 @@ from django.conf import settings from django.conf.urls import url, include from django.conf.urls.static import static from django.contrib import admin -from django.urls import re_path, path +from django.urls import re_path from django.views.generic import RedirectView from wagtail.admin import urls as wagtailadmin_urls from wagtail.core import urls as wagtail_urls diff --git a/server/registration/tests/test_registration_proxy.py b/server/registration/tests/test_registration_proxy.py new file mode 100644 index 00000000..d8d3b0f5 --- /dev/null +++ b/server/registration/tests/test_registration_proxy.py @@ -0,0 +1,86 @@ +# -*- coding: utf-8 -*- +# +# ITerativ GmbH +# http://www.iterativ.ch/ +# +# Copyright (c) 2020 ITerativ GmbH. All rights reserved. +# +# Created on 25.02.20 +# @author: chrigu +import json + +from unittest.mock import patch + +import requests +from django.test import TestCase, Client +from django.urls import reverse + +from core.hep_client import HepClient +from core.tests.mock_hep_data_factory import MockResponse + +RESPONSE = { + 'id': 1234, + 'confirmation': 'abdc1234', + 'firstname': 'Pesche', + 'lastname': 'Zubrüti', + 'email': 'aschima@ch.ch', + 'prefix': 'Herr', + 'gender': 1, + 'addresses': [ + { + 'country_id': 'CH', + 'street': ['Weg 1'], + 'postcode': '1234', + 'city': 'Äussere Einöde', + 'firstname': 'Pesche', + 'lastname': 'Zubrüti', + 'prefix': 'Herr', + 'default_shipping': True, + 'default_billing': True, + } + ], +} + +DATA = { + 'customer': { + 'firstname': 'Pesche', + 'lastname': 'Zubrüti', + 'email': 'aschima@ch.ch', + 'prefix': 'Herr', + 'gender': 1, + 'addresses': [ + { + 'country_id': 'CH', + 'street': ['Weg 1'], + 'postcode': '1234', + 'city': 'Äussere Einöde', + 'firstname': 'Pesche', + 'lastname': 'Zubrüti', + 'prefix': 'Herr', + 'default_shipping': True, + 'default_billing': True, + } + ], + 'password': '123454abasfd' + } +} + + +class ProxyTest(TestCase): + + def setUp(self): + self.client = Client() + + @patch.object(HepClient, 'customer_create', return_value=RESPONSE) + def test_proxy_filters_confirmation_key(self, create_mock): + + response = self.client.post(reverse('api:registration:proxy'), DATA) + found = 'confirmation' in response.json().keys() + self.assertFalse(found) + + @patch.object(requests, 'post', return_value=MockResponse(400, + data={'message': 'Ein Kunde mit der gleichen E-Mail-Adresse existiert bereits in einer zugeordneten Website.'})) + def test_handles_400(self, create_mock): + + response = self.client.post(reverse('api:registration:proxy'), DATA) + self.assertEquals(response.json()['message'], 'Ein Kunde mit der gleichen E-Mail-Adresse existiert bereits in einer zugeordneten Website.') diff --git a/server/registration/urls.py b/server/registration/urls.py new file mode 100644 index 00000000..f038f6d0 --- /dev/null +++ b/server/registration/urls.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +# +# ITerativ GmbH +# http://www.iterativ.ch/ +# +# Copyright (c) 2020 ITerativ GmbH. All rights reserved. +# +# Created on 25.02.20 +# @author: chrigu +from django.conf.urls import url +from django.views.decorators.csrf import csrf_exempt + +from registration.view import RegistrationProxyView + +app_name = 'registration' +urlpatterns = [ + url(r'^registration/', csrf_exempt(RegistrationProxyView.as_view()), name="proxy"), +] diff --git a/server/registration/view.py b/server/registration/view.py new file mode 100644 index 00000000..19db6b1a --- /dev/null +++ b/server/registration/view.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +# +# ITerativ GmbH +# http://www.iterativ.ch/ +# +# Copyright (c) 2020 ITerativ GmbH. All rights reserved. +# +# Created on 25.02.20 +# @author: chrigu +import json + +from django.http import HttpResponse +from django.views import View + +from core.hep_client import HepClient, HepClientException + + +class RegistrationProxyView(View): + + def post(self, request, *args, **kwargs): + hep_client = HepClient() + + if request.POST: + data = request.POST + else: + data = json.loads(request.body) + + try: + hep_data = hep_client.customer_create(data) + except HepClientException as e: + return HttpResponse(json.dumps(e.args[1]), status=e.args[0], content_type='application/json') + + response_data = hep_data.copy() + del response_data['confirmation'] + + return HttpResponse(json.dumps(response_data), content_type='application/json') +