92 lines
3.5 KiB
Python
92 lines
3.5 KiB
Python
import pytest
|
|
from django.urls import reverse
|
|
from wg_connection_manager.models import UserConnection
|
|
|
|
|
|
@pytest.fixture
|
|
def test_data(django_user_model):
|
|
import random
|
|
import base64
|
|
|
|
connections = []
|
|
users = []
|
|
users.append(django_user_model.objects.create_user(username="user1", password="something"))
|
|
users.append(django_user_model.objects.create_user(username="user2", password="something"))
|
|
|
|
for i in range(10):
|
|
key = base64.b64encode(random.randbytes(32))
|
|
note = f"connection n: {i}"
|
|
connection = UserConnection(public_key=key, notes=note, vpn_ip=f"10.0.0.{i}", user=users[i % 2])
|
|
connections.append(connection)
|
|
connection.save()
|
|
return {"users": users, "connections": connections}
|
|
|
|
|
|
def test_bao(client, admin_client, test_data, mocker):
|
|
mock = mocker.patch("wg_connection_manager.views.DJWGManager", autospec=True)
|
|
mock.remove_peer = mocker.Mock()
|
|
|
|
# admin sees all connections
|
|
response = admin_client.get(reverse("connections_list"))
|
|
assert response.status_code == 200
|
|
content = response.content.decode("utf8")
|
|
for i, c in enumerate(test_data["connections"]):
|
|
assert c.notes in content
|
|
|
|
# user must authenticate
|
|
response = client.get(reverse("connections_list"))
|
|
assert response.status_code == 302
|
|
|
|
# user sees his connection
|
|
client.login(username="user1", password="something")
|
|
response = client.get(reverse("connections_list"))
|
|
assert response.status_code == 200
|
|
content = response.content.decode("utf8")
|
|
for i, c in enumerate(test_data["connections"]):
|
|
if i % 2 == 0:
|
|
assert c.notes in content
|
|
else:
|
|
assert c.notes not in content
|
|
|
|
# DJWGManager is called to remove peer on connection disable
|
|
connection = UserConnection.objects.get(public_key=test_data["connections"][0].public_key)
|
|
connection_public_key = connection.public_key
|
|
response = client.post(reverse("toggle_connection", args=[connection.id]), {"state": "disabled"})
|
|
mock.assert_has_calls([mocker.call(), mocker.call().remove_peer(connection)])
|
|
assert response.status_code == 302
|
|
|
|
# DJWGManager is called to add peer on connection enable
|
|
mock.reset_mock()
|
|
response = client.post(reverse("toggle_connection", args=[connection.id]), {"state": "enabled"})
|
|
mock.assert_has_calls([mocker.call(), mocker.call().add_peer(connection)])
|
|
assert response.status_code == 302
|
|
|
|
# user can delete his connection and DJWGManager is called to remove peer
|
|
mock.reset_mock()
|
|
# activate connection to call remove_peer
|
|
connection.active = True
|
|
connection.save()
|
|
response = client.post(reverse("connection_delete", args=[connection.id]))
|
|
assert response.status_code == 302
|
|
|
|
# connection is deleted
|
|
with pytest.raises(UserConnection.DoesNotExist):
|
|
UserConnection.objects.get(pk=1)
|
|
|
|
# the connection is not in the list anymore
|
|
response = admin_client.get(reverse("connections_list"))
|
|
assert response.status_code == 200
|
|
content = response.content.decode("utf8")
|
|
connection_public_key not in content
|
|
|
|
# user can't delete a connection id doesn't own
|
|
mock.reset_mock()
|
|
response = client.post(reverse("connection_delete", args=[2]))
|
|
mock.assert_not_called()
|
|
assert response.status_code == 404
|
|
|
|
# user can't disable a connection id doesn't own
|
|
mock.reset_mock()
|
|
response = client.post(reverse("toggle_connection", args=[2]), {"state": "disabled"})
|
|
mock.assert_not_called()
|
|
assert response.status_code == 404
|