From 26f52778ad3b17e8d0f3a24bc0d6487267084d18 Mon Sep 17 00:00:00 2001 From: Fabio Vilela Date: Tue, 4 Jun 2024 14:10:31 +1000 Subject: [PATCH] Fix and Improve Live Specs --- spec/core/spec_helper.rb | 4 ++ spec/fixtures/zendesk.rb | 1 + spec/live/automation_spec.rb | 2 +- spec/live/brand_spec.rb | 4 +- spec/live/custom_status_spec.rb | 2 +- spec/live/dynamic_content/item_spec.rb | 6 +-- spec/live/dynamic_content/variant_spec.rb | 2 +- spec/live/identity_spec.rb | 2 +- spec/live/organization_spec.rb | 4 +- spec/live/topic_spec.rb | 13 ------ spec/live/user_field_spec.rb | 6 +-- spec/macros/resource_macros.rb | 53 +++++++++++------------ 12 files changed, 44 insertions(+), 55 deletions(-) diff --git a/spec/core/spec_helper.rb b/spec/core/spec_helper.rb index ffa278b5..8bf01c1f 100644 --- a/spec/core/spec_helper.rb +++ b/spec/core/spec_helper.rb @@ -156,6 +156,10 @@ def stub_json_request(verb, path_matcher, body = json, options = {}) c.default_cassette_options = { :record => :new_episodes, :decode_compressed_response => true, :serialize_with => :json, :preserve_exact_body_bytes => true } c.hook_into :webmock c.configure_rspec_metadata! + + # In CI this doesn't matter, since we start by deleting all the cassettes. + # In development, this helps debugging. + c.allow_http_connections_when_no_cassette = true end include WebMock::API diff --git a/spec/fixtures/zendesk.rb b/spec/fixtures/zendesk.rb index 9941d52f..58a6316e 100644 --- a/spec/fixtures/zendesk.rb +++ b/spec/fixtures/zendesk.rb @@ -30,6 +30,7 @@ def find_or_create_user(role) def topic VCR.use_cassette('valid_topic') do + @topic ||= client.topics.fetch.find { |t| t.name == "Test Topic" } @topic ||= client.topics.create( :name => "Test Topic", :description => "This is the body of a topic." diff --git a/spec/live/automation_spec.rb b/spec/live/automation_spec.rb index ffe5e6a7..45fdef6e 100644 --- a/spec/live/automation_spec.rb +++ b/spec/live/automation_spec.rb @@ -3,7 +3,7 @@ describe ZendeskAPI::Automation, :delete_after do def valid_attributes { - :title => "my test automation", + :title => "my test automation_ruby_sdk_test", :conditions => { :all => [{ :field => "status", :operator => "is", :value => "open" }] }, diff --git a/spec/live/brand_spec.rb b/spec/live/brand_spec.rb index 34f4258a..32e4b8bf 100644 --- a/spec/live/brand_spec.rb +++ b/spec/live/brand_spec.rb @@ -2,11 +2,11 @@ describe ZendeskAPI::Brand, :delete_after do def valid_attributes - { :name => "awesomesauce", :subdomain => "zendeskapi#{SecureRandom.hex(3)}" } + { :name => "awesomesauce_ruby_sdk_test", :subdomain => "zendeskapi#{SecureRandom.hex(3)}" } end it_should_be_creatable - it_should_be_updatable :name + it_should_be_updatable :name, "awesomesauce_ruby_sdk_updated_name" it_should_be_readable :brands # Deleted brands are still findable by id, but in the index action diff --git a/spec/live/custom_status_spec.rb b/spec/live/custom_status_spec.rb index 38d7bb8b..d52a2d7b 100644 --- a/spec/live/custom_status_spec.rb +++ b/spec/live/custom_status_spec.rb @@ -14,7 +14,7 @@ def valid_attributes end it_should_be_creatable - it_should_be_updatable :agent_label, 'New Agent Label' + it_should_be_updatable :agent_label, "ruby_sdk_test_agent_label_updated" it_should_be_updatable :end_user_label, 'New End User Label' it_should_be_updatable :description, 'New Description' it_should_be_updatable :end_user_description, 'New End User Description' diff --git a/spec/live/dynamic_content/item_spec.rb b/spec/live/dynamic_content/item_spec.rb index c62b63e0..91f938ae 100644 --- a/spec/live/dynamic_content/item_spec.rb +++ b/spec/live/dynamic_content/item_spec.rb @@ -3,14 +3,14 @@ describe ZendeskAPI::DynamicContent::Item, :delete_after do def valid_attributes { - :name => "Snowboard Problem", + :name => "Dynamic Content Item name Ruby SDK test", :default_locale_id => 1, - :content => "Snowboard Problem variant" + :content => "Ruby SDK test content" } end it_should_be_readable :dynamic_content, :items, :create => true it_should_be_creatable - it_should_be_updatable :name + it_should_be_updatable :name, 'Updated Dynamic Content Item name Ruby SDK test' it_should_be_deletable end diff --git a/spec/live/dynamic_content/variant_spec.rb b/spec/live/dynamic_content/variant_spec.rb index fad28093..f7fbe50e 100644 --- a/spec/live/dynamic_content/variant_spec.rb +++ b/spec/live/dynamic_content/variant_spec.rb @@ -6,7 +6,7 @@ def valid_attributes :locale_id => 2, :active => true, :default => false, - :content => 'Mon dieu!' + :content => 'Ruby SDK Test Variant Content' } end diff --git a/spec/live/identity_spec.rb b/spec/live/identity_spec.rb index fa18fedf..8a6fed74 100644 --- a/spec/live/identity_spec.rb +++ b/spec/live/identity_spec.rb @@ -2,7 +2,7 @@ describe ZendeskAPI::User::Identity, :delete_after do def valid_attributes - { :type => "email", :value => "abcdef@example.com" } + { :type => "email", :value => "ruby_sdk_test@example.com" } end under current_user do diff --git a/spec/live/organization_spec.rb b/spec/live/organization_spec.rb index ef2416c5..ed92194c 100644 --- a/spec/live/organization_spec.rb +++ b/spec/live/organization_spec.rb @@ -2,11 +2,11 @@ describe ZendeskAPI::Organization, :delete_after do def valid_attributes - { :name => "Awesome-O" } + { :name => 'organization_name_ruby_sdk_test' } end it_should_be_creatable - it_should_be_updatable :name + it_should_be_updatable :name, 'organization_name_ruby_sdk_test_updated' it_should_be_deletable it_should_be_readable :organizations, :create => true diff --git a/spec/live/topic_spec.rb b/spec/live/topic_spec.rb index 68f6a5f8..b8aeacea 100644 --- a/spec/live/topic_spec.rb +++ b/spec/live/topic_spec.rb @@ -1,19 +1,6 @@ require 'core/spec_helper' RSpec.describe ZendeskAPI::Topic do - # Cleanup topics (:delete_after doesn't work here) - before :all do - VCR.configure do |c| - @previous_allow_http_connections = c.allow_http_connections_when_no_cassette? - c.allow_http_connections_when_no_cassette = true - end - client.topics.fetch!.reject { |t| t == @topic }.map(&:destroy!) - ensure - VCR.configure do |c| - c.allow_http_connections_when_no_cassette = @previous_allow_http_connections - end - end - def valid_attributes { :name => "My Topic", diff --git a/spec/live/user_field_spec.rb b/spec/live/user_field_spec.rb index 54854325..e7dfd3d2 100644 --- a/spec/live/user_field_spec.rb +++ b/spec/live/user_field_spec.rb @@ -2,11 +2,11 @@ describe ZendeskAPI::UserField, :delete_after do def valid_attributes - { :type => "text", :title => "Age", :key => "age" } + { :type => "text", :title => "title_ruby_sdk_test", :key => 'ruby_sdk_test_key' } end - it_should_be_creatable - it_should_be_updatable :title, "key" it_should_be_deletable + it_should_be_creatable + it_should_be_updatable :title, "updated_title_ruby_sdk_test" it_should_be_readable :user_fields, :create => true end diff --git a/spec/macros/resource_macros.rb b/spec/macros/resource_macros.rb index 990340da..7ed1f7d3 100644 --- a/spec/macros/resource_macros.rb +++ b/spec/macros/resource_macros.rb @@ -21,26 +21,26 @@ def it_should_be_creatable(options = {}) before(:all) do VCR.use_cassette("#{described_class.to_s}_create") do - @object = described_class.create(client, valid_attributes.merge(default_options)) + @creatable_object = described_class.create!(client, valid_attributes.merge(default_options)) end end it "should have an id" do - expect(@object).to_not be_nil - expect(@object.send(:id)).to_not be_nil + expect(@creatable_object).to_not be_nil + expect(@creatable_object.send(:id)).to_not be_nil end it "should be findable", :unless => metadata[:not_findable] do options = default_options - options.merge!(:id => @object.id) unless described_class.ancestors.include?(ZendeskAPI::SingularResource) - expect(described_class.find(client, options)).to eq(@object) + options.merge!(:id => @creatable_object.id) unless described_class.ancestors.include?(ZendeskAPI::SingularResource) + expect(described_class.find(client, options)).to eq(@creatable_object) end after(:all) do - return unless @object.id + return unless @creatable_object.id VCR.use_cassette("#{described_class.to_s}_create_delete") do - @object.destroy + @creatable_object.destroy end end if metadata[:delete_after] end @@ -50,40 +50,38 @@ def it_should_be_updatable(attribute, value = "TESTDATA", extra = {}) context "update", :vcr do before(:all) do VCR.use_cassette("#{described_class.to_s}_update_create") do - @object = described_class.create(client, valid_attributes.merge(default_options)) + @updatable_object = described_class.create!(client, valid_attributes.merge(default_options)) end end before(:each) do - @object.send("#{attribute}=", value) - extra.each { |k, v| @object.send("#{k}=", v) } + @updatable_object.public_send("#{attribute}=", value) + extra.each { |k, v| @updatable_object.public_send("#{k}=", v) } end it "should be savable" do - expect(@object.save).to be(true) + expect(@updatable_object.save).to be(true), "Expected object to save, but it failed with errors: #{@updatable_object.errors&.full_messages&.join(', ')}" end context "after save" do before(:each) do - @object.save + @updatable_object.save end it "should keep attributes" do - expect(@object.send(attribute)).to eq(value) + expect(@updatable_object.send(attribute)).to eq(value) end it "should be findable", :unless => metadata[:not_findable] do options = default_options - options.merge!(:id => @object.id) unless described_class.ancestors.include?(ZendeskAPI::SingularResource) - expect(described_class.find(client, options)).to eq(@object) + options.merge!(:id => @updatable_object.id) unless described_class.ancestors.include?(ZendeskAPI::SingularResource) + expect(described_class.find(client, options)).to eq(@updatable_object) end end after(:all) do - return unless @object.id - VCR.use_cassette("#{described_class.to_s}_update_delete") do - @object.destroy + @updatable_object.destroy end end if metadata[:delete_after] end @@ -93,21 +91,20 @@ def it_should_be_deletable(options = {}) context "deletion", :vcr do before(:all) do if options[:object] - @object = options.delete(:object) + @deletable_object = options.delete(:object) else VCR.use_cassette("#{described_class.to_s}_delete_create") do - @object = described_class.create(client, valid_attributes.merge(default_options)) + @deletable_object = described_class.create!(client, valid_attributes.merge(default_options)) end end end it "should be destroyable" do |example| - expect(@object.destroy).to be(true) - expect(@object.destroyed?).to be(true) - + expect(@deletable_object.destroy).to be(true) + expect(@deletable_object.destroyed?).to be(true) if (!options.key?(:find) || options[:find]) && !example.metadata[:not_findable] opts = default_options - opts.merge!(:id => @object.id) unless described_class.ancestors.include?(ZendeskAPI::SingularResource) + opts.merge!(:id => @deletable_object.id) unless described_class.ancestors.include?(ZendeskAPI::SingularResource) obj = described_class.find(client, opts) if options[:find] @@ -129,13 +126,13 @@ def it_should_be_readable(*args) context context_name, :vcr do before(:all) do VCR.use_cassette("#{described_class.to_s}_#{context_name}_create") do - @object = described_class.create!(client, valid_attributes.merge(default_options)) + @readable_object = described_class.create!(client, valid_attributes.merge(default_options)) end end if create after(:all) do VCR.use_cassette("#{described_class.to_s}_#{context_name}_delete") do - @object.destroy + @readable_object.destroy end end if create @@ -148,7 +145,7 @@ def it_should_be_readable(*args) object = nil result.all do |o| - object = o if @object == o + object = o if @readable_object == o end expect(object).to_not be_nil @@ -158,7 +155,7 @@ def it_should_be_readable(*args) end else expect(result).to_not be_nil - expect(result).to eq(@object) if create + expect(result).to eq(@readable_object) if create object = result end