mailgun 0.8 → 0.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,7 @@ require 'spec_helper'
3
3
  describe Mailgun::Base do
4
4
 
5
5
  it "should raise an error if the api_key has not been set" do
6
+ Mailgun.config { |c| c.api_key = nil }
6
7
  expect do
7
8
  Mailgun()
8
9
  end.to raise_error ArgumentError
@@ -24,7 +25,7 @@ describe Mailgun::Base do
24
25
  describe "Mailgun.new" do
25
26
  it "Mailgun() method should return a new Mailgun object" do
26
27
  mailgun = Mailgun({:api_key => "some-junk-string"})
27
- mailgun.should be_kind_of(Mailgun::Base)
28
+ expect(mailgun).to be_kind_of(Mailgun::Base)
28
29
  end
29
30
  end
30
31
 
@@ -34,16 +35,14 @@ describe Mailgun::Base do
34
35
  end
35
36
 
36
37
  it "Mailgun#mailboxes should return an instance of Mailgun::Mailbox" do
37
- @mailgun.mailboxes.should be_kind_of(Mailgun::Mailbox)
38
+ expect(@mailgun.mailboxes).to be_kind_of(Mailgun::Mailbox)
38
39
  end
39
40
 
40
41
  it "Mailgun#routes should return an instance of Mailgun::Route" do
41
- @mailgun.routes.should be_kind_of(Mailgun::Route)
42
+ expect(@mailgun.routes).to be_kind_of(Mailgun::Route)
42
43
  end
43
44
  end
44
45
 
45
-
46
-
47
46
  describe "internal helper methods" do
48
47
  before :each do
49
48
  @mailgun = Mailgun({:api_key => "some-junk-string"})
@@ -51,39 +50,44 @@ describe Mailgun::Base do
51
50
 
52
51
  describe "Mailgun#base_url" do
53
52
  it "should return https url if use_https is true" do
54
- @mailgun.base_url.should == "https://api:#{Mailgun.api_key}@#{Mailgun.mailgun_host}/#{Mailgun.api_version}"
53
+ expect(@mailgun.base_url).to eq "https://api:#{Mailgun.api_key}@#{Mailgun.mailgun_host}/#{Mailgun.api_version}"
55
54
  end
56
55
  end
57
56
 
58
57
  describe "Mailgun.submit" do
59
- it "should send method and arguments to RestClient" do
60
- RestClient.should_receive(:test_method)
61
- .with({:arg1=>"val1"},{})
62
- .and_return({})
63
- Mailgun.submit :test_method, :arg1=>"val1"
58
+ let(:client_double) { double(Mailgun::Client) }
59
+
60
+ it "should send method and arguments to Mailgun::Client" do
61
+ expect(Mailgun::Client).to receive(:new)
62
+ .with('/')
63
+ .and_return(client_double)
64
+ expect(client_double).to receive(:test_method)
65
+ .with({:arg1=>"val1"})
66
+ .and_return('{}')
67
+
68
+ Mailgun.submit :test_method, '/', :arg1=>"val1"
64
69
  end
65
70
  end
66
-
67
71
  end
68
72
 
69
73
  describe "configuration" do
70
74
  describe "default settings" do
71
- it "api_version is v2" do
72
- Mailgun.api_version.should eql 'v2'
75
+ it "api_version is v3" do
76
+ expect(Mailgun.api_version).to eql 'v3'
73
77
  end
74
78
  it "should use https by default" do
75
- Mailgun.protocol.should == "https"
79
+ expect(Mailgun.protocol).to eq "https"
76
80
  end
77
81
  it "mailgun_host is 'api.mailgun.net'" do
78
- Mailgun.mailgun_host.should eql 'api.mailgun.net'
82
+ expect(Mailgun.mailgun_host).to eql 'api.mailgun.net'
79
83
  end
80
84
 
81
85
  it "test_mode is false" do
82
- Mailgun.test_mode.should eql false
86
+ expect(Mailgun.test_mode).to eql false
83
87
  end
84
88
 
85
89
  it "domain is not set" do
86
- Mailgun.domain.should be_nil
90
+ expect(Mailgun.domain).to be_nil
87
91
  end
88
92
  end
89
93
 
@@ -99,27 +103,29 @@ describe Mailgun::Base do
99
103
  end
100
104
  end
101
105
 
106
+ after(:each) { Mailgun.configure { |c| c.domain = nil } }
107
+
102
108
  it "allows me to set my API key easily" do
