[deploy] HTML Variant admin dashboard (#10188)

* HTML Variant admin dashboard

* Tweaks to actions & spec fixes

* Update app/controllers/admin/html_variants_controller.rb

Co-authored-by: Michael Kohl <citizen428@dev.to>

* Removes old html_variants dashboards

* Removes old lingering spec

Co-authored-by: Michael Kohl <citizen428@dev.to>
This commit is contained in:
Fernando Valverde 2020-09-28 09:22:13 -06:00 committed by GitHub
parent 4b434bab45
commit 2297e84f35
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 343 additions and 654 deletions

View file

@ -16,6 +16,7 @@ module Admin
{ name: "display_ads", controller: "display_ads" },
{ name: "events", controller: "events" },
{ name: "growth", controller: "growth" },
{ name: "html_variants", controller: "html_variants" },
{ name: "listings", controller: "listings" },
{ name: "moderator_actions", controller: "moderator_actions" },
{ name: "mods", controller: "mods" },

View file

@ -0,0 +1,84 @@
module Admin
class HtmlVariantsController < Admin::ApplicationController
layout "admin"
def index
relation = if params[:state] == "mine"
current_user.html_variants.order(created_at: :desc)
elsif params[:state] == "admin"
HtmlVariant.where(published: true, approved: false).order(created_at: :desc)
elsif params[:state].present?
HtmlVariant.where(published: true, approved: true, group: params[:state]).order(success_rate: :desc)
else
HtmlVariant.where(published: true, approved: true).order(success_rate: :desc)
end
@html_variants = relation.includes(:user).page(params[:page]).per(30)
end
def new
@html_variant = HtmlVariant.new
return unless params[:fork_id]
@fork = HtmlVariant.find(params[:fork_id])
@html_variant.name = "#{@fork.name} FORK-#{rand(10_000)}"
@html_variant.html = @fork.html
end
def show
@html_variant = HtmlVariant.find(params[:id])
render layout: "application"
end
def edit
@html_variant = HtmlVariant.find(params[:id])
end
def create
@html_variant = HtmlVariant.new(html_variant_params)
@html_variant.user_id = current_user.id
if @html_variant.save
flash[:success] = "HTML Variant has been created!"
redirect_to admin_html_variants_path(state: "mine")
else
flash[:danger] = @html_variant.errors_as_sentence
render new_admin_html_variant_path
end
end
def update
@html_variant = HtmlVariant.find(params[:id])
if @html_variant.update(html_variant_params)
flash[:success] = "HTML Variant has been updated!"
redirect_to edit_admin_html_variant_path(@html_variant)
else
flash[:danger] = @html_variant.errors_as_sentence
render :edit
end
end
def destroy
@html_variant = HtmlVariant.find(params[:id])
if @html_variant.destroy
flash[:success] = "HTML Variant has been deleted!"
redirect_to admin_html_variants_path
else
flash[:danger] = "Something went wrong with deleting the HTML Variant."
render :edit
end
end
private
def html_variant_params
params.permit(:html, :name, :published, :approved, :target_tag, :group)
end
def authorize_admin
authorize HtmlVariant, :access?, policy_class: InternalPolicy
end
end
end

View file

@ -1,71 +0,0 @@
class HtmlVariantsController < ApplicationController
after_action :verify_authorized
def index
authorize HtmlVariant
relation = if params[:state] == "mine"
current_user.html_variants.order(created_at: :desc)
elsif params[:state] == "admin"
HtmlVariant.where(published: true, approved: false).order(created_at: :desc)
elsif params[:state].present?
HtmlVariant.where(published: true, approved: true, group: params[:state]).order(success_rate: :desc)
else
HtmlVariant.where(published: true, approved: true).order(success_rate: :desc)
end
@html_variants = relation.includes(:user).page(params[:page]).per(30)
end
def new
authorize HtmlVariant
@html_variant = HtmlVariant.new
return unless params[:fork_id]
@fork = HtmlVariant.find(params[:fork_id])
@html_variant.name = @fork.name + " FORK-#{rand(10_000)}"
@html_variant.html = @fork.html
end
def show
@story_show = true
@article_show = true
@html_variant = HtmlVariant.find(params[:id])
authorize @html_variant
render layout: false
end
def edit
@html_variant = HtmlVariant.find(params[:id])
authorize @html_variant
end
def create
authorize HtmlVariant
@html_variant = HtmlVariant.new(html_variant_params)
@html_variant.user_id = current_user.id
if @html_variant.save
flash[:success] = "HTML Variant Created"
redirect_to "/html_variants/#{@html_variant.id}/edit"
else
render :new
end
end
def update
@html_variant = HtmlVariant.find(params[:id])
authorize @html_variant
if @html_variant.update(html_variant_params)
flash[:success] = "HTML Variant Updated"
redirect_to "/html_variants/#{@html_variant.id}/edit"
else
render :edit
end
end
private
def html_variant_params
params.require(:html_variant).permit(policy(HtmlVariant).permitted_attributes)
end
end

View file

@ -24,9 +24,6 @@ class DashboardManifest
feedback_messages
badges
badge_achievements
html_variants
html_variant_trials
html_variant_successes
sponsorships
listing_categories
].freeze

