Move acepting of ToS change to a reflex

Spree::Admin::UserController is for super admin user only. Moving to a
reflex simplifies the code by getting rid of a new route and a new stimulus
controller
This commit is contained in:
Gaetan Craig-Riou
2023-11-23 14:17:04 +11:00
committed by Konrad
parent 91862c126b
commit d0ba881aa2
8 changed files with 42 additions and 111 deletions

View File

@@ -57,12 +57,6 @@ module Spree
end
end
def accept_terms_of_services
@user.update(terms_of_service_accepted_at: DateTime.now)
head :ok
end
protected
def collection

View File

@@ -0,0 +1,11 @@
# frozen_string_literal: true
class Enterprise
class UserReflex < ApplicationReflex
def accept_terms_of_services
current_user.update(terms_of_service_accepted_at: DateTime.now)
morph "#banner-container", ""
end
end
end

View File

@@ -1,9 +1,9 @@
.banner-container
.banner-container#banner-container
- if @terms_of_service_banner == true
%div{ class: "terms-of-service-banner", data: { controller: "terms-of-service-banner", "terms-of-service-banner-url-value": accept_terms_of_services_admin_user_path(current_spree_user&.id) }}
.terms-of-service-banner
.column-left
%p= t("admin.terms_of_service_have_been_updated_html", tos_link: link_to(t("admin.terms_of_service"), TermsOfServiceFile.current_url, target: "_blank"))
.column-right
%button{ data: { action: "click->terms-of-service-banner#accept click->terms-of-service-banner#close_banner" } }
%button{ data: { reflex: "click->Enterprise::User#accept_terms_of_services", id: current_spree_user&.id } }
= t("admin.accept_terms_of_service")

View File

@@ -1,16 +0,0 @@
import { Controller } from "stimulus";
export default class extends Controller {
static values = { url: String };
accept() {
const token = document.querySelector('meta[name="csrf-token"]').content;
// We don't really care if the update fails, if it fails it will result in the banner still
// being shown.
fetch(this.urlValue, { method: "post", headers: { "X-CSRF-Token": token } });
}
close_banner() {
this.element.remove();
}
}

View File

@@ -48,11 +48,7 @@ Spree::Core::Engine.routes.draw do
get '/search/known_users' => "search#known_users", :as => :search_known_users
get '/search/customers' => 'search#customers', :as => :search_customers
resources :users do
member do
post :accept_terms_of_services
end
end
resources :users
constraints FeatureToggleConstraint.new(:admin_style_v3, negate: true) do
# Show old bulk products screen

View File

@@ -38,21 +38,4 @@ describe Spree::Admin::UsersController do
expect(response).to redirect_to('/unauthorized')
end
end
describe "#accept_terms_of_services" do
let(:user) { create(:user) }
before do
allow(controller).to receive_messages spree_current_user: user
user.spree_roles << Spree::Role.find_or_create_by(name: 'admin')
end
it "updates terms_of_service_accepted_at" do
expect do
spree_post :accept_terms_of_services, id: user.id
end.to change { user.reload.terms_of_service_accepted_at }
expect(response).to have_http_status(:ok)
end
end
end

View File

@@ -1,64 +0,0 @@
/**
* @jest-environment jsdom
*/
import { Application } from "stimulus"
import terms_of_service_banner_controller from "../../../app/webpacker/controllers/terms_of_service_banner_controller"
describe("TermsOfServiceBannerController", () => {
beforeAll(() => {
const application = Application.start()
application.register("terms-of-service-banner", terms_of_service_banner_controller)
})
beforeEach(() => {
document.body.innerHTML = `
<meta content="abc123authenticitytoken" name="csrf-token">
<div
id="test-controller-div"
data-controller="terms-of-service-banner"
data-terms-of-service-banner-url-value="admin/users/10/accept_terms_of_service"
>
<div>
<span>Terms of service has been updated </span>
<button id="test-accept-banner" data-action="click->terms-of-service-banner#accept">
Accept terms of service
</button>
<button id="test-close-banner" data-action="click->terms-of-service-banner#close_banner">close</button>
</div>
</div>
`
})
describe("#close", () => {
it("removes the banner", () => {
const closeButton = document.getElementById("test-close-banner")
closeButton.click()
expect(document.getElementById("test-controller-div")).toBeNull()
})
})
describe("#accept", () => {
it("fires a request to accept terms of service", () => {
const mockFetch = jest.fn().mockImplementation( (_url, _options) =>
Promise.resolve({
ok: true,
json: () => "",
})
)
window.fetch = (_url, _options) => {
return mockFetch(_url, _options)
}
const button = document.getElementById("test-accept-banner")
button.click()
expect(mockFetch).toHaveBeenCalledWith(
"admin/users/10/accept_terms_of_service",
{ headers: { "X-CSRF-Token": "abc123authenticitytoken" }, method: "post" }
)
})
})
})

View File

@@ -0,0 +1,27 @@
# frozen_string_literal: true
require "reflex_helper"
describe Enterprise::UserReflex, type: :reflex do
let(:current_user) { create(:user) }
let(:context) { { url: spree.admin_dashboard_url, connection: { current_user: } } }
describe "#accept_terms_of_services" do
subject(:reflex) do
build_reflex(
method_name: :accept_terms_of_services, **context, params: { id: current_user.id }
)
end
it "updates terms_of_service_accepted_at" do
expect{
reflex.run(:accept_terms_of_services)
current_user.reload
}.to change{ current_user.terms_of_service_accepted_at }
end
it "removes banner from the page" do
expect(reflex.run(:accept_terms_of_services)).to morph("#banner-container").with("")
end
end
end