103
- Mailgun.api_key.should eql 'some-api-key'
109
+ expect(Mailgun.api_key).to eql 'some-api-key'
104
110
  end
105
111
 
106
112
  it "allows me to set the api_version attribute" do
107
- Mailgun.api_version.should eql 'v2'
113
+ expect(Mailgun.api_version).to eql 'v2'
108
114
  end
109
115
 
110
116
  it "allows me to set the protocol attribute" do
111
- Mailgun.protocol.should eql 'https'
117
+ expect(Mailgun.protocol).to eql 'https'
112
118
  end
113
-
119
+
114
120
  it "allows me to set the mailgun_host attribute" do
115
- Mailgun.mailgun_host.should eql 'api.mailgun.net'
121
+ expect(Mailgun.mailgun_host).to eql 'api.mailgun.net'
116
122
  end
117
123
  it "allows me to set the test_mode attribute" do
118
- Mailgun.test_mode.should eql false
124
+ expect(Mailgun.test_mode).to eql false
119
125
  end
120
126
 
121
127
  it "allows me to set my domain easily" do
122
- Mailgun.domain.should eql 'some-domain'
128
+ expect(Mailgun.domain).to eql 'some-domain'
123
129
  end
124
130
  end
125
131
  end
@@ -17,10 +17,10 @@ describe Mailgun::Bounce do
17
17
  sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.mailgun.org\" } ]}"
18
18
  bounces_url = @mailgun.bounces(@sample[:domain]).send(:bounce_url)
19
19
 
20
- Mailgun.should_receive(:submit).
20
+ expect(Mailgun).to receive(:submit).
21
21
  with(:get, bounces_url, {}).
22
22
  and_return(sample_response)
23
-
23
+
24
24
  @mailgun.bounces(@sample[:domain]).list
25
25
  end
26
26
  end
@@ -30,7 +30,7 @@ describe Mailgun::Bounce do
30
30
  sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.mailgun.org\" } ]}"
31
31
  bounces_url = @mailgun.bounces(@sample[:domain]).send(:bounce_url, @sample[:email])
32
32
 
33
- Mailgun.should_receive(:submit).
33
+ expect(Mailgun).to receive(:submit).
34
34
  with(:get, bounces_url).
35
35
  and_return(sample_response)
36
36
 
@@ -43,7 +43,7 @@ describe Mailgun::Bounce do
43
43
  sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.mailgun.org\" } ]}"
44
44
  bounces_url = @mailgun.bounces(@sample[:domain]).send(:bounce_url)
45
45
 
46
- Mailgun.should_receive(:submit).
46
+ expect(Mailgun).to receive(:submit).
47
47
  with(:post, bounces_url, {:address => @sample[:email]} ).
48
48
  and_return(sample_response)
49
49
 
@@ -56,7 +56,7 @@ describe Mailgun::Bounce do
56
56
  sample_response = "{\"message\"=>\"Bounced address has been removed\", \"address\"=>\"postmaster@bsample.mailgun.org\"}"
57
57
  bounces_url = @mailgun.bounces(@sample[:domain]).send(:bounce_url, @sample[:email])
58
58
 
59
- Mailgun.should_receive(:submit).
59
+ expect(Mailgun).to receive(:submit).
60
60
  with(:delete, bounces_url).
61
61
  and_return(sample_response)
62
62
 