View file

@ -1,79 +0,0 @@
require "administrate/base_dashboard"
class HtmlVariantDashboard < Administrate::BaseDashboard
# ATTRIBUTE_TYPES
# a hash that describes the type of each of the model's fields.
#
# Each different type represents an Administrate::Field object,
# which determines how the attribute is displayed
# on pages throughout the dashboard.
ATTRIBUTE_TYPES = {
user: Field::BelongsTo,
html_variant_trials: Field::HasMany,
html_variant_successes: Field::HasMany,
id: Field::Number,
group: Field::String,
name: Field::String,
html: Field::Text,
target_tag: Field::String,
success_rate: Field::Number.with_options(decimals: 2),
published: Field::Boolean,
approved: Field::Boolean,
created_at: Field::DateTime,
updated_at: Field::DateTime
}.freeze
# COLLECTION_ATTRIBUTES
# an array of attributes that will be displayed on the model's index page.
#
# By default, it's limited to four items to reduce clutter on index pages.
# Feel free to add, remove, or rearrange items.
COLLECTION_ATTRIBUTES = %i[
user
html_variant_trials
html_variant_successes
success_rate
id
].freeze
# SHOW_PAGE_ATTRIBUTES
# an array of attributes that will be displayed on the model's show page.
SHOW_PAGE_ATTRIBUTES = %i[
user
html_variant_trials
html_variant_successes
id
group
name
html
target_tag
success_rate
published
approved
created_at
updated_at
].freeze
# FORM_ATTRIBUTES
# an array of attributes that will be displayed
# on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = %i[
user
html_variant_trials
html_variant_successes
group
name
html
target_tag
success_rate
published
approved
].freeze
# Overwrite this method to customize how html variants are displayed
# across all pages of the admin dashboard.
#
# def display_resource(html_variant)
# "HtmlVariant ##{html_variant.id}"
# end
end

View file

@ -1,58 +0,0 @@
require "administrate/base_dashboard"
class HtmlVariantSuccessDashboard < Administrate::BaseDashboard
# ATTRIBUTE_TYPES
# a hash that describes the type of each of the model's fields.
#
# Each different type represents an Administrate::Field object,
# which determines how the attribute is displayed
# on pages throughout the dashboard.
ATTRIBUTE_TYPES = {
html_variant: Field::BelongsTo,
article: Field::BelongsTo
}.freeze
# COLLECTION_ATTRIBUTES
# an array of attributes that will be displayed on the model's index page.
#
# By default, it's limited to four items to reduce clutter on index pages.
# Feel free to add, remove, or rearrange items.
COLLECTION_ATTRIBUTES = %i[
html_variant
article
].freeze
# SHOW_PAGE_ATTRIBUTES
# an array of attributes that will be displayed on the model's show page.
SHOW_PAGE_ATTRIBUTES = %i[
html_variant
article
].freeze
# FORM_ATTRIBUTES
# an array of attributes that will be displayed
# on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = %i[
html_variant
article
].freeze
# COLLECTION_FILTERS
# a hash that defines filters that can be used while searching via the search
# field of the dashboard.
#
# For example to add an option to search for open resources by typing "open:"
# in the search field:
#
# COLLECTION_FILTERS = {
# open: ->(resources) { where(open: true) }
# }.freeze
COLLECTION_FILTERS = {}.freeze
# Overwrite this method to customize how html variant successes are displayed
# across all pages of the admin dashboard.
#
# def display_resource(html_variant_success)
# "HtmlVariantSuccess ##{html_variant_success.id}"
# end
end

