diff --git a/app/controllers/admin/tools_controller.rb b/app/controllers/admin/tools_controller.rb index 4d51bd2a3..c66a7ea35 100644 --- a/app/controllers/admin/tools_controller.rb +++ b/app/controllers/admin/tools_controller.rb @@ -22,6 +22,27 @@ module Admin redirect_to admin_tools_path end + def feed_playground + return if params[:config].blank? + + begin + config_json = JSON.parse(params[:config]) + config = Articles::Feeds::VariantAssembler + .build_with(catalog: Articles::Feeds.lever_catalog, config: config_json, variant: "test") + @user = User.find_by(username: params[:username]) || current_user + @feed = Articles::Feeds::VariantQuery.new( + config: config, + user: @user, + number_of_articles: params[:number_of_articles] || 25, + page: 1, + tag: nil, + ) + @articles = @feed.more_comments_minimal_weight_randomized.to_a + rescue KeyError, JSON::ParserError, ActiveRecord::StatementInvalid => e + flash[:danger] = e.message + end + end + private def handle_dead_path diff --git a/app/models/articles/feeds/variant_assembler.rb b/app/models/articles/feeds/variant_assembler.rb index f4c73ea8f..cb4bdd64d 100644 --- a/app/models/articles/feeds/variant_assembler.rb +++ b/app/models/articles/feeds/variant_assembler.rb @@ -88,7 +88,6 @@ module Articles reseed_randomizer_on_each_request: config.fetch("reseed_randomizer_on_each_request"), ) end - private_class_method :build_with end end end diff --git a/app/services/articles/feeds/variant_query.rb b/app/services/articles/feeds/variant_query.rb index 57fbbfc99..3c055f7ec 100644 --- a/app/services/articles/feeds/variant_query.rb +++ b/app/services/articles/feeds/variant_query.rb @@ -25,8 +25,6 @@ module Articles new(config: config, **kwargs) end - # Let's make sure that folks initialize this with a variant configuration. - private_class_method :new Config = Struct.new( :variant, diff --git a/app/views/admin/tools/feed_playground.html.erb b/app/views/admin/tools/feed_playground.html.erb new file mode 100644 index 000000000..041919059 --- /dev/null +++ b/app/views/admin/tools/feed_playground.html.erb @@ -0,0 +1,33 @@ +

Feed Playground

+
+ <%= form_tag feed_playground_admin_tools_path do %> +
+ <%= label_tag :config %> + <%= text_area_tag :config, params[:config], placeholder: '{"description": ..., "order_by": ..., etc.}', class: "block w-100 mb-4 rounded", style: "height: 300px" %> + <%= label_tag :username, "User" %> + <%= text_field_tag :username, params[:username] || current_user.username, placeholder: "sloan" %> + <%= label_tag :number_of_articles %> + <%= text_field_tag :number_of_articles, params[:number_of_articles] || 25, placeholder: "25" %> + <%= submit_tag("Preview feed") %> +
+ <% end %> +
+ +<% if @user && @articles %> +

Feed Preview for @<%= @user.username %>

+
+ <% Follow.follower_tag(@user.id).includes(:followable).order("points DESC").each do |follow| %> + + #<%= follow.followable.name %>: <%= follow.points.round(3) %> + + <% end %> +
+ +
+
+ <% @articles.each do |article| %> + <%= render "articles/single_story", story: article.decorate, featured: article.main_image.present? %> + <% end %> +
+
+<% end %> diff --git a/config/routes/admin.rb b/config/routes/admin.rb index 3a5a93f6e..14c048e7c 100644 --- a/config/routes/admin.rb +++ b/config/routes/admin.rb @@ -132,6 +132,8 @@ namespace :admin do resources :tools, only: %i[index create] do collection do post "bust_cache" + get "feed_playground" + post "feed_playground" end end diff --git a/spec/requests/admin/feed_playground_spec.rb b/spec/requests/admin/feed_playground_spec.rb new file mode 100644 index 000000000..6425ea0ed --- /dev/null +++ b/spec/requests/admin/feed_playground_spec.rb @@ -0,0 +1,78 @@ +require "rails_helper" + +RSpec.describe "/admin/advanced/tools/feed_playground", type: :request do + describe "GET /admin/advanced/tools/feed_playground" do + context "when the user is not an admin" do + let(:user) { create(:user) } + + before do + sign_in user + end + + it "blocks the request" do + expect do + get feed_playground_admin_tools_path + end.to raise_error(Pundit::NotAuthorizedError) + end + end + + context "when the user is a super admin" do + let(:super_admin) { create(:user, :super_admin) } + + before do + sign_in super_admin + get feed_playground_admin_tools_path + end + + it "allows the request" do + expect(response).to have_http_status(:ok) + end + end + + context "when the user is a single resource admin" do + let(:single_resource_admin) { create(:user, :single_resource_admin, resource: Tool) } + + before do + sign_in single_resource_admin + get feed_playground_admin_tools_path + end + + it "allows the request" do + expect(response).to have_http_status(:ok) + end + end + + context "when the user is the wrong single resource admin" do + let(:single_resource_admin) { create(:user, :single_resource_admin, resource: Article) } + + before do + sign_in single_resource_admin + end + + it "blocks the request" do + expect do + get feed_playground_admin_tools_path + end.to raise_error(Pundit::NotAuthorizedError) + end + end + end + + describe "POST /admin/advanced/tools/feed_playground" do + context "when the user is a super admin" do + let(:super_admin) { create(:user, :super_admin) } + + before do + sign_in super_admin + post feed_playground_admin_tools_path, params: { config: "sddsdsdsdsds" } + end + + it "allows the request" do + expect(response).to have_http_status(:ok) + end + + it "shows proper error" do + expect(response.body).to include("unexpected token at") + end + end + end +end