From ce14a24138e584129470e175f240c1f5ac4ea486 Mon Sep 17 00:00:00 2001 From: James Lamont <958588+jylamont@users.noreply.github.com> Date: Fri, 21 Feb 2025 18:08:35 -0500 Subject: [PATCH] refactor: Refactor --- .rubocop-rspec.yml | 19 ++ .standard.yml | 3 + Gemfile | 1 + lib/vero/config.rb | 7 +- lib/vero/sender.rb | 3 +- lib/vero/senders/base.rb | 26 ++- lib/vero/senders/delayed_job.rb | 17 +- lib/vero/senders/resque.rb | 8 +- lib/vero/senders/sidekiq.rb | 12 +- lib/vero/senders/sucker_punch.rb | 8 +- lib/vero/trackable/base.rb | 26 ++- spec/lib/api/events/track_api_spec.rb | 77 ------- spec/lib/api/users/delete_api_spec.rb | 39 ---- spec/lib/api/users/edit_api_spec.rb | 43 ---- spec/lib/api/users/edit_tags_api_spec.rb | 80 -------- spec/lib/api/users/reidentify_spec.rb | 53 ----- spec/lib/api/users/resubscribe_api_spec.rb | 35 ---- spec/lib/api/users/track_api_spec.rb | 75 ------- spec/lib/api/users/unsubscribe_api_spec.rb | 40 ---- spec/lib/api_spec.rb | 107 ---------- spec/lib/app_spec.rb | 41 ++-- spec/lib/sender_spec.rb | 22 -- spec/lib/senders/sidekiq_spec.rb | 32 --- spec/lib/vero/api/events_spec.rb | 36 ++++ spec/lib/vero/api/users_spec.rb | 73 +++++++ .../vero/api/workers/events/track_api_spec.rb | 59 ++++++ .../vero/api/workers/users/delete_api_spec.rb | 26 +++ .../vero/api/workers/users/edit_api_spec.rb | 23 +++ .../api/workers/users/edit_tags_api_spec.rb | 62 ++++++ .../api/workers/users/reidentify_api_spec.rb | 35 ++++ .../api/workers/users/resubscribe_api_spec.rb | 21 ++ .../vero/api/workers/users/track_api_spec.rb | 57 ++++++ .../api/workers/users/unsubscribe_api_spec.rb | 23 +++ spec/lib/{ => vero}/config_spec.rb | 32 +-- spec/lib/{ => vero}/context_spec.rb | 25 +-- .../{dsl_spec.rb => vero/dsl/proxy_spec.rb} | 12 +- spec/lib/vero/dsl_spec.rb | 13 ++ spec/lib/vero/sender_spec.rb | 20 ++ spec/lib/vero/senders/sidekiq_spec.rb | 18 ++ spec/lib/vero/sidekiq_worker_spec.rb | 21 ++ spec/lib/{ => vero}/trackable_spec.rb | 189 +++++++++--------- spec/lib/vero/view_helpers/javascript_spec.rb | 45 +++++ spec/lib/view_helpers_spec.rb | 46 ----- spec/support/base_config_shared_examples.rb | 25 ++- 44 files changed, 790 insertions(+), 845 deletions(-) create mode 100644 .rubocop-rspec.yml delete mode 100644 spec/lib/api/events/track_api_spec.rb delete mode 100644 spec/lib/api/users/delete_api_spec.rb delete mode 100644 spec/lib/api/users/edit_api_spec.rb delete mode 100644 spec/lib/api/users/edit_tags_api_spec.rb delete mode 100644 spec/lib/api/users/reidentify_spec.rb delete mode 100644 spec/lib/api/users/resubscribe_api_spec.rb delete mode 100644 spec/lib/api/users/track_api_spec.rb delete mode 100644 spec/lib/api/users/unsubscribe_api_spec.rb delete mode 100644 spec/lib/api_spec.rb delete mode 100644 spec/lib/sender_spec.rb delete mode 100644 spec/lib/senders/sidekiq_spec.rb create mode 100644 spec/lib/vero/api/events_spec.rb create mode 100644 spec/lib/vero/api/users_spec.rb create mode 100644 spec/lib/vero/api/workers/events/track_api_spec.rb create mode 100644 spec/lib/vero/api/workers/users/delete_api_spec.rb create mode 100644 spec/lib/vero/api/workers/users/edit_api_spec.rb create mode 100644 spec/lib/vero/api/workers/users/edit_tags_api_spec.rb create mode 100644 spec/lib/vero/api/workers/users/reidentify_api_spec.rb create mode 100644 spec/lib/vero/api/workers/users/resubscribe_api_spec.rb create mode 100644 spec/lib/vero/api/workers/users/track_api_spec.rb create mode 100644 spec/lib/vero/api/workers/users/unsubscribe_api_spec.rb rename spec/lib/{ => vero}/config_spec.rb (62%) rename spec/lib/{ => vero}/context_spec.rb (59%) rename spec/lib/{dsl_spec.rb => vero/dsl/proxy_spec.rb} (65%) create mode 100644 spec/lib/vero/dsl_spec.rb create mode 100644 spec/lib/vero/sender_spec.rb create mode 100644 spec/lib/vero/senders/sidekiq_spec.rb create mode 100644 spec/lib/vero/sidekiq_worker_spec.rb rename spec/lib/{ => vero}/trackable_spec.rb (55%) create mode 100644 spec/lib/vero/view_helpers/javascript_spec.rb delete mode 100644 spec/lib/view_helpers_spec.rb diff --git a/.rubocop-rspec.yml b/.rubocop-rspec.yml new file mode 100644 index 0000000..44c04fd --- /dev/null +++ b/.rubocop-rspec.yml @@ -0,0 +1,19 @@ +require: + - rubocop-rspec + +RSpec/MultipleExpectations: + Enabled: false + +RSpec/ExampleLength: + Enabled: false + +RSpec/NestedGroups: + Enabled: false + +RSpec/MessageSpies: + Enabled: false + +RSpec/SpecFilePathFormat: + Enabled: true + Exclude: + - spec/lib/app_spec.rb diff --git a/.standard.yml b/.standard.yml index 2054e90..4099389 100644 --- a/.standard.yml +++ b/.standard.yml @@ -1,3 +1,6 @@ # For available configuration options, see: # https://github.com/standardrb/standard ruby_version: 2.7 + +extend_config: + - .rubocop-rspec.yml diff --git a/Gemfile b/Gemfile index 127f0fe..c93c5fc 100644 --- a/Gemfile +++ b/Gemfile @@ -10,6 +10,7 @@ group :development do gem "rails" gem "resque" gem "rubocop" + gem "rubocop-rspec" gem "sidekiq" gem "sucker_punch" gem "standard", ">= 1.35.1" diff --git a/lib/vero/config.rb b/lib/vero/config.rb index f3dbc12..80c34a3 100644 --- a/lib/vero/config.rb +++ b/lib/vero/config.rb @@ -58,9 +58,8 @@ def reset! def update_attributes(attributes = {}) return unless attributes.is_a?(Hash) - Vero::Config::ACCEPTED_ATTRIBUTES.each do |symbol| - method_name = :"#{symbol}=" - send(method_name, attributes[symbol]) if attributes.key?(symbol) && respond_to?(method_name) - end + Vero::Config::ACCEPTED_ATTRIBUTES + .select { |attr| attributes.key?(attr) } + .each { |attr| public_send(:"#{attr}=", attributes[attr]) } end end diff --git a/lib/vero/sender.rb b/lib/vero/sender.rb index 60b602a..c213226 100644 --- a/lib/vero/sender.rb +++ b/lib/vero/sender.rb @@ -7,7 +7,8 @@ def self.senders end def self.call(api_class, sender_strategy, domain, options) - senders[sender_strategy].new.call(api_class, domain, options) + sender = senders[sender_strategy].new + sender.call(api_class, domain, options) rescue => e Vero::App.log(new, "method: #{api_class.name}, options: #{JSON.dump(options)}, error: #{e.message}") raise e diff --git a/lib/vero/senders/base.rb b/lib/vero/senders/base.rb index 5147a9d..50a6372 100644 --- a/lib/vero/senders/base.rb +++ b/lib/vero/senders/base.rb @@ -2,9 +2,29 @@ class Vero::Senders::Base def call(api_class, domain, options) - response = api_class.perform(domain, options) + api_class = get_api_class(api_class) - Vero::App.log(self, "method: #{api_class.name}, options: #{JSON.dump(options)}, response: job performed") - response + resp = enqueue_work(api_class, domain, options) + Vero::App.log(self, "method: #{api_class.name}, options: #{JSON.dump(options)}, response: #{log_message}") + + resp + end + + def enqueue_work(api_class, domain, options) + api_class.perform(domain, options) + end + + def log_message + "job performed" + end + + def get_api_class(klass_name) + return klass_name unless klass_name.is_a?(String) + + if Object.const_defined?(klass_name) + Object.const_get(klass_name) + else + raise ArgumentError, "Invalid API class name: #{klass_name}" + end end end diff --git a/lib/vero/senders/delayed_job.rb b/lib/vero/senders/delayed_job.rb index ef488e8..ba067f7 100644 --- a/lib/vero/senders/delayed_job.rb +++ b/lib/vero/senders/delayed_job.rb @@ -2,15 +2,18 @@ require "delayed_job" -class Vero::Senders::DelayedJob - def call(api_class, domain, options) - response = ::Delayed::Job.enqueue api_class.new(domain, options) - - Vero::App.log(self, "method: #{api_class.name}, options: #{JSON.dump(options)}, response: delayed job queued") - response +class Vero::Senders::DelayedJob < Vero::Senders::Base + def enqueue_work(api_class, domain, options) + ::Delayed::Job.enqueue api_class.new(domain, options) rescue => e - raise "To send ratings asynchronously, you must configure delayed_job. Run `rails generate delayed_job:active_record` then `rake db:migrate`." if e.message == "Could not find table 'delayed_jobs'" + if e.message == "Could not find table 'delayed_jobs'" + raise "To send requests asynchronously, you must configure delayed_job. Run `rails generate delayed_job:active_record` then `rake db:migrate`." + end raise e end + + def log_message + "delayed job queued" + end end diff --git a/lib/vero/senders/resque.rb b/lib/vero/senders/resque.rb index b34bcbf..2bf96b3 100644 --- a/lib/vero/senders/resque.rb +++ b/lib/vero/senders/resque.rb @@ -1,5 +1,9 @@ -class Vero::Senders::Resque - def call(api_class, domain, options) +class Vero::Senders::Resque < Vero::Senders::Base + def enqueue_work(api_class, domain, options) ::Resque.enqueue(::Vero::ResqueWorker, api_class.to_s, domain, options) end + + def log_message + "resque job queued" + end end diff --git a/lib/vero/senders/sidekiq.rb b/lib/vero/senders/sidekiq.rb index aa50651..dcdacb9 100644 --- a/lib/vero/senders/sidekiq.rb +++ b/lib/vero/senders/sidekiq.rb @@ -1,9 +1,9 @@ -class Vero::Senders::Sidekiq - def call(api_class, domain, options) - response = ::Vero::SidekiqWorker.perform_async(api_class.to_s, domain, options) - - Vero::App.log(self, "method: #{api_class.name}, options: #{JSON.dump(options)}, response: sidekiq job queued") +class Vero::Senders::Sidekiq < Vero::Senders::Base + def enqueue_work(api_class, domain, options) + ::Vero::SidekiqWorker.perform_async(api_class.to_s, domain, options) + end - response + def log_message + "sidekiq job queued" end end diff --git a/lib/vero/senders/sucker_punch.rb b/lib/vero/senders/sucker_punch.rb index 65e5195..3bfb184 100644 --- a/lib/vero/senders/sucker_punch.rb +++ b/lib/vero/senders/sucker_punch.rb @@ -1,5 +1,9 @@ -class Vero::Senders::SuckerPunch - def call(api_class, domain, options) +class Vero::Senders::SuckerPunch < Vero::Senders::Base + def enqueue_work(api_class, domain, options) ::Vero::SuckerPunchWorker.perform_async(api_class, domain, options) end + + def log_message + "sucker punch job queued" + end end diff --git a/lib/vero/trackable/base.rb b/lib/vero/trackable/base.rb index 4fc7bf5..c332387 100644 --- a/lib/vero/trackable/base.rb +++ b/lib/vero/trackable/base.rb @@ -27,25 +27,33 @@ def reset_trackable_map! end def to_vero - klass = self.class - symbols, other = klass.trackable_map.partition { |i| i.is_a?(Symbol) } + trackable_attrs, other = self.class.trackable_map.partition { |i| i.is_a?(Symbol) } - result = symbols.each_with_object({}) do |symbol, hash| - t = respond_to?(symbol) ? send(symbol) : nil - hash[symbol] = t unless t.nil? + result = trackable_attrs.each_with_object({}) do |attr, hash| + value = public_send(attr) if respond_to?(attr) + hash[attr] = value unless value.nil? end if other.is_a?(Array) && !other.empty? other.select! { |i| i.is_a?(Hash) && i.key?(:extras) } + other.each do |h| - symbol = h[:extras] - t = respond_to?(symbol, true) ? send(symbol) : nil - result.merge!(t) if t.is_a?(Hash) + attr = h[:extras] + + # `extras` methods can be private + if respond_to?(attr, true) + value = send(attr) + result.merge!(value) if value.is_a?(Hash) + end end end - result[:email] = result.delete(:email_address) if result.key?(:email_address) + if result.key?(:email_address) + result[:email] = result.delete(:email_address) + end + result[:_user_type] = self.class.name + result end diff --git a/spec/lib/api/events/track_api_spec.rb b/spec/lib/api/events/track_api_spec.rb deleted file mode 100644 index 54945e7..0000000 --- a/spec/lib/api/events/track_api_spec.rb +++ /dev/null @@ -1,77 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -describe Vero::Api::Workers::Events::TrackAPI do - let(:payload) do - {auth_token: "abcd", identity: {email: "test@test.com"}, event_name: "test_event"} - end - - subject { Vero::Api::Workers::Events::TrackAPI.new("https://api.getvero.com", payload) } - - it_behaves_like "a Vero wrapper" do - let(:end_point) { "/api/v2/events/track.json" } - end - - context "request with properties" do - describe :validate! do - it "should raise an error if event_name is a blank String" do - subject.options = payload.except(:event_name) - expect { subject.send(:validate!) }.to raise_error(ArgumentError) - - subject.options = payload - expect { subject.send(:validate!) }.to_not raise_error - end - - it "should raise an error if data is not either nil or a Hash" do - subject.options = payload.merge(data: []) - expect { subject.send(:validate!) }.to raise_error(ArgumentError) - - subject.options = payload.merge(data: nil) - expect { subject.send(:validate!) }.to_not raise_error - - subject.options = payload.merge(data: {}) - expect { subject.send(:validate!) }.to_not raise_error - end - - it "should not raise an error when the keys are Strings" do - options = {"auth_token" => "abcd", "identity" => {"email" => "test@test.com"}, "event_name" => "test_event", - "data" => {}} - subject.options = options - expect { subject.send(:validate!) }.to_not raise_error - end - - it "should not raise an error when keys are Strings for initialization" do - payload.transform_keys!(&:to_s) - - expect do - Vero::Api::Workers::Events::TrackAPI.new("https://api.getvero.com", payload).send(:validate!) - end.to_not raise_error - end - end - - describe "request" do - it "should send a request to the Vero API" do - stub = stub_request(:post, "https://api.getvero.com/api/v2/events/track.json") - .with(body: payload.to_json) - .to_return(status: 200) - - subject.send(:request) - - expect(stub).to have_been_requested - end - end - end - - describe "integration test" do - it "should not raise any errors" do - obj = Vero::Api::Workers::Events::TrackAPI.new("https://api.getvero.com", payload) - - stub_request(:post, "https://api.getvero.com/api/v2/events/track.json") - .with(body: payload.to_json) - .to_return(status: 200) - - expect { obj.perform }.to_not raise_error - end - end -end diff --git a/spec/lib/api/users/delete_api_spec.rb b/spec/lib/api/users/delete_api_spec.rb deleted file mode 100644 index 4617cc6..0000000 --- a/spec/lib/api/users/delete_api_spec.rb +++ /dev/null @@ -1,39 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -describe Vero::Api::Workers::Users::DeleteAPI do - subject { Vero::Api::Workers::Users::DeleteAPI.new("https://api.getvero.com", {auth_token: "abcd", id: "1234"}) } - - it_behaves_like "a Vero wrapper" do - let(:end_point) { "/api/v2/users/delete.json" } - end - - it_behaves_like "a Vero wrapper" do - let(:end_point) { "/api/v2/users/delete.json" } - end - - describe :validate! do - it "should not raise an error when the keys are Strings" do - subject.options = {"auth_token" => "abcd", "id" => "1234"} - expect { subject.send(:validate!) }.to_not raise_error - end - - it "should raise an error for missing keys" do - subject.options = {"auth_token" => "abcd"} - expect { subject.send(:validate!) }.to raise_error(ArgumentError) - end - end - - describe "request" do - it "should send a request to the Vero API" do - stub = stub_request(:post, "https://api.getvero.com/api/v2/users/delete.json") - .with(body: {auth_token: "abcd", id: "1234"}) - .to_return(status: 200) - - subject.send(:request) - - expect(stub).to have_been_requested - end - end -end diff --git a/spec/lib/api/users/edit_api_spec.rb b/spec/lib/api/users/edit_api_spec.rb deleted file mode 100644 index c896409..0000000 --- a/spec/lib/api/users/edit_api_spec.rb +++ /dev/null @@ -1,43 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -describe Vero::Api::Workers::Users::EditAPI do - subject { Vero::Api::Workers::Users::EditAPI.new("https://api.getvero.com", {auth_token: "abcd", email: "test@test.com", changes: {email: "test@test.com"}}) } - - it_behaves_like "a Vero wrapper" do - let(:end_point) { "/api/v2/users/edit.json" } - end - - describe :validate! do - it "should not raise an error when the keys are Strings" do - options = {"auth_token" => "abcd", "email" => "test@test.com", "changes" => {"email" => "test@test.com"}} - subject.options = options - expect { subject.send(:validate!) }.to_not raise_error - end - end - - describe "request" do - it "should send a request to the Vero API" do - stub = stub_request(:put, "https://api.getvero.com/api/v2/users/edit.json") - .with( - body: {auth_token: "abcd", email: "test@test.com", changes: {email: "test@test.com"}}.to_json, - headers: {"Content-Type" => "application/json", "Accept" => "application/json"} - ) - .to_return(status: 200) - - subject.send(:request) - - expect(stub).to have_been_requested - end - end - - describe "integration test" do - it "should not raise any errors" do - stub_request(:put, "https://api.getvero.com/api/v2/users/edit.json") - .to_return(status: 200) - - expect { subject.perform }.to_not raise_error - end - end -end diff --git a/spec/lib/api/users/edit_tags_api_spec.rb b/spec/lib/api/users/edit_tags_api_spec.rb deleted file mode 100644 index cc4dc44..0000000 --- a/spec/lib/api/users/edit_tags_api_spec.rb +++ /dev/null @@ -1,80 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -describe Vero::Api::Workers::Users::EditTagsAPI do - subject { Vero::Api::Workers::Users::EditTagsAPI.new("https://api.getvero.com", {auth_token: "abcd", email: "test@test.com", add: ["test"]}) } - - it_behaves_like "a Vero wrapper" do - let(:end_point) { "/api/v2/users/tags/edit.json" } - end - - describe :validate! do - it "should raise an error if email is a blank String" do - options = {auth_token: "abcd", identity: {email: "test@test.com"}, email: nil, add: []} - subject.options = options - expect { subject.send(:validate!) }.to raise_error(ArgumentError) - - options = {auth_token: "abcd", identity: {email: "test@test.com"}, email: "test@test.com", add: []} - subject.options = options - expect { subject.send(:validate!) }.to_not raise_error - end - - it "should raise an error if add is not an Array or missing" do - options = {auth_token: "abcd", identity: {email: "test@test.com"}, email: "test@test.com", add: "foo"} - - subject.options = options - expect { subject.send(:validate!) }.to raise_error(ArgumentError) - end - - it "should raise an error if remove is not an Array or missing" do - options = {auth_token: "abcd", identity: {email: "test@test.com"}, email: "test@test.com", remove: "foo"} - - subject.options = options - expect { subject.send(:validate!) }.to raise_error(ArgumentError) - end - - it "should raise an error if botha add and remove are missing" do - options = {auth_token: "abcd", identity: {email: "test@test.com"}, email: "test@test.com"} - - subject.options = options - expect { subject.send(:validate!) }.to raise_error(ArgumentError) - end - - it "should not raise an error if the correct arguments are passed" do - options = {auth_token: "abcd", identity: {email: "test@test.com"}, email: "test@test.com", remove: ["Hi"]} - - subject.options = options - expect { subject.send(:validate!) }.to_not raise_error - end - - it "should not raise an error when the keys are Strings" do - options = {"auth_token" => "abcd", "identity" => {"email" => "test@test.com"}, "email" => "test@test.com", "remove" => ["Hi"]} - subject.options = options - expect { subject.send(:validate!) }.to_not raise_error - end - end - - describe "request" do - it "should send a request to the Vero API" do - stub = stub_request(:put, "https://api.getvero.com/api/v2/users/tags/edit.json") - .with( - body: {auth_token: "abcd", email: "test@test.com", add: ["test"]}.to_json, - headers: {"Content-Type" => "application/json", "Accept" => "application/json"} - ) - .to_return(status: 200) - - subject.send(:request) - - expect(stub).to have_been_requested - end - end - - describe "integration test" do - it "should not raise any errors" do - stub_request(:put, "https://api.getvero.com/api/v2/users/tags/edit.json") - .to_return(status: 200) - expect { subject.perform }.to_not raise_error - end - end -end diff --git a/spec/lib/api/users/reidentify_spec.rb b/spec/lib/api/users/reidentify_spec.rb deleted file mode 100644 index aa48178..0000000 --- a/spec/lib/api/users/reidentify_spec.rb +++ /dev/null @@ -1,53 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -describe Vero::Api::Workers::Users::ReidentifyAPI do - subject { Vero::Api::Workers::Users::ReidentifyAPI.new("https://api.getvero.com", {auth_token: "abcd", id: "test@test.com", new_id: "test2@test.com"}) } - - it_behaves_like "a Vero wrapper" do - let(:end_point) { "/api/v2/users/reidentify.json" } - end - - describe :validate! do - it "should not raise an error when the keys are Strings" do - options = {"auth_token" => "abcd", "id" => "test@test.com", "new_id" => "test2@test.com"} - subject.options = options - expect { subject.send(:validate!) }.to_not raise_error - end - - it "should raise an error if id is missing" do - subject.options = {auth_token: "abcd", new_id: "test2@test.com"} - expect { subject.send(:validate!) }.to raise_error(ArgumentError) - end - - it "should raise an error if new_id is missing" do - subject.options = {auth_token: "abcd", id: "test@test.com"} - expect { subject.send(:validate!) }.to raise_error(ArgumentError) - end - end - - describe "request" do - it "should send a request to the Vero API" do - stub = stub_request(:put, "https://api.getvero.com/api/v2/users/reidentify.json") - .with( - body: {auth_token: "abcd", id: "test@test.com", new_id: "test2@test.com"}.to_json, - headers: {"Accept" => "application/json", "Content-Type" => "application/json"} - ) - .to_return(status: 200) - - subject.send(:request) - - expect(stub).to have_been_requested - end - end - - describe "integration test" do - it "should not raise any errors" do - stub_request(:put, "https://api.getvero.com/api/v2/users/reidentify.json") - .to_return(status: 200) - - expect { subject.perform }.to_not raise_error - end - end -end diff --git a/spec/lib/api/users/resubscribe_api_spec.rb b/spec/lib/api/users/resubscribe_api_spec.rb deleted file mode 100644 index 0e72ee3..0000000 --- a/spec/lib/api/users/resubscribe_api_spec.rb +++ /dev/null @@ -1,35 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -describe Vero::Api::Workers::Users::ResubscribeAPI do - subject { Vero::Api::Workers::Users::ResubscribeAPI.new("https://api.getvero.com", {auth_token: "abcd", id: "1234"}) } - - it_behaves_like "a Vero wrapper" do - let(:end_point) { "/api/v2/users/resubscribe.json" } - end - - describe :validate! do - it "should not raise an error when the keys are Strings" do - subject.options = {"auth_token" => "abcd", "id" => "1234"} - expect { subject.send(:validate!) }.to_not raise_error - end - - it "should raise an error for missing keys" do - subject.options = {"auth_token" => "abcd"} - expect { subject.send(:validate!) }.to raise_error(ArgumentError) - end - end - - describe "request" do - it "should send a request to the Vero API" do - stub = stub_request(:post, "https://api.getvero.com/api/v2/users/resubscribe.json") - .with(body: {auth_token: "abcd", id: "1234"}) - .to_return(status: 200) - - subject.send(:request) - - expect(stub).to have_been_requested - end - end -end diff --git a/spec/lib/api/users/track_api_spec.rb b/spec/lib/api/users/track_api_spec.rb deleted file mode 100644 index 2f2a3b8..0000000 --- a/spec/lib/api/users/track_api_spec.rb +++ /dev/null @@ -1,75 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -describe Vero::Api::Workers::Users::TrackAPI do - subject { Vero::Api::Workers::Users::TrackAPI.new("https://api.getvero.com", {auth_token: "abcd", identity: {email: "test@test.com"}, email: "test@test.com"}) } - - it_behaves_like "a Vero wrapper" do - let(:end_point) { "/api/v2/users/track.json" } - end - - describe :validate! do - it "should raise an error if email and id are are blank String" do - options = {auth_token: "abcd", identity: {email: "test@test.com"}, id: nil, email: nil} - subject.options = options - expect { subject.send(:validate!) }.to raise_error(ArgumentError) - - options = {auth_token: "abcd", identity: {email: "test@test.com"}, id: nil, email: "test@test.com"} - subject.options = options - expect { subject.send(:validate!) }.to_not raise_error - - options = {auth_token: "abcd", identity: {email: "test@test.com"}, id: "", email: nil} - subject.options = options - expect { subject.send(:validate!) }.to raise_error(ArgumentError) - - options = {auth_token: "abcd", identity: {email: "test@test.com"}, id: "user123", email: nil} - subject.options = options - expect { subject.send(:validate!) }.to_not raise_error - end - - it "should raise an error if data is not either nil or a Hash" do - options = {auth_token: "abcd", identity: {email: "test@test.com"}, email: "test@test.com", data: []} - subject.options = options - expect { subject.send(:validate!) }.to raise_error(ArgumentError) - - options = {auth_token: "abcd", identity: {email: "test@test.com"}, email: "test@test.com", data: nil} - subject.options = options - expect { subject.send(:validate!) }.to_not raise_error - - options = {auth_token: "abcd", identity: {email: "test@test.com"}, email: "test@test.com", data: {}} - subject.options = options - expect { subject.send(:validate!) }.to_not raise_error - end - - it "should not raise an error when the keys are Strings" do - options = {"auth_token" => "abcd", "identity" => {"email" => "test@test.com"}, "email" => "test@test.com", "data" => {}} - subject.options = options - expect { subject.send(:validate!) }.to_not raise_error - end - end - - describe "request" do - it "should send a request to the Vero API" do - stub = stub_request(:post, "https://api.getvero.com/api/v2/users/track.json") - .with( - body: {auth_token: "abcd", identity: {email: "test@test.com"}, email: "test@test.com"}.to_json, - headers: {"Content-Type" => "application/json", "Accept" => "application/json"} - ) - .to_return(status: 200) - - subject.send(:request) - - expect(stub).to have_been_requested - end - end - - describe "integration test" do - it "should not raise any errors" do - stub_request(:post, "https://api.getvero.com/api/v2/users/track.json") - .to_return(status: 200) - - expect { subject.perform }.to_not raise_error - end - end -end diff --git a/spec/lib/api/users/unsubscribe_api_spec.rb b/spec/lib/api/users/unsubscribe_api_spec.rb deleted file mode 100644 index ee69aee..0000000 --- a/spec/lib/api/users/unsubscribe_api_spec.rb +++ /dev/null @@ -1,40 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -describe Vero::Api::Workers::Users::UnsubscribeAPI do - subject { Vero::Api::Workers::Users::UnsubscribeAPI.new("https://api.getvero.com", {auth_token: "abcd", email: "test@test.com", changes: {email: "test@test.com"}}) } - - it_behaves_like "a Vero wrapper" do - let(:end_point) { "/api/v2/users/unsubscribe.json" } - end - - describe :validate! do - it "should not raise an error when the keys are Strings" do - options = {"auth_token" => "abcd", "email" => "test@test.com", "changes" => {"email" => "test@test.com"}} - subject.options = options - expect { subject.send(:validate!) }.to_not raise_error - end - end - - describe "request" do - it "should send a request to the Vero API" do - stub = stub_request(:post, "https://api.getvero.com/api/v2/users/unsubscribe.json") - .with(body: {auth_token: "abcd", email: "test@test.com", changes: {email: "test@test.com"}}) - .to_return(status: 200) - - subject.send(:request) - - expect(stub).to have_been_requested - end - end - - describe "integration test" do - it "should not raise any errors" do - stub_request(:post, "https://api.getvero.com/api/v2/users/unsubscribe.json") - .to_return(status: 200) - - expect { subject.perform }.to_not raise_error - end - end -end diff --git a/spec/lib/api_spec.rb b/spec/lib/api_spec.rb deleted file mode 100644 index 33c530c..0000000 --- a/spec/lib/api_spec.rb +++ /dev/null @@ -1,107 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -describe Vero::Api::Events do - let(:subject) { Vero::Api::Events } - let(:mock_context) { Vero::Context.new } - - let(:input) { {event_name: "test_event", identity: {email: "james@getvero.com"}, data: {test: "test"}} } - let(:expected) { input.merge(tracking_api_key: "abc123", _config: {http_timeout: 60}) } - - before do - allow(mock_context.config).to receive(:configured?).and_return(true) - allow(mock_context.config).to receive(:tracking_api_key).and_return("abc123") - allow(Vero::App).to receive(:default_context).and_return(mock_context) - end - - it "should pass http_timeout to API requests" do - allow(mock_context.config).to receive(:http_timeout).and_return(30) - - expect(Vero::Sender).to( - receive(:call).with( - Vero::Api::Workers::Events::TrackAPI, true, "https://api.getvero.com", expected.merge(_config: {http_timeout: 30}) - ) - ) - subject.track!(input) - end - - describe :track! do - context "should call the TrackAPI object via the configured sender" do - specify do - expect(Vero::Sender).to receive(:call).with(Vero::Api::Workers::Events::TrackAPI, true, "https://api.getvero.com", expected) - subject.track!(input) - end - end - end -end - -describe Vero::Api::Users do - let(:subject) { Vero::Api::Users } - let(:mock_context) { Vero::Context.new } - let(:expected) { input.merge(tracking_api_key: "abc123", _config: {http_timeout: 60}) } - - before do - allow(mock_context.config).to receive(:configured?).and_return(true) - allow(mock_context.config).to receive(:tracking_api_key).and_return("abc123") - allow(Vero::App).to receive(:default_context).and_return(mock_context) - end - - describe :track! do - context "should call the TrackAPI object via the configured sender" do - let(:input) { {email: "james@getvero.com", data: {age: 25}} } - - specify do - expect(Vero::Sender).to receive(:call).with(Vero::Api::Workers::Users::TrackAPI, true, "https://api.getvero.com", expected) - subject.track!(input) - end - end - end - - describe :edit_user! do - context "should call the TrackAPI object via the configured sender" do - let(:input) { {email: "james@getvero.com", changes: {age: 25}} } - - specify do - expect(Vero::Sender).to receive(:call).with(Vero::Api::Workers::Users::EditAPI, true, "https://api.getvero.com", expected) - subject.edit_user!(input) - end - end - end - - describe :edit_user_tags! do - context "should call the TrackAPI object via the configured sender" do - let(:input) { {add: ["boom"], remove: ["tish"]} } - - specify do - expect(Vero::Sender).to receive(:call).with(Vero::Api::Workers::Users::EditTagsAPI, true, "https://api.getvero.com", expected) - subject.edit_user_tags!(input) - end - end - end - - describe :unsubscribe! do - context "should call the TrackAPI object via the configured sender" do - let(:input) { {email: "james@getvero"} } - - specify do - expect(Vero::Sender).to receive(:call).with(Vero::Api::Workers::Users::UnsubscribeAPI, true, "https://api.getvero.com", expected) - subject.unsubscribe!(input) - end - end - end - - describe :resubscribe! do - context "should call the TrackAPI object via the configured sender" do - let(:input) { {email: "james@getvero"} } - - specify do - expect(Vero::Sender).to( - receive(:call) - .with(Vero::Api::Workers::Users::ResubscribeAPI, true, "https://api.getvero.com", expected) - ) - subject.resubscribe!(input) - end - end - end -end diff --git a/spec/lib/app_spec.rb b/spec/lib/app_spec.rb index fec2490..e809564 100644 --- a/spec/lib/app_spec.rb +++ b/spec/lib/app_spec.rb @@ -3,58 +3,51 @@ require "spec_helper" describe Vero::App do - describe "self.default_context" do + let(:context) { described_class.default_context } + + describe ".default_context" do it "inherits from Vero::Context" do - actual = Vero::App.default_context + actual = described_class.default_context expect(actual).to be_a(Vero::Context) end end - let(:context) { Vero::App.default_context } - describe :init do - it "should ignore configuring the config if no block is provided" do - Vero::App.init + describe ".init" do + it "ignores configuring the config if no block is provided" do + described_class.init expect(context.configured?).to be(false) end - it "should pass configuration defined in the block to the config file" do - Vero::App.init + it "passes configuration defined in the block to the config file" do + described_class.init expect(context.config.tracking_api_key).to be_nil - Vero::App.init do |c| + described_class.init do |c| c.tracking_api_key = "abcd1234" end expect(context.config.tracking_api_key).to eq("abcd1234") end - it "should init should be able to set async" do - Vero::App.init do |c| + it "allows init to set async" do + described_class.init do |c| c.async = false end expect(context.config.async).to be(false) - Vero::App.init do |c| + described_class.init do |c| c.async = true end expect(context.config.async).to be(true) end end - describe :disable_requests! do - it "should change config.disabled" do - Vero::App.init + describe ".disable_requests!" do + it "changes config.disabled" do + described_class.init expect(context.config.disabled).to be(false) - Vero::App.disable_requests! + described_class.disable_requests! expect(context.config.disabled).to be(true) end end - - describe :log do - it "should have a log method" do - Vero::App.init - expect(Vero::App).to receive(:log) - Vero::App.log(Object, "test") - end - end end diff --git a/spec/lib/sender_spec.rb b/spec/lib/sender_spec.rb deleted file mode 100644 index 88ed26d..0000000 --- a/spec/lib/sender_spec.rb +++ /dev/null @@ -1,22 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -describe Vero::Sender do - subject { Vero::Sender } - - describe ".senders" do - it "should automatically find senders that are not defined" do - expect(subject.senders[:delayed_job]).to eq(Vero::Senders::DelayedJob) - expect(subject.senders[:sucker_punch]).to eq(Vero::Senders::SuckerPunch) - expect(subject.senders[:resque]).to eq(Vero::Senders::Resque) - expect(subject.senders[:sidekiq]).to eq(Vero::Senders::Sidekiq) - expect(subject.senders[:invalid]).to eq(Vero::Senders::Invalid) - expect(subject.senders[:none]).to eq(Vero::Senders::Base) - end - - it "should fallback to Vero::Senders::Base" do - expect(subject.senders[:unsupported_sender]).to eq(Vero::Senders::Base) - end - end -end diff --git a/spec/lib/senders/sidekiq_spec.rb b/spec/lib/senders/sidekiq_spec.rb deleted file mode 100644 index 2a0568b..0000000 --- a/spec/lib/senders/sidekiq_spec.rb +++ /dev/null @@ -1,32 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -describe Vero::Senders::Sidekiq do - subject { Vero::Senders::Sidekiq.new } - describe :call do - it "should perform_async a Vero::SidekiqWorker" do - expect(Vero::SidekiqWorker).to( - receive(:perform_async) - .with("Vero::Api::Workers::Events::TrackAPI", "abc", {test: "abc"}) - .once - ) - subject.call(Vero::Api::Workers::Events::TrackAPI, "abc", {test: "abc"}) - end - end -end - -describe Vero::SidekiqWorker do - subject { Vero::SidekiqWorker.new } - describe :perform do - it "should call the api method" do - mock_api = double(Vero::Api::Workers::Events::TrackAPI) - expect(mock_api).to receive(:perform).once - - allow(Vero::Api::Workers::Events::TrackAPI).to receive(:new).and_return(mock_api) - expect(Vero::Api::Workers::Events::TrackAPI).to receive(:new).with("abc", {test: "abc"}).once - - subject.perform("Vero::Api::Workers::Events::TrackAPI", "abc", {test: "abc"}) - end - end -end diff --git a/spec/lib/vero/api/events_spec.rb b/spec/lib/vero/api/events_spec.rb new file mode 100644 index 0000000..68b7f2e --- /dev/null +++ b/spec/lib/vero/api/events_spec.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Vero::Api::Events do + let(:api) { described_class } + let(:mock_context) { Vero::Context.new } + + let(:input) { {event_name: "test_event", identity: {email: "james@getvero.com"}, data: {test: "test"}} } + let(:expected) { input.merge(tracking_api_key: "abc123", _config: {http_timeout: 60}) } + + before do + allow(mock_context.config).to receive_messages(configured?: true, tracking_api_key: "abc123") + allow(Vero::App).to receive(:default_context).and_return(mock_context) + end + + it "passes http_timeout to API requests" do + allow(mock_context.config).to receive(:http_timeout).and_return(30) + + expect(Vero::Sender).to( + receive(:call).with( + Vero::Api::Workers::Events::TrackAPI, true, "https://api.getvero.com", expected.merge(_config: {http_timeout: 30}) + ) + ) + api.track!(input) + end + + describe ".track!" do + context "when calling via the configured sender" do + specify do + expect(Vero::Sender).to receive(:call).with(Vero::Api::Workers::Events::TrackAPI, true, "https://api.getvero.com", expected) + api.track!(input) + end + end + end +end diff --git a/spec/lib/vero/api/users_spec.rb b/spec/lib/vero/api/users_spec.rb new file mode 100644 index 0000000..46fe50c --- /dev/null +++ b/spec/lib/vero/api/users_spec.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Vero::Api::Users do + subject(:api) { described_class } + + let(:mock_context) { Vero::Context.new } + let(:expected) { input.merge(tracking_api_key: "abc123", _config: {http_timeout: 60}) } + + before do + allow(mock_context.config).to receive_messages(configured?: true, tracking_api_key: "abc123") + allow(Vero::App).to receive(:default_context).and_return(mock_context) + end + + describe ".track!" do + context "when calling via a configured sender" do + let(:input) { {email: "james@getvero.com", data: {age: 25}} } + + specify do + expect(Vero::Sender).to receive(:call).with(Vero::Api::Workers::Users::TrackAPI, true, "https://api.getvero.com", expected) + api.track!(input) + end + end + end + + describe ".edit_user!" do + context "when calling via a configured sender" do + let(:input) { {email: "james@getvero.com", changes: {age: 25}} } + + specify do + expect(Vero::Sender).to receive(:call).with(Vero::Api::Workers::Users::EditAPI, true, "https://api.getvero.com", expected) + api.edit_user!(input) + end + end + end + + describe ".edit_user_tags!" do + context "when calling via a configured sender" do + let(:input) { {add: ["boom"], remove: ["tish"]} } + + specify do + expect(Vero::Sender).to receive(:call).with(Vero::Api::Workers::Users::EditTagsAPI, true, "https://api.getvero.com", expected) + api.edit_user_tags!(input) + end + end + end + + describe ".unsubscribe!" do + context "when calling via a configured sender" do + let(:input) { {email: "james@getvero"} } + + specify do + expect(Vero::Sender).to receive(:call).with(Vero::Api::Workers::Users::UnsubscribeAPI, true, "https://api.getvero.com", expected) + api.unsubscribe!(input) + end + end + end + + describe ".resubscribe!" do + context "when calling via a configured sender" do + let(:input) { {email: "james@getvero"} } + + specify do + expect(Vero::Sender).to( + receive(:call) + .with(Vero::Api::Workers::Users::ResubscribeAPI, true, "https://api.getvero.com", expected) + ) + api.resubscribe!(input) + end + end + end +end diff --git a/spec/lib/vero/api/workers/events/track_api_spec.rb b/spec/lib/vero/api/workers/events/track_api_spec.rb new file mode 100644 index 0000000..3f98589 --- /dev/null +++ b/spec/lib/vero/api/workers/events/track_api_spec.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Vero::Api::Workers::Events::TrackAPI do + subject(:api) { described_class.new("https://api.getvero.com", payload) } + + let(:payload) do + {auth_token: "abcd", identity: {email: "test@test.com"}, event_name: "test_event"} + end + + it_behaves_like "a Vero wrapper" do + let(:request_method) { :post } + let(:endpoint) { "/api/v2/events/track.json" } + end + + context "with a request with properties" do + describe "#validate!" do + it "raises an error if event_name is a blank String" do + api.options = payload.except(:event_name) + expect { api.send(:validate!) }.to raise_error(ArgumentError) + + api.options = payload + expect { api.send(:validate!) }.not_to raise_error + end + + it "raises an error if data is not either nil or a Hash" do + api.options = payload.merge(data: []) + expect { api.send(:validate!) }.to raise_error(ArgumentError) + + api.options = payload.merge(data: nil) + expect { api.send(:validate!) }.not_to raise_error + + api.options = payload.merge(data: {}) + expect { api.send(:validate!) }.not_to raise_error + end + + it "does not raise an error when keys are Strings for initialization" do + payload.transform_keys!(&:to_s) + + expect do + described_class.new("https://api.getvero.com", payload).send(:validate!) + end.not_to raise_error + end + end + end + + describe "integration test" do + it "completes without raising errors" do + obj = described_class.new("https://api.getvero.com", payload) + + stub_request(:post, "https://api.getvero.com/api/v2/events/track.json") + .with(body: payload, headers: {content_type: "application/json"}) + .to_return(status: 200) + + expect { obj.perform }.not_to raise_error + end + end +end diff --git a/spec/lib/vero/api/workers/users/delete_api_spec.rb b/spec/lib/vero/api/workers/users/delete_api_spec.rb new file mode 100644 index 0000000..99c74a7 --- /dev/null +++ b/spec/lib/vero/api/workers/users/delete_api_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Vero::Api::Workers::Users::DeleteAPI do + subject(:api) { described_class.new("https://api.getvero.com", payload) } + + let(:payload) { {auth_token: "abcd", id: "1234"} } + + it_behaves_like "a Vero wrapper" do + let(:request_method) { :post } + let(:endpoint) { "/api/v2/users/delete.json" } + end + + it_behaves_like "a Vero wrapper" do + let(:request_method) { :post } + let(:endpoint) { "/api/v2/users/delete.json" } + end + + describe "#validate!" do + it "raises an error for missing keys" do + api.options = {"auth_token" => "abcd"} + expect { api.send(:validate!) }.to raise_error(ArgumentError) + end + end +end diff --git a/spec/lib/vero/api/workers/users/edit_api_spec.rb b/spec/lib/vero/api/workers/users/edit_api_spec.rb new file mode 100644 index 0000000..6323593 --- /dev/null +++ b/spec/lib/vero/api/workers/users/edit_api_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Vero::Api::Workers::Users::EditAPI do + subject(:api) { described_class.new("https://api.getvero.com", payload) } + + let(:payload) { {auth_token: "abcd", email: "test@test.com", changes: {email: "test@test.com"}} } + + it_behaves_like "a Vero wrapper" do + let(:request_method) { :put } + let(:endpoint) { "/api/v2/users/edit.json" } + end + + describe "integration test" do + it "does not raise any errors" do + stub_request(:put, "https://api.getvero.com/api/v2/users/edit.json") + .to_return(status: 200) + + expect { api.perform }.not_to raise_error + end + end +end diff --git a/spec/lib/vero/api/workers/users/edit_tags_api_spec.rb b/spec/lib/vero/api/workers/users/edit_tags_api_spec.rb new file mode 100644 index 0000000..5fc9db2 --- /dev/null +++ b/spec/lib/vero/api/workers/users/edit_tags_api_spec.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Vero::Api::Workers::Users::EditTagsAPI do + subject(:api) { described_class.new("https://api.getvero.com", payload) } + + let(:payload) { {email: "test@test.com", add: ["test"]} } + + it_behaves_like "a Vero wrapper" do + let(:request_method) { :put } + let(:endpoint) { "/api/v2/users/tags/edit.json" } + end + + describe "#validate!" do + it "raises an error if email is a blank String" do + options = {auth_token: "abcd", identity: {email: "test@test.com"}, email: nil, add: []} + api.options = options + expect { api.send(:validate!) }.to raise_error(ArgumentError) + + options = {auth_token: "abcd", identity: {email: "test@test.com"}, email: "test@test.com", add: []} + api.options = options + expect { api.send(:validate!) }.not_to raise_error + end + + it "raises an error if add is not an Array or missing" do + options = {auth_token: "abcd", identity: {email: "test@test.com"}, email: "test@test.com", add: "foo"} + + api.options = options + expect { api.send(:validate!) }.to raise_error(ArgumentError) + end + + it "raises an error if remove is not an Array or missing" do + options = {auth_token: "abcd", identity: {email: "test@test.com"}, email: "test@test.com", remove: "foo"} + + api.options = options + expect { api.send(:validate!) }.to raise_error(ArgumentError) + end + + it "raises an error if both add and remove are missing" do + options = {auth_token: "abcd", identity: {email: "test@test.com"}, email: "test@test.com"} + + api.options = options + expect { api.send(:validate!) }.to raise_error(ArgumentError) + end + + it "does not raise an error if the correct arguments are passed" do + options = {auth_token: "abcd", identity: {email: "test@test.com"}, email: "test@test.com", remove: ["Hi"]} + + api.options = options + expect { api.send(:validate!) }.not_to raise_error + end + end + + describe "integration test" do + it "does not raise any errors" do + stub_request(:put, "https://api.getvero.com/api/v2/users/tags/edit.json") + .to_return(status: 200) + expect { api.perform }.not_to raise_error + end + end +end diff --git a/spec/lib/vero/api/workers/users/reidentify_api_spec.rb b/spec/lib/vero/api/workers/users/reidentify_api_spec.rb new file mode 100644 index 0000000..11be3dc --- /dev/null +++ b/spec/lib/vero/api/workers/users/reidentify_api_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Vero::Api::Workers::Users::ReidentifyAPI do + subject(:api) { described_class.new("https://api.getvero.com", payload) } + + let(:payload) { {auth_token: "abcd", id: "test@test.com", new_id: "test2@test.com"} } + + it_behaves_like "a Vero wrapper" do + let(:request_method) { :put } + let(:endpoint) { "/api/v2/users/reidentify.json" } + end + + describe "#validate!" do + it "raises an error if id is missing" do + api.options = {auth_token: "abcd", new_id: "test2@test.com"} + expect { api.send(:validate!) }.to raise_error(ArgumentError) + end + + it "raises an error if new_id is missing" do + api.options = {auth_token: "abcd", id: "test@test.com"} + expect { api.send(:validate!) }.to raise_error(ArgumentError) + end + end + + describe "integration test" do + it "does not raise any errors" do + stub_request(:put, "https://api.getvero.com/api/v2/users/reidentify.json") + .to_return(status: 200) + + expect { api.perform }.not_to raise_error + end + end +end diff --git a/spec/lib/vero/api/workers/users/resubscribe_api_spec.rb b/spec/lib/vero/api/workers/users/resubscribe_api_spec.rb new file mode 100644 index 0000000..1895d5d --- /dev/null +++ b/spec/lib/vero/api/workers/users/resubscribe_api_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Vero::Api::Workers::Users::ResubscribeAPI do + subject(:api) { described_class.new("https://api.getvero.com", payload) } + + let(:payload) { {auth_token: "abcd", id: "1234"} } + + it_behaves_like "a Vero wrapper" do + let(:request_method) { :post } + let(:endpoint) { "/api/v2/users/resubscribe.json" } + end + + describe "#validate!" do + it "raises an error for missing keys" do + api.options = {"auth_token" => "abcd"} + expect { api.send(:validate!) }.to raise_error(ArgumentError) + end + end +end diff --git a/spec/lib/vero/api/workers/users/track_api_spec.rb b/spec/lib/vero/api/workers/users/track_api_spec.rb new file mode 100644 index 0000000..1540299 --- /dev/null +++ b/spec/lib/vero/api/workers/users/track_api_spec.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Vero::Api::Workers::Users::TrackAPI do + subject(:api) { described_class.new("https://api.getvero.com", payload) } + + let(:payload) { {auth_token: "abcd", identity: {email: "test@test.com"}, email: "test@test.com"} } + + it_behaves_like "a Vero wrapper" do + let(:request_method) { :post } + let(:endpoint) { "/api/v2/users/track.json" } + end + + describe "#validate!" do + it "raises an error if email and id are are blank String" do + options = {auth_token: "abcd", identity: {email: "test@test.com"}, id: nil, email: nil} + api.options = options + expect { api.send(:validate!) }.to raise_error(ArgumentError) + + options = {auth_token: "abcd", identity: {email: "test@test.com"}, id: nil, email: "test@test.com"} + api.options = options + expect { api.send(:validate!) }.not_to raise_error + + options = {auth_token: "abcd", identity: {email: "test@test.com"}, id: "", email: nil} + api.options = options + expect { api.send(:validate!) }.to raise_error(ArgumentError) + + options = {auth_token: "abcd", identity: {email: "test@test.com"}, id: "user123", email: nil} + api.options = options + expect { api.send(:validate!) }.not_to raise_error + end + + it "raises an error if data is not either nil or a Hash" do + options = {auth_token: "abcd", identity: {email: "test@test.com"}, email: "test@test.com", data: []} + api.options = options + expect { api.send(:validate!) }.to raise_error(ArgumentError) + + options = {auth_token: "abcd", identity: {email: "test@test.com"}, email: "test@test.com", data: nil} + api.options = options + expect { api.send(:validate!) }.not_to raise_error + + options = {auth_token: "abcd", identity: {email: "test@test.com"}, email: "test@test.com", data: {}} + api.options = options + expect { api.send(:validate!) }.not_to raise_error + end + end + + describe "integration test" do + it "does not raise any errors" do + stub_request(:post, "https://api.getvero.com/api/v2/users/track.json") + .to_return(status: 200) + + expect { api.perform }.not_to raise_error + end + end +end diff --git a/spec/lib/vero/api/workers/users/unsubscribe_api_spec.rb b/spec/lib/vero/api/workers/users/unsubscribe_api_spec.rb new file mode 100644 index 0000000..a50f66e --- /dev/null +++ b/spec/lib/vero/api/workers/users/unsubscribe_api_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Vero::Api::Workers::Users::UnsubscribeAPI do + subject(:api) { described_class.new("https://api.getvero.com", payload) } + + let(:payload) { {auth_token: "abcd", email: "test@test.com", changes: {email: "test@test.com"}} } + + it_behaves_like "a Vero wrapper" do + let(:request_method) { :post } + let(:endpoint) { "/api/v2/users/unsubscribe.json" } + end + + describe "integration test" do + it "does not raise any errors" do + stub_request(:post, "https://api.getvero.com/api/v2/users/unsubscribe.json") + .to_return(status: 200) + + expect { api.perform }.not_to raise_error + end + end +end diff --git a/spec/lib/config_spec.rb b/spec/lib/vero/config_spec.rb similarity index 62% rename from spec/lib/config_spec.rb rename to spec/lib/vero/config_spec.rb index 9ffa2d8..335561e 100644 --- a/spec/lib/config_spec.rb +++ b/spec/lib/vero/config_spec.rb @@ -3,14 +3,14 @@ require "spec_helper" describe Vero::Config do - let(:config) { Vero::Config.new } + subject(:config) { described_class.new } - it "should be async by default" do + it "is async by default" do expect(config.async).to be(true) end - describe :reset! do - it "should reset all attributes" do + describe "#reset!" do + it "resets all attributes" do config.tracking_api_key = "abcd1234" config.reset! @@ -18,36 +18,36 @@ end end - describe :tracking_api_key do - it "should return nil if tracking_api_key is not set" do + describe "#tracking_api_key" do + it "returns nil if tracking_api_key is not set" do config.tracking_api_key = nil expect(config.tracking_api_key).to be_nil end - it "should return an expected tracking_api_key" do + it "returns the expected tracking_api_key" do config.tracking_api_key = "abcd1234" expect(config.tracking_api_key).to eq("abcd1234") end end - describe :request_params do - it "should return a hash containing tracking_api_key if set" do + describe "#request_params" do + it "returns a hash containing tracking_api_key if set" do config.tracking_api_key = nil - expect(config.request_params.key?(:tracking_api_key)).to be_falsey + expect(config.request_params).not_to be_key(:tracking_api_key) config.tracking_api_key = "abcd1234" expect(config.request_params).to include(tracking_api_key: "abcd1234") end end - describe :domain do - it "should return https://api.getvero.com when not set" do + describe "#domain" do + it "returns https://api.getvero.com when not set" do expect(config.domain).to eq("https://api.getvero.com") config.domain = "blah.com" expect(config.domain).not_to eq("https://api.getvero.com") end - it "should return the domain value" do + it "returns the domain value" do config.domain = "test.unbelieveable.com.au" expect(config.domain).to eq("http://test.unbelieveable.com.au") @@ -56,13 +56,13 @@ end end - describe :test_mode do - it "should not raise error even though not configured properly" do + describe "#test_mode" do + it "raise nothing even when not configured correctly" do input = {event_name: "test_event"} mock_context = Vero::Context.new allow(mock_context.config).to receive(:configured?).and_return(false) - expect { Vero::Api::Events.track!(input) }.to_not raise_error + expect { Vero::Api::Events.track!(input) }.not_to raise_error end end end diff --git a/spec/lib/context_spec.rb b/spec/lib/vero/context_spec.rb similarity index 59% rename from spec/lib/context_spec.rb rename to spec/lib/vero/context_spec.rb index bb85289..2785114 100644 --- a/spec/lib/context_spec.rb +++ b/spec/lib/vero/context_spec.rb @@ -3,39 +3,36 @@ require "spec_helper" describe Vero::Context do - let(:context) { Vero::Context.new } + let(:context) { described_class.new } - describe :initialize do + describe "#initialize" do it "accepts multiple parameter types" do - context1 = Vero::Context.new({tracking_api_key: "didah"}) - expect(context1).to be_a(Vero::Context) + context1 = described_class.new({tracking_api_key: "didah"}) expect(context1.config.tracking_api_key).to eq("didah") - context2 = Vero::Context.new(context1) - expect(context2).to be_a(Vero::Context) + context2 = described_class.new(context1) expect(context2).not_to be(context1) expect(context2.config.tracking_api_key).to eq("didah") - context3 = Vero::Context.new VeroUser.new("tracking_api_key") - expect(context3).to be_a(Vero::Context) + context3 = described_class.new VeroUser.new("tracking_api_key") expect(context3.config.tracking_api_key).to eq("tracking_api_key") end end - describe :configure do - it "should ignore configuring the config if no block is provided" do + describe "#configure" do + it "ignores configuring the config if no block is provided" do context.configure expect(context.configured?).to be(false) end - it "should pass configuration defined in the block to the config file" do + it "passes configuration defined in the block to the config file" do context.configure do |c| c.tracking_api_key = "abcd1234" end expect(context.config.tracking_api_key).to eq("abcd1234") end - it "should init should be able to set async" do + it "allows init to set async" do context.configure do |c| c.async = false end @@ -48,8 +45,8 @@ end end - describe :disable_requests! do - it "should change config.disabled" do + describe "#disable_requests!" do + it "changes config.disabled" do expect(context.config.disabled).to be(false) context.disable_requests! expect(context.config.disabled).to be(true) diff --git a/spec/lib/dsl_spec.rb b/spec/lib/vero/dsl/proxy_spec.rb similarity index 65% rename from spec/lib/dsl_spec.rb rename to spec/lib/vero/dsl/proxy_spec.rb index 5f56b9e..9e3b15f 100644 --- a/spec/lib/dsl_spec.rb +++ b/spec/lib/vero/dsl/proxy_spec.rb @@ -2,16 +2,6 @@ require "spec_helper" -describe Vero::DSL do - subject(:dsl) { Class.new.extend(Vero::DSL) } - - describe "#vero" do - it "is a proxy to the API" do - expect(dsl.vero).to be_an_instance_of(Vero::DSL::Proxy) - end - end -end - describe Vero::DSL::Proxy do subject(:proxy) { described_class.new } @@ -20,7 +10,7 @@ expect(proxy.users).to eql(Vero::Api::Users) end - it "should respond to reidentify!" do + it "responds to reidentify!" do expect(proxy.users.respond_to?(:reidentify!)).to be(true) end end diff --git a/spec/lib/vero/dsl_spec.rb b/spec/lib/vero/dsl_spec.rb new file mode 100644 index 0000000..d81e3a1 --- /dev/null +++ b/spec/lib/vero/dsl_spec.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Vero::DSL do + subject(:dsl) { Class.new.extend(described_class) } + + describe "#vero" do + it "is a proxy to the API" do + expect(dsl.vero).to be_an_instance_of(Vero::DSL::Proxy) + end + end +end diff --git a/spec/lib/vero/sender_spec.rb b/spec/lib/vero/sender_spec.rb new file mode 100644 index 0000000..66ceffe --- /dev/null +++ b/spec/lib/vero/sender_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Vero::Sender do + describe ".senders" do + it "automatically finds senders that are not defined" do + expect(described_class.senders[:delayed_job]).to eq(Vero::Senders::DelayedJob) + expect(described_class.senders[:sucker_punch]).to eq(Vero::Senders::SuckerPunch) + expect(described_class.senders[:resque]).to eq(Vero::Senders::Resque) + expect(described_class.senders[:sidekiq]).to eq(Vero::Senders::Sidekiq) + expect(described_class.senders[:invalid]).to eq(Vero::Senders::Invalid) + expect(described_class.senders[:none]).to eq(Vero::Senders::Base) + end + + it "falls back to Vero::Senders::Base" do + expect(described_class.senders[:unsupported_sender]).to eq(Vero::Senders::Base) + end + end +end diff --git a/spec/lib/vero/senders/sidekiq_spec.rb b/spec/lib/vero/senders/sidekiq_spec.rb new file mode 100644 index 0000000..a8ff26e --- /dev/null +++ b/spec/lib/vero/senders/sidekiq_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Vero::Senders::Sidekiq do + subject(:sender) { described_class.new } + + describe "#call" do + it "perform_asyncs a Vero::SidekiqWorker" do + expect(Vero::SidekiqWorker).to( + receive(:perform_async) + .with("Vero::Api::Workers::Events::TrackAPI", "abc", {test: "abc"}) + .once + ) + sender.call(Vero::Api::Workers::Events::TrackAPI, "abc", {test: "abc"}) + end + end +end diff --git a/spec/lib/vero/sidekiq_worker_spec.rb b/spec/lib/vero/sidekiq_worker_spec.rb new file mode 100644 index 0000000..af12dbf --- /dev/null +++ b/spec/lib/vero/sidekiq_worker_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Vero::SidekiqWorker do + subject(:worker) { described_class.new } + + describe "#perform" do + it "calls the api method" do + mock_api = instance_double(Vero::Api::Workers::Events::TrackAPI) + expect(mock_api).to receive(:perform).once + + expect(Vero::Api::Workers::Events::TrackAPI).to receive(:new) + .with("abc", {test: "abc"}) + .once + .and_return(mock_api) + + worker.perform("Vero::Api::Workers::Events::TrackAPI", "abc", {test: "abc"}) + end + end +end diff --git a/spec/lib/trackable_spec.rb b/spec/lib/vero/trackable_spec.rb similarity index 55% rename from spec/lib/trackable_spec.rb rename to spec/lib/vero/trackable_spec.rb index 3143451..3f1180d 100644 --- a/spec/lib/trackable_spec.rb +++ b/spec/lib/vero/trackable_spec.rb @@ -13,30 +13,29 @@ def vero_context(user, logging = true, async = false, disabled = true) end describe Vero::Trackable do - before do - @request_params = { + let(:request_params) do + { event_name: "test_event", tracking_api_key: "YWJjZDEyMzQ6ZWZnaDU2Nzg=", identity: {email: "user@getvero.com", age: 20, _user_type: "User"}, data: {test: 1} } - @url = "https://api.getvero.com/api/v1/track.json" - @user = User.new - - @content_type_params = {content_type: :json, accept: :json} end + let(:user) { User.new } + let(:url) { "https://api.getvero.com/api/v2/users/track.json" } - context "the gem has not been configured" do + context "when the gem hasn't been configured" do before { Vero::App.reset! } - it "should raise an error when API requests are made" do - expect { @user.track(@request_params[:event_name], @request_params[:data]) }.to raise_error(RuntimeError) - allow(@user).to receive(:post_later).and_return("success") - expect { @user.identity! }.to raise_error(NoMethodError) + it "raises an error when API requests are made" do + expect { user.track(request_params[:event_name], request_params[:data]) }.to raise_error(RuntimeError) + + allow(user).to receive(:post_later).and_return("success") + expect { user.identity! }.to raise_error(NoMethodError) end end - context "the gem has been configured" do + context "when the gem is configured" do before do Vero::App.init do |c| c.tracking_api_key = "YWJjZDEyMzQ6ZWZnaDU2Nzg=" @@ -44,211 +43,213 @@ def vero_context(user, logging = true, async = false, disabled = true) end end - describe :track! do - before do - @request_params = { + describe ".track!" do + let(:request_params) { + { event_name: "test_event", identity: {email: "user@getvero.com", age: 20, _user_type: "User"}, data: {test: 1}, extras: {} } - @url = "https://api.getvero.com/api/v1/track.json" - end + } + let(:url) { "https://api.getvero.com/api/v1/track.json" } - it "should not send a track request when the required parameters are invalid" do - expect { @user.track!(nil) }.to raise_error(ArgumentError) - expect { @user.track!("") }.to raise_error(ArgumentError) - expect { @user.track!("test", "") }.to raise_error(ArgumentError) + it "does not send a track request when the required parameters are invalid" do + expect { user.track!(nil) }.to raise_error(ArgumentError) + expect { user.track!("") }.to raise_error(ArgumentError) + expect { user.track!("test", "") }.to raise_error(ArgumentError) end - it "should send a `track!` request when async is set to false" do - context = vero_context(@user) - allow(@user).to receive(:with_vero_context).and_return(context) + it "sends a `track!` request when async is set to false" do + context = vero_context(user) + allow(user).to receive(:with_vero_context).and_return(context) allow(Vero::Api::Events).to receive(:track!).and_return(200) - expect(Vero::Api::Events).to receive(:track!).with(@request_params, context) - expect(@user.track!(@request_params[:event_name], @request_params[:data])).to eq(200) + expect(Vero::Api::Events).to receive(:track!).with(request_params, context) + expect(user.track!(request_params[:event_name], request_params[:data])).to eq(200) allow(Vero::Api::Events).to receive(:track!).and_return(200) - expect(Vero::Api::Events).to receive(:track!).with(@request_params.merge(data: {}), context) - expect(@user.track!(@request_params[:event_name])).to eq(200) + expect(Vero::Api::Events).to receive(:track!).with(request_params.merge(data: {}), context) + expect(user.track!(request_params[:event_name])).to eq(200) end context "when set to be async" do + let(:context) { vero_context(user, true, true) } + before do - @context = vero_context(@user, true, true) - allow(@user).to receive(:with_vero_context).and_return(@context) + allow(user).to receive(:with_vero_context).and_return(context) end it "sends" do - expect(@user.track!(@request_params[:event_name], @request_params[:data])).to be_nil - expect(@user.track!(@request_params[:event_name])).to be_nil + expect(user.track!(request_params[:event_name], request_params[:data])).to be_nil + expect(user.track!(request_params[:event_name])).to be_nil end end end - describe :identify! do - before do - @request_params = { + describe ".identify!" do + let(:request_params) { + { id: nil, email: "user@getvero.com", data: {email: "user@getvero.com", age: 20, _user_type: "User"} } - @url = "https://api.getvero.com/api/v2/users/track.json" - end + } + let(:url) { "https://api.getvero.com/api/v2/users/track.json" } - it "should send an `identify` request when async is set to false" do - context = vero_context(@user) - allow(@user).to receive(:with_vero_context).and_return(context) + it "sends an `identify` request when async is set to false" do + context = vero_context(user) + allow(user).to receive(:with_vero_context).and_return(context) allow(Vero::Api::Users).to receive(:track!).and_return(200) - expect(Vero::Api::Users).to receive(:track!).with(@request_params, context) + expect(Vero::Api::Users).to receive(:track!).with(request_params, context) - expect(@user.identify!).to eq(200) + expect(user.identify!).to eq(200) end context "when set to use async" do + let(:context) { vero_context(user, false, true) } + before do - @context = vero_context(@user, false, true) - allow(@user).to receive(:with_vero_context).and_return(@context) + allow(user).to receive(:with_vero_context).and_return(context) end it "sends" do - expect(@user.identify!).to be_nil + expect(user.identify!).to be_nil end end end - describe :update_user! do - before do - @request_params = { + describe ".update_user!" do + let(:request_params) do + { id: nil, email: "user@getvero.com", changes: {email: "user@getvero.com", age: 20, _user_type: "User"} } - @url = "https://api.getvero.com/api/v2/users/edit.json" end + let(:url) { "https://api.getvero.com/api/v2/users/edit.json" } - it "should send an `update_user` request when async is set to false" do + it "sends an `update_user` request when async is set to false" do context = Vero::Context.new(Vero::App.default_context) - context.subject = @user + context.subject = user - allow(@user).to receive(:with_vero_context).and_return(context) + allow(user).to receive(:with_vero_context).and_return(context) allow(Vero::Api::Users).to receive(:edit_user!).and_return(200) - expect(Vero::Api::Users).to receive(:edit_user!).with(@request_params, context) + expect(Vero::Api::Users).to receive(:edit_user!).with(request_params, context) - expect(@user.with_vero_context.update_user!).to eq(200) + expect(user.with_vero_context.update_user!).to eq(200) end context "when set to use async" do before do - @context = vero_context(@user, false, true) - allow(@user).to receive(:with_vero_context).and_return(@context) + context = vero_context(user, false, true) + allow(user).to receive(:with_vero_context).and_return(context) end it "sends" do - expect(@user.with_vero_context.update_user!).to be_nil + expect(user.with_vero_context.update_user!).to be_nil end end end - describe :update_user_tags! do - before do - @request_params = { + describe ".update_user_tags!" do + let(:request_params) do + { id: nil, email: "user@getvero.com", add: [], remove: [] } - @url = "https://api.getvero.com/api/v2/users/tags/edit.json" end + let(:url) { "https://api.getvero.com/api/v2/users/tags/edit.json" } - it "should send an `update_user_tags` request when async is set to false" do + it "sends an `update_user_tags` request when async is set to false" do context = Vero::Context.new(Vero::App.default_context) - context.subject = @user + context.subject = user context.config.async = false - allow(@user).to receive(:with_vero_context).and_return(context) + allow(user).to receive(:with_vero_context).and_return(context) allow(Vero::Api::Users).to receive(:edit_user_tags!).and_return(200) - expect(Vero::Api::Users).to receive(:edit_user_tags!).with(@request_params, context) + expect(Vero::Api::Users).to receive(:edit_user_tags!).with(request_params, context) - expect(@user.with_vero_context.update_user_tags!).to eq(200) + expect(user.with_vero_context.update_user_tags!).to eq(200) end - it "should send using another thread when async is set to true" do + it "sends using another thread when async is set to true" do context = Vero::Context.new(Vero::App.default_context) - context.subject = @user + context.subject = user context.config.async = true - allow(@user).to receive(:with_vero_context).and_return(context) + allow(user).to receive(:with_vero_context).and_return(context) - expect(@user.with_vero_context.update_user_tags!).to be_nil + expect(user.with_vero_context.update_user_tags!).to be_nil end end - describe :unsubscribe! do - before do - @request_params = { + describe ".unsubscribe!" do + let(:request_params) do + { id: nil, email: "user@getvero.com" } - @url = "https://api.getvero.com/api/v2/users/unsubscribe.json" end + let(:url) { "https://api.getvero.com/api/v2/users/unsubscribe.json" } - it "should send an `update_user` request when async is set to false" do + it "sends an `update_user` request when async is set to false" do context = Vero::Context.new(Vero::App.default_context) - context.subject = @user + context.subject = user context.config.async = false - allow(@user).to receive(:with_vero_context).and_return(context) + allow(user).to receive(:with_vero_context).and_return(context) allow(Vero::Api::Users).to receive(:unsubscribe!).and_return(200) - expect(Vero::Api::Users).to receive(:unsubscribe!).with(@request_params, context) + expect(Vero::Api::Users).to receive(:unsubscribe!).with(request_params, context) - expect(@user.with_vero_context.unsubscribe!).to eq(200) + expect(user.with_vero_context.unsubscribe!).to eq(200) end context "when using async" do before do - @context = vero_context(@user, false, true) - allow(@user).to receive(:with_vero_context).and_return(@context) + context = vero_context(user, false, true) + allow(user).to receive(:with_vero_context).and_return(context) end it "sends" do - expect(@user.with_vero_context.unsubscribe!).to be_nil + expect(user.with_vero_context.unsubscribe!).to be_nil end end end - describe :trackable do + describe ".trackable" do before { User.reset_trackable_map! } - it "should build an array of trackable params" do + it "builds an array of trackable params" do User.trackable :email, :age expect(User.trackable_map).to eq(%i[email age]) end - it "should append new trackable items to an existing trackable map" do + it "appends new trackable items to an existing trackable map" do User.trackable :email, :age User.trackable :hair_colour expect(User.trackable_map).to eq(%i[email age hair_colour]) end - it "should append an extra's hash to the trackable map" do + it "appends an extra's hash to the trackable map" do User.trackable :email, {extras: :properties} expect(User.trackable_map).to eq([:email, {extras: :properties}]) end end - describe :to_vero do - before :all do + describe "#to_vero" do + before do User.reset_trackable_map! User.trackable :email, :age end - it "should return a hash of all values mapped by trackable" do + it "returns a hash of all values mapped by trackable" do user = User.new expect(user.to_vero).to eq({email: "user@getvero.com", age: 20, _user_type: "User"}) @@ -265,7 +266,7 @@ def vero_context(user, logging = true, async = false, disabled = true) expect(user.to_vero).to eq({email: "user@getvero.com", _user_type: "UserWithNilAttributes"}) end - it "should take into account any defined extras" do + it "takes into account any defined extras" do user = UserWithExtras.new user.properties = nil expect(user.to_vero).to eq({email: "user@getvero.com", _user_type: "UserWithExtras"}) @@ -286,22 +287,22 @@ def vero_context(user, logging = true, async = false, disabled = true) expect(user.to_vero).to eq({email: "user@getvero.com", age: 26, _user_type: "UserWithPrivateExtras"}) end - it "should allow extras to be provided instead :id or :email" do + it "allows extras to be provided instead :id or :email" do user = UserWithOnlyExtras.new user.properties = {email: user.email} expect(user.to_vero).to eq({email: "user@getvero.com", _user_type: "UserWithOnlyExtras"}) end end - describe :with_vero_context do - it "should be able to change contexts" do + describe "#with_vero_context" do + it "can change contexts" do user = User.new expect(user.with_default_vero_context.config.config_params).to eq({tracking_api_key: "YWJjZDEyMzQ6ZWZnaDU2Nzg="}) expect(user.with_vero_context({tracking_api_key: "boom"}).config.config_params).to eq({tracking_api_key: "boom"}) end end - it "should work when Vero::Trackable::Interface is not included" do + it "works when Vero::Trackable::Interface is not included" do user = UserWithoutInterface.new request_params = { diff --git a/spec/lib/vero/view_helpers/javascript_spec.rb b/spec/lib/vero/view_helpers/javascript_spec.rb new file mode 100644 index 0000000..545b39e --- /dev/null +++ b/spec/lib/vero/view_helpers/javascript_spec.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require "spec_helper" + +require "rails" +require "action_view" +require "active_support" +require "vero/view_helpers/javascript" + +# rubocop:disable Style/MixinUsage +include Vero::ViewHelpers::Javascript +include ActionView::Helpers +include ActionView::Context +# rubocop:enable Style/MixinUsage + +describe Vero::ViewHelpers::Javascript do + subject(:helper) { described_class } + + before { Vero::App.reset! } + + describe ".vero_javascript_tag" do + it "returns an empty string if Vero::App is not properly configured" do + expect(helper.vero_javascript_tag).to eq("") + + Vero::App.init + expect(helper.vero_javascript_tag).to eq("") + end + + context "when Vero::App has been properly configured" do + let(:tracking_api_key) { "abcd1234" } + + before do + Vero::App.init do |c| + c.tracking_api_key = tracking_api_key + end + end + + it "returns a properly formatted javascript snippet" do + expect(helper.vero_javascript_tag).to eq(<<~HTML.strip) + + HTML + end + end + end +end diff --git a/spec/lib/view_helpers_spec.rb b/spec/lib/view_helpers_spec.rb deleted file mode 100644 index 868457f..0000000 --- a/spec/lib/view_helpers_spec.rb +++ /dev/null @@ -1,46 +0,0 @@ -# frozen_string_literal: true - -require "spec_helper" - -require "rails" -require "action_view" -require "active_support" -require "vero/view_helpers/javascript" - -# rubocop:disable Style/MixinUsage -include Vero::ViewHelpers::Javascript -include ActionView::Helpers -include ActionView::Context -# rubocop:enable Style/MixinUsage - -describe Vero::ViewHelpers::Javascript do - before do - Vero::App.reset! - end - - subject { Vero::ViewHelpers::Javascript } - describe :vero_javascript_tag do - it "should return an empty string if Vero::App is not properly configured" do - expect(subject.vero_javascript_tag).to eq("") - - Vero::App.init - expect(subject.vero_javascript_tag).to eq("") - end - - context "Vero::App has been properly configured" do - before do - @tracking_api_key = "abcd1234" - - Vero::App.init do |c| - c.tracking_api_key = @tracking_api_key - end - end - - it "should return a properly formatted javascript snippet" do - expect(subject.vero_javascript_tag).to eq(<<~HTML.strip) - - HTML - end - end - end -end diff --git a/spec/support/base_config_shared_examples.rb b/spec/support/base_config_shared_examples.rb index df630cc..485e091 100644 --- a/spec/support/base_config_shared_examples.rb +++ b/spec/support/base_config_shared_examples.rb @@ -1,11 +1,30 @@ # frozen_string_literal: true RSpec.shared_examples "a Vero wrapper" do - it "should inherit from Vero::Api::Workers::BaseCaller" do + it "inherits from Vero::Api::Workers::BaseCaller" do expect(subject).to be_a(Vero::Api::Workers::BaseAPI) end - it "should map to current version of Vero API" do - expect(subject.send(:url)).to eq("https://api.getvero.com#{end_point}") + it "maps to current version of Vero API" do + expect(subject.send(:url)).to eq("https://api.getvero.com#{endpoint}") + end + + describe "#validate!" do + it "does not raise an error when the keys are Strings" do + subject.options = payload.deep_stringify_keys + expect { subject.send(:validate!) }.not_to raise_error + end + end + + describe "#request" do + it "sends a request to the Vero API" do + stub = stub_request(request_method, ["https://api.getvero.com", endpoint].join) + .with(body: payload, headers: {content_type: "application/json"}) + .to_return(status: 200) + + subject.send(:request) + + expect(stub).to have_been_requested + end end end