View file

@ -1,58 +0,0 @@
require "administrate/base_dashboard"
class HtmlVariantTrialDashboard < Administrate::BaseDashboard
# ATTRIBUTE_TYPES
# a hash that describes the type of each of the model's fields.
#
# Each different type represents an Administrate::Field object,
# which determines how the attribute is displayed
# on pages throughout the dashboard.
ATTRIBUTE_TYPES = {
html_variant: Field::BelongsTo,
article: Field::BelongsTo
}.freeze
# COLLECTION_ATTRIBUTES
# an array of attributes that will be displayed on the model's index page.
#
# By default, it's limited to four items to reduce clutter on index pages.
# Feel free to add, remove, or rearrange items.
COLLECTION_ATTRIBUTES = %i[
html_variant
article
].freeze
# SHOW_PAGE_ATTRIBUTES
# an array of attributes that will be displayed on the model's show page.
SHOW_PAGE_ATTRIBUTES = %i[
html_variant
article
].freeze
# FORM_ATTRIBUTES
# an array of attributes that will be displayed
# on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = %i[
html_variant
article
].freeze
# COLLECTION_FILTERS
# a hash that defines filters that can be used while searching via the search
# field of the dashboard.
#
# For example to add an option to search for open resources by typing "open:"
# in the search field:
#
# COLLECTION_FILTERS = {
# open: ->(resources) { where(open: true) }
# }.freeze
COLLECTION_FILTERS = {}.freeze
# Overwrite this method to customize how html variant trials are displayed
# across all pages of the admin dashboard.
#
# def display_resource(html_variant_trial)
# "HtmlVariantTrial ##{html_variant_trial.id}"
# end
end

View file

@ -17,6 +17,7 @@ module Constants
"Resource Admin: FeedbackMessage",
"Resource Admin: Config",
"Resource Admin: Broadcast",
"Resource Admin: HtmlVariant",
"Resource Admin: DisplayAd"].freeze
end
end

View file

@ -1,4 +1,6 @@
class HtmlVariant < ApplicationRecord
resourcify
GROUP_NAMES = %w[article_show_below_article_cta badge_landing_page campaign].freeze
belongs_to :user, optional: true

View file

@ -0,0 +1,38 @@
<% if params[:action] == 'edit' %>
<div class="form-group">
<%= label_tag :user_id, "User ID:" %>
<%= text_field_tag :user_id, @html_variant.user_id, class: "form-control", disabled: true %>
</div>
<% end %>
<div class="form-group">
<%= label_tag :name, "Name:" %>
<%= text_field_tag :name, @html_variant.name, class: "form-control" %>
</div>
<div class="form-group">
<%= label_tag :group, "Group:" %>
<%= select_tag :group, options_for_select(HtmlVariant::GROUP_NAMES, selected: @html_variant.group) %>
</div>
<div class="form-group">
<%= label_tag :target_tag, "Target Tag:" %>
<%= text_field_tag :target_tag, @html_variant.target_tag, size: "100x10", class: "form-control" %>
</div>
<div class="form-group">
<%= label_tag :html, "HTML:" %>
<%= text_area_tag :html, @html_variant.html, size: "100x10", class: "form-control" %>
</div>
<div class="form-group">
<%= label_tag :published, "Published:" %>
<%= select_tag :published, options_for_select([false, true], selected: @html_variant.published) %>
</div>
<% if params[:action] == 'edit' %>
<div class="form-group">
<%= label_tag :approved, "Approved:" %>
<%= select_tag :approved, options_for_select([false, true], selected: @html_variant.approved) %>
</div>
<% end %>

View file

@ -0,0 +1,9 @@
<h2 class="fs-2xl s:fs-3xl mb-6">Edit HTML Variant:</h2>
<div class="crayons-card p-6">
<%= form_for([:admin, @html_variant], method: :patch) do %>
<%= render "form" %>
<%= submit_tag "Update HTML Variant", class: "crayons-btn" %>
<%= link_to "Preview", admin_html_variant_path(@html_variant), class: "crayons-btn crayons-btn--outlined float-right mx-2" %>
<%= link_to "Fork", new_admin_html_variant_path(fork_id: @html_variant.id), class: "crayons-btn crayons-btn--outlined float-right" %>
<% end %>
</div>

View file

