From bd530fc0ea053e99c471e455b98381dd66a19995 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Wed, 7 Oct 2020 16:38:33 +0200 Subject: [PATCH 1/5] List direct_verification's authorization records --- .../direct_verifications/authorizations.scss | 23 +++++++++++ .../admin/authorizations_controller.rb | 17 ++++++++ .../admin/authorizations/index.html.erb | 35 ++++++++++++++++ .../admin/direct_verifications/index.html.erb | 1 + config/locales/en.yml | 8 ++++ .../direct_verifications/tests/factories.rb | 11 +++++ .../verification/admin_engine.rb | 1 + .../admin/authorizations_controller_spec.rb | 24 +++++++++++ spec/factories.rb | 2 +- ...in_manages_imported_authorizations_spec.rb | 41 +++++++++++++++++++ 10 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 app/assets/stylesheets/decidim/direct_verifications/authorizations.scss create mode 100644 app/controllers/decidim/direct_verifications/verification/admin/authorizations_controller.rb create mode 100644 app/views/decidim/direct_verifications/verification/admin/authorizations/index.html.erb create mode 100644 lib/decidim/direct_verifications/tests/factories.rb create mode 100644 spec/controllers/decidim/direct_verifications/verification/admin/authorizations_controller_spec.rb create mode 100644 spec/system/decidim/direct_verifications/admin/admin_manages_imported_authorizations_spec.rb diff --git a/app/assets/stylesheets/decidim/direct_verifications/authorizations.scss b/app/assets/stylesheets/decidim/direct_verifications/authorizations.scss new file mode 100644 index 0000000..dcbad04 --- /dev/null +++ b/app/assets/stylesheets/decidim/direct_verifications/authorizations.scss @@ -0,0 +1,23 @@ +// Copied from "decidim/admin/utils/settings". We better import the file + +$black: #1a181d; +$font-family-monospace: Consolas, 'Liberation Mono', Courier, monospace; +$global-weight-normal: normal; +$light-gray: #eee; +$medium-gray: #adadad; + +$code-color: $black; +$code-font-family: $font-family-monospace; +$code-font-weight: $global-weight-normal; +$code-background: $light-gray; +$code-border: 1px solid $medium-gray; +$code-padding: rem-calc(2 5 1); + +.code { + background: $code-background; + color: $code-color; + font-family: $code-font-family; + font-weight: $code-font-weight; + border: $code-border; + padding: $code-padding; +} diff --git a/app/controllers/decidim/direct_verifications/verification/admin/authorizations_controller.rb b/app/controllers/decidim/direct_verifications/verification/admin/authorizations_controller.rb new file mode 100644 index 0000000..ab354d1 --- /dev/null +++ b/app/controllers/decidim/direct_verifications/verification/admin/authorizations_controller.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module Decidim + module DirectVerifications + module Verification + module Admin + class AuthorizationsController < Decidim::Admin::ApplicationController + layout "decidim/admin/users" + + def index + @authorizations = Decidim::Authorization.where(name: "direct_verifications") + end + end + end + end + end +end diff --git a/app/views/decidim/direct_verifications/verification/admin/authorizations/index.html.erb b/app/views/decidim/direct_verifications/verification/admin/authorizations/index.html.erb new file mode 100644 index 0000000..2f6f2c8 --- /dev/null +++ b/app/views/decidim/direct_verifications/verification/admin/authorizations/index.html.erb @@ -0,0 +1,35 @@ +<%= stylesheet_link_tag "decidim/direct_verifications/authorizations" %> + +
+
+

+ <%= t('.title') %> +

+
+
+
+ + + + + + + + + + + <% @authorizations.each do |authorization| %> + + + + + + + <% end %> + +
<%= t('.name') %><%= t('.metadata') %><%= t('.user_name') %><%= t('.created_at') %>
<%= authorization.name %><%= authorization.decidim_user_id %><%= authorization.created_at %>
+
+
+
diff --git a/app/views/decidim/direct_verifications/verification/admin/direct_verifications/index.html.erb b/app/views/decidim/direct_verifications/verification/admin/direct_verifications/index.html.erb index ef4a22b..307464c 100644 --- a/app/views/decidim/direct_verifications/verification/admin/direct_verifications/index.html.erb +++ b/app/views/decidim/direct_verifications/verification/admin/direct_verifications/index.html.erb @@ -3,6 +3,7 @@

