From 4e1d4d73e7a33c34de2b2b30b7db87ab0c3bc3c0 Mon Sep 17 00:00:00 2001 From: the-last-pastafarian Date: Tue, 28 Nov 2023 14:51:45 +0100 Subject: [PATCH 01/10] Add interface for deleting project --- .../field_hub_web/live/project_show_live.ex | 27 ++++++++++++++++ .../live/project_show_live.html.heex | 31 ++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/server/lib/field_hub_web/live/project_show_live.ex b/server/lib/field_hub_web/live/project_show_live.ex index f5c235c4f2..37aaf18af4 100644 --- a/server/lib/field_hub_web/live/project_show_live.ex +++ b/server/lib/field_hub_web/live/project_show_live.ex @@ -42,6 +42,8 @@ defmodule FieldHubWeb.ProjectShowLive do |> assign(:project, project) |> assign(:current_user, user_name) |> assign(:new_password, "") + |> assign(:deletion_dialog_started, false) + |> assign(:confirm_project_name,"") |> read_project_doc() } @@ -112,6 +114,31 @@ defmodule FieldHubWeb.ProjectShowLive do {:noreply, assign(socket, :new_password, password)} end + def handle_event("display_delete_dialogue", _values, socket) do + {:noreply,assign(socket,:deletion_dialog_started, true)} + end + + def handle_event("update", %{"confirm_project_name" => confirm_project_name} = _values, socket) do + {:noreply, assign(socket, :confirm_project_name, confirm_project_name)} + end + + + def handle_event("delete_the_project", _values, socket) do + + identifier = socket.assigns.stats.name + CouchService.delete_database(identifier) + + socket = put_flash( + socket, + :info, + "Project `#{identifier}` has been deleted successfully." + ) + + {:noreply,redirect(socket, to: "/")} + + end + + def handle_event("generate_password", _values, socket) do {:noreply, assign(socket, :new_password, CouchService.create_password())} end diff --git a/server/lib/field_hub_web/live/project_show_live.html.heex b/server/lib/field_hub_web/live/project_show_live.html.heex index 5877682552..0c8bb1b7d0 100644 --- a/server/lib/field_hub_web/live/project_show_live.html.heex +++ b/server/lib/field_hub_web/live/project_show_live.html.heex @@ -145,7 +145,7 @@
-
@@ -153,5 +153,34 @@
+ <%!--

Delete project

--%> +
+
+
+ +
+
+ + <%= if @deletion_dialog_started == true do %> +
+
+ +
+
+ +
+
+ <% end %> +
<% end %> From 7698d9651bfdc94105de7dbce689853622e490aa Mon Sep 17 00:00:00 2001 From: the-last-pastafarian Date: Tue, 28 Nov 2023 15:12:43 +0100 Subject: [PATCH 02/10] correct the disabled condition of the button set new password --- server/lib/field_hub_web/live/project_show_live.html.heex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/lib/field_hub_web/live/project_show_live.html.heex b/server/lib/field_hub_web/live/project_show_live.html.heex index 0c8bb1b7d0..bc7d821c16 100644 --- a/server/lib/field_hub_web/live/project_show_live.html.heex +++ b/server/lib/field_hub_web/live/project_show_live.html.heex @@ -145,7 +145,7 @@
-
From bb406a9886f016d9d86e7cc200f2b00a5f6d5b9c Mon Sep 17 00:00:00 2001 From: the-last-pastafarian Date: Tue, 28 Nov 2023 18:00:20 +0100 Subject: [PATCH 03/10] add ids to forms change pwd and delete project --- server/lib/field_hub_web/live/project_show_live.ex | 10 +++++++++- .../lib/field_hub_web/live/project_show_live.html.heex | 4 ++-- .../test/field_hub_web/live/project_show_live_test.exs | 6 +++--- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/server/lib/field_hub_web/live/project_show_live.ex b/server/lib/field_hub_web/live/project_show_live.ex index 37aaf18af4..2812573b0d 100644 --- a/server/lib/field_hub_web/live/project_show_live.ex +++ b/server/lib/field_hub_web/live/project_show_live.ex @@ -118,7 +118,15 @@ defmodule FieldHubWeb.ProjectShowLive do {:noreply,assign(socket,:deletion_dialog_started, true)} end - def handle_event("update", %{"confirm_project_name" => confirm_project_name} = _values, socket) do + # def handle_event("display_delete_dialogue", _values, socket) do + # if %{"confirm_project_name" => confirm_project_name} =_values do + # assign(socket, :confirm_project_name, confirm_project_name) + # end + # {:noreply,assign(socket,:deletion_dialog_started, true)} + # end + + def handle_event("delete", %{"confirm_project_name" => confirm_project_name} = _values, socket) do + # def handle_event("update", %{"confirm_project_name" => confirm_project_name} = _values, socket) do {:noreply, assign(socket, :confirm_project_name, confirm_project_name)} end diff --git a/server/lib/field_hub_web/live/project_show_live.html.heex b/server/lib/field_hub_web/live/project_show_live.html.heex index bc7d821c16..48f5d3c54a 100644 --- a/server/lib/field_hub_web/live/project_show_live.html.heex +++ b/server/lib/field_hub_web/live/project_show_live.html.heex @@ -126,7 +126,7 @@ <%= if User.is_admin?(@current_user) do %>