@@ -0,0 +1,118 @@
1
+ require 'spec_helper'
2
+
3
+ require 'webmock/rspec'
4
+
5
+ describe Mailgun::Client do
6
+ subject { described_class.new(url) }
7
+
8
+ describe '#get' do
9
+ context 'without query params' do
10
+ let(:url) { 'https://api:key@api.mailgun.net/v3/routes' }
11
+
12
+ it 'sends a GET request to the given path' do
13
+ stub = stub_request(:get, 'https://api.mailgun.net/v3/routes')
14
+ .with(basic_auth: ['api', 'key'])
15
+
16
+ subject.get
17
+
18
+ expect(stub).to have_been_requested
19
+ end
20
+ end
21
+
22
+ context 'with query params' do
23
+ let(:url) { 'https://api:key@api.mailgun.net/v3/routes' }
24
+
25
+ it 'sends a GET request to the given path with the params' do
26
+ stub = stub_request(:get, 'https://api.mailgun.net/v3/routes?limit=10')
27
+ .with(basic_auth: ['api', 'key'])
28
+
29
+ subject.get(limit: 10)
30
+
31
+ expect(stub).to have_been_requested
32
+ end
33
+ end
34
+
35
+ context 'when an error happens' do
36
+ let(:url) { 'https://api:key@api.mailgun.net/v3/routes/123' }
37
+ let(:error_body) { { "message" => "Expression is missing" }.to_json }
38
+
39
+ before do
40
+ stub_request(:get, 'https://api.mailgun.net/v3/routes/123')
41
+ .with(basic_auth: ['api', 'key'])
42
+ .to_return(status: [400, "Bad Request"],
43
+ body: error_body)
44
+ end
45
+
46
+ it 'raises exception that contains the error code and body' do
47
+ begin
48
+ subject.get
49
+ rescue => e
50
+ @exception = e
51
+ end
52
+
53
+ expect(@exception.http_code).to eq(400)
54
+ expect(@exception.http_body).to eq(error_body)
55
+ end
56
+ end
57
+ end
58
+
59
+ describe '#post' do
60
+ let(:url) { 'https://api:key@api.mailgun.net/v3/routes' }
61
+ let(:params) { { action: ['forward', 'stop'], description: 'yolo' } }
62
+ let(:response) do
63
+ {
64
+ "message" => "Route has been created",
65
+ "route" => {
66
+ "actions" => [
67
+ "forward(\"stefan@metrilo.com\")",
68
+ "stop()"
69
+ ],
70
+ "created_at" => "Wed, 15 Jun 2016 07:10:09 GMT",
71
+ "description" => "Sample route",
72
+ "expression" => "match_recipient(\".*@metrilo.com\")",
73
+ "id" => "5760ff5163badc3a756f9d2c",
74
+ "priority" => 5
75
+ }
76
+ }
77
+ end
78
+
79
+ before do
80
+ stub_request(:post, 'https://api.mailgun.net/v3/routes')
81
+ .with(basic_auth: ['api', 'key'],
82
+ body: 'action=forward&action=stop&description=yolo')
83
+ .to_return(body: response.to_json)
84
+ end
85
+
86
+ it 'sends a POST request with the params form-encoded' do
87
+ expect(subject.post(params)).to eq(response.to_json)
88
+ end
89
+ end
90
+
91
+ describe '#put' do
92
+ let(:url) { 'https://api:key@api.mailgun.net/v3/routes/123' }
93
+
94
+ it 'sends a PUT request with the params form-encoded' do
95
+ stub = stub_request(:put, 'https://api.mailgun.net/v3/routes/123')
96
+ .with(basic_auth: ['api', 'key'],
97
+ body: 'action=forward&action=stop&description=yolo')
98
+
99
+ subject.put(action: ['forward', 'stop'],
100
+ description: 'yolo')
101
+
102
+ expect(stub).to have_been_requested
103
+ end
104
+ end
105
+
106
+ describe '#delete' do
107
+ let(:url) { 'https://api:key@api.mailgun.net/v3/routes/123' }
108
+
109
+ it 'sends a DELETE request with the params form-encoded' do
110
+ stub = stub_request(:delete, 'https://api.mailgun.net/v3/routes/123')
111
+ .with(basic_auth: ['api', 'key'])
112
+
113
+ subject.delete
114
+
115
+ expect(stub).to have_been_requested
116
+ end
117
+ end
118
+ end
@@ -29,7 +29,7 @@ EOF
29
29
 
30
30
  complaints_url = @mailgun.complaints(@sample[:domain]).send(:complaint_url)
31
31
 
32
- Mailgun.should_receive(:submit).
32
+ expect(Mailgun).to receive(:submit).
33
33
  with(:get, complaints_url, {}).
34
34
  and_return(sample_response)
35
35
 
@@ -49,7 +49,7 @@ EOF
49
49
 
50
50
  complaints_url = @mailgun.complaints(@sample[:domain]).send(:complaint_url)
51
51
 
52
- Mailgun.should_receive(:submit)
52
+ expect(Mailgun).to receive(:submit)
53
53
  .with(:post, complaints_url, {:address => @sample[:email]})
54
54
  .and_return(sample_response)
55
55
 
@@ -72,7 +72,7 @@ EOF
72
72
 
73
73
  complaints_url = @mailgun.complaints(@sample[:domain]).send(:complaint_url, @sample[:email])