@ -0,0 +1,54 @@
<nav class="flex mb-4" aria-label="Sponsorships navigation">
<div class="crayons-tabs">
<%= link_to "Leaderboard", admin_html_variants_path, class: "crayons-tabs__item #{'crayons-tabs__item--current' if params[:state].blank?}" %>
<%= link_to "Mine", admin_html_variants_path(state: "mine"), class: "crayons-tabs__item #{'crayons-tabs__item--current' if params[:state] == 'mine'}" %>
<%= link_to "Admin", admin_html_variants_path(state: "admin"), class: "crayons-tabs__item #{'crayons-tabs__item--current' if params[:state] == 'admin'}" %>
<% HtmlVariant::GROUP_NAMES.each do |group_name| %>
<%= link_to group_name.humanize.capitalize, admin_html_variants_path(state: group_name), class: "crayons-tabs__item #{'crayons-tabs__item--current' if params[:state] == group_name}" %>
<% end %>
</div>
<div class="ml-auto">
<div class="justify-content-end">
<%= link_to "New HTML Variant", new_admin_html_variant_path, class: "crayons-btn" %>
</div>
</div>
</nav>
<hr>
<%= paginate @html_variants %>
<table class="crayons-table" width="100%">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">User</th>
<th scope="col">Group</th>
<th scope="col">Published</th>
<th scope="col">Approved</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody class="crayons-card">
<% @html_variants.each do |html_variant| %>
<tr>
<td><%= link_to html_variant.name, admin_html_variant_path(html_variant) %></td>
<td><%= link_to "@#{html_variant.user.username}", "/#{html_variant.user.username}" %></td>
<td><%= html_variant.group %></td>
<td><%= html_variant.published %></td>
<td><%= html_variant.approved %></td>
<td>
<%= link_to "Fork", new_admin_html_variant_path(fork_id: html_variant.id), class: "crayons-btn crayons-btn--outlined" %>
</td>
<td>
<%= link_to "Edit", edit_admin_html_variant_path(html_variant), class: "crayons-btn" %>
</td>
<td>
<%= link_to "Destroy", admin_html_variant_path(html_variant), class: "crayons-btn crayons-btn--danger", method: :delete, data: { confirm: "Are you sure?" } %>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= paginate @html_variants %>

View file

@ -0,0 +1,7 @@
<h2 class="fs-2xl s:fs-3xl mb-6">Make a new HTML Variant:</h2>
<div class="crayons-card p-6">
<%= form_for([:admin, @html_variant], method: :post) do %>
<%= render "form" %>
<%= submit_tag "Create HTML Variant", class: "crayons-btn" %>
<% end %>
</div>

View file

@ -0,0 +1,7 @@
<div class="content-center crayons-notice crayons-notice--info my-4 ml-1">
<span class="fs-2xl">HMTL Variant Preview</span>
<br>
<%= link_to "Back to admin page", "javascript:history.back()" %>
</div>
<%= @html_variant.html.html_safe %>

View file

@ -1,42 +0,0 @@
<% flash.each do |key, value| %>
<div class="flash flash-<%= key %>"><%= value %></div>
<% end %>
<% if @html_variant.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@html_variant.errors.count, "error") %> prohibited this block from being saved:</h2>
<ul>
<% @html_variant.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form_for(@html_variant) do |f| %>
<% if @html_variant.new_record? %>
<%= f.label :name %>
<%= f.text_field :name, placeholder: "Unique, descriptive name" %>
<% end %>
<%= f.label :html %>
<%= f.text_area :html, placeholder: "HTML to be shown. Make sure all CSS is properly scoped, and all HTML tags are closed, etc. Mandatory field." %>
<div class="form-sub-details">
<%= f.label :published %>
<%= f.check_box :published %>
<%= f.label :group %>
<%= f.select(:group, options_for_select(HtmlVariant::GROUP_NAMES, @html_variant.group || "article_show_below_article_cta")) %>
</div>
<%= f.submit %>
<% end %>
<% if @html_variant.html.present? %>
<h1 style="text-align:center;">Preview:</h1>
<div class="html-variant-wrapper" style="width: 360px;margin:20px auto;">
<%= @html_variant.html.html_safe %>
</div>
<% if @html_variant.group == "article_show_below_article_cta" %>
<div class="html-variant-wrapper" style="width: 880px;margin:20px auto;">
<%= @html_variant.html.html_safe %>
</div>
<% end %>
<% end %>

View file

