From 1f85ed9fd4f0bfdd801958f45de4d8972486192f Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@esss.se>
Date: Mon, 10 Dec 2018 16:10:03 +0100
Subject: [PATCH] Fix extra interface creation

Not sure why a tuple instead of network was returned...
There must have been a comma at the end of the line due to copy/pasting
and black converted that to a tuple.

Added a test for the create_interface function.

JIRA INFRA-697 #action In Progress
---
 app/network/views.py         |  2 +-
 tests/functional/test_web.py | 47 ++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/app/network/views.py b/app/network/views.py
index 192998d..2835a05 100644
--- a/app/network/views.py
+++ b/app/network/views.py
@@ -265,7 +265,7 @@ def create_interface(hostname):
     )
     if form.validate_on_submit():
         # User shall have access to the new interface domain
-        network = (models.Network.query.get(form.network_id.data),)
+        network = models.Network.query.get(form.network_id.data)
         if not current_user.has_access_to_network(network):
             abort(403)
         # The total number of tags will always be quite small
diff --git a/tests/functional/test_web.py b/tests/functional/test_web.py
index 8143466..9b32ac6 100644
--- a/tests/functional/test_web.py
+++ b/tests/functional/test_web.py
@@ -305,3 +305,50 @@ def test_create_host(client, domain_factory, network_factory, device_type):
     assert host.interfaces[0].ip == ip
     assert host.interfaces[0].mac == mac
     assert host.interfaces[0].name == name
+
+
+def test_create_interface(
+    client, host_factory, domain_factory, network_factory, interface_factory
+):
+    host = host_factory(name="myhost")
+    domain = domain_factory(name="prod.example.org")
+    network1 = network_factory(domain=domain)
+    interface_factory(network=network1, host=host)
+    network2 = network_factory(
+        address="192.168.2.0/24",
+        first_ip="192.168.2.10",
+        last_ip="192.168.2.250",
+        domain=domain,
+    )
+    name = host.name + "-2"
+    ip = "192.168.2.11"
+    mac = "02:42:42:46:3c:75"
+    form = {
+        "host_id": host.id,
+        "interface_name": name,
+        "network_id": network2.id,
+        "random_mac": False,
+        "ip": ip,
+        "mac": mac,
+        "cnames_string": "",
+        "tags": [],
+    }
+    # Permission denied
+    # user_lab doesn't have permissions for the host domain: prod.example.org
+    login(client, "user_lab", "userlab")
+    response = client.post(f"/network/interfaces/create/{host.name}", data=form)
+    assert response.status_code == 403
+    # The host wasn't created
+    assert models.Interface.query.filter_by(name=name).first() is None
+    logout(client)
+    # Success with user_prod user
+    login(client, "user_prod", "userprod")
+    response = client.post(f"/network/interfaces/create/{host.name}", data=form)
+    assert response.status_code == 302
+    # The interface was created
+    interface = models.Interface.query.filter_by(name=name).first()
+    assert interface is not None
+    assert interface.ip == ip
+    assert interface.mac == mac
+    assert interface.name == name
+    assert interface.host == host
-- 
GitLab