74
74
 
75
- Mailgun.should_receive(:submit)
75
+ expect(Mailgun).to receive(:submit)
76
76
  .with(:get, complaints_url)
77
77
  .and_return(sample_response)
78
78
 
@@ -92,7 +92,7 @@ EOF
92
92
 
93
93
  complaints_url = @mailgun.complaints(@sample[:domain]).send(:complaint_url, @sample[:email])
94
94
 
95
- Mailgun.should_receive(:submit)
95
+ expect(Mailgun).to receive(:submit)
96
96
  .with(:delete, complaints_url)
97
97
  .and_return(sample_response)
98
98
 
@@ -18,10 +18,10 @@ describe Mailgun::Domain do
18
18
  sample_response = "{\"total_count\": 1, \"items\": [{\"created_at\": \"Tue, 12 Feb 2013 20:13:49 GMT\", \"smtp_login\": \"postmaster@sample.mailgun.org\", \"name\": \"sample.mailgun.org\", \"smtp_password\": \"67bw67bz7w\" }]}"
19
19
  domains_url = @mailgun.domains.send(:domain_url)
20
20
 
21
- Mailgun.should_receive(:submit).
21
+ expect(Mailgun).to receive(:submit).
22
22
  with(:get, domains_url, {}).
23
23
  and_return(sample_response)
24
-
24
+
25
25
  @mailgun.domains.list
26
26
  end
27
27
  end
@@ -31,7 +31,7 @@ describe Mailgun::Domain do
31
31
  sample_response = "{\"domain\": {\"created_at\": \"Tue, 12 Feb 2013 20:13:49 GMT\", \"smtp_login\": \"postmaster@bample.mailgun.org\", \"name\": \"sample.mailgun.org\", \"smtp_password\": \"67bw67bz7w\" }, \"receiving_dns_records\": [], \"sending_dns_records\": []}"
32
32
  domains_url = @mailgun.domains.send(:domain_url, @sample[:domain])
33
33
 
34
- Mailgun.should_receive(:submit).
34
+ expect(Mailgun).to receive(:submit).
35
35
  with(:get, domains_url).
36
36
  and_return(sample_response)
37
37
 
@@ -44,7 +44,7 @@ describe Mailgun::Domain do
44
44
  sample_response = "{\"domain\": {\"created_at\": \"Tue, 12 Feb 2013 20:13:49 GMT\", \"smtp_login\": \"postmaster@sample.mailgun.org\",\"name\": \"sample.mailgun.org\",\"smtp_password\": \"67bw67bz7w\"}, \"message\": \"Domain has been created\"}"
45
45
  domains_url = @mailgun.domains.send(:domain_url)
46
46
 
47
- Mailgun.should_receive(:submit).
47
+ expect(Mailgun).to receive(:submit).
48
48
  with(:post, domains_url, {:name => @sample[:domain]} ).
49
49
  and_return(sample_response)
50
50
 
@@ -57,7 +57,7 @@ describe Mailgun::Domain do
57
57
  sample_response = "{\"message\": \"Domain has been deleted\"}"
58
58
  domains_url = @mailgun.domains.send(:domain_url, @sample[:domain])
59
59
 
60
- Mailgun.should_receive(:submit).
60
+ expect(Mailgun).to receive(:submit).
61
61
  with(:delete, domains_url).
62
62
  and_return(sample_response)
63
63
 
@@ -65,4 +65,16 @@ describe Mailgun::Domain do
65
65
  end
66
66
  end
67
67
 
68
+ describe 'verify domain' do
69
+ it 'should make a PUT request to verify with correct params' do
70
+ sample_response = "{\"domain\": {\"created_at\": \"Tue, 12 Feb 2013 20:13:49 GMT\", \"smtp_login\": \"postmaster@bample.mailgun.org\", \"name\": \"sample.mailgun.org\", \"smtp_password\": \"67bw67bz7w\", \"state\": \"active\"}, \"message\": \"Domain DNS records have been updated\", \"receiving_dns_records\": [], \"sending_dns_records\": []}"
71
+ verify_domain_url = "#{@mailgun.domains.send(:domain_url, @sample[:domain])}/verify"
72
+
73
+ expect(Mailgun).to receive(:submit)
74
+ .with(:put, verify_domain_url)
75
+ .and_return(sample_response)
76
+
77
+ @mailgun.domains.verify(@sample[:domain])
78
+ end
79
+ end
68
80
  end