@ -1,144 +0,0 @@
<style>
.html-variants-page {
margin: 100px auto;
width: 1100px;
max-width: 96%;
overflow: hidden;
}
.html-variants-page nav a {
display: inline-block;
padding: 5px 12px;
border-radius: 3px;
color: blue;
}
.html-variants-page nav a.selected {
background: blue;
color: white;
font-size: 1.1em;
font-weight: bold;
}
h1 a {
border: 1px solid black;
padding: 10px 40px;
border-radius: 100px;
}
.flash {
background: green;
color: white;
padding: 10px 20px;
border-radius: 3px;
font-weight: bold;
margin-bottom: 10px;
}
input, textarea {
width: 100%;
font-size: 20px;
font-family: monospace;
padding: 10px;
margin-bottom: 20px;
}
input[type="submit"] {
background: blue;
color: white;
font-size: 30px;
padding: 15px 0px;
border: 0px;
}
textarea {
height: calc(100vh - 500px);
font-size: 0.8em;
overflow-x: scroll;
overflow-wrap: normal;
}
.form-sub-details input {
display: inline-block;
max-width: 40%;
margin-left: 1%;
}
.form-sub-details input[type="checkbox"] {
max-width: 5%;
}
#error_explanation {
margin-bottom: 20px;
}
.html-variants-page-single-variant {
border: 1px solid black;
padding: 15px 15px 5px;
border-radius: 3px;
margin: 15px;
width: 450px;
height: 620px;
float: left;
overflow: hidden;
position: relative;
}
.html-variants-page-single-big {
width: 962px;
}
.html-variants-page-single-variant h3 {
margin-top: 0px;
font-size: 1.3em;
}
.html-variants-page-single-variant iframe {
border: 0px;
width: 310px;
height: 350px;
border: 1px solid #888888;
}
.html-variants-page-single-big iframe {
width: 800px;
}
.html-variants-page-single-variant-details {
padding: 10px;
border-top: 1px solid gray;
position: absolute;
bottom: 0;
left: 0;
right: 0;
}
.pill {
padding: 3px 10px;
display: inline-block;
margin-right: 5px;
border-radius: 100px;
background: gray;
color: white;
}
.published {
background: blue;
}
.approved {
background: green;
}
.html-variants-page-single-variant-details input {
background: green;
padding: 20px;
font-size: 35px;
border-radius: 3px;
border: 0px;
margin-top: 15px;
color: white;
font-weight: bold;
}
</style>

View file

@ -1,23 +0,0 @@
<div class="html-variants-page-single-variant <%= "html-variants-page-single-big" if html_variant.group == "article_show_below_article_cta" %>">
<%= html_variant.group %> | @<%= html_variant.user.username %>:
<h3>
<a href="/html_variants/<%= html_variant.id %>"><%= html_variant.name %></a>
</h3>
<iframe src="/html_variants/<%= html_variant.id %>" loading="lazy"></iframe>
<div class="html-variants-page-single-variant-details">
<% if html_variant.published %>
<span class='pill published'>published</span>
<a class='pill' href="/html_variants/new?fork_id=<%= html_variant.id %>">fork</a>
<% else %>
<a class='pill' href="/html_variants/<%= html_variant.id %>/edit">view/edit</a>
<% end %>
<% if html_variant.approved %>
<span class='pill approved'><%= html_variant.success_rate %></span>
<% elsif admin %>
<%= form_for(html_variant) do |f| %>
<%= f.hidden_field :approved, value: "true" %>
<%= f.submit "APPROVE" %>
<% end %>
<% end %>
</div>
</div>

View file

@ -1,9 +0,0 @@
<%= render "page_styles" %>
<div class="html-variants-page">
<div>
<a href="/html_variants">👈 View All</a>
<h1><%= @html_variant.name %></h1>
<%= render "form" %>
</div>
</div>

View file

