forked from maximadeka/convertkit-ruby
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubscribers_spec.rb
More file actions
94 lines (78 loc) · 3.21 KB
/
subscribers_spec.rb
File metadata and controls
94 lines (78 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
require "spec_helper"
require "securerandom"
module Convertkit
class Client
describe Subscribers do
before do
Convertkit.configure do |config|
config.api_secret = ENV["API_SECRET"]
config.api_key = ENV["API_KEY"]
end
@client = Convertkit::Client.new
end
describe "#subscribers" do
it "sends the right request" do
r = @client.subscribers
expect(r.success?).to be_truthy
expect(r.body).to_not eql({"error"=>"Authorization Failed", "message"=>"API Key not present"})
end
end
describe "#subscriber" do
it "sends the right request" do
subscriber_id = ENV['SUBSCRIBER_ID']
r = @client.subscriber(subscriber_id)
expect(r.success?).to be_truthy
expect(r.body).to_not eql({"error"=>"Not Found", "message"=>"The entity you were trying to find doesn't exist"})
end
end
describe "#subscriber_stats" do
it "sends the right request" do
subscriber_id = ENV['SUBSCRIBER_ID']
r = @client.subscriber_stats(subscriber_id)
expect(r.success?).to be_truthy
end
end
describe "#update_subscriber" do
it "updates a subscriber" do
old_email = "old-email-#{Time.now.to_i}@example.com"
new_email = "new-email-#{Time.now.to_i}@example.com"
subscriber_id = @client.add_subscriber_to_tag(ENV["TAG_ID"], old_email).body["subscription"]["subscriber"]["id"]
response = @client.update_subscriber(subscriber_id, { email_address: new_email })
subscriber = response["subscriber"]
expect(subscriber["email_address"]).to eq(new_email)
end
it "only updates provided attributes" do
email = "old-email-#{Time.now.to_i}@example.com"
first_name = "first_name"
subscriber_id = @client.add_subscriber_to_tag(ENV["TAG_ID"], email, first_name: first_name).body["subscription"]["subscriber"]["id"]
response = @client.update_subscriber(subscriber_id)
subscriber = response["subscriber"]
expect(subscriber["email_address"]).to eq(email)
expect(subscriber["first_name"]).to eq(first_name)
end
end
describe "#unsubscribe" do
it "sends the right request" do
tag_id = ENV['TAG_ID']
email = "crt-subscribers+#{Time.now.to_i}@example.com"
@client.add_subscriber_to_tag(tag_id, email)
r = @client.unsubscribe(email)
expect(r.success?).to be_truthy
expect(r.body).to_not eql({"error"=>"Not Found", "message"=>"The entity you were trying to find doesn't exist"})
end
end
describe "#remove_tag_from_subscriber" do
it "removes tag from a subscriber" do
tag_id = ENV['TAG_ID']
email = "#{SecureRandom.hex}@example.com"
subscriber = @client.add_subscriber_to_tag(tag_id, email).body["subscription"]["subscriber"]
expect {
@client.remove_tag_from_subscriber(subscriber["id"], tag_id)
}.to change {
@client.connection.get("tags/#{tag_id}/subscriptions").body["total_subscriptions"]
}.by(-1)
end
end
end
end
end