Password change

-
+
<%!--

Delete project

--%> - +
-
-
- - <%= if @deletion_dialog_started == true do %> -
-
+
-
+
+
+ +
-
+
+
+
+ -
+
- <% end %> <% end %>
From 6b5771d891f012c965816e85c972ce14135e8558 Mon Sep 17 00:00:00 2001 From: the-last-pastafarian Date: Wed, 6 Dec 2023 10:23:56 +0100 Subject: [PATCH 06/10] fix tests: add id to the input "New password" --- server/lib/field_hub_web/live/project_show_live.html.heex | 1 + server/test/field_hub_web/live/project_show_live_test.exs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/server/lib/field_hub_web/live/project_show_live.html.heex b/server/lib/field_hub_web/live/project_show_live.html.heex index f9c5be5222..9291f24118 100644 --- a/server/lib/field_hub_web/live/project_show_live.html.heex +++ b/server/lib/field_hub_web/live/project_show_live.html.heex @@ -132,6 +132,7 @@ diff --git a/server/test/field_hub_web/live/project_show_live_test.exs b/server/test/field_hub_web/live/project_show_live_test.exs index aa528bf5e0..e5170c921a 100644 --- a/server/test/field_hub_web/live/project_show_live_test.exs +++ b/server/test/field_hub_web/live/project_show_live_test.exs @@ -341,7 +341,7 @@ defmodule FieldHubWeb.ProjectShowLiveTest do {:ok, %{conn: conn}} end - test "admin has passwort setting interface", %{conn: conn} do + test "admin has password setting interface", %{conn: conn} do {:ok, view, _html_on_mount} = live(conn, "/ui/projects/show/#{@project}") html = render(view) From e9ba3e222aa779f3a57cbeea46f4622ee364d193 Mon Sep 17 00:00:00 2001 From: the-last-pastafarian Date: Wed, 6 Dec 2023 10:27:18 +0100 Subject: [PATCH 07/10] change label font-weight --- server/assets/css/phoenix.css | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/server/assets/css/phoenix.css b/server/assets/css/phoenix.css index 0d59050f89..961499844d 100644 --- a/server/assets/css/phoenix.css +++ b/server/assets/css/phoenix.css @@ -28,6 +28,11 @@ select { width: auto; } +label { + font-weight:300; + display:inline; +} + /* Phoenix promo and logo */ .phx-hero { text-align: center; From 3342d54afecaecc912c28443041aecbdd31ed963 Mon Sep 17 00:00:00 2001 From: the-last-pastafarian Date: Tue, 19 Dec 2023 15:16:46 +0100 Subject: [PATCH 08/10] tests for project deletion --- server/lib/field_hub/project.ex | 3 +- .../field_hub_web/live/project_show_live.ex | 2 - .../live/project_show_live_test.exs | 92 +++++++++++++++++++ 3 files changed, 94 insertions(+), 3 deletions(-) diff --git a/server/lib/field_hub/project.ex b/server/lib/field_hub/project.ex index 9d55713786..66094fc335 100644 --- a/server/lib/field_hub/project.ex +++ b/server/lib/field_hub/project.ex @@ -130,7 +130,8 @@ defmodule FieldHub.Project do __Parameters__ - `project_identifier` the project's name. """ - def exists?(project_identifier) do + def exists?(project_identifier) + when project_identifier != "" and not is_nil(project_identifier) do CouchService.get_db_infos(project_identifier) |> case do %{status_code: 200} -> diff --git a/server/lib/field_hub_web/live/project_show_live.ex b/server/lib/field_hub_web/live/project_show_live.ex index f3040468f9..9f0f769b6a 100644 --- a/server/lib/field_hub_web/live/project_show_live.ex +++ b/server/lib/field_hub_web/live/project_show_live.ex @@ -169,8 +169,6 @@ defmodule FieldHubWeb.ProjectShowLive do |> put_flash(:error, msg) end - IO.inspect(delete_files) - {:noreply, redirect(socket, to: "/")} end diff --git a/server/test/field_hub_web/live/project_show_live_test.exs b/server/test/field_hub_web/live/project_show_live_test.exs index e5170c921a..6535f2ca12 100644 --- a/server/test/field_hub_web/live/project_show_live_test.exs +++ b/server/test/field_hub_web/live/project_show_live_test.exs @@ -24,6 +24,7 @@ defmodule FieldHubWeb.ProjectShowLiveTest do @user_password "test_password" @admin_user Application.compile_env(:field_hub, :couchdb_admin_name) + @index_cache_name Application.compile_env(:field_hub, :file_index_cache_name) test "redirect to login if not authenticated", %{conn: conn} do # Test the authentication plug (http) @@ -422,6 +423,97 @@ defmodule FieldHubWeb.ProjectShowLiveTest do }) end + # test "file index cache can be deleted through the interface", %{conn: conn} do + # assert {:ok, nil } = Cachex.get(@index_cache_name, @project) + + # {:ok, view, _html_on_mount} = live(conn, "/ui/projects/show/#{@project}") + + # html = + # view + # |> element("button", "Clear cache") + # |> render_click() + # |> IO.inspect() + + # end + + test "admin is able to delete a project's database", %{conn: conn} do + {:ok, view, _html_on_mount} = live(conn, "/ui/projects/show/#{@project}") + + # Check if the system knows the project currently + assert true == FieldHub.Project.exists?(@project) + + assert FieldHub.FileStore.file_index(@project) |> Enum.count() > 0 + + # Simulate the repeated project name input + view + |> element("#del_form") + |> render_change(%{repeat_project_name_input: @project}) + + # Check if we are beeing redirected to the landing page + {:error, {:redirect, %{to: "/"}}} = + view + |> element("button", "Delete") + |> render_click() + + # Check if the project got deleted. + assert false == FieldHub.Project.exists?(@project) + + assert FieldHub.FileStore.file_index(@project) |> Enum.count() > 0 + end + + test "admin is able to delete a project's database and its files", %{conn: conn} do + {:ok, view, _html_on_mount} = live(conn, "/ui/projects/show/#{@project}") + + # Check if the system knows the project. + assert true == FieldHub.Project.exists?(@project) + + # Check if the project's file directory exists. + assert File.exists?("test/tmp/#{@project}/") + + # Simulate the repeated project name input + view + |> element("#del_form") + |> render_change(%{repeat_project_name_input: @project}) + + html = + view + |> element("#del_form") + |> render_change(%{delete_files_radio: "delete_files"}) + + assert html =~ "value=\"delete_files\" checked" + + # Check if we are beeing redirected to the landing page + {:error, {:redirect, %{to: "/"}}} = + view + |> element("button", "Delete") + |> render_click() + + # Check if the project got deleted. + assert false == FieldHub.Project.exists?(@project) + + # Check if files have been deleted. + assert not File.exists?("test/tmp/#{@project}/") + end + + test "project deletion button is disabled until project name is repeated", %{conn: conn} do + {:ok, view, _html_on_mount} = live(conn, "/ui/projects/show/#{@project}") + + assert true == FieldHub.Project.exists?(@project) + + html = render(view) + + # The "Delete" button should be disabled as long as the repeated project name does not match. + assert html =~ "phx-click=\"delete\" disabled=\"disabled\"" + + html = + view + |> element("#del_form") + |> render_change(%{repeat_project_name_input: @project}) + + # The "Delete" button should be enabled now. + assert not (html =~ "phx-click=\"delete\" disabled=\"disabled\"") + end + test "throws warning if default user is missing", %{conn: conn} do # This case is highly unlikely, but is checked by the view nonetheless for completeness sake. From 22cfeba37c215de9a4969e3477311b235107dba5 Mon Sep 17 00:00:00 2001 From: the-last-pastafarian Date: Tue, 19 Dec 2023 15:46:25 +0100 Subject: [PATCH 09/10] tests for project deletion --- .../live/project_show_live_test.exs | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/server/test/field_hub_web/live/project_show_live_test.exs b/server/test/field_hub_web/live/project_show_live_test.exs index 6535f2ca12..2966bbbd13 100644 --- a/server/test/field_hub_web/live/project_show_live_test.exs +++ b/server/test/field_hub_web/live/project_show_live_test.exs @@ -24,7 +24,7 @@ defmodule FieldHubWeb.ProjectShowLiveTest do @user_password "test_password" @admin_user Application.compile_env(:field_hub, :couchdb_admin_name) - @index_cache_name Application.compile_env(:field_hub, :file_index_cache_name) + # @index_cache_name Application.compile_env(:field_hub, :file_index_cache_name) test "redirect to login if not authenticated", %{conn: conn} do # Test the authentication plug (http) @@ -461,6 +461,45 @@ defmodule FieldHubWeb.ProjectShowLiveTest do assert FieldHub.FileStore.file_index(@project) |> Enum.count() > 0 end + test "admin is able to delete a project's database and files still exist after having changed the radio button selection", + %{conn: conn} do + {:ok, view, _html_on_mount} = live(conn, "/ui/projects/show/#{@project}") + + # Check if the system knows the project. + assert true == FieldHub.Project.exists?(@project) + + # Check if the project's file directory exists. + assert File.exists?("test/tmp/#{@project}/") + + # Simulate the repeated project name input + view + |> element("#del_form") + |> render_change(%{repeat_project_name_input: @project}) + + view + |> element("#del_form") + |> render_change(%{delete_files_radio: "delete_files"}) + + html = + view + |> element("#del_form") + |> render_change(%{delete_files_radio: "keep_files"}) + + assert html =~ "value=\"keep_files\" checked" + + # Check if we are beeing redirected to the landing page + {:error, {:redirect, %{to: "/"}}} = + view + |> element("button", "Delete") + |> render_click() + + # Check if the project got deleted. + assert false == FieldHub.Project.exists?(@project) + + # Check if files have been deleted. + assert File.exists?("test/tmp/#{@project}/") + end + test "admin is able to delete a project's database and its files", %{conn: conn} do {:ok, view, _html_on_mount} = live(conn, "/ui/projects/show/#{@project}") From 3ff931e487eb5a234620439dbc30204a1d108892 Mon Sep 17 00:00:00 2001 From: the-last-pastafarian Date: Tue, 19 Dec 2023 15:54:34 +0100 Subject: [PATCH 10/10] remove commented test for cache clearing --- .../field_hub_web/live/project_show_live_test.exs | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/server/test/field_hub_web/live/project_show_live_test.exs b/server/test/field_hub_web/live/project_show_live_test.exs index 2966bbbd13..ac4f1d74d2 100644 --- a/server/test/field_hub_web/live/project_show_live_test.exs +++ b/server/test/field_hub_web/live/project_show_live_test.exs @@ -24,7 +24,6 @@ defmodule FieldHubWeb.ProjectShowLiveTest do @user_password "test_password" @admin_user Application.compile_env(:field_hub, :couchdb_admin_name) - # @index_cache_name Application.compile_env(:field_hub, :file_index_cache_name) test "redirect to login if not authenticated", %{conn: conn} do # Test the authentication plug (http) @@ -423,19 +422,6 @@ defmodule FieldHubWeb.ProjectShowLiveTest do }) end - # test "file index cache can be deleted through the interface", %{conn: conn} do - # assert {:ok, nil } = Cachex.get(@index_cache_name, @project) - - # {:ok, view, _html_on_mount} = live(conn, "/ui/projects/show/#{@project}") - - # html = - # view - # |> element("button", "Clear cache") - # |> render_click() - # |> IO.inspect() - - # end - test "admin is able to delete a project's database", %{conn: conn} do {:ok, view, _html_on_mount} = live(conn, "/ui/projects/show/#{@project}")