@ -1,26 +0,0 @@
<%= render "page_styles" %>
<div class="html-variants-page">
<h1>
HTML Variants <a href="/html_variants/new">New</a>
</h1>
<nav>
<a href="/html_variants" class="<%= "selected" if params[:state].blank? %>">Leaderboard</a>
<a href="/html_variants?state=mine" class="<%= "selected" if params[:state] == "mine" %>">Mine</a>
<a href="/html_variants?state=article_show_below_article_cta" class="<%= "selected" if params[:state] == "article_show_below_article_cta" %>">Show Page Below Article</a>
<a href="/html_variants?state=badge_landing_page" class="<%= "selected" if params[:state] == "badge_landing_page" %>">Badge Landing Page</a>
<a href="/html_variants?state=campaign" class="<%= "selected" if params[:state] == "campaign" %>">Campaign</a>
</nav>
<% if params[:state] == "mine" %>
<h2>My Entries</h2>
<% elsif params[:state] == "admin" %>
<h2>All Published Entries</h2>
<% else %>
<h2>Leaderboard</h2>
<% end %>
<% @html_variants.each do |html_variant| %>
<%= render "single_html_variant", html_variant: html_variant, admin: params[:state] == "admin" %>
<% end %>
</div>
<%= paginate @html_variants %>

View file

@ -1,7 +0,0 @@
<%= render "page_styles" %>
<div class="html-variants-page">
<div>
<a href="/html_variants">👈 View All</a>
<%= render "form" %>
</div>
</div>

View file

@ -1,24 +0,0 @@
<style>
body {
all: unset;
}
</style>
<%= render "layouts/styles" %>
<style>
body {
padding-top: 20px !important;
}
</style>
<% if @html_variant.group == "article_show_below_article_cta" %>
<div class="home">
<div class="container">
<%= @html_variant.html.html_safe %>
</div>
</div>
<% else %>
<div style="width: 310px">
<div class="html-variant-wrapper">
<%= @html_variant.html.html_safe %>
</div>
</div>
<% end %>

View file

@ -126,6 +126,7 @@ Rails.application.routes.draw do
resource :config
resources :badges, only: %i[index edit update new create]
resources :display_ads, only: %i[index edit update new create destroy]
resources :html_variants, only: %i[index edit update new create show destroy]
# These redirects serve as a safegaurd to prevent 404s for any Admins
# who have the old badge_achievement URLs bookmarked.
get "/badges/badge_achievements", to: redirect("/admin/badge_achievements")
@ -249,7 +250,6 @@ Rails.application.routes.draw do
resources :videos, only: %i[index create new]
resources :video_states, only: [:create]
resources :twilio_tokens, only: [:show]
resources :html_variants, only: %i[index new create show edit update]
resources :html_variant_trials, only: [:create]
resources :html_variant_successes, only: [:create]
resources :tag_adjustments, only: %i[create destroy]

View file

@ -0,0 +1,139 @@
require "rails_helper"
require "requests/shared_examples/internal_policy_dependant_request"
RSpec.describe "/admin/html_variants", type: :request do
let(:get_resource) { get "/admin/html_variants" }
let(:params) do
{ name: "Banner", html: "<h1>Hello HTML Variants!</h1>", group: "campaign",
approved: true, published: true }
end
let(:post_resource) { post "/admin/html_variants", params: params }
it_behaves_like "an InternalPolicy dependant request", HtmlVariant do
let(:request) { get_resource }
end
context "when the user is not an admin" do
let(:user) { create(:user) }
before { sign_in user }
describe "GET /admin/html_variants" do
it "blocks the request" do
expect { get_resource }.to raise_error(Pundit::NotAuthorizedError)
end
end
describe "POST /admin/html_variants" do
it "blocks the request" do
expect { post_resource }.to raise_error(Pundit::NotAuthorizedError)
end
end
end
context "when the user is a super admin" do
let(:super_admin) { create(:user, :super_admin) }
before { sign_in super_admin }
describe "GET /admin/html_variants" do
it "allows the request" do
get_resource
expect(response).to have_http_status(:ok)
end
end
describe "POST /admin/html_variants" do
it "creates a new html_variant" do
expect do
post_resource
end.to change { HtmlVariant.all.count }.by(1)
end
end
describe "PUT /admin/html_variants" do
let!(:html_variant) { create(:html_variant, approved: false) }
it "updates HtmlVariant's approved value" do
Timecop.freeze(Time.current) do
expect do
put "/admin/html_variants/#{html_variant.id}", params: params
end.to change { html_variant.reload.approved }.from(false).to(true)
end
end
end
describe "DELETE /admin/html_variants/:id" do
let!(:html_variant) { create(:html_variant) }
it "deletes the Display Ad" do
expect do
delete "/admin/html_variants/#{html_variant.id}"
end.to change { HtmlVariant.all.count }.by(-1)
expect(response.body).to redirect_to "/admin/html_variants"
end
end
end
context "when the user is a single resource admin" do
let(:single_resource_admin) { create(:user, :single_resource_admin, resource: HtmlVariant) }
before { sign_in single_resource_admin }
describe "GET /admin/html_variants" do
it "allows the request" do
get_resource
expect(response).to have_http_status(:ok)
end
end
describe "POST /admin/html_variants" do
it "creates a new html_variant" do
expect do
post_resource
end.to change { HtmlVariant.all.count }.by(1)
end
end
describe "PUT /admin/html_variants" do
let!(:html_variant) { create(:html_variant, approved: false) }
it "updates HtmlVariant's approved value" do
Timecop.freeze(Time.current) do
expect do
put "/admin/html_variants/#{html_variant.id}", params: params
end.to change { html_variant.reload.approved }.from(false).to(true)
end
end
end
describe "DELETE /admin/html_variants/:id" do
let!(:html_variant) { create(:html_variant) }
it "deletes the Display Ad" do
expect do
delete "/admin/html_variants/#{html_variant.id}"
end.to change { HtmlVariant.all.count }.by(-1)
expect(response.body).to redirect_to "/admin/html_variants"
end
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 { sign_in single_resource_admin }
describe "GET /admin/html_variants" do
it "blocks the request" do
expect { get_resource }.to raise_error(Pundit::NotAuthorizedError)
end
end
describe "POST /admin/html_variants" do
it "blocks the request" do
expect { post_resource }.to raise_error(Pundit::NotAuthorizedError)
end
end
end
end

