From 4e023c0e3dbde2cc17741938c8ecc206a982ff3e Mon Sep 17 00:00:00 2001 From: Zee Spencer <50284+zspencer@users.noreply.github.com> Date: Sat, 11 Mar 2023 06:36:59 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20`Furniture`:=20Appease=20the=20F?= =?UTF-8?q?uzz?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - https://github.com/zinc-collective/convene/pull/1209#discussion_r1133016759 - https://github.com/zinc-collective/convene/issues/709 I don't really like the direction this pushes specs, but I can live with it. In particular, a single assert per request/response seems... excruciatingly slow. But there are other mechanisms we can explore to mitigate that as it gets out of hand. --- .rubocop_todo.yml | 18 ----- spec/furniture/embedded_form_spec.rb | 24 +++++-- .../furniture/journal/entries_request_spec.rb | 65 ++++++++++++------- spec/furniture/journal/entry_policy_spec.rb | 2 +- spec/furniture/markdown_text_block_spec.rb | 13 ++-- .../checkouts_controller_request_spec.rb | 8 +-- .../products_controller_request_spec.rb | 35 ++++++---- ...stripe_accounts_controller_request_spec.rb | 5 +- .../stripe_events_controller_request_spec.rb | 4 +- spec/models/furniture_spec.rb | 12 +++- .../furnitures_controller_request_spec.rb | 11 ++-- 11 files changed, 115 insertions(+), 82 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index f0b5569c6..5e0d22677 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -23,7 +23,6 @@ RSpec/AnyInstance: # Prefixes: when, with, without RSpec/ContextWording: Exclude: - - 'spec/furniture/journal/entry_policy_spec.rb' - 'spec/models/authenticated_session_spec.rb' - 'spec/support/shared_examples/a_space_member_only_route.rb' @@ -37,15 +36,9 @@ RSpec/DescribeClass: # Configuration parameters: Max, CountAsOne. RSpec/ExampleLength: Exclude: - - 'spec/furniture/embedded_form_spec.rb' - - 'spec/furniture/journal/entries_request_spec.rb' - - 'spec/furniture/markdown_text_block_spec.rb' - - 'spec/furniture/marketplace/products_controller_request_spec.rb' - - 'spec/models/furniture_spec.rb' - 'spec/models/membership_spec.rb' - 'spec/models/room_spec.rb' - 'spec/policies/room_policy_spec.rb' - - 'spec/requests/furnitures_controller_request_spec.rb' - 'spec/requests/rsvps_controller_request_spec.rb' - 'spec/requests/spaces/invitations_request_spec.rb' - 'spec/requests/spaces_controller_request_spec.rb' @@ -66,8 +59,6 @@ RSpec/LeakyConstantDeclaration: # Offense count: 4 RSpec/LetSetup: Exclude: - - 'spec/furniture/marketplace/checkouts_controller_request_spec.rb' - - 'spec/furniture/marketplace/stripe_accounts_controller_request_spec.rb' - 'spec/policies/membership_policy_spec.rb' # Offense count: 6 @@ -80,11 +71,6 @@ RSpec/NamedSubject: - 'spec/models/membership_spec.rb' - 'spec/models/space_spec.rb' -# Offense count: 2 -RSpec/RepeatedExampleGroupBody: - Exclude: - - 'spec/furniture/marketplace/checkout_policy_spec.rb' - # Offense count: 2 RSpec/RepeatedExampleGroupDescription: Exclude: @@ -94,9 +80,6 @@ RSpec/RepeatedExampleGroupDescription: # Configuration parameters: IgnoreNameless, IgnoreSymbolicNames. RSpec/VerifiedDoubles: Exclude: - - 'spec/furniture/marketplace/checkouts_controller_request_spec.rb' - - 'spec/furniture/marketplace/stripe_accounts_controller_request_spec.rb' - - 'spec/furniture/marketplace/stripe_events_controller_request_spec.rb' - 'spec/requests/spaces/invitations_request_spec.rb' # Offense count: 6 @@ -142,7 +125,6 @@ Rails/HasManyOrHasOneDependent: # Offense count: 2 Rails/I18nLocaleTexts: Exclude: - - 'app/furniture/marketplace/stripe_accounts_controller.rb' - 'app/mailers/authenticated_session_mailer.rb' # Offense count: 1 diff --git a/spec/furniture/embedded_form_spec.rb b/spec/furniture/embedded_form_spec.rb index 2e3380773..a05e558e8 100644 --- a/spec/furniture/embedded_form_spec.rb +++ b/spec/furniture/embedded_form_spec.rb @@ -3,16 +3,26 @@ require "rails_helper" RSpec.describe EmbeddedForm do + subject(:embedded_form) { build(:embedded_form, form_url: form_url) } + describe "#embeddable_form_url" do - it "converts Airtable Share URLs into the Embed url" do - embedded_form = build(:embedded_form, form_url: "https://airtable.com/shrCnrBzflvzDIlvg") - expect(embedded_form.embeddable_form_url).to eql("https://airtable.com/embed/shrCnrBzflvzDIlvg") + subject(:embeddable_form_url) { embedded_form.embeddable_form_url } + context "with a #form_url that is the basic share url" do + let(:form_url) { "https://airtable.com/shrCnrBzflvzDIlvg" } + + it { is_expected.to eql "https://airtable.com/embed/shrCnrBzflvzDIlvg" } + end + + context "with a #form_url that is the embed instructions url" do + let(:form_url) { "https://airtable.com/shrCnrBzflvzDIlvg/embed" } + + it { is_expected.to eql("https://airtable.com/embed/shrCnrBzflvzDIlvg") } + end - embedded_form = build(:embedded_form, form_url: "https://airtable.com/shrCnrBzflvzDIlvg/embed") - expect(embedded_form.embeddable_form_url).to eql("https://airtable.com/embed/shrCnrBzflvzDIlvg") + context "with a #form_url that is the actual embed url" do + let(:form_url) { "https://airtable.com/embed/shrCnrBzflvzDIlvg" } - embedded_form = build(:embedded_form, form_url: "https://airtable.com/embed/shrCnrBzflvzDIlvg") - expect(embedded_form.embeddable_form_url).to eql("https://airtable.com/embed/shrCnrBzflvzDIlvg") + it { is_expected.to eql("https://airtable.com/embed/shrCnrBzflvzDIlvg") } end end end diff --git a/spec/furniture/journal/entries_request_spec.rb b/spec/furniture/journal/entries_request_spec.rb index 0a70df296..ef60a4c06 100644 --- a/spec/furniture/journal/entries_request_spec.rb +++ b/spec/furniture/journal/entries_request_spec.rb @@ -7,44 +7,59 @@ let(:room) { journal.room } let(:member) { create(:person, spaces: [space]) } - describe "#index" do - it "shows all the journal entries" do - sign_in(space, member) - unpublished_entry = create(:journal_entry, journal: journal) - published_entry = create(:journal_entry, journal: journal, published_at: 1.week.ago) + before { sign_in(space, member) } + describe "#index" do + subject(:perform_request) do get polymorphic_path(journal.location(child: :entries)) + response + end + + let!(:unpublished_entry) { create(:journal_entry, journal: journal) } + let!(:published_entry) { create(:journal_entry, journal: journal, published_at: 1.week.ago) } + + it { is_expected.to be_ok } + + describe "response body" do + subject(:response_body) { response.body } - expect(response).to be_ok - expect(response.body).to include(CGI.escapeHTML(published_entry.headline)) - expect(response.body).to include(CGI.escapeHTML(unpublished_entry.headline)) + before { perform_request } + it { is_expected.to include(CGI.escapeHTML(published_entry.headline)) } + it { is_expected.to include(CGI.escapeHTML(unpublished_entry.headline)) } end end describe "#create" do - it "Creates an Entry in the Journal" do - sign_in(space, member) + subject(:perform_request) do + post polymorphic_path([space, room, journal, :entries]), params: {entry: journal_entry_attributes} + end - attributes = attributes_for(:journal_entry) + let(:journal_entry_attributes) { attributes_for(:journal_entry) } + let(:created_journal_entry) { journal.entries.first } - expect do - post polymorphic_path([space, room, journal, :entries]), params: {entry: attributes} - end.to change(journal.entries, :count).by(1) + specify { expect { perform_request }.to change(journal.entries, :count).by(1) } - created_entry = journal.entries.first - expect(created_entry.headline).to eql(attributes[:headline]) - expect(created_entry.body).to eql(attributes[:body]) - end + specify { + perform_request + expect(created_journal_entry.headline).to eql(journal_entry_attributes[:headline]) + } + + specify { + perform_request + expect(created_journal_entry.body).to eql(journal_entry_attributes[:body]) + } end describe "#update" do - it "allows members to update Journal Entries" do - sign_in(space, member) - entry = create(:journal_entry, journal: journal) - published_at = 1.day.ago.beginning_of_day - expect do - put polymorphic_path(entry.location), params: {entry: {published_at: published_at}} - end.to change { entry.reload.published_at }.from(nil).to(published_at.to_time) + subject(:perform_request) do + put polymorphic_path(entry.location), params: {entry: {published_at: published_at}} end + + let(:published_at) { 1.day.ago.beginning_of_day } + let(:entry) { create(:journal_entry, journal: journal) } + + before { sign_in(space, member) } + + specify { expect { perform_request }.to change { entry.reload.published_at }.from(nil).to(published_at.to_time) } end end diff --git a/spec/furniture/journal/entry_policy_spec.rb b/spec/furniture/journal/entry_policy_spec.rb index b9396f78b..b9e5b2ed0 100644 --- a/spec/furniture/journal/entry_policy_spec.rb +++ b/spec/furniture/journal/entry_policy_spec.rb @@ -51,7 +51,7 @@ let!(:unpublished_entry) { create(:journal_entry, journal: published_entry.journal) } - context "When a Guest" do + context "when a Guest" do let(:user) { Guest.new } it { is_expected.not_to include(unpublished_entry) } diff --git a/spec/furniture/markdown_text_block_spec.rb b/spec/furniture/markdown_text_block_spec.rb index 3f3869ad4..a1b2f3ca7 100644 --- a/spec/furniture/markdown_text_block_spec.rb +++ b/spec/furniture/markdown_text_block_spec.rb @@ -6,17 +6,20 @@ subject(:content_block) { create(:furniture).becomes(described_class) } describe "#to_html" do - it "renders markdown correctly" do + subject(:to_html) { content_block.to_html } + + before do content_block.content = <<~MARKDOWN # Title ## Subtitle * item the first * item the _second_ MARKDOWN - expect(content_block.to_html).to include '

