action_smser 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +191 -0
  3. data/Rakefile +39 -0
  4. data/app/assets/javascripts/action_smser/delivery_reports.js +2 -0
  5. data/app/assets/stylesheets/action_smser/application.css +7 -0
  6. data/app/assets/stylesheets/action_smser/delivery_reports.css +4 -0
  7. data/app/assets/stylesheets/scaffold.css +56 -0
  8. data/app/controllers/action_smser/application_controller.rb +4 -0
  9. data/app/controllers/action_smser/delivery_reports_controller.rb +63 -0
  10. data/app/models/action_smser/delivery_report.rb +30 -0
  11. data/app/views/action_smser/delivery_reports/index.html.erb +143 -0
  12. data/app/views/layouts/action_smser/delivery_reports.html.erb +56 -0
  13. data/config/routes.rb +7 -0
  14. data/db/migrate/20120102215215_create_action_smser_delivery_reports.rb +19 -0
  15. data/lib/action_smser.rb +24 -0
  16. data/lib/action_smser/base.rb +133 -0
  17. data/lib/action_smser/delivery_methods/simple_http.rb +70 -0
  18. data/lib/action_smser/delivery_methods/test_array.rb +30 -0
  19. data/lib/action_smser/engine.rb +5 -0
  20. data/lib/action_smser/version.rb +3 -0
  21. data/lib/tasks/action_smser_tasks.rake +4 -0
  22. data/test/action_smser_test.rb +7 -0
  23. data/test/dummy/Rakefile +7 -0
  24. data/test/dummy/app/assets/javascripts/application.js +7 -0
  25. data/test/dummy/app/assets/stylesheets/application.css +7 -0
  26. data/test/dummy/app/controllers/application_controller.rb +3 -0
  27. data/test/dummy/app/helpers/application_helper.rb +2 -0
  28. data/test/dummy/app/mailers/hello_smser.rb +9 -0
  29. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  30. data/test/dummy/config.ru +4 -0
  31. data/test/dummy/config/application.rb +51 -0
  32. data/test/dummy/config/boot.rb +10 -0
  33. data/test/dummy/config/database.yml +26 -0
  34. data/test/dummy/config/environment.rb +5 -0
  35. data/test/dummy/config/environments/development.rb +30 -0
  36. data/test/dummy/config/environments/production.rb +60 -0
  37. data/test/dummy/config/environments/test.rb +39 -0
  38. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  39. data/test/dummy/config/initializers/git_ignored_passwords.rb +10 -0
  40. data/test/dummy/config/initializers/inflections.rb +10 -0
  41. data/test/dummy/config/initializers/mime_types.rb +5 -0
  42. data/test/dummy/config/initializers/secret_token.rb +7 -0
  43. data/test/dummy/config/initializers/session_store.rb +8 -0
  44. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  45. data/test/dummy/config/locales/en.yml +5 -0
  46. data/test/dummy/config/routes.rb +4 -0
  47. data/test/dummy/db/development.sqlite3 +0 -0
  48. data/test/dummy/db/schema.rb +28 -0
  49. data/test/dummy/db/test.sqlite3 +0 -0
  50. data/test/dummy/log/development.log +540 -0
  51. data/test/dummy/log/test.log +18246 -0
  52. data/test/dummy/public/404.html +26 -0
  53. data/test/dummy/public/422.html +26 -0
  54. data/test/dummy/public/500.html +26 -0
  55. data/test/dummy/public/favicon.ico +0 -0
  56. data/test/dummy/script/rails +6 -0
  57. data/test/functional/action_smser/delivery_reports_controller_test.rb +118 -0
  58. data/test/integration/navigation_test.rb +11 -0
  59. data/test/test_helper.rb +25 -0
  60. data/test/unit/action_smser/base_test.rb +83 -0
  61. data/test/unit/action_smser/delivery_methods/simple_http_test.rb +52 -0
  62. data/test/unit/action_smser/delivery_methods/test_array_test.rb +42 -0
  63. data/test/unit/action_smser/delivery_report_test.rb +54 -0
  64. metadata +212 -0
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ <p>We've been notified about this issue and we'll take a look at it shortly.</p>
24
+ </div>
25
+ </body>
26
+ </html>
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,118 @@
1
+ require 'test_helper'
2
+
3
+ class ActionSmser::DeliveryReportsControllerTest < ActionController::TestCase
4
+
5
+ class SmsTestSetup
6
+ def self.admin_access(controller)
7
+ if controller.session[:admin_logged].blank?
8
+ return controller.session[:admin_logged]
9
+ else
10
+ return true
11
+ end
12
+ end
13
+
14
+ def self.process_delivery_report(params)
15
+ processable_array = []
16
+ if params["DeliveryReport"] && params["DeliveryReport"]["message"]
17
+ reports = params["DeliveryReport"]["message"]
18
+ reports = [reports] unless reports.is_a?(Array)
19
+ reports.each do |report|
20
+ processable_array << {'msg_id' => report['id'], 'status' => report['status']}
21
+ end
22
+ end
23
+
24
+ return processable_array
25
+ end
26
+ end
27
+
28
+ test "gateway_commit with existing dr" do
29
+ @msg_id = "102010314204056202"
30
+ @dr = ActionSmser::DeliveryReport.create(:msg_id => @msg_id, :status => 'ORIGINAL_STATUS')
31
+
32
+ ActionSmser.delivery_options[:gateway_commit] = {'test_gateway' => SmsTestSetup}
33
+
34
+ get 'gateway_commit', :use_route => :action_smser, :gateway => 'test_gateway',
35
+ "DeliveryReport"=>{"message"=>{"id"=>@msg_id, "donedate"=>"2012/01/03 14:20:45", "sentdate"=>"2012/01/03 14:20:40", "status"=>"DELIVERED"}}
36
+
37
+
38
+ assert_response :success
39
+ assert @response.body.downcase.include?("update"), "should have responed about saving"
40
+ @dr.reload
41
+ assert_equal "DELIVERED", @dr.status
42
+ end
43
+
44
+ test "gateway_commit with multiple records" do
45
+ @msg_id = "102010314204056202"
46
+ @msg_id2 = "99999999999999999"
47
+ @dr = ActionSmser::DeliveryReport.create(:msg_id => @msg_id, :status => 'ORIGINAL_STATUS')
48
+ @dr2 = ActionSmser::DeliveryReport.create(:msg_id => @msg_id2, :status => 'ORIGINAL_STATUS')
49
+
50
+ ActionSmser.delivery_options[:gateway_commit] = {'test_gateway' => SmsTestSetup}
51
+
52
+ get 'gateway_commit', :use_route => :action_smser, :gateway => 'test_gateway',
53
+ "DeliveryReport"=>
54
+ {"message"=>[{"id"=>@msg_id.to_s, "status"=>"DELIVERED"},
55
+ {"id"=>@msg_id2, "status"=>"DELIVERED"}]}
56
+
57
+ assert_response :success
58
+ assert @response.body.downcase.include?("update"), "should have responded about saving"
59
+ @dr.reload
60
+ @dr2.reload
61
+ assert_equal "DELIVERED", @dr.status, "should have updated first record"
62
+ assert_equal "DELIVERED", @dr2.status, "should have updated second record"
63
+ end
64
+
65
+
66
+
67
+ test "gateway_commit without dr" do
68
+ ActionSmser.delivery_options[:gateway_commit] = {'test_gateway' => SmsTestSetup}
69
+
70
+ get 'gateway_commit', :use_route => :action_smser, :gateway => 'test_gateway',
71
+ "DeliveryReport"=>{"message"=>{"id"=>"wrongid", "donedate"=>"2012/01/03 14:20:45", "sentdate"=>"2012/01/03 14:20:40", "status"=>"DELIVERED"}}
72
+
73
+ assert_response :success
74
+ assert @response.body =~ /not/i, "should have responded not saved"
75
+ end
76
+
77
+ test "gateway_commit without any parser" do
78
+ get 'gateway_commit', :use_route => :action_smser, :gateway => 'gateway_not_found',
79
+ "DeliveryReport"=>{"message"=>{"id"=>"wrongid", "donedate"=>"2012/01/03 14:20:45", "sentdate"=>"2012/01/03 14:20:40", "status"=>"DELIVERED"}}
80
+
81
+ assert_response :success
82
+ assert @response.body =~ /not/i, "should have responded not saved"
83
+ end
84
+
85
+
86
+ test "index with forbidden admin_access (default access mode)" do
87
+ get 'index', :use_route => :action_smser
88
+ assert_response 403
89
+ assert_template nil
90
+ end
91
+
92
+ test "index with always enabled admin" do
93
+ 2.times do
94
+ ActionSmser::DeliveryReport.create(:msg_id => "idtest_#{rand(10)}")
95
+ end
96
+ ActionSmser::DeliveryReportsController.any_instance.stubs(:admin_access_only).returns(true)
97
+ get 'index', :use_route => :action_smser
98
+ assert_response :success
99
+ assert_template :index
100
+ end
101
+
102
+ test "index with admin_access lambda with right login info" do
103
+ default = ActionSmser.delivery_options[:admin_access]
104
+ ActionSmser.delivery_options[:admin_access] = SmsTestSetup
105
+ 2.times do
106
+ ActionSmser::DeliveryReport.create(:msg_id => "idtest_#{rand(10)}")
107
+ end
108
+ session[:admin_logged] = true
109
+ get 'index', :use_route => :action_smser
110
+
111
+ assert_response :success
112
+ assert_template :index
113
+
114
+ ActionSmser.delivery_options[:admin_access] = default
115
+
116
+ end
117
+
118
+ end
@@ -0,0 +1,11 @@
1
+ require 'test_helper'
2
+
3
+ class NavigationTest < ActionDispatch::IntegrationTest
4
+ fixtures :all
5
+
6
+ test "basic gateway_commit delivery_report" do
7
+ get 'action_smser/delivery_reports/gateway_commit/example_gateway'
8
+ assert_response :success
9
+ end
10
+ end
11
+
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+
3
+ # Configure Rails Environment
4
+ ENV["RAILS_ENV"] = "test"
5
+
6
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
7
+ require "rails/test_help"
8
+ #require 'ruby-debug'
9
+
10
+ Rails.backtrace_cleaner.remove_silencers!
11
+
12
+ # Load support files
13
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
14
+
15
+ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
16
+
17
+ silence_stream(STDOUT) do
18
+ ActiveRecord::Migrator.migrate File.expand_path('../../db/migrate/', __FILE__)
19
+ end
20
+
21
+ #def drop_all_tables
22
+ # ActiveRecord::Base.connection.tables.each do |table|
23
+ # ActiveRecord::Base.connection.drop_table(table)
24
+ # end
25
+ #end
@@ -0,0 +1,83 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class ActionSmser::BaseTest < ActiveSupport::TestCase
5
+ class MockSms<ActionSmser::Base
6
+ def basic_sms(to, from, body)
7
+ sms(:to => to, :from => from, :body => body)
8
+ end
9
+
10
+ def after_delivery(result)
11
+ puts "generic delivery observer, mock this"
12
+ end
13
+ end
14
+
15
+ setup do
16
+ @receivers = ["555123555", "123", "+358123555123"]
17
+ @sender = "555666"
18
+ @body = "Body with ääkköset end"
19
+ @sms = MockSms.basic_sms(@receivers, @sender, @body)
20
+ end
21
+
22
+ test "receivers should be joined by commas" do
23
+ assert_equal "555123555,123,358123555123", @sms.to_encoded
24
+ end
25
+
26
+ test "should be ok with single receivers" do
27
+ str = "123123"
28
+ @sms = MockSms.basic_sms(str, @sender, @body)
29
+ assert_equal [str], @sms.to_as_array
30
+ end
31
+
32
+ test "from_encoded should remove leading + and 0" do
33
+ @sms = MockSms.basic_sms("+358555", "+358123" , @body)
34
+ assert_equal "358123", @sms.from_encoded
35
+ end
36
+
37
+ test "body should be possible to encode" do
38
+ assert_equal "Body+with+%E4%E4kk%F6set+end", @sms.body_encoded_escaped
39
+ end
40
+
41
+ test "body should be cropped to 500 chars, limit extra looong messages" do
42
+ @sms.body = (1..1000).map{'a'}.join
43
+ assert_equal 500, @sms.body_encoded_escaped.size
44
+ end
45
+
46
+ test "should be valid sms" do
47
+ assert @sms.valid?
48
+ end
49
+
50
+ test "should not be valid if no recipients" do
51
+ @sms = MockSms.basic_sms([nil, ""], @sender, @body)
52
+ assert !@sms.valid?
53
+ end
54
+
55
+ test "should have saved sms_type" do
56
+ assert_equal "ActionSmser::BaseTest::MockSms.basic_sms", @sms.sms_type
57
+ end
58
+
59
+ test "should have copied the delivery_options when initializing" do
60
+ assert @sms.delivery_options == ActionSmser.delivery_options
61
+ assert @sms.delivery_options.object_id != ActionSmser.delivery_options.object_id
62
+ end
63
+
64
+ test "should have test_array as delivery method" do
65
+ assert_equal ActionSmser::DeliveryMethods::TestArray, @sms.delivery_method
66
+ end
67
+
68
+ test "after delivery it should call delivery_observer if it's present" do
69
+ @sms.expects(:after_delivery).once
70
+ assert @sms.deliver
71
+ end
72
+
73
+ test "message_real_length should return real sms lenght" do
74
+ assert_equal 4, ActionSmser::Base.message_real_length("a[a")
75
+ end
76
+ test "message_real_crop should return cropped message with sms chars taken account" do
77
+ assert_equal "a[a[", ActionSmser::Base.message_real_cropped("a[a[", 6)
78
+ assert_equal "a[a", ActionSmser::Base.message_real_cropped("a[a[", 5)
79
+ assert_equal "a[a", ActionSmser::Base.message_real_cropped("a[a[", 4)
80
+ assert_equal "a[", ActionSmser::Base.message_real_cropped("a[a[", 3)
81
+ end
82
+
83
+ end
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class ActionSmser::SimpleHttpTest < ActiveSupport::TestCase
5
+ class MockSms<ActionSmser::Base
6
+ def basic_sms(to, from, body)
7
+ sms(:to => to, :from => from, :body => body)
8
+ end
9
+ end
10
+
11
+ setup do
12
+ @receivers = ["555123555", "123555123"]
13
+ @sender = "555666"
14
+ @body = "Body with ääkköset end"
15
+ @sms = MockSms.basic_sms(@receivers, @sender, @body)
16
+
17
+ @sms.delivery_options[:delivery_method] = :simple_http
18
+ @sms.delivery_options[:simple_http] = {:username => 'user', :password => 'pass'}
19
+ assert_equal ActionSmser::DeliveryMethods::SimpleHttp, @sms.delivery_method, "cant run tests, wrong delivery method"
20
+
21
+ http_mock = stub(:body => "id_1234\nid_6666")
22
+ ActionSmser::DeliveryMethods::SimpleHttp.stubs(:deliver_http_request).returns(http_mock)
23
+
24
+ end
25
+
26
+ test "should be able to deliver" do
27
+ @sms_delivery = @sms.deliver
28
+ assert @sms_delivery
29
+ assert_equal @sms_delivery.count, 2, "two messages"
30
+ end
31
+
32
+ test "with saving delivery_reports" do
33
+ @sms.delivery_options[:save_delivery_reports] = true
34
+ #debugger
35
+ @delivery_reports_count = ActionSmser::DeliveryReport.count
36
+ @sms_delivery = @sms.deliver
37
+
38
+ assert @sms_delivery.is_a?(Array)
39
+ assert @sms_delivery.first.is_a?(ActionSmser::DeliveryReport), "should return deliveryreport array"
40
+
41
+ assert_equal @delivery_reports_count + 2, ActionSmser::DeliveryReport.count
42
+
43
+ @dr = @sms_delivery.last
44
+ assert_equal "123555123", @dr.to, "receiver wrong"
45
+ assert_equal "id_6666", @dr.msg_id, "id wrong"
46
+ end
47
+
48
+ # There are better frameworks for testing http requests
49
+ # http://railscasts.com/episodes/276-testing-time-web-requests
50
+ # https://github.com/chrisk/fakeweb
51
+
52
+ end
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class ActionSmser::TestArrayTest < ActiveSupport::TestCase
5
+ class MockSms<ActionSmser::Base
6
+ def basic_sms(to, from, body)
7
+ sms(:to => to, :from => from, :body => body)
8
+ end
9
+ end
10
+
11
+ setup do
12
+ @receivers = ["555123555", "123555123"]
13
+ @sender = "555666"
14
+ @body = "Body with ääkköset end"
15
+ @sms = MockSms.basic_sms(@receivers, @sender, @body)
16
+
17
+ @sms.delivery_options[:delivery_method] = :test_array
18
+ assert_equal ActionSmser::DeliveryMethods::TestArray, @sms.delivery_method, "cant run tests, wrong delivery method"
19
+ ActionSmser::DeliveryMethods::TestArray.deliveries.clear
20
+
21
+ @sms_delivery = @sms.deliver
22
+ end
23
+
24
+ test "should be able to deliver" do
25
+ assert @sms_delivery
26
+ end
27
+
28
+ test "should add to TestArray" do
29
+ assert_equal 1, ActionSmser::DeliveryMethods::TestArray.deliveries.size
30
+ assert_equal @sms, ActionSmser::DeliveryMethods::TestArray.deliveries.first
31
+ end
32
+
33
+ test "with saving delivery_reports" do
34
+ ActionSmser::DeliveryMethods::TestArray.deliveries.clear
35
+ @sms.delivery_options[:save_delivery_reports] = true
36
+ @delivery_reports_count = ActionSmser::DeliveryReport.count
37
+ @sms_delivery = @sms.deliver
38
+
39
+ assert_equal @delivery_reports_count + 2, ActionSmser::DeliveryReport.count
40
+ end
41
+
42
+ end
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class ActionSmser::DeliveryReportTest < ActiveSupport::TestCase
5
+
6
+ class MockSms<ActionSmser::Base
7
+ def basic_sms(to, from, body)
8
+ sms(:to => to, :from => from, :body => body)
9
+ end
10
+ end
11
+
12
+ setup do
13
+ @receivers = ["555123555", "", "123555123"]
14
+ @sender = "555666"
15
+ @body = "Body with ääkköset end"
16
+ @sms = MockSms.basic_sms(@receivers, @sender, @body)
17
+ end
18
+
19
+ test "build DeliveryReport from sms" do
20
+ @dr = ActionSmser::DeliveryReport.build_from_sms(@sms, "123", "msg_id_a")
21
+
22
+ assert @sender == @dr.from, "should have set @sms.from"
23
+ assert @body == @dr.body, "should have set @sms.body"
24
+ assert @sms.sms_type == @dr.sms_type, "should have set @sms.sms_type"
25
+
26
+ assert "123" == @dr.to, "should have set right to info"
27
+
28
+ assert "msg_id_a" == @dr.msg_id, "should have set right msg_id"
29
+
30
+ end
31
+
32
+ test "create_from_save" do
33
+ @dr_count = ActionSmser::DeliveryReport.count
34
+ @dr = ActionSmser::DeliveryReport.create_from_sms(@sms, "123", "msg_id_a")
35
+
36
+ assert_equal @dr_count + 1, ActionSmser::DeliveryReport.count, "should have created one in db"
37
+ assert @dr.is_a?(ActionSmser::DeliveryReport), "should have returned right item"
38
+ end
39
+
40
+
41
+ test "updating status" do
42
+ @dr = ActionSmser::DeliveryReport.new
43
+ @dr.status = "LOCAL_TEST"
44
+ @dr.save!
45
+ log = @dr.log
46
+
47
+ @dr.status = "TEST_2"
48
+ assert_equal "TEST_2", @dr.status
49
+ assert log != @dr.log, "should have updated log"
50
+ assert @dr.log.include?("TEST_2")
51
+ assert @dr.save
52
+ end
53
+
54
+ end