View file

@ -1,109 +0,0 @@
require "rails_helper"
RSpec.describe "HtmlVariants", type: :request do
let(:user) { create(:user) }
let(:article) { create(:article, user_id: user.id, approved: true) }
before do
sign_in user
end
describe "GET /html_variants" do
it "rejects non-permissioned user" do
expect { get "/html_variants" }.to raise_error(Pundit::NotAuthorizedError)
end
it "accepts permissioned" do
user.add_role(:super_admin)
get "/html_variants"
expect(response.body).to include("HTML Variants")
end
end
describe "GET /html_variants/new" do
it "rejects non-permissioned user" do
expect { get "/html_variants/new" }.to raise_error(Pundit::NotAuthorizedError)
end
it "accepts permissioned" do
user.add_role(:super_admin)
get "/html_variants/new"
expect(response.body).to include("<form")
end
end
describe "GET /html_variants/:id/edit" do
it "rejects non-permissioned user" do
html_variant = create(:html_variant)
expect { get "/html_variants/#{html_variant.id}/edit" }.to raise_error(Pundit::NotAuthorizedError)
end
it "accepts permissioned" do
user.add_role(:super_admin)
html_variant = create(:html_variant)
get "/html_variants/#{html_variant.id}/edit"
expect(response.body).to include("<form")
end
end
describe "Post /html_variants" do
it "rejects non-permissioned user" do
expect { post "/html_variants" }.to raise_error(Pundit::NotAuthorizedError)
end
it "creates" do
user.add_role(:super_admin)
post "/html_variants", params: {
html_variant: {
name: "New post",
html: "Yo ho ho", tag_list: "yoyo",
published: true,
group: "article_show_below_article_cta"
}
}
expect(HtmlVariant.all.size).to eq(1)
end
it "does not create with invalid params" do
user.add_role(:super_admin)
post "/html_variants", params: {
html_variant: {
# name: NOTHING HERE
html: "Yo ho ho", tag_list: "yoyo",
published: true
}
}
expect(HtmlVariant.all.size).to eq(0)
end
end
describe "Put /html_variants" do
it "rejects non-permissioned user" do
expect { post "/html_variants" }.to raise_error(Pundit::NotAuthorizedError)
end
it "updates when appropriate" do
user.add_role(:super_admin)
html_variant = create(:html_variant)
new_html = "Yo ho ho"
put "/html_variants/#{html_variant.id}", params: {
html_variant: {
html: new_html
}
}
expect(html_variant.reload.html).to eq(new_html)
end
it "does not create with invalid params" do
user.add_role(:super_admin)
html_variant = create(:html_variant, approved: true, published: true)
new_html = "Yo ho ho"
put "/html_variants/#{html_variant.id}", params: {
html_variant: {
html: new_html
}
}
expect(html_variant.reload.html).not_to eq(new_html)
end
end
end