diff --git a/test/towerops/uisp/config_snapshot_test.exs b/test/towerops/uisp/config_snapshot_test.exs index cc95b6f2..7b6a7102 100644 --- a/test/towerops/uisp/config_snapshot_test.exs +++ b/test/towerops/uisp/config_snapshot_test.exs @@ -2,6 +2,7 @@ defmodule Towerops.Uisp.ConfigSnapshotTest do use Towerops.DataCase, async: true use ExUnitProperties + alias Towerops.Uisp.Client alias Towerops.Uisp.ConfigSnapshot describe "compute_diff/2" do @@ -200,6 +201,71 @@ defmodule Towerops.Uisp.ConfigSnapshotTest do end end + describe "sync_configs/2" do + setup do + user = Towerops.AccountsFixtures.user_fixture() + org = Towerops.OrganizationsFixtures.organization_fixture(user.id) + + {:ok, site} = + Towerops.Sites.create_site(%{ + name: "S-sync #{System.unique_integer([:positive])}", + organization_id: org.id + }) + + device = + Towerops.DevicesFixtures.device_fixture(%{ + organization_id: org.id, + site_id: site.id, + name: "uisp-#{System.unique_integer([:positive])}" + }) + + integration = %{ + credentials: %{ + "url" => "http://uisp.example.com", + "api_key" => "test-key" + } + } + + %{integration: integration, device: device} + end + + test "stores configs and counts stored vs unchanged", %{ + integration: integration, + device: device + } do + counter = :counters.new(1, [:atomics]) + + Req.Test.stub(Client, fn conn -> + :counters.add(counter, 1, 1) + # Each request returns a different config so first store, second unchanged + Req.Test.json(conn, %{"id" => "uisp-1", "name" => "AP", "version" => 1}) + end) + + device_map = %{"uisp-1" => device} + + assert {:ok, %{stored: 1, unchanged: 0}} = + ConfigSnapshot.sync_configs(integration, device_map) + + # Same config — should be unchanged + assert {:ok, %{stored: 0, unchanged: 1}} = + ConfigSnapshot.sync_configs(integration, device_map) + end + + test "skips devices when fetch_device_config errors", %{ + integration: integration, + device: device + } do + Req.Test.stub(Client, fn conn -> + Req.Test.transport_error(conn, :econnrefused) + end) + + device_map = %{"uisp-1" => device} + + assert {:ok, %{stored: 0, unchanged: 0}} = + ConfigSnapshot.sync_configs(integration, device_map) + end + end + describe "diff_snapshots/2" do setup do user = Towerops.AccountsFixtures.user_fixture()