postageapp 0.0.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/.gitignore +3 -0
  2. data/LICENSE +1 -1
  3. data/README.md +176 -0
  4. data/Rakefile +14 -12
  5. data/VERSION +1 -1
  6. data/generators/postageapp/postageapp_generator.rb +34 -0
  7. data/generators/postageapp/templates/initializer.rb +3 -0
  8. data/generators/postageapp/templates/postageapp_tasks.rake +78 -0
  9. data/lib/generators/postageapp/postageapp_generator.rb +27 -0
  10. data/lib/postageapp.rb +56 -0
  11. data/lib/postageapp/configuration.rb +91 -0
  12. data/lib/postageapp/failed_request.rb +64 -0
  13. data/lib/postageapp/logger.rb +16 -0
  14. data/lib/postageapp/mailer.rb +48 -0
  15. data/lib/postageapp/mailer/mailer_2.rb +92 -0
  16. data/lib/postageapp/mailer/mailer_3.rb +192 -0
  17. data/lib/postageapp/rails.rb +14 -0
  18. data/lib/postageapp/request.rb +87 -0
  19. data/lib/postageapp/response.rb +39 -0
  20. data/lib/postageapp/utils.rb +30 -0
  21. data/lib/postageapp/version.rb +7 -0
  22. data/postageapp.gemspec +61 -7
  23. data/test/configuration_test.rb +66 -0
  24. data/test/failed_request_test.rb +79 -0
  25. data/test/helper.rb +66 -0
  26. data/test/mailer/action_mailer_2/notifier.rb +64 -0
  27. data/test/mailer/action_mailer_2/notifier/with_body_and_attachment.erb +1 -0
  28. data/test/mailer/action_mailer_2/notifier/with_html_and_text_views.text.html.erb +1 -0
  29. data/test/mailer/action_mailer_2/notifier/with_html_and_text_views.text.plain.erb +1 -0
  30. data/test/mailer/action_mailer_2/notifier/with_simple_view.erb +1 -0
  31. data/test/mailer/action_mailer_2/notifier/with_text_only_view.text.plain.erb +1 -0
  32. data/test/mailer/action_mailer_3/notifier.rb +98 -0
  33. data/test/mailer/action_mailer_3/notifier/with_html_and_text_views.html.erb +1 -0
  34. data/test/mailer/action_mailer_3/notifier/with_html_and_text_views.text.erb +1 -0
  35. data/test/mailer/action_mailer_3/notifier/with_old_api.html.erb +1 -0
  36. data/test/mailer/action_mailer_3/notifier/with_old_api.text.erb +1 -0
  37. data/test/mailer/action_mailer_3/notifier/with_simple_view.erb +1 -0
  38. data/test/mailer/action_mailer_3/notifier/with_text_only_view.text.erb +1 -0
  39. data/test/mailer_2_test.rb +87 -0
  40. data/test/mailer_3_test.rb +104 -0
  41. data/test/mailer_helper_methods_test.rb +24 -0
  42. data/test/postageapp_test.rb +18 -0
  43. data/test/rails_initialization_test.rb +29 -0
  44. data/test/request_integration_test.rb +78 -0
  45. data/test/request_test.rb +81 -0
  46. data/test/response_test.rb +40 -0
  47. metadata +84 -9
  48. data/README.rdoc +0 -17
  49. data/test/test_postageapp.rb +0 -7
