-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathabstract_sendgrid_client_spec.rb
91 lines (81 loc) · 3.84 KB
/
abstract_sendgrid_client_spec.rb
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
require File.expand_path("#{File.dirname(__FILE__)}/../../helper")
describe SendgridToolkit::AbstractSendgridClient do
before do
backup_env
end
after do
restore_env
end
describe "#api_post" do
it "throws error when authentication fails" do
FakeWeb.register_uri(:post, %r|https://#{REGEX_ESCAPED_BASE_URI}/profile\.get\.json|, :body => '{"error":{"code":401,"message":"Permission denied, wrong credentials"}}')
@obj = SendgridToolkit::AbstractSendgridClient.new("fakeuser", "fakepass")
lambda {
@obj.send(:api_post, "profile", "get", {})
}.should raise_error SendgridToolkit::AuthenticationFailed
end
it "throws error when bad username or password presented" do
FakeWeb.register_uri(:post, %r|https://#{REGEX_ESCAPED_BASE_URI}/stats\.getAdvanced\.json\?|, :body => '{"error":{"code":403,"message":"Bad username / password"}}', :status => ['403', 'Forbidden'])
@obj = SendgridToolkit::AbstractSendgridClient.new("fakeuser", "fakepass")
expect {
@obj.send(:api_post, "stats", "getAdvanced", {})
}.to raise_error SendgridToolkit::AuthenticationFailed
end
it "thows error when sendgrid response is a server error" do
FakeWeb.register_uri(:post, %r|https://#{REGEX_ESCAPED_BASE_URI}/profile\.get\.json|, :body => '{}', :status => ['500', 'Internal Server Error'])
@obj = SendgridToolkit::AbstractSendgridClient.new("someuser", "somepass")
lambda {
@obj.send(:api_post, "profile", "get", {})
}.should raise_error SendgridToolkit::SendgridServerError
end
it "thows error when sendgrid response is an API error" do
FakeWeb.register_uri(:post, %r|https://#{REGEX_ESCAPED_BASE_URI}/stats\.get\.json|, :body => '{"error": "error in end_date: end date is in the future"}', :status => ['400', 'Bad Request'])
@obj = SendgridToolkit::AbstractSendgridClient.new("someuser", "somepass")
lambda {
@obj.send(:api_post, "stats", "get", {})
}.should raise_error SendgridToolkit::APIError
end
end
describe "#initialize" do
after(:each) do
SendgridToolkit.api_user = nil
SendgridToolkit.api_key = nil
end
it "stores api credentials when passed in" do
ENV['SMTP_USERNAME'] = "env_username"
ENV['SMTP_PASSWORD'] = "env_apikey"
@obj = SendgridToolkit::AbstractSendgridClient.new("username", "apikey")
@obj.instance_variable_get("@api_user").should == "username"
@obj.instance_variable_get("@api_key").should == "apikey"
end
it "uses module level user and key if they are set" do
SendgridToolkit.api_user = "username"
SendgridToolkit.api_key = "apikey"
SendgridToolkit.api_key.should == "apikey"
SendgridToolkit.api_user.should == "username"
@obj = SendgridToolkit::AbstractSendgridClient.new
@obj.instance_variable_get("@api_user").should == "username"
@obj.instance_variable_get("@api_key").should == "apikey"
end
it "resorts to environment variables when no credentials specified" do
ENV['SMTP_USERNAME'] = "env_username"
ENV['SMTP_PASSWORD'] = "env_apikey"
@obj = SendgridToolkit::AbstractSendgridClient.new()
@obj.instance_variable_get("@api_user").should == "env_username"
@obj.instance_variable_get("@api_key").should == "env_apikey"
end
it "throws error when no credentials are found" do
ENV['SMTP_USERNAME'] = nil
ENV['SMTP_PASSWORD'] = nil
lambda {
@obj = SendgridToolkit::AbstractSendgridClient.new()
}.should raise_error SendgridToolkit::NoAPIUserSpecified
lambda {
@obj = SendgridToolkit::AbstractSendgridClient.new(nil, "password")
}.should raise_error SendgridToolkit::NoAPIUserSpecified
lambda {
@obj = SendgridToolkit::AbstractSendgridClient.new("user", nil)
}.should raise_error SendgridToolkit::NoAPIKeySpecified
end
end
end