Skip to content

Commit f474251

Browse files
paulanthonywilsonfhunleth
authored andcommitted
Avoids overwritting connection details on shutdown
1 parent 03c9a19 commit f474251

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/vintage_net_wizard/backend_server.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,17 @@ defmodule VintageNetWizard.BackendServer do
278278
{:reply, state, state}
279279
end
280280

281+
def handle_call(
282+
:complete,
283+
_from,
284+
%State{backend_state: %{data: %{configuration_status: :good}}} = state
285+
) do
286+
# As the configuration status is good, we are already completed setup and the configurations
287+
# have been blanked in `handle_info/3` - calling complete on the backend will disconnect us and
288+
# write over the saved configuration. Do nothing.
289+
{:reply, :ok, state}
290+
end
291+
281292
def handle_call(
282293
:complete,
283294
_from,

test/vintage_net_wizard/backend_server_test.exs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule VintageNetWizard.BackendServer.Test do
22
use ExUnit.Case, async: true
33

44
alias VintageNetWizard.BackendServer
5+
alias VintageNetWizard.BackendServer.State
56

67
test "Can save a WiFi configuration" do
78
:ok = BackendServer.reset()
@@ -59,4 +60,31 @@ defmodule VintageNetWizard.BackendServer.Test do
5960
:ok = BackendServer.reset()
6061
assert :ok = BackendServer.complete()
6162
end
63+
64+
describe "on completion" do
65+
defmodule FakeBackend do
66+
def complete(wifi_configurations, backend_state) do
67+
send(self(), {:complete_called, wifi_configurations, backend_state})
68+
{:ok, backend_state}
69+
end
70+
end
71+
72+
test "if the configuration status is not `:good`, calls complete on the backend" do
73+
BackendServer.handle_call(:complete, :ignored, %State{
74+
backend: FakeBackend,
75+
backend_state: %{data: %{configuration_status: :not_configured}}
76+
})
77+
78+
assert_received {:complete_called, [], _backend_state}
79+
end
80+
81+
test "if the configuration status is `:good` then does not call complete on the backend" do
82+
BackendServer.handle_call(:complete, :ignored, %State{
83+
backend: FakeBackend,
84+
backend_state: %{data: %{configuration_status: :good}}
85+
})
86+
87+
refute_received {:complete_called, _, _}
88+
end
89+
end
6290
end

0 commit comments

Comments
 (0)