@@ -0,0 +1,9 @@
1
+ module MailgunHelper
2
+ def generate_request_auth(api_key, offset=0)
3
+ timestamp = Time.now.to_i + offset * 60
4
+ token = ([nil]*50).map { ((48..57).to_a+(65..90).to_a+(97..122).to_a).sample.chr }.join
5
+ signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), api_key, '%s%s' % [timestamp, token])
6
+
7
+ return timestamp, token, signature
8
+ end
9
+ end
@@ -18,10 +18,10 @@ describe Mailgun::MailingList::Member do
18
18
  sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.mailgun.org\" } ]}"
19
19
  mailing_list_members_url = @mailgun.list_members(@sample[:list_email]).send(:list_member_url)
20
20
 
21
- Mailgun.should_receive(:submit).
21
+ expect(Mailgun).to receive(:submit).
22
22
  with(:get, mailing_list_members_url, {}).
23
23
  and_return(sample_response)
24
-
24
+
25
25
  @mailgun.list_members(@sample[:list_email]).list
26
26
  end
27
27
  end
@@ -30,8 +30,8 @@ describe Mailgun::MailingList::Member do
30
30
  it "should make a GET request with correct params to find given email address" do
31
31
  sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.mailgun.org\" } ]}"
32
32
  mailing_list_members_url = @mailgun.list_members(@sample[:list_email]).send(:list_member_url, @sample[:email])
33
-
34
- Mailgun.should_receive(:submit).
33
+
34
+ expect(Mailgun).to receive(:submit).
35
35
  with(:get, mailing_list_members_url).
36
36
  and_return(sample_response)
37
37
 
@@ -44,7 +44,7 @@ describe Mailgun::MailingList::Member do
44
44
  sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.mailgun.org\" } ]}"
45
45
  mailing_list_members_url = @mailgun.list_members(@sample[:list_email]).send(:list_member_url)
46
46
 
47
- Mailgun.should_receive(:submit).
47
+ expect(Mailgun).to receive(:submit).
48
48
  with(:post, mailing_list_members_url, {
49
49
  :address => @sample[:email]
50
50
  }).
@@ -57,7 +57,7 @@ describe Mailgun::MailingList::Member do
57
57
  describe "update member in list" do
58
58
  it "should make a PUT request with correct params" do
59
59
  sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.mailgun.org\" } ]}"
60
- Mailgun.should_receive(:submit).
60
+ expect(Mailgun).to receive(:submit).
61
61
  with(:put, "#{@mailgun.list_members(@sample[:list_email]).send(:list_member_url, @sample[:email])}", {
62
62
  :address => @sample[:email]
63
63
  }).
@@ -71,7 +71,7 @@ describe Mailgun::MailingList::Member do
71
71
  it "should make a DELETE request with correct params" do
72
72
  sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.mailgun.org\" } ]}"
73
73
  mailing_list_members_url = @mailgun.list_members(@sample[:list_email]).send(:list_member_url, @sample[:email])
74
- Mailgun.should_receive(:submit).
74
+ expect(Mailgun).to receive(:submit).
75
75
  with(:delete, mailing_list_members_url).
76
76
  and_return(sample_response)
77
77
 
@@ -16,7 +16,7 @@ describe Mailgun::Log do
16
16
  it "should make a POST request to send an email" do
17
17
 
18
18
  sample_response = "{\"items\": [{\"size_bytes\": 0, \"mailbox\": \"postmaster@bsample.mailgun.org\" } ]}"
19
- Mailgun.should_receive(:submit)
19
+ expect(Mailgun).to receive(:submit)
20
20
  .with(:get, "#{@mailgun.lists.send(:list_url, @sample[:list_email])}")
21
21
  .and_return(sample_response)
22
22
 
@@ -29,7 +29,7 @@ describe Mailgun::Log do
29
29
  :text => "yeah, we're gonna need you to come in on friday...yeah.",
30
30
  :from => "lumberg.bill@initech.mailgun.domain"
31
31
  }
32
- Mailgun.should_receive(:submit) \
32
+ expect(Mailgun).to receive(:submit) \
33
33
  .with(:post, @mailgun.messages.messages_url, parameters) \
34
34
  .and_return(sample_response)
35
35
 
@@ -37,4 +37,4 @@ describe Mailgun::Log do
37
37
  end
38
38
  end
39
39
 
40
- end
40
+ end