|
3 | 3 | require "spec_helper" |
4 | 4 |
|
5 | 5 | describe Vero::Api::Workers::Events::TrackAPI do |
6 | | - subject { Vero::Api::Workers::Events::TrackAPI.new("https://api.getvero.com", {auth_token: "abcd", identity: {email: "test@test.com"}, event_name: "test_event"}) } |
| 6 | + let(:payload) do |
| 7 | + {auth_token: "abcd", identity: {email: "test@test.com"}, event_name: "test_event"} |
| 8 | + end |
| 9 | + |
| 10 | + subject { Vero::Api::Workers::Events::TrackAPI.new("https://api.getvero.com", payload) } |
7 | 11 |
|
8 | 12 | it_behaves_like "a Vero wrapper" do |
9 | 13 | let(:end_point) { "/api/v2/events/track.json" } |
|
12 | 16 | context "request with properties" do |
13 | 17 | describe :validate! do |
14 | 18 | it "should raise an error if event_name is a blank String" do |
15 | | - options = {auth_token: "abcd", identity: {email: "test@test.com"}, event_name: nil} |
16 | | - subject.options = options |
| 19 | + subject.options = payload.except(:event_name) |
17 | 20 | expect { subject.send(:validate!) }.to raise_error(ArgumentError) |
18 | 21 |
|
19 | | - options = {auth_token: "abcd", identity: {email: "test@test.com"}, event_name: "test_event"} |
20 | | - subject.options = options |
| 22 | + subject.options = payload |
21 | 23 | expect { subject.send(:validate!) }.to_not raise_error |
22 | 24 | end |
23 | 25 |
|
24 | 26 | it "should raise an error if data is not either nil or a Hash" do |
25 | | - options = {auth_token: "abcd", identity: {email: "test@test.com"}, event_name: "test_event", data: []} |
26 | | - subject.options = options |
| 27 | + subject.options = payload.merge(data: []) |
27 | 28 | expect { subject.send(:validate!) }.to raise_error(ArgumentError) |
28 | 29 |
|
29 | | - options = {auth_token: "abcd", identity: {email: "test@test.com"}, event_name: "test_event", data: nil} |
30 | | - subject.options = options |
| 30 | + subject.options = payload.merge(data: nil) |
31 | 31 | expect { subject.send(:validate!) }.to_not raise_error |
32 | 32 |
|
33 | | - options = {auth_token: "abcd", identity: {email: "test@test.com"}, event_name: "test_event", data: {}} |
34 | | - subject.options = options |
| 33 | + subject.options = payload.merge(data: {}) |
35 | 34 | expect { subject.send(:validate!) }.to_not raise_error |
36 | 35 | end |
37 | 36 |
|
38 | 37 | it "should not raise an error when the keys are Strings" do |
39 | | - options = {"auth_token" => "abcd", "identity" => {"email" => "test@test.com"}, "event_name" => "test_event", "data" => {}} |
| 38 | + options = {"auth_token" => "abcd", "identity" => {"email" => "test@test.com"}, "event_name" => "test_event", |
| 39 | + "data" => {}} |
40 | 40 | subject.options = options |
41 | 41 | expect { subject.send(:validate!) }.to_not raise_error |
42 | 42 | end |
43 | 43 |
|
44 | 44 | it "should not raise an error when keys are Strings for initialization" do |
45 | | - options = {"auth_token" => "abcd", "identity" => {"email" => "test@test.com"}, "event_name" => "test_event", "data" => {}} |
46 | | - expect { Vero::Api::Workers::Events::TrackAPI.new("https://api.getvero.com", options).send(:validate!) }.to_not raise_error |
| 45 | + payload.transform_keys!(&:to_s) |
| 46 | + |
| 47 | + expect do |
| 48 | + Vero::Api::Workers::Events::TrackAPI.new("https://api.getvero.com", payload).send(:validate!) |
| 49 | + end.to_not raise_error |
47 | 50 | end |
48 | 51 | end |
49 | 52 |
|
50 | 53 | describe "request" do |
51 | 54 | it "should send a request to the Vero API" do |
52 | 55 | stub = stub_request(:post, "https://api.getvero.com/api/v2/events/track.json") |
53 | | - .with( |
54 | | - body: {auth_token: "abcd", identity: {email: "test@test.com"}, event_name: "test_event"}.to_json, |
55 | | - headers: {"Content-Type" => "application/json", "Accept" => "application/json"} |
56 | | - ) |
| 56 | + .with(body: payload.to_json) |
57 | 57 | .to_return(status: 200) |
58 | 58 |
|
59 | 59 | subject.send(:request) |
|
65 | 65 |
|
66 | 66 | describe "integration test" do |
67 | 67 | it "should not raise any errors" do |
68 | | - obj = Vero::Api::Workers::Events::TrackAPI.new("https://api.getvero.com", {auth_token: "abcd", identity: {email: "test@test.com"}, event_name: "test_event"}) |
| 68 | + obj = Vero::Api::Workers::Events::TrackAPI.new("https://api.getvero.com", payload) |
| 69 | + |
| 70 | + stub_request(:post, "https://api.getvero.com/api/v2/events/track.json") |
| 71 | + .with(body: payload.to_json) |
| 72 | + .to_return(status: 200) |
69 | 73 |
|
70 | | - allow(RestClient).to receive(:post).and_return(200) |
71 | 74 | expect { obj.perform }.to_not raise_error |
72 | 75 | end |
73 | 76 | end |
|
0 commit comments