From 0ccfe5c5afef7fda9fbfebd109eee0dff4013f55 Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Thu, 9 May 2019 17:05:17 +0200
Subject: [PATCH 4/4] lingo: make manager views compliant with PaymentBackend
 (#32441)

---
 combo/apps/lingo/manager_views.py             | 23 ++++-
 .../lingo/paymentbackend_confirm_delete.html  | 17 ++++
 .../templates/lingo/paymentbackend_form.html  | 22 +++++
 .../templates/lingo/paymentbackend_list.html  | 38 +++++++++
 .../templates/lingo/transaction_list.html     |  1 +
 combo/apps/lingo/urls.py                      | 12 ++-
 tests/test_lingo_cells.py                     |  8 +-
 tests/test_lingo_manager.py                   | 85 ++++++++++++++++---
 tests/test_notification.py                    |  9 +-
 9 files changed, 196 insertions(+), 19 deletions(-)
 create mode 100644 combo/apps/lingo/templates/lingo/paymentbackend_confirm_delete.html
 create mode 100644 combo/apps/lingo/templates/lingo/paymentbackend_form.html
 create mode 100644 combo/apps/lingo/templates/lingo/paymentbackend_list.html

diff --git a/combo/apps/lingo/manager_views.py b/combo/apps/lingo/manager_views.py
index 72a9911..7995540 100644
--- a/combo/apps/lingo/manager_views.py
+++ b/combo/apps/lingo/manager_views.py
@@ -27,7 +27,7 @@ from django.http import HttpResponse
 
 import eopayment
 
-from .models import Regie, Transaction
+from .models import PaymentBackend, Regie, Transaction
 
 class RegieListView(ListView):
     model = Regie
@@ -55,6 +55,27 @@ class RegieDeleteView(DeleteView):
     success_url = reverse_lazy('lingo-manager-regie-list')
 
 
+class PaymentBackendListView(ListView):
+    model = PaymentBackend
+
+
+class PaymentBackendCreateView(CreateView):
+    model = PaymentBackend
+    fields = '__all__'
+    success_url = reverse_lazy('lingo-manager-paymentbackend-list')
+
+
+class PaymentBackendUpdateView(UpdateView):
+    model = PaymentBackend
+    fields = '__all__'
+    success_url = reverse_lazy('lingo-manager-paymentbackend-list')
+
+
+class PaymentBackendDeleteView(DeleteView):
+    model = PaymentBackend
+    success_url = reverse_lazy('lingo-manager-paymentbackend-list')
+
+
 class TransactionListView(ListView):
     model = Transaction
     paginate_by = 10
diff --git a/combo/apps/lingo/templates/lingo/paymentbackend_confirm_delete.html b/combo/apps/lingo/templates/lingo/paymentbackend_confirm_delete.html
new file mode 100644
index 0000000..d1617fe
--- /dev/null
+++ b/combo/apps/lingo/templates/lingo/paymentbackend_confirm_delete.html
@@ -0,0 +1,17 @@
+{% extends "combo/manager_base.html" %}
+{% load i18n %}
+
+{% block appbar %}
+<h2>{{ view.model.get_verbose_name }}</h2>
+{% endblock %}
+
+{% block content %}
+<form method="post">
+  {% csrf_token %}
+  {% blocktrans %}Are you sure you want to delete this ?{% endblocktrans %}
+  <div class="buttons">
+    <button class="delete-button">{% trans 'Delete' %}</button>
+    <a class="cancel" href="{% url 'lingo-manager-paymentbackend-list' %}">{% trans 'Cancel' %}</a>
+  </div>
+</form>
+{% endblock %}
diff --git a/combo/apps/lingo/templates/lingo/paymentbackend_form.html b/combo/apps/lingo/templates/lingo/paymentbackend_form.html
new file mode 100644
index 0000000..d49a384
--- /dev/null
+++ b/combo/apps/lingo/templates/lingo/paymentbackend_form.html
@@ -0,0 +1,22 @@
+{% extends "lingo/paymentbackend_list.html" %}
+{% load i18n %}
+
+{% block appbar %}
+{% if object.id %}
+<h2>{% trans "Edit Payment backend" %}</h2>
+{% else %}
+<h2>{% trans "New Payment backend" %}</h2>
+{% endif %}
+{% endblock %}
+
+{% block content %}
+
+<form method="post" enctype="multipart/form-data">
+  {% csrf_token %}
+  {{ form.as_p }}
+  <div class="buttons">
+    <button class="submit-button">{% trans "Save" %}</button>
+    <a class="cancel" href="{% url 'lingo-manager-homepage' %}">{% trans 'Cancel' %}</a>
+  </div>
+</form>
+{% endblock %}
diff --git a/combo/apps/lingo/templates/lingo/paymentbackend_list.html b/combo/apps/lingo/templates/lingo/paymentbackend_list.html
new file mode 100644
index 0000000..f9af2f7
--- /dev/null
+++ b/combo/apps/lingo/templates/lingo/paymentbackend_list.html
@@ -0,0 +1,38 @@
+{% extends "lingo/manager_base.html" %}
+{% load i18n %}
+
+{% block appbar %}
+<h2>{% trans 'Payment backends' %}</h2>
+<span class="actions">
+<a rel="popup" href="{% url 'lingo-manager-paymentbackend-add' %}">{% trans 'New' %}</a>
+</span>
+{% endblock %}
+
+{% block breadcrumb %}
+{{ block.super }}
+<a href="{% url 'lingo-manager-paymentbackend-list' %}">{% trans 'Payment backends' %}</a>
+{% endblock %}
+
+{% block content %}
+
+{% if object_list %}
+<ul class="objects-list single-links">
+ {% for payment_backend in object_list %}
+ <li>
+ <a href="{% url 'lingo-manager-paymentbackend-edit' pk=payment_backend.id %}">{{ payment_backend.label }}
+   <span>({{payment_backend.service}})</span>
+ </a>
+  <a rel="popup" class="delete" href="{% url 'lingo-manager-paymentbackend-delete' pk=payment_backend.id %}">{% trans "remove" %}</a>
+ </li>
+ {% endfor %}
+</ul>
+{% else %}
+<div class="big-msg-info">
+  {% blocktrans %}
+  This site doesn't have any payment backend yet. Click on the "New" button in the top
+  right of the page to add a first one.
+  {% endblocktrans %}
+</div>
+{% endif %}
+
+{% endblock %}
diff --git a/combo/apps/lingo/templates/lingo/transaction_list.html b/combo/apps/lingo/templates/lingo/transaction_list.html
index 29786ad..67b6264 100644
--- a/combo/apps/lingo/templates/lingo/transaction_list.html
+++ b/combo/apps/lingo/templates/lingo/transaction_list.html
@@ -4,6 +4,7 @@
 {% block appbar %}
 <h2>{% trans 'Transactions' %}</h2>
 <span class="actions">
+<a href="{% url 'lingo-manager-paymentbackend-list' %}">{% trans 'Payment backends' %}</a>
 <a href="{% url 'lingo-manager-regie-list' %}">{% trans 'Regies' %}</a>
 <a href="{% url 'lingo-manager-transactions-download' %}">{% trans 'download CSV' %}</a>
 </span>
diff --git a/combo/apps/lingo/urls.py b/combo/apps/lingo/urls.py
index bc5aba4..5498d9f 100644
--- a/combo/apps/lingo/urls.py
+++ b/combo/apps/lingo/urls.py
@@ -23,7 +23,9 @@ from .views import (RegiesApiView, AddBasketItemApiView, PayView, CallbackView,
                     RemoveBasketItemApiView, ValidateTransactionApiView,
                     CancelTransactionApiView, SelfInvoiceView, BasketItemPayView)
 from .manager_views import (RegieListView, RegieCreateView, RegieUpdateView,
-        RegieDeleteView, TransactionListView, download_transactions_csv)
+                            RegieDeleteView, TransactionListView, download_transactions_csv,
+                            PaymentBackendListView, PaymentBackendCreateView,
+                            PaymentBackendUpdateView, PaymentBackendDeleteView)
 
 lingo_manager_urls = [
     url('^$', TransactionListView.as_view(), name='lingo-manager-homepage'),
@@ -34,6 +36,14 @@ lingo_manager_urls = [
         name='lingo-manager-regie-edit'),
     url(r'^regies/(?P<pk>\w+)/delete$', RegieDeleteView.as_view(),
         name='lingo-manager-regie-delete'),
+    url('^paymentbackends/$', PaymentBackendListView.as_view(),
+        name='lingo-manager-paymentbackend-list'),
+    url('^paymentbackends/add/$', PaymentBackendCreateView.as_view(),
+        name='lingo-manager-paymentbackend-add'),
+    url(r'^paymentbackends/(?P<pk>\w+)/edit$', PaymentBackendUpdateView.as_view(),
+        name='lingo-manager-paymentbackend-edit'),
+    url(r'^paymentbackends/(?P<pk>\w+)/delete$', PaymentBackendDeleteView.as_view(),
+        name='lingo-manager-paymentbackend-delete'),
 ]
 
 urlpatterns = [
diff --git a/tests/test_lingo_cells.py b/tests/test_lingo_cells.py
index 8bbc037..c825051 100644
--- a/tests/test_lingo_cells.py
+++ b/tests/test_lingo_cells.py
@@ -10,7 +10,7 @@ from django.utils.http import quote
 
 from combo.data.models import Page
 from combo.apps.lingo.models import Regie, BasketItem, Transaction
-from combo.apps.lingo.models import (LingoBasketCell,
+from combo.apps.lingo.models import (LingoBasketCell, PaymentBackend,
         LingoRecentTransactionsCell, LingoBasketLinkCell, TipiPaymentFormCell)
 
 pytestmark = pytest.mark.django_db
@@ -25,6 +25,11 @@ def user():
 
 @pytest.fixture
 def regie():
+    try:
+        payment_backend = PaymentBackend.objects.get(slug='test1')
+    except PaymentBackend.DoesNotExist:
+        payment_backend = PaymentBackend.objects.create(
+            label='test1', slug='test1')
     try:
         regie = Regie.objects.get(slug='test')
     except Regie.DoesNotExist:
@@ -32,6 +37,7 @@ def regie():
         regie.label = 'Test'
         regie.slug = 'test'
         regie.description = 'test'
+        regie.payment_backend = payment_backend
         regie.save()
     return regie
 
diff --git a/tests/test_lingo_manager.py b/tests/test_lingo_manager.py
index ffff1f3..7267b71 100644
--- a/tests/test_lingo_manager.py
+++ b/tests/test_lingo_manager.py
@@ -10,12 +10,19 @@ import pytest
 
 import eopayment
 from combo.data.models import Page
-from combo.apps.lingo.models import Regie, BasketItem, Transaction, ActiveItems, TipiPaymentFormCell
+from combo.apps.lingo.models import (Regie, BasketItem, Transaction, ActiveItems,
+                                     TipiPaymentFormCell, PaymentBackend)
 from decimal import Decimal
 
 pytestmark = pytest.mark.django_db
 
 
+@pytest.fixture
+def payment_backend():
+    return PaymentBackend.objects.create(
+            label='test1', slug='test1', service='dummy', service_options={'siret': '1234'})
+
+
 def login(app, username='admin', password='admin'):
     login_page = app.get('/login/')
     login_form = login_page.forms[0]
@@ -30,7 +37,7 @@ def test_access(app, admin_user):
     resp = app.get('/manage/', status=200)
     assert '/manage/lingo/' in resp.text
 
-def test_add_regie(app, admin_user):
+def test_add_regie(app, admin_user, payment_backend):
     Regie.objects.all().delete()
     app = login(app)
     resp = app.get('/manage/lingo/regies/', status=200)
@@ -38,7 +45,7 @@ def test_add_regie(app, admin_user):
     resp.forms[0]['label'] = 'Test'
     resp.forms[0]['slug'] = 'test'
     resp.forms[0]['description'] = 'description'
-    resp.forms[0]['service'] = 'dummy'
+    resp.forms[0]['payment_backend'] = payment_backend.pk
     assert resp.form['is_default'].checked is True
     resp = resp.forms[0].submit()
     assert resp.location.endswith('/manage/lingo/regies/')
@@ -47,8 +54,8 @@ def test_add_regie(app, admin_user):
     assert regie.label == 'Test'
     assert regie.is_default is True
 
-def test_edit_regie(app, admin_user):
-    test_add_regie(app, admin_user)
+def test_edit_regie(app, admin_user, payment_backend):
+    test_add_regie(app, admin_user, payment_backend)
     app = login(app)
     resp = app.get('/manage/lingo/regies/', status=200)
     resp = resp.click('Test')
@@ -59,18 +66,17 @@ def test_edit_regie(app, admin_user):
     regie = Regie.objects.all()[0]
     assert regie.description == 'other description'
 
-def test_delete_regie(app, admin_user):
-    test_add_regie(app, admin_user)
+def test_delete_regie(app, admin_user, payment_backend):
+    test_add_regie(app, admin_user, payment_backend)
     app = login(app)
     resp = app.get('/manage/lingo/regies/', status=200)
     resp = resp.click('remove')
     assert 'Are you sure you want to delete this?' in resp.text
     resp = resp.forms[0].submit()
-    assert resp.location.endswith('/manage/lingo/regies/')
     assert Regie.objects.count() == 0
 
-def test_add_second_regie(app, admin_user):
-    test_add_regie(app, admin_user)
+def test_add_second_regie(app, admin_user, payment_backend):
+    test_add_regie(app, admin_user, payment_backend)
     regie = Regie.objects.all()[0]
 
     app = login(app)
@@ -79,7 +85,7 @@ def test_add_second_regie(app, admin_user):
     resp.forms[0]['label'] = 'Test2'
     resp.forms[0]['slug'] = 'test2'
     resp.forms[0]['description'] = 'description'
-    resp.forms[0]['service'] = 'dummy'
+    resp.forms[0]['payment_backend'] = payment_backend.pk
     assert resp.form['is_default'].checked is False
     resp = resp.forms[0].submit()
     assert resp.location.endswith('/manage/lingo/regies/')
@@ -88,8 +94,8 @@ def test_add_second_regie(app, admin_user):
     assert Regie.objects.get(id=regie.id).is_default is True
     assert Regie.objects.exclude(id=regie.id)[0].is_default is False
 
-def test_download_transaction(app, admin_user):
-    test_add_regie(app, admin_user)
+def test_download_transaction(app, admin_user, payment_backend):
+    test_add_regie(app, admin_user, payment_backend)
     regie = Regie.objects.filter(slug='test')[0]
     user = User.objects.create_user('dimebag', 'dime@bag.pan', 'pwd')
     user.last_name = u'Darëll'
@@ -164,7 +170,7 @@ def test_configure_tipi_cell(app, admin_user):
     assert resp.text.count('ROLDEB:') == 1
     assert resp.text.count('ROLDET:') == 1
 
-def test_configure_invoices_cell(app, admin_user):
+def test_configure_invoices_cell(app, admin_user, payment_backend):
     page = Page(title='xxx', slug='test', template_name='standard')
     page.save()
 
@@ -179,6 +185,7 @@ def test_configure_invoices_cell(app, admin_user):
     regie.label = 'Test'
     regie.slug = 'test'
     regie.description = 'test'
+    regie.payment_backend = payment_backend
     regie.save()
 
     resp = app.get('/manage/pages/%s/' % page.id, status=200)
@@ -202,6 +209,7 @@ def test_configure_invoices_cell(app, admin_user):
     regie2.slug = 'test2'
     regie2.description = 'test2'
     regie2.webservice_url = 'http://example.net/'
+    regie2.payment_backend = payment_backend
     regie2.save()
 
     resp = app.get('/manage/pages/%s/' % page.id, status=200)
@@ -215,3 +223,52 @@ def test_configure_invoices_cell(app, admin_user):
     resp.form['clingo_activeitems-%s-regie' % cell.id].value = 'test2'
     resp.form.submit()
     assert ActiveItems.objects.get(id=cell.id).regie == regie2.slug
+
+
+def test_payment_backend_list(app, admin_user):
+    PaymentBackend.objects.create(label='label1', slug='slug1')
+    PaymentBackend.objects.create(label='label2', slug='slug2')
+    app = login(app)
+    resp = app.get('/manage/lingo/paymentbackends/', status=200)
+
+    assert '/manage/lingo/paymentbackends/add' in resp.text
+    assert 'label1' in resp.content
+    assert 'label2' in resp.content
+
+    for payment_backend in PaymentBackend.objects.all():
+        assert '/manage/lingo/paymentbackends/%s' % payment_backend.pk in resp.text
+        assert '/manage/lingo/paymentbackends/%s/delete' % payment_backend.pk in resp.text
+
+
+def test_add_payment_backend(app, admin_user):
+    assert not PaymentBackend.objects.count()
+    app = login(app)
+    resp = app.get('/manage/lingo/paymentbackends/add/', status=200)
+    resp.forms[0]['label'] = 'Test'
+    resp.forms[0]['slug'] = 'test-add'
+    resp.forms[0]['service'] = 'dummy'
+    resp.forms[0]['service_options'] = '{"siret": "1234"}'
+    resp = resp.forms[0].submit()
+
+    assert PaymentBackend.objects.count() == 1
+    payment_backend = PaymentBackend.objects.get(slug='test-add')
+    assert payment_backend.label == 'Test'
+    assert payment_backend.service_options == {'siret': '1234'}
+
+    assert resp.location.endswith('/manage/lingo/paymentbackends/')
+
+
+def test_edit_payment_backend(app, admin_user):
+    payment_backend = PaymentBackend.objects.create(label='label1', slug='slug1')
+    app = login(app)
+    resp = app.get('/manage/lingo/paymentbackends/%s/edit' % payment_backend.pk, status=200)
+
+    resp.forms[0]['label'] = 'label1-modified'
+    resp.forms[0]['slug'] = 'slug1'
+    resp.forms[0]['service'] = 'dummy'
+    resp.forms[0]['service_options'] = '{"siret": "1234"}'
+    resp = resp.forms[0].submit()
+
+    assert resp.location.endswith('/manage/lingo/paymentbackends/')
+    payment_backend = PaymentBackend.objects.get(slug='slug1')
+    assert payment_backend.label == 'label1-modified'
diff --git a/tests/test_notification.py b/tests/test_notification.py
index fddb88c..ae5ea2c 100644
--- a/tests/test_notification.py
+++ b/tests/test_notification.py
@@ -14,7 +14,7 @@ from django.test import Client
 
 from combo.data.models import Page
 from combo.apps.notifications.models import Notification, NotificationsCell
-from combo.apps.lingo.models import Regie, ActiveItems
+from combo.apps.lingo.models import Regie, ActiveItems, PaymentBackend
 
 pytestmark = pytest.mark.django_db
 
@@ -28,6 +28,11 @@ def login(user):
 
 @pytest.fixture
 def regie():
+    try:
+        payment_backend = PaymentBackend.objects.get(slug='test1')
+    except PaymentBackend.DoesNotExist:
+        payment_backend = PaymentBackend.objects.create(
+            label='test1', slug='test1', service='dummy')
     try:
         regie = Regie.objects.get(slug='remote')
     except Regie.DoesNotExist:
@@ -36,7 +41,7 @@ def regie():
         regie.slug = 'remote'
         regie.description = 'remote'
         regie.payment_min_amount = Decimal(2.0)
-        regie.service = 'dummy'
+        regie.payment_backend = payment_backend
         regie.save()
     return regie
 
-- 
2.20.1