@@ -0,0 +1,64 @@
1
+ # Test mailer for ActionMailer 2
2
+ class Notifier < PostageApp::Mailer
3
+
4
+ self.template_root = File.expand_path('../', __FILE__)
5
+
6
+ def blank
7
+ # ... nothing to see here
8
+ end
9
+
10
+ def with_no_content
11
+ setup_headers
12
+ end
13
+
14
+ def with_text_only_view
15
+ setup_headers
16
+ end
17
+
18
+ def with_html_and_text_views
19
+ setup_headers
20
+ end
21
+
22
+ def with_simple_view
23
+ setup_headers
24
+ end
25
+
26
+ def with_manual_parts
27
+ setup_headers
28
+ part :content_type => 'text/html',
29
+ :body => 'html content'
30
+ part :content_type => 'text/plain',
31
+ :body => 'text content'
32
+ attachment :content_type => 'image/jpeg',
33
+ :filename => 'foo.jpg',
34
+ :body => '123456789'
35
+ end
36
+
37
+ def with_body_and_attachment
38
+ setup_headers
39
+ attachment :content_type => 'image/jpeg',
40
+ :filename => 'foo.jpg',
41
+ :body => '123456789'
42
+ end
43
+
44
+ def with_custom_postage_variables
45
+ from 'test@test.test'
46
+ subject 'Test Email'
47
+
48
+ recipients ({
49
+ 'test1@test.text' => { 'name' => 'Test 1' },
50
+ 'test2@test.text' => { 'name' => 'Test 2' }
51
+ })
52
+ postage_template 'test_template'
53
+ postage_variables 'variable' => 'value'
54
+ end
55
+
56
+ private
57
+
58
+ def setup_headers
59
+ recipients 'test@test.test'
60
+ from 'text@test.test'
61
+ subject 'Test Email'
62
+ end
63
+
64
+ end
@@ -0,0 +1,98 @@
1
+ # Test mailer for ActionMailer 3
2
+ class Notifier < PostageApp::Mailer
3
+
4
+ self.append_view_path(File.expand_path('../', __FILE__))
5
+
6
+ def blank
7
+ # ... nothing to see here
8
+ end
9
+
10
+ def with_no_content
11
+ mail(headers_hash)
12
+ end
13
+
14
+ def with_text_only_view
15
+ mail(headers_hash)
16
+ end
17
+
18
+ def with_html_and_text_views
19
+ mail(headers_hash) do |format|
20
+ format.text
21
+ format.html
22
+ end
23
+ end
24
+
25
+ def with_simple_view
26
+ mail(headers_hash)
27
+ end
28
+
29
+ def with_body_and_attachment_as_file
30
+ attachments['sample_file.txt'] = 'File content'
31
+ mail(headers_hash) do |format|
32
+ format.html { render :text => 'manual body text'}
33
+ end
34
+ end
35
+
36
+ def with_body_and_attachment_as_hash
37
+ attachments['sample_file.txt'] = {
38
+ :content_type => 'text/rich',
39
+ :body => 'File content'
40
+ }
41
+ mail(headers_hash) do |format|
42
+ format.html { render :text => 'manual body text'}
43
+ end
44
+ end
45
+
46
+ def with_custom_postage_variables
47
+ postage_template 'test_template'
48
+ postage_variables 'variable' => 'value'
49
+
50
+ mail(
51
+ :from => 'test@test.test',
52
+ :subject => 'Test Message',
53
+ :to => {
54
+ 'test1@test.test' => { 'name' => 'Test 1' },
55
+ 'test2@test.test' => { 'name' => 'Test 2' }
56
+ }
57
+ )
58
+ end
59
+
60
+ def with_old_api
61
+ from 'test@test.test'
62
+ subject 'Test Email'
63
+ recipients 'test@test.test'
64
+ end
65
+
66
+ def with_old_api_and_manual_parts
67
+ from 'test@test.test'
68
+ subject 'Test Email'
69
+
70
+ headers ({'custom_header' => 'value'})
71
+
72
+ recipients ({
73
+ 'test1@test.test' => { 'name' => 'Test 1' },
74
+ 'test2@test.test' => { 'name' => 'Test 2' }
75
+ })
76
+
77
+ part :content_type => 'text/html',
78
+ :body => 'html content'
79
+ part :content_type => 'text/plain',
80
+ :body => 'text content'
81
+ attachment :content_type => 'image/jpeg',
82
+ :filename => 'foo.jpg',
83
+ :body => '123456789'
84
+
85
+ postage_template 'test_template'
86
+ postage_variables 'variable' => 'value'
87
+ end
88
+
89
+ private
90
+
91
+ def headers_hash(options = {})
92
+ { :from => 'sender@test.test',
93
+ :subject => 'Test Message',
94
+ :to => 'test@test.test'
95
+ }.merge(options)
96
+ end
97
+
98
+ end
@@ -0,0 +1,87 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ # tests for ActionMailer bundled with Rails 2
4
+ class Mailer3Test < Test::Unit::TestCase
5
+
6
+ if ActionMailer::VERSION::MAJOR < 3
7
+
8
+ require File.expand_path('../mailer/action_mailer_2/notifier', __FILE__)
9
+ puts "\e[0m\e[32mRunning #{File.basename(__FILE__)} for action_mailer #{ActionMailer::VERSION::STRING}\e[0m"
10
+
11
+ def test_create_blank
12
+ assert mail = Notifier.create_blank
13
+ assert_equal :send_message, mail.method
14
+ assert_equal 'https://api.postageapp.com/v.1.0/send_message.json', mail.url.to_s
15
+ assert mail.arguments.blank?
16
+ end
17
+
18
+ def test_create_with_no_content
19
+ assert mail = Notifier.create_with_no_content
20
+ assert_equal 'test@test.test', mail.arguments['recipients']
21
+ assert_equal ({ 'from' => 'text@test.test', 'subject' => 'Test Email' }), mail.arguments['headers']
22
+ assert mail.arguments['content'].blank?
23
+ end
24
+
25
+ def test_create_with_text_only_view
26
+ assert mail = Notifier.create_with_text_only_view
27
+ assert_equal 'text only: plain text', mail.arguments['content']['text/plain']
28
+ end
29
+
30
+ def test_create_with_html_and_text_views
31
+ assert mail = Notifier.create_with_html_and_text_views
32
+ assert_equal 'html and text: plain text', mail.arguments['content']['text/plain']
33
+ assert_equal 'html and text: html', mail.arguments['content']['text/html']
34
+ end
35
+
36
+ def test_deliver_with_html_and_text_views
37
+ mock_successful_send
38
+
39
+ assert response = Notifier.deliver_with_html_and_text_views
40
+ assert response.is_a?(PostageApp::Response)
41
+ assert response.ok?
42
+ end
43
+
44
+ def test_create_with_simple_view
45
+ assert mail = Notifier.create_with_simple_view
46
+ assert_equal 'simple view content', mail.arguments['content']['text/plain']
47
+ end
48
+
49
+ def test_create_with_manual_parts
50
+ assert mail = Notifier.create_with_manual_parts
51
+ assert_equal 'text content', mail.arguments['content']['text/plain']
52
+ assert_equal 'html content', mail.arguments['content']['text/html']
53
+ assert !mail.arguments['attachments'].blank?
54
+ assert !mail.arguments['attachments']['foo.jpg']['content'].blank?
55
+ assert_equal 'image/jpeg', mail.arguments['attachments']['foo.jpg']['content_type']
56
+ end
57
+
58
+ def test_create_with_body_and_attachment
59
+ assert mail = Notifier.create_with_body_and_attachment
60
+ assert !mail.arguments['content'].blank?
61
+ assert !mail.arguments['content']['text/plain'].blank?
62
+ assert_equal 'body text', mail.arguments['content']['text/plain']
63
+ assert !mail.arguments['attachments'].blank?
64
+ assert !mail.arguments['attachments']['foo.jpg']['content'].blank?
65
+ assert_equal 'image/jpeg', mail.arguments['attachments']['foo.jpg']['content_type']
66
+ end
67
+
68
+ def test_create_with_custom_postage_variables
69
+ assert mail = Notifier.create_with_custom_postage_variables
70
+ assert_equal 'test_template', mail.arguments['template']
71
+ assert_equal ({ 'variable' => 'value' }), mail.arguments['variables']
72
+ assert_equal ({ 'test2@test.text' => { 'name' => 'Test 2'},
73
+ 'test1@test.text' => { 'name' => 'Test 1'}}), mail.arguments['recipients']
74
+ end
75
+
76
+ def test_create_with_recipient_override
77
+ PostageApp.configuration.recipient_override = 'oleg@test.test'
78
+ assert mail = Notifier.create_with_html_and_text_views
79
+ assert_equal 'test@test.test', mail.arguments['recipients']
80
+ assert_equal 'oleg@test.test', mail.arguments_to_send['arguments']['recipient_override']
81
+ end
82
+
83
+ else
84
+ puts "\e[0m\e[31mSkipping #{File.basename(__FILE__)}\e[0m"
85
+ def test_nothing ; end
86
+ end
87
+ end
@@ -0,0 +1,104 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ # tests for ActionMailer bundled with Rails 3
4
+ class Mailer3Test < Test::Unit::TestCase
5
+
6
+ if ActionMailer::VERSION::MAJOR >= 3
7
+
8
+ require File.expand_path('../mailer/action_mailer_3/notifier', __FILE__)
9
+ puts "\e[0m\e[32mRunning #{File.basename(__FILE__)} for action_mailer #{ActionMailer::VERSION::STRING}\e[0m"
10
+
11
+ def test_create_blank
12
+ mail = Notifier.blank
13
+ assert mail.is_a?(PostageApp::Request)
14
+ end
15
+
16
+ def test_create_with_no_content
17
+ mail = Notifier.with_no_content
18
+ assert_equal ({}), mail.arguments['content']
19
+ end
20
+
21
+ def test_create_with_simple_view
22
+ mail = Notifier.with_simple_view
23
+ assert_equal 'simple view content', mail.arguments['content']['text/html']
24
+ end
25
+
26
+ def test_create_with_text_only_view
27
+ mail = Notifier.with_text_only_view
28
+ assert_equal 'text content', mail.arguments['content']['text/plain']
29
+ end
30
+
31
+ def test_create_with_html_and_text_views
32
+ mail = Notifier.with_html_and_text_views
33
+ assert_equal 'text content', mail.arguments['content']['text/plain']
34
+ assert_equal 'html content', mail.arguments['content']['text/html']
35
+ end
36
+
37
+ def test_deliver_with_html_and_text_views
38
+ mock_successful_send
39
+
40
+ assert response = Notifier.with_html_and_text_views.deliver
41
+ assert response.is_a?(PostageApp::Response)
42
+ assert response.ok?
43
+ end
44
+
45
+ def test_create_with_body_and_attachment_as_file
46
+ mail = Notifier.with_body_and_attachment_as_file
47
+ assert_equal 'manual body text', mail.arguments['content']['text/html']
48
+ assert_equal 'text/plain', mail.arguments['attachments']['sample_file.txt']['content_type']
49
+ assert_equal "RmlsZSBjb250ZW50\n", mail.arguments['attachments']['sample_file.txt']['content']
50
+ end
51
+
52
+ def test_create_with_body_and_attachment_as_hash
53
+ mail = Notifier.with_body_and_attachment_as_hash
54
+ assert_equal 'manual body text', mail.arguments['content']['text/html']
55
+ assert_equal 'text/rich', mail.arguments['attachments']['sample_file.txt']['content_type']
56
+ assert_equal "RmlsZSBjb250ZW50\n", mail.arguments['attachments']['sample_file.txt']['content']
57
+ end
58
+
59
+ def test_create_with_custom_postage_variables
60
+ mail = Notifier.with_custom_postage_variables
61
+ assert_equal ({
62
+ 'test1@test.test' => { 'name' => 'Test 1'},
63
+ 'test2@test.test' => { 'name' => 'Test 2'}
64
+ }), mail.arguments['recipients']
65
+ assert_equal 'test_template', mail.arguments['template']
66
+ assert_equal ({ 'variable' => 'value' }), mail.arguments['variables']
67
+ end
68
+
69
+ def test_create_with_old_api
70
+ mail = Notifier.with_old_api
71
+ assert_equal 'test@test.test', mail.arguments['headers']['from']
72
+ assert_equal 'Test Email', mail.arguments['headers']['subject']
73
+ assert_equal 'test@test.test', mail.arguments['recipients']
74
+ assert_equal 'html content', mail.arguments['content']['text/html']
75
+ assert_equal 'text content', mail.arguments['content']['text/plain']
76
+ end
77
+
78
+ def test_create_with_old_api_and_manual_parts
79
+ mail = Notifier.with_old_api_and_manual_parts
80
+ assert_equal ({
81
+ 'test2@test.test' => { 'name' => 'Test 2'},
82
+ 'test1@test.test' => { 'name' => 'Test 1'}
83
+ }), mail.arguments['recipients']
84
+ assert_equal 'test@test.test', mail.arguments['headers']['from']
85
+ assert_equal 'Test Email', mail.arguments['headers']['subject']
86
+ assert_equal 'value', mail.arguments['headers']['custom_header']
87
+ assert_equal 'html content', mail.arguments['content']['text/html']
88
+ assert_equal 'text content', mail.arguments['content']['text/plain']
89
+ assert_equal 'image/jpeg', mail.arguments['attachments']['foo.jpg']['content_type']
90
+ assert_equal "MTIzNDU2Nzg5\n", mail.arguments['attachments']['foo.jpg']['content']
91
+ end
92
+
93
+ def test_create_with_recipient_override
94
+ PostageApp.configuration.recipient_override = 'oleg@test.test'
95
+ assert mail = Notifier.with_html_and_text_views
96
+ assert_equal 'test@test.test', mail.arguments['recipients']
97
+ assert_equal 'oleg@test.test', mail.arguments_to_send['arguments']['recipient_override']
98
+ end
99
+
100
+ else
101
+ puts "\e[0m\e[31mSkipping #{File.basename(__FILE__)}\e[0m"
102
+ def test_nothing ; end
103
+ end
104
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class MailerHelperMethodsTest < Test::Unit::TestCase
4
+
5
+ def test_mailer_helper_methods
6
+ request = PostageApp::Request.new(:send_message, {
7
+ :headers => { 'from' => 'sender@test.test',
8
+ 'subject' => 'Test Message'},
9
+ :recipients => 'test@test.test',
10
+ :content => {
11
+ 'text/plain' => 'text content',
12
+ 'text/html' => 'html content'
13
+ }
14
+ })
15
+ assert_equal 'test@test.test', request.to
16
+ assert_equal 'sender@test.test', request.from
17
+ assert_equal 'Test Message', request.subject
18
+ assert_equal ({
19
+ 'text/html' => 'html content',
20
+ 'text/plain' => 'text content'
21
+ }), request.body
22
+ end
23
+
24
+ end
@@ -0,0 +1,18 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ class PostageAppTest < Test::Unit::TestCase
4
+
5
+ def test_method_configure
6
+ PostageApp.configure do |config|
7
+ config.api_key = 'abcdefg12345'
8
+ config.host = 'test.test'
9
+ end
10
+ assert_equal 'abcdefg12345', PostageApp.configuration.api_key
11
+ assert_equal 'test.test', PostageApp.configuration.host
12
+ end
13
+
14
+ def test_logger
15
+ assert PostageApp.logger.is_a?(Logger)
16
+ end
17
+
18
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path('../helper', __FILE__)
2
+
3
+ require 'postageapp/rails'
4
+
5
+ class RailsInitializationTest < Test::Unit::TestCase
6
+
7
+ include ConstantDefinitions
8
+
9
+ def test_initialization
10
+ rails = Module.new do
11
+ def self.version
12
+ '9.9.9'
13
+ end
14
+ def self.root
15
+ 'RAILS ROOT'
16
+ end
17
+ def self.env
18
+ "RAILS ENV"
19
+ end
20
+ end
21
+ define_constant('Rails', rails)
22
+ PostageApp::Rails.initialize
23
+
24
+ assert_equal 'Rails 9.9.9', PostageApp.configuration.framework
25
+ assert_equal 'RAILS ROOT', PostageApp.configuration.project_root
26
+ assert_equal 'RAILS ENV', PostageApp.configuration.environment
27
+ end
28
+
29
+ end