<%= t('admin.index.title', scope: 'decidim.direct_verifications.verification') %> <%= link_to t("admin.index.stats", scope: 'decidim.direct_verifications.verification'), stats_path, class: "button tiny button--title" %> + <%= link_to t("admin.index.authorizations", scope: "decidim.direct_verifications.verification"), authorizations_path, class: "button tiny button--title" %>

diff --git a/config/locales/en.yml b/config/locales/en.yml index e9ef8ef..94241c2 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -21,6 +21,13 @@ en: direct_verifications: verification: admin: + authorizations: + index: + created_at: Created at + metadata: Metadata + name: Name + title: Authorizations + user_name: User name direct_verifications: create: authorized: "%{authorized} users have been successfully verified using @@ -35,6 +42,7 @@ en: need to have explicit consent from your users in order to register them. Otherwise you will be infringing the GDPR regulation in EU countries. index: + authorizations: Imported emails stats: User stats title: Register and authorize users new: diff --git a/lib/decidim/direct_verifications/tests/factories.rb b/lib/decidim/direct_verifications/tests/factories.rb new file mode 100644 index 0000000..5cbf669 --- /dev/null +++ b/lib/decidim/direct_verifications/tests/factories.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +require "decidim/core/test/factories" + +FactoryBot.modify do + factory :authorization do + trait :direct_verification do + name { "direct_verifications" } + end + end +end diff --git a/lib/decidim/direct_verifications/verification/admin_engine.rb b/lib/decidim/direct_verifications/verification/admin_engine.rb index 22af2b2..ec0c480 100644 --- a/lib/decidim/direct_verifications/verification/admin_engine.rb +++ b/lib/decidim/direct_verifications/verification/admin_engine.rb @@ -10,6 +10,7 @@ class AdminEngine < ::Rails::Engine routes do resources :direct_verifications, only: [:index, :create, :stats] resources :stats, only: [:index] + resources :authorizations, only: [:index] root to: "direct_verifications#index" end diff --git a/spec/controllers/decidim/direct_verifications/verification/admin/authorizations_controller_spec.rb b/spec/controllers/decidim/direct_verifications/verification/admin/authorizations_controller_spec.rb new file mode 100644 index 0000000..8280c3b --- /dev/null +++ b/spec/controllers/decidim/direct_verifications/verification/admin/authorizations_controller_spec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require "spec_helper" + +module Decidim::DirectVerifications::Verification::Admin + describe AuthorizationsController, type: :controller do + routes { Decidim::DirectVerifications::Verification::AdminEngine.routes } + + let(:organization) { create(:organization) } + let(:user) { create(:user, :admin, :confirmed, organization: organization) } + + before do + request.env["decidim.current_organization"] = organization + sign_in user + end + + describe "#index" do + it "renders the decidim/admin/users layout" do + get :index + expect(response).to render_template("layouts/decidim/admin/users") + end + end + end +end diff --git a/spec/factories.rb b/spec/factories.rb index 0e6d87b..0dcb8dd 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -1,3 +1,3 @@ # frozen_string_literal: true -require "decidim/core/test/factories" +require "decidim/direct_verifications/tests/factories" diff --git a/spec/system/decidim/direct_verifications/admin/admin_manages_imported_authorizations_spec.rb b/spec/system/decidim/direct_verifications/admin/admin_manages_imported_authorizations_spec.rb new file mode 100644 index 0000000..bd581f2 --- /dev/null +++ b/spec/system/decidim/direct_verifications/admin/admin_manages_imported_authorizations_spec.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe "Admin manages imported authorizations", type: :system do + let(:organization) { create(:organization) } + let(:user) { create(:user, :admin, :confirmed, organization: organization) } + + let!(:authorization) { create(:authorization, :direct_verification) } + let!(:non_direct_authorization) { create(:authorization) } + + let(:scope) { "decidim.direct_verifications.verification.admin" } + + before do + switch_to_host(organization.host) + login_as user, scope: :user + end + + it "lists authorizations imported through direct_verifications" do + visit decidim_admin_direct_verifications.direct_verifications_path + click_link I18n.t("index.authorizations", scope: scope) + + expect(page).to have_current_path(decidim_admin_direct_verifications.authorizations_path) + + within "table thead" do + expect(page).to have_content(I18n.t("authorizations.index.name", scope: scope).upcase) + expect(page).to have_content(I18n.t("authorizations.index.metadata", scope: scope).upcase) + expect(page).to have_content(I18n.t("authorizations.index.user_name", scope: scope).upcase) + expect(page).to have_content(I18n.t("authorizations.index.created_at", scope: scope).upcase) + end + + within "tr[data-authorization-id=\"#{authorization.id}\"]" do + expect(page).to have_content(authorization.name) + expect(page).to have_content(authorization.metadata) + expect(page).to have_content(authorization.decidim_user_id) + expect(page).to have_content(authorization.created_at) + end + + expect(page).not_to have_content(non_direct_authorization.name) + end +end From 26726d2d1eda095fdada27576fba2aa9afd657af Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Wed, 7 Oct 2020 17:06:29 +0200 Subject: [PATCH 2/5] Implement destroy authorization action --- .../admin/authorizations_controller.rb | 19 +++++++- .../admin/authorizations/index.html.erb | 6 +++ config/locales/en.yml | 3 +- .../verification/admin_engine.rb | 2 +- ...in_manages_imported_authorizations_spec.rb | 43 ++++++++++++------- 5 files changed, 54 insertions(+), 19 deletions(-) diff --git a/app/controllers/decidim/direct_verifications/verification/admin/authorizations_controller.rb b/app/controllers/decidim/direct_verifications/verification/admin/authorizations_controller.rb index ab354d1..46dd3a3 100644 --- a/app/controllers/decidim/direct_verifications/verification/admin/authorizations_controller.rb +++ b/app/controllers/decidim/direct_verifications/verification/admin/authorizations_controller.rb @@ -8,7 +8,24 @@ class AuthorizationsController < Decidim::Admin::ApplicationController layout "decidim/admin/users" def index - @authorizations = Decidim::Authorization.where(name: "direct_verifications") + @authorizations = collection + end + + def destroy + if authorization.destroy + flash[:notice] = "successfully" + redirect_to authorizations_path + end + end + + private + + def collection + Decidim::Authorization.where(name: "direct_verifications") + end + + def authorization + @authorization ||= collection.find_by(id: params[:id]) end end end diff --git a/app/views/decidim/direct_verifications/verification/admin/authorizations/index.html.erb b/app/views/decidim/direct_verifications/verification/admin/authorizations/index.html.erb index 2f6f2c8..ced73d2 100644 --- a/app/views/decidim/direct_verifications/verification/admin/authorizations/index.html.erb +++ b/app/views/decidim/direct_verifications/verification/admin/authorizations/index.html.erb @@ -4,6 +4,7 @@