Title

' - expect(content_block.to_html).to include '

Subtitle

' - expect(content_block.to_html).to include "
  • item the first
  • " - expect(content_block.to_html).to include "
  • item the second
  • " end + + it { is_expected.to include '

    Title

    ' } + it { is_expected.to include '

    Subtitle

    ' } + it { is_expected.to include "
  • item the first
  • " } + it { is_expected.to include "
  • item the second
  • " } end end diff --git a/spec/furniture/marketplace/checkouts_controller_request_spec.rb b/spec/furniture/marketplace/checkouts_controller_request_spec.rb index 4da43c7f3..ab31dde34 100644 --- a/spec/furniture/marketplace/checkouts_controller_request_spec.rb +++ b/spec/furniture/marketplace/checkouts_controller_request_spec.rb @@ -4,7 +4,7 @@ let(:marketplace) { create(:marketplace) } let(:space) { marketplace.space } let(:room) { marketplace.room } - let!(:cart) { create(:marketplace_cart, :with_products, marketplace: marketplace) } + let(:cart) { create(:marketplace_cart, :with_products, marketplace: marketplace) } let(:checkout) { build(:marketplace_checkout, cart: cart) } before { create(:stripe_utility, space: space) } @@ -16,7 +16,7 @@ } let(:stripe_checkout_session) do - double(Stripe::Checkout::Session, url: "http://stripe.redirect") + double(Stripe::Checkout::Session, url: "http://stripe.redirect") # rubocop:disable RSpec/VerifiedDoubles end before do @@ -25,7 +25,7 @@ end context "when a Guest checks out their Cart" do - let!(:cart) { create(:marketplace_cart, :with_products, marketplace: marketplace) } + let(:cart) { create(:marketplace_cart, :with_products, marketplace: marketplace) } it "Redirects to Stripe" do expect(completed_request).to redirect_to(stripe_checkout_session.url) @@ -33,7 +33,7 @@ end context "when the Cart is empty" do - let!(:cart) { create(:marketplace_cart, marketplace: marketplace) } + let(:cart) { create(:marketplace_cart, marketplace: marketplace) } it "shows an error notice" do completed_request diff --git a/spec/furniture/marketplace/products_controller_request_spec.rb b/spec/furniture/marketplace/products_controller_request_spec.rb index 8d38c3ebb..e36deb342 100644 --- a/spec/furniture/marketplace/products_controller_request_spec.rb +++ b/spec/furniture/marketplace/products_controller_request_spec.rb @@ -7,23 +7,30 @@ let(:member) { create(:membership, space: space).member } describe "#create" do - it "Creates a Product in the Marketplace" do - tax_rate = create(:marketplace_tax_rate, marketplace: marketplace) - attributes = attributes_for(:marketplace_product, tax_rate_ids: [tax_rate.id]) + subject(:perform_request) do + post polymorphic_path([space, room, marketplace, :products]), + params: {product: product_attributes} + end + + let(:tax_rate) { create(:marketplace_tax_rate, marketplace: marketplace) } + let(:product_attributes) { attributes_for(:marketplace_product, tax_rate_ids: [tax_rate.id]) } + before do sign_in(space, member) + end + + specify { expect { perform_request }.to change(marketplace.products, :count).by(1) } + + describe "the created product" do + subject(:created_product) { marketplace.products.last } + + before { perform_request } - expect do - post polymorphic_path([space, room, marketplace, :products]), - params: {product: attributes} - end.to change(marketplace.products, :count).by(1) - - created_product = marketplace.products.last - expect(created_product.name).to eql(attributes[:name]) - expect(created_product.description).to eql(attributes[:description]) - expect(created_product.price_cents).to eql(attributes[:price_cents]) - expect(created_product.price_currency).to eql(Money.default_currency.to_s) - expect(created_product.tax_rates).to include(tax_rate) + specify { expect(created_product.name).to eql(product_attributes[:name]) } + specify { expect(created_product.description).to eql(product_attributes[:description]) } + specify { expect(created_product.price_cents).to eql(product_attributes[:price_cents]) } + specify { expect(created_product.price_currency).to eql(Money.default_currency.to_s) } + specify { expect(created_product.tax_rates).to include(tax_rate) } end end diff --git a/spec/furniture/marketplace/stripe_accounts_controller_request_spec.rb b/spec/furniture/marketplace/stripe_accounts_controller_request_spec.rb index e4bd8d305..01fa67977 100644 --- a/spec/furniture/marketplace/stripe_accounts_controller_request_spec.rb +++ b/spec/furniture/marketplace/stripe_accounts_controller_request_spec.rb @@ -1,15 +1,17 @@ require "rails_helper" +# rubocop:disable Rspec/VerifiedDoubles RSpec.describe Marketplace::StripeAccountsController, type: :request do let(:marketplace) { create(:marketplace) } let(:space) { marketplace.space } let(:member) { create(:membership, space: space).member } - let!(:stripe) { create(:stripe_utility, space: space, configuration: {"api_token" => "asdf"}) } + let(:stripe_account_link) { double(Stripe::AccountLink, url: "http://example.com/") } let(:stripe_account) { double(Stripe::Account, id: "ac_1234", details_submitted?: false) } let(:stripe_webhook_endpoint) { double(Stripe::WebhookEndpoint, id: "whe_1234", secret: "oooooooooooo") } before do + create(:stripe_utility, space: space, configuration: {"api_token" => "asdf"}) allow(Stripe::Account).to receive(:create).and_return(stripe_account) allow(Stripe::AccountLink).to receive(:create).and_return(stripe_account_link) allow(Stripe::WebhookEndpoint).to receive(:create) @@ -47,3 +49,4 @@ end end end +# rubocop:enable Rspec/VerifiedDoubles diff --git a/spec/furniture/marketplace/stripe_events_controller_request_spec.rb b/spec/furniture/marketplace/stripe_events_controller_request_spec.rb index 197b538b8..3d2033d28 100644 --- a/spec/furniture/marketplace/stripe_events_controller_request_spec.rb +++ b/spec/furniture/marketplace/stripe_events_controller_request_spec.rb @@ -1,5 +1,6 @@ require "rails_helper" - +# `Stripe` gem doesn't support verified doubles for some reason... +# rubocop:disable Rspec/VerifiedDoubles RSpec.describe Marketplace::StripeEventsController, type: :request do let(:marketplace) { create(:marketplace, :with_stripe_utility, stripe_account: "sa_1234", stripe_webhook_endpoint_secret: "whsec_1234") } let(:space) { marketplace.space } @@ -51,3 +52,4 @@ end end end +# rubocop:enable Rspec/VerifiedDoubles diff --git a/spec/models/furniture_spec.rb b/spec/models/furniture_spec.rb index 80f3c25a9..689c38ba5 100644 --- a/spec/models/furniture_spec.rb +++ b/spec/models/furniture_spec.rb @@ -15,10 +15,18 @@ describe "#slot" do let(:room) { create(:room) } - let!(:placements) { create_list(:furniture, 3, room: room) } + let(:placements) { create_list(:furniture, 3, room: room) } + let(:placement1) { placements[0] } + let(:placement2) { placements[1] } + let(:placement3) { placements[2] } + + before { + placement1 + placement2 + placement3 + } it "inserts new placement between existing slots" do - placement1, placement2, placement3 = placements new_placement = create(:furniture, room: room, slot_position: 1) expect(placement1.slot_rank).to eq(0) expect(new_placement.slot_rank).to eq(1) diff --git a/spec/requests/furnitures_controller_request_spec.rb b/spec/requests/furnitures_controller_request_spec.rb index f4e01fda4..628b7853f 100644 --- a/spec/requests/furnitures_controller_request_spec.rb +++ b/spec/requests/furnitures_controller_request_spec.rb @@ -6,16 +6,19 @@ let(:space) { room.space } describe "#create" do + subject(:perform_request) do + post polymorphic_path(room.location(child: :furnitures)), params: {furniture: {furniture_kind: :markdown_text_block}} + end + let(:membership) { create(:membership, space: space) } let!(:person) { membership.member } before { sign_in(space, person) } - it "creates a furniture placement of the kind of furniture provided within the room" do - expect do - post polymorphic_path(room.location(child: :furnitures)), params: {furniture: {furniture_kind: :markdown_text_block}} - end.to change { room.furnitures.count }.by(1) + specify { expect { perform_request }.to change { room.furnitures.count }.by(1) } + it "creates a furniture placement of the kind of furniture provided within the room" do + perform_request placement = room.furnitures.last expect(placement.furniture).to be_a(MarkdownTextBlock) expect(placement.slot).to be(0)