<%= t('.title') %> + <%= link_to t(".new_import"), direct_verifications_path, class: "button tiny button--title" %>

@@ -15,6 +16,7 @@ <%= t('.metadata') %> <%= t('.user_name') %> <%= t('.created_at') %> +   @@ -26,6 +28,10 @@ <%= authorization.decidim_user_id %> <%= authorization.created_at %> + + + <%= icon_link_to "circle-x", authorization_path(authorization), t("actions.destroy", scope: "decidim.admin"), class: "action-icon--remove", method: :delete, data: { confirm: t("actions.confirm_destroy", scope: "decidim.admin") } %> + <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 94241c2..695f652 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -28,6 +28,7 @@ en: name: Name title: Authorizations user_name: User name + new_import: New import direct_verifications: create: authorized: "%{authorized} users have been successfully verified using @@ -42,7 +43,7 @@ en: need to have explicit consent from your users in order to register them. Otherwise you will be infringing the GDPR regulation in EU countries. index: - authorizations: Imported emails + authorizations: Authorized users stats: User stats title: Register and authorize users new: diff --git a/lib/decidim/direct_verifications/verification/admin_engine.rb b/lib/decidim/direct_verifications/verification/admin_engine.rb index ec0c480..838476e 100644 --- a/lib/decidim/direct_verifications/verification/admin_engine.rb +++ b/lib/decidim/direct_verifications/verification/admin_engine.rb @@ -10,7 +10,7 @@ class AdminEngine < ::Rails::Engine routes do resources :direct_verifications, only: [:index, :create, :stats] resources :stats, only: [:index] - resources :authorizations, only: [:index] + resources :authorizations, only: [:index, :destroy] root to: "direct_verifications#index" end diff --git a/spec/system/decidim/direct_verifications/admin/admin_manages_imported_authorizations_spec.rb b/spec/system/decidim/direct_verifications/admin/admin_manages_imported_authorizations_spec.rb index bd581f2..d11ff28 100644 --- a/spec/system/decidim/direct_verifications/admin/admin_manages_imported_authorizations_spec.rb +++ b/spec/system/decidim/direct_verifications/admin/admin_manages_imported_authorizations_spec.rb @@ -14,28 +14,39 @@ before do switch_to_host(organization.host) login_as user, scope: :user - end - it "lists authorizations imported through direct_verifications" do visit decidim_admin_direct_verifications.direct_verifications_path click_link I18n.t("index.authorizations", scope: scope) + end - expect(page).to have_current_path(decidim_admin_direct_verifications.authorizations_path) - - within "table thead" do - expect(page).to have_content(I18n.t("authorizations.index.name", scope: scope).upcase) - expect(page).to have_content(I18n.t("authorizations.index.metadata", scope: scope).upcase) - expect(page).to have_content(I18n.t("authorizations.index.user_name", scope: scope).upcase) - expect(page).to have_content(I18n.t("authorizations.index.created_at", scope: scope).upcase) + context "when listing authorizations" do + it "lists authorizations imported through direct_verifications" do + within "table thead" do + expect(page).to have_content(I18n.t("authorizations.index.name", scope: scope).upcase) + expect(page).to have_content(I18n.t("authorizations.index.metadata", scope: scope).upcase) + expect(page).to have_content(I18n.t("authorizations.index.user_name", scope: scope).upcase) + expect(page).to have_content(I18n.t("authorizations.index.created_at", scope: scope).upcase) + end + + within "tr[data-authorization-id=\"#{authorization.id}\"]" do + expect(page).to have_content(authorization.name) + expect(page).to have_content(authorization.metadata) + expect(page).to have_content(authorization.decidim_user_id) + expect(page).to have_content(authorization.created_at) + end + + expect(page).not_to have_content(non_direct_authorization.name) end + end - within "tr[data-authorization-id=\"#{authorization.id}\"]" do - expect(page).to have_content(authorization.name) - expect(page).to have_content(authorization.metadata) - expect(page).to have_content(authorization.decidim_user_id) - expect(page).to have_content(authorization.created_at) - end + context "when destroying an authorization" do + it "destroys the authorizations" do + within "tr[data-authorization-id=\"#{authorization.id}\"]" do + accept_confirm { click_link "Delete" } + end - expect(page).not_to have_content(non_direct_authorization.name) + expect(page).not_to have_content("tr[data-authorization-id=\"#{authorization.id}\"]") + expect(page).to have_admin_callout("successfully") + end end end From cb65e72b4fefc8ab2f86fda4fbeffc2ae298231a Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Fri, 23 Oct 2020 11:47:48 +0200 Subject: [PATCH 3/5] Display user name instead of id in table column --- .../verification/admin/authorizations_controller.rb | 2 +- .../verification/admin/authorizations/index.html.erb | 2 +- .../admin/admin_manages_imported_authorizations_spec.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/decidim/direct_verifications/verification/admin/authorizations_controller.rb b/app/controllers/decidim/direct_verifications/verification/admin/authorizations_controller.rb index 46dd3a3..bde487c 100644 --- a/app/controllers/decidim/direct_verifications/verification/admin/authorizations_controller.rb +++ b/app/controllers/decidim/direct_verifications/verification/admin/authorizations_controller.rb @@ -21,7 +21,7 @@ def destroy private def collection - Decidim::Authorization.where(name: "direct_verifications") + Decidim::Authorization.where(name: "direct_verifications").includes(:user) end def authorization diff --git a/app/views/decidim/direct_verifications/verification/admin/authorizations/index.html.erb b/app/views/decidim/direct_verifications/verification/admin/authorizations/index.html.erb index ced73d2..1c2ff73 100644 --- a/app/views/decidim/direct_verifications/verification/admin/authorizations/index.html.erb +++ b/app/views/decidim/direct_verifications/verification/admin/authorizations/index.html.erb @@ -26,7 +26,7 @@ <%= authorization.metadata %> - <%= authorization.decidim_user_id %> + <%= authorization.user.name %> <%= authorization.created_at %> diff --git a/spec/system/decidim/direct_verifications/admin/admin_manages_imported_authorizations_spec.rb b/spec/system/decidim/direct_verifications/admin/admin_manages_imported_authorizations_spec.rb index d11ff28..bdfd50f 100644 --- a/spec/system/decidim/direct_verifications/admin/admin_manages_imported_authorizations_spec.rb +++ b/spec/system/decidim/direct_verifications/admin/admin_manages_imported_authorizations_spec.rb @@ -31,7 +31,7 @@ within "tr[data-authorization-id=\"#{authorization.id}\"]" do expect(page).to have_content(authorization.name) expect(page).to have_content(authorization.metadata) - expect(page).to have_content(authorization.decidim_user_id) + expect(page).to have_content(authorization.user.name) expect(page).to have_content(authorization.created_at) end From 3ef0ffd3e26a54d46a21f46571df7d3ed07cd026 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Fri, 23 Oct 2020 12:11:46 +0200 Subject: [PATCH 4/5] Enforce permissions to index authorizations --- .../verification/admin/authorizations_controller.rb | 1 + .../verification/admin/authorizations_controller_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/app/controllers/decidim/direct_verifications/verification/admin/authorizations_controller.rb b/app/controllers/decidim/direct_verifications/verification/admin/authorizations_controller.rb index bde487c..9c92666 100644 --- a/app/controllers/decidim/direct_verifications/verification/admin/authorizations_controller.rb +++ b/app/controllers/decidim/direct_verifications/verification/admin/authorizations_controller.rb @@ -8,6 +8,7 @@ class AuthorizationsController < Decidim::Admin::ApplicationController layout "decidim/admin/users" def index + enforce_permission_to :index, :authorization @authorizations = collection end diff --git a/spec/controllers/decidim/direct_verifications/verification/admin/authorizations_controller_spec.rb b/spec/controllers/decidim/direct_verifications/verification/admin/authorizations_controller_spec.rb index 8280c3b..a298629 100644 --- a/spec/controllers/decidim/direct_verifications/verification/admin/authorizations_controller_spec.rb +++ b/spec/controllers/decidim/direct_verifications/verification/admin/authorizations_controller_spec.rb @@ -15,6 +15,12 @@ module Decidim::DirectVerifications::Verification::Admin end describe "#index" do + it "authorizes the action" do + expect(controller).to receive(:allowed_to?).with(:index, :authorization, {}) + + get :index + end + it "renders the decidim/admin/users layout" do get :index expect(response).to render_template("layouts/decidim/admin/users") From 3098eaca8779f6e20a13c946147076901c49a369 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Fri, 23 Oct 2020 12:45:24 +0200 Subject: [PATCH 5/5] Link to user stats for consistency --- .../verification/admin/authorizations/index.html.erb | 1 + .../admin/admin_manages_imported_authorizations_spec.rb | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/app/views/decidim/direct_verifications/verification/admin/authorizations/index.html.erb b/app/views/decidim/direct_verifications/verification/admin/authorizations/index.html.erb index 1c2ff73..e9e8f39 100644 --- a/app/views/decidim/direct_verifications/verification/admin/authorizations/index.html.erb +++ b/app/views/decidim/direct_verifications/verification/admin/authorizations/index.html.erb @@ -4,6 +4,7 @@

<%= t('.title') %> + <%= link_to t("admin.index.stats", scope: 'decidim.direct_verifications.verification'), stats_path, class: "button tiny button--title" %> <%= link_to t(".new_import"), direct_verifications_path, class: "button tiny button--title" %>

diff --git a/spec/system/decidim/direct_verifications/admin/admin_manages_imported_authorizations_spec.rb b/spec/system/decidim/direct_verifications/admin/admin_manages_imported_authorizations_spec.rb index bdfd50f..3ca8563 100644 --- a/spec/system/decidim/direct_verifications/admin/admin_manages_imported_authorizations_spec.rb +++ b/spec/system/decidim/direct_verifications/admin/admin_manages_imported_authorizations_spec.rb @@ -37,6 +37,11 @@ expect(page).not_to have_content(non_direct_authorization.name) end + + it "lets users navigate to stats and new import" do + expect(page).to have_link(t("decidim.direct_verifications.verification.admin.index.stats")) + expect(page).to have_link(t("decidim.direct_verifications.verification.admin.authorizations.index.new_import")) + end end context "when destroying an authorization" do