carterdte_smtp_filter 0.0.3.6 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7055df6b6f82a4aad397d0fbf3ca159a0dc16d91
4
- data.tar.gz: 8a86125592c46b870b02d3fe4afcd2cfea7370e5
3
+ metadata.gz: 48b021452547b95cf8e39e280ba9313cfbf42b95
4
+ data.tar.gz: f48def6d7ee510c4b5433e5ff0e994fa50f8e22e
5
5
  SHA512:
6
- metadata.gz: 32c766d0b421c68ad68553d0ee19584cc07c50fd8ed8ede6fbdf9d4025d3c7366953dbf7c1ae01ff0e487b1836f56c9a4a2e0cf342bcce41c03cff726926438f
7
- data.tar.gz: 80b13d792cc64598012d976ff721603fb5324be5fea6db46606bee4d4f841cb59b109007d34a0323336efc4ed2c45e37fbfd0fc82ae26a7a879ee056d35bb3bd
6
+ metadata.gz: b436782a20003d01854a65b571da4781b76468b8f64d28c9bbc014a11cf7811daaeae108f9901c2ba65fc07e807db232fbc02262dd43f4b0267ebe6f9ace0133
7
+ data.tar.gz: 236b3d79c940ae603b9e3fc59099b844849616138bb490a0701a45c7a2889884c1486199e19f916ac6b117bc3794d4708a9f38c8df1438ec3b17895a95c9a27a
@@ -32,6 +32,22 @@ if options[:config]
32
32
  logger = CarterdteSmtpFilter.logger
33
33
  logger.info("#{Time.now}: Starting CarterDte SMTP Filter v: #{CarterdteSmtpFilter::VERSION}")
34
34
 
35
+ # Create Spool directory
36
+ CarterdteSmtpFilter::Spool.directory_setup
37
+
38
+ # Create a new Spool Manager
39
+ spool_manager = CarterdteSmtpFilter::SpoolManager.new
40
+
41
+ # Create a Rufus Scheduler
42
+ # https://github.com/jmettraux/rufus-scheduler
43
+
44
+ scheduler = Rufus::Scheduler.new
45
+
46
+ # Process de Queue directory every 10 seconds
47
+ scheduler.every '10s' do
48
+ spool_manager.async.perform
49
+ end
50
+
35
51
  # Create a new server instance
36
52
  server = CarterdteSmtpFilter::SmtpServer.new()
37
53
 
@@ -42,6 +58,7 @@ if options[:config]
42
58
  sleep 1
43
59
 
44
60
  # Run forever and ever
61
+ scheduler.join
45
62
  server.join
46
63
 
47
64
  # setup exit code
@@ -24,6 +24,8 @@ Gem::Specification.new do |spec|
24
24
  spec.add_dependency "json", "~> 1.8"
25
25
  spec.add_dependency "bundler", "~> 1.6"
26
26
  spec.add_dependency "nokogiri", "~> 1.6"
27
+ spec.add_dependency 'sucker_punch', '~> 1.0'
28
+ spec.add_dependency 'rufus-scheduler', '~> 3.0'
27
29
 
28
30
  spec.add_development_dependency "rake", "~> 10.4"
29
31
  spec.add_development_dependency "guard", "~> 2.12"
@@ -6,6 +6,9 @@ require 'time'
6
6
  require 'date'
7
7
  require 'logger'
8
8
  require 'rest-client'
9
+ require 'sucker_punch'
10
+ require 'fileutils'
11
+ require 'rufus-scheduler'
9
12
 
10
13
  require "carterdte_smtp_filter/version"
11
14
  require "carterdte_smtp_filter/config"
@@ -13,6 +16,8 @@ require "carterdte_smtp_filter/smtp_server"
13
16
  require "carterdte_smtp_filter/message"
14
17
  require "carterdte_smtp_filter/dte"
15
18
  require "carterdte_smtp_filter/api_client"
19
+ require "carterdte_smtp_filter/spool"
20
+ require "carterdte_smtp_filter/spool_manager"
16
21
 
17
22
  module CarterdteSmtpFilter
18
23
 
@@ -1,42 +1,64 @@
1
1
  module CarterdteSmtpFilter
2
2
 
3
- module ApiClient
3
+ class ApiClient
4
4
  require 'rest-client'
5
+ include SuckerPunch::Job
5
6
 
6
- def self.logger
7
+ def logger
7
8
  CarterdteSmtpFilter.logger
8
9
  end
10
+
11
+ def perform(message)
12
+ push message if valid?(message)
13
+ end
9
14
 
10
- def self.push(message)
11
- post({payload: message})
15
+ def push(message, url = nil)
16
+ response = post({payload: message.to_json, url: url})
17
+ return response if response
18
+ message.enqueue
19
+ end
20
+
21
+ def valid?(message)
22
+ return false if CarterdteSmtpFilter::Config::testing
23
+ is_json?(message.to_json)
24
+ end
25
+
26
+ def is_json?(message)
27
+ begin
28
+ result = JSON.parse message
29
+ rescue Exception => e
30
+ result = false
31
+ end
32
+ result.is_a? Hash
12
33
  end
13
34
 
14
- def self.api_user
35
+ def api_user
15
36
  CarterdteSmtpFilter::Config::api_user
16
37
  end
17
38
 
18
- def self.api_host
39
+ def api_host
19
40
  CarterdteSmtpFilter::Config::api_host
20
41
  end
21
42
 
22
- def self.api_password
43
+ def api_password
23
44
  CarterdteSmtpFilter::Config::api_password
24
45
  end
25
46
 
26
- def self.post(opts = {})
27
- url = opts[:url] || "https://#{CarterdteSmtpFilter::Config::api_host}/dtes"
47
+ def post(opts = {})
48
+ protocol = CarterdteSmtpFilter::Config::use_https ? "https" : "http"
49
+ url = opts[:url] || "#{protocol}://#{CarterdteSmtpFilter::Config::api_host}/messages"
28
50
  payload = opts[:payload] || {}
29
51
  begin
30
- # We make sure we are sending JSON
31
- JSON.parse payload
32
- logger.info("Post #{payload} to #{url}")
33
52
  resource = RestClient::Resource.new url, api_user, api_password
34
- return if CarterdteSmtpFilter::Config::testing
53
+ logger.debug("Post #{payload} to #{url}") if CarterdteSmtpFilter::Config::debug
35
54
  response = resource.post payload, :content_type => :json, :accept => :json, :verify_ssl => OpenSSL::SSL::VERIFY_NONE
36
55
  logger.info("Api response #{response}")
37
56
  rescue Exception => e
38
- logger.error("#{e} - #{url}")
39
- response = false
57
+ logger.error("#{e} #{e.http_code} - #{url}")
58
+
59
+ # Esto significa que se trato de enviar un mensaje duplicado
60
+ # Por lo tanto no se debe considerar como un fallo de envio
61
+ response = e.http_code == 422 ? true : false
40
62
  end
41
63
  response
42
64
  end
@@ -2,17 +2,32 @@ module CarterdteSmtpFilter
2
2
 
3
3
  module Config
4
4
  attr_accessor :config
5
- OPTIONS = %w(bind_address bind_port return_host return_port elasticsearch_host elasticsearch_port max_connections debug api_user api_password api_host log_file stand_alone testing)
5
+
6
+ SPOOL_DIR = "/var/spool/carter_smtp_filter"
7
+
8
+ def self.spool_dir
9
+ @config["spool_dir"] || SPOOL_DIR
10
+ end
6
11
 
7
12
  def self.parse(file = nil)
8
13
  @config = YAML.load_file(file)
9
14
  end
10
15
 
11
- OPTIONS.each do |op|
12
- self.class.instance_eval do
13
- define_method(op) {@config[op].nil? ? false : @config[op].to_s}
14
- end
16
+ def self.method_missing(m, *args, &block)
17
+ option = m.to_s
18
+ return get_value(option) unless /=$/.match(option)
19
+ set_value(option, args[0])
20
+ end
21
+
22
+ def self.get_value(option)
23
+ return false unless @config[option]
24
+ @config[option].to_s
25
+ end
26
+
27
+ def self.set_value(option, value)
28
+ @config[option.to_sym] = value
15
29
  end
30
+
16
31
 
17
32
  end
18
33
 
@@ -76,8 +76,7 @@ module CarterdteSmtpFilter
76
76
  end
77
77
 
78
78
  def fecha_emision
79
- time_stamp = get_data "TmstFirmaEnv" if envio?
80
- time_stamp = get_data "TmstFirmaResp" if respuesta?
79
+ time_stamp = get_data "FchEmis"
81
80
  Time.parse(time_stamp).to_date
82
81
  end
83
82
 
@@ -4,23 +4,42 @@ module CarterdteSmtpFilter
4
4
 
5
5
  TMP_MESSAGE_DIR = "/tmp/carterdte_smtp_filter"
6
6
 
7
- attr_accessor :raw_data, :qid, :email, :response, :dte
7
+ attr_accessor :raw_data, :return_qid, :email, :response, :dte, :qid
8
8
 
9
- def initialize(raw_data)
9
+ def initialize(raw_data, qid = nil)
10
10
  set_mail_defaults
11
11
  @raw_data = raw_data
12
12
  @email = Mail.read_from_string raw_data
13
13
  @logger = CarterdteSmtpFilter.logger
14
14
  @dte = extract_dte
15
- @qid = nil
15
+ @qid = qid || generate_qid
16
+ @return_qid = nil
17
+ end
18
+
19
+ def queue_file
20
+ queue_sub_dir = qid.split(//).first
21
+ "#{Config::spool_directory}/#{queue_sub_dir}/#{qid}"
22
+ end
23
+
24
+ def enqueue
25
+ @logger.info("Queueing message #{qid} - #{queue_file}")
26
+ begin
27
+ File.open("#{queue_file}", 'w') { |file| file.write(@raw_data) }
28
+ rescue Exception => e
29
+ @logger.info("Could not save #{queue_file} - #{e}")
30
+ end
16
31
  end
17
32
 
18
33
  def extract_dte
19
34
  return false unless @email.attachments.any?
20
- xml_attachments = @email.attachments.select {|m| m.sub_type == "xml"}
35
+ xml_attachments = @email.attachments.select {|m| File.extname(m.filename) == ".xml"}
21
36
  file = xml_attachments.any? ? xml_attachments.first : false
22
37
  return false unless file
23
- CarterdteSmtpFilter::Dte.new file.body.decoded
38
+ Dte.new file.body.decoded
39
+ end
40
+
41
+ def generate_qid
42
+ (("A".."F").to_a + (0..9).to_a).sample(11).join("")
24
43
  end
25
44
 
26
45
  def has_dte?
@@ -28,20 +47,21 @@ module CarterdteSmtpFilter
28
47
  end
29
48
 
30
49
  def to_json
31
- JSON.generate({
32
- to: @email.to,
33
- from: @email.from,
50
+ JSON.generate(message: {
51
+ to: @email.to.first,
52
+ from: @email.from.first,
53
+ qid: @qid,
34
54
  message_id: @email.message_id,
35
55
  cc: @email.cc,
36
- date: @email.date.to_s,
37
- qid: qid,
38
- dte: JSON.parse(@dte.to_json)
56
+ sent_date: @email.date.to_s,
57
+ return_qid: @return_qid,
58
+ dte_attributes: JSON.parse(@dte.to_json)
39
59
  })
40
60
  end
41
61
 
42
62
  def set_mail_defaults
43
63
  Mail.defaults do
44
- delivery_method :smtp, address: CarterdteSmtpFilter::Config::return_host, port: CarterdteSmtpFilter::Config::return_port,
64
+ delivery_method :smtp, address: Config::return_host, port: Config::return_port,
45
65
  return_response: true, enable_starttls_auto: false
46
66
  end
47
67
  end
@@ -50,7 +70,7 @@ module CarterdteSmtpFilter
50
70
  return false unless response.status == "250"
51
71
  # We suppose the string is like "250 2.0.0 Ok: queued as J5D0AWOR4F8\n"
52
72
  return false unless /Ok: queued as/.match response.string
53
- @qid = response.string.split(/\s+/).last
73
+ @return_qid = response.string.split(/\s+/).last
54
74
  end
55
75
 
56
76
  def save_tmp
@@ -10,9 +10,13 @@ module CarterdteSmtpFilter
10
10
  super
11
11
  end
12
12
 
13
+ def carterdte_logger
14
+ CarterdteSmtpFilter.logger
15
+ end
16
+
13
17
  def on_message_data_event(ctx)
14
18
  message = CarterdteSmtpFilter::Message.new(ctx[:message][:data])
15
-
19
+ carterdte_logger.info("Processing message: #{message.qid}")
16
20
  # Save a copy when testing
17
21
  # /tmp/carterdte_smtp_filter/<message-id>
18
22
  message.save_tmp if CarterdteSmtpFilter::Config::testing
@@ -22,17 +26,18 @@ module CarterdteSmtpFilter
22
26
  message.return_email unless CarterdteSmtpFilter::Config::stand_alone
23
27
 
24
28
  # We send it to CarterDte App
25
- CarterdteSmtpFilter::ApiClient.push message.to_json if message.has_dte?
29
+ @logger.debug("Message DTE: #{message.dte}") if CarterdteSmtpFilter::Config::debug
30
+ CarterdteSmtpFilter::ApiClient.new.async.perform message if message.has_dte?
26
31
  end
27
32
 
28
33
  # get event before Connection
29
34
  def on_connect_event(ctx)
30
- CarterdteSmtpFilter.logger.info("Connection from #{ctx[:server][:remote_ip]}")
35
+ carterdte_logger.info("Connection from #{ctx[:server][:remote_ip]}")
31
36
  end
32
37
 
33
38
  # get event before DISONNECT
34
39
  def on_disconnect_event(ctx)
35
- CarterdteSmtpFilter.logger.info("Disconnect from #{ctx[:server][:remote_ip]}")
40
+ carterdte_logger.info("Disconnect from #{ctx[:server][:remote_ip]}")
36
41
  end
37
42
 
38
43
  end
@@ -0,0 +1,29 @@
1
+ module CarterdteSmtpFilter
2
+
3
+ module Spool
4
+
5
+
6
+ def self.directory_setup
7
+ build_spool
8
+ build_spool_structure
9
+ end
10
+
11
+ def self.spool_directory
12
+ CarterdteSmtpFilter::Config::spool_directory
13
+ end
14
+
15
+ def self.build_spool
16
+ FileUtils.mkdir_p spool_directory
17
+ end
18
+
19
+ def self.build_spool_structure
20
+ (("A".."F").to_a + (0..9).to_a).each do |d|
21
+ base_dir = "#{spool_directory}/#{d.to_s}"
22
+ FileUtils.mkdir_p base_dir
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+
29
+ end
@@ -0,0 +1,38 @@
1
+ module CarterdteSmtpFilter
2
+
3
+ class SpoolManager
4
+ include SuckerPunch::Job
5
+
6
+ def logger
7
+ CarterdteSmtpFilter.logger
8
+ end
9
+
10
+ def perform()
11
+ logger.info("Checking queued files...")
12
+ queued_files.each do |qf|
13
+ resend_message qf
14
+ end
15
+ end
16
+
17
+ def resend_message(file_path)
18
+ qid = queue_id file_path
19
+ logger.info("Resending #{qid} - #{file_path}")
20
+ message = CarterdteSmtpFilter::Message.new(File.read(file_path), qid)
21
+ FileUtils.rm file_path
22
+ CarterdteSmtpFilter::ApiClient.new.async.perform message
23
+ message
24
+ end
25
+
26
+ def queued_files
27
+ Dir.glob("#{Config::spool_directory}/[A-F0-9]/*")
28
+ end
29
+
30
+ def queue_id(file_path)
31
+ File.basename file_path
32
+ end
33
+
34
+ end
35
+
36
+ end
37
+
38
+
@@ -1,3 +1,3 @@
1
1
  module CarterdteSmtpFilter
2
- VERSION = "0.0.3.6"
2
+ VERSION = "0.0.4"
3
3
  end
data/test/fake_api.rb CHANGED
@@ -30,7 +30,7 @@ class FakeApi < Sinatra::Base
30
30
  end
31
31
 
32
32
 
33
- post '/dtes' do
33
+ post '/messages' do
34
34
  request.body.rewind
35
35
  data = JSON.parse request.body.read
36
36
  status = 200
@@ -38,6 +38,11 @@ class FakeApi < Sinatra::Base
38
38
  JSON.generate(data)
39
39
  end
40
40
 
41
+ post '/messages/uniq' do
42
+ status 422
43
+ ""
44
+ end
45
+
41
46
  private
42
47
 
43
48
  def authenticate(env)
@@ -1,5 +1,5 @@
1
1
  ---
2
- #debug: true
2
+ debug: true
3
3
  bind_address: 127.0.0.1
4
4
  bind_port: 30024
5
5
  return_host: 127.0.0.1
@@ -10,4 +10,6 @@ elasticsearch_port: 9200
10
10
  api_user: pbruna@example.com
11
11
  api_password: 123456
12
12
  api_host: "api.dte.zboxapp.com"
13
- log_file: "./test/tmp/carterdte_smtp_filter.log"
13
+ log_file: "./test/tmp/carterdte_smtp_filter.log"
14
+ spool_directory: "./test/tmp/spool"
15
+ use_https: true
@@ -1,7 +1,7 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class TestApiClient < Minitest::Test
4
-
4
+ require 'sucker_punch/testing/inline'
5
5
 
6
6
  def setup
7
7
  CarterdteSmtpFilter::Config.parse("./test/fixtures/config.yml")
@@ -11,7 +11,7 @@ class TestApiClient < Minitest::Test
11
11
  end
12
12
 
13
13
  def test_push_should_return_false_if_message_is_not_json
14
- response = CarterdteSmtpFilter::ApiClient.push "hola"
14
+ response = CarterdteSmtpFilter::ApiClient.new.async.perform "hola"
15
15
  assert(!response, "Failure message.")
16
16
  end
17
17
 
@@ -22,22 +22,36 @@ class TestApiClient < Minitest::Test
22
22
  end
23
23
 
24
24
  def test_post_should_workout_unauthorized
25
- response = CarterdteSmtpFilter::ApiClient.post({url: "#{@api_url}/api/v1/denied" })
25
+ response = CarterdteSmtpFilter::ApiClient.new.post({url: "#{@api_url}/api/v1/denied" })
26
26
  assert(!response, "Failure message.")
27
27
  end
28
28
 
29
29
  def test_post_should_workout_application_error
30
- response = CarterdteSmtpFilter::ApiClient.post({url: "#{@api_url}/api/v1/app_error" })
30
+ response = CarterdteSmtpFilter::ApiClient.new.post({url: "#{@api_url}/api/v1/app_error" })
31
31
  assert(!response, "Failure message.")
32
32
  end
33
33
 
34
34
  def test_post_should_return_json
35
- hash = {dte_type: 33, msg_type: "envio"}
36
- json = JSON.generate hash
37
- response = CarterdteSmtpFilter::ApiClient.post({payload: json})
35
+ raw_mail = File.open("./test/fixtures/mail_with_dte.eml", "rb").read
36
+ message = CarterdteSmtpFilter::Message.new raw_mail
37
+ response = CarterdteSmtpFilter::ApiClient.new.async.perform(message)
38
38
  new_hash = JSON.parse response
39
- assert_equal("123456", new_hash["id"])
40
- assert_equal(hash[:dte_type], new_hash["dte_type"])
39
+ # assert_equal("123456", new_hash["id"])
40
+ # assert_equal(hash[:dte_type], new_hash["dte_type"])
41
+ end
42
+
43
+ def test_post_duplicated_message_should_return_true
44
+ response = CarterdteSmtpFilter::ApiClient.new.post({url: "#{@api_url}/messages/uniq" })
45
+ assert(response, "Failure message.")
41
46
  end
47
+
48
+ def test_should_save_queue_file_if_negative_response
49
+ CarterdteSmtpFilter::Spool.directory_setup
50
+ raw_mail = File.open("./test/fixtures/mail_with_dte.eml", "rb").read
51
+ message = CarterdteSmtpFilter::Message.new raw_mail
52
+ CarterdteSmtpFilter::ApiClient.new.push(message, "#{@api_url}/api/v1/app_error")
53
+ assert(File.file?("#{message.queue_file}"), "Failure message.")
54
+ end
55
+
42
56
 
43
57
  end
data/test/test_config.rb CHANGED
@@ -2,19 +2,30 @@ require 'test_helper'
2
2
 
3
3
  class TestConfig < Minitest::Test
4
4
 
5
+ def setup
6
+ @config = YAML.load_file("./test/fixtures/config.yml")
7
+ @config.each do |k,v|
8
+ @config[k] = v.to_s
9
+ end
10
+ CarterdteSmtpFilter::Config.parse("./test/fixtures/config.yml")
11
+ end
12
+
5
13
  def test_should_raise_not_file_if_file_not_exists
6
14
  assert_raises(Errno::ENOENT) { CarterdteSmtpFilter::Config.parse("/tmp/file") }
7
15
  end
8
16
 
9
17
  def test_should_return_config_values
10
- CarterdteSmtpFilter::Config.parse("./test/fixtures/config.yml")
11
- assert_equal("127.0.0.1", CarterdteSmtpFilter::Config::bind_address)
12
- assert_equal("127.0.0.1", CarterdteSmtpFilter::Config::return_host)
13
- assert_equal("30025", CarterdteSmtpFilter::Config::return_port)
14
- assert_equal("pbruna@example.com", CarterdteSmtpFilter::Config::api_user)
15
- assert_equal("123456", CarterdteSmtpFilter::Config::api_password)
16
- assert_equal("api.dte.zboxapp.com", CarterdteSmtpFilter::Config::api_host)
17
- assert_equal("./test/tmp/carterdte_smtp_filter.log", CarterdteSmtpFilter::Config::log_file)
18
+ assert_equal(@config["bind_address"], CarterdteSmtpFilter::Config::bind_address)
19
+ assert_equal(@config["return_host"], CarterdteSmtpFilter::Config::return_host)
20
+ assert_equal(@config["return_port"], CarterdteSmtpFilter::Config::return_port)
21
+ assert_equal(@config["api_user"], CarterdteSmtpFilter::Config::api_user)
22
+ assert_equal(@config["api_password"], CarterdteSmtpFilter::Config::api_password)
23
+ assert_equal(@config["api_host"], CarterdteSmtpFilter::Config::api_host)
24
+ assert_equal(@config["log_file"], CarterdteSmtpFilter::Config::log_file)
25
+ end
26
+
27
+ def test_should_return_spool_dir_from_config_file_or_default_if_is_not_defined
28
+ assert_equal("/var/spool/carter_smtp_filter", CarterdteSmtpFilter::Config::spool_dir)
18
29
  end
19
30
 
20
31
  end
data/test/test_dte.rb CHANGED
@@ -66,9 +66,9 @@ class TestDte < Minitest::Test
66
66
  end
67
67
 
68
68
  def test_dte_fecha_emision
69
- date_envio = Time.parse("2015-05-09T11:10:10").to_date
70
- date_resultado = Time.parse("2015-05-09T12:30:11").to_date
71
- date_problema = Time.parse("2015-05-12T01:03:30").to_date
69
+ date_envio = Date.parse("2015-05-09")
70
+ date_resultado = Date.parse("2015-05-11")
71
+ date_problema = Date.parse("2015-05-11")
72
72
  assert_equal(date_resultado, @dte_resultado.fecha_emision)
73
73
  assert_equal(date_envio, @dte_envio.fecha_emision)
74
74
  assert_equal(date_problema, @dte_problema.fecha_emision)
data/test/test_helper.rb CHANGED
@@ -12,10 +12,10 @@ require 'fake_api'
12
12
  Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # spec-like progress
13
13
 
14
14
  # Only log Celluloid errors
15
- #Celluloid.logger.level = Logger::ERROR
15
+ Celluloid.logger.level = Logger::ERROR
16
16
 
17
17
 
18
- #CarterDteSmtp::Config.parse("./test/fixtures/config.yml")
18
+ CarterdteSmtpFilter::Config.parse("./test/fixtures/config.yml")
19
19
 
20
20
  def check_imap()
21
21
  require "mail"
@@ -74,7 +74,7 @@ class ReturnSmtp < MidiSmtpServer::Smtpd
74
74
  attr_accessor :mail
75
75
 
76
76
  def start
77
- @logger = Logger.new('/dev/null')
77
+ @logger = CarterdteSmtpFilter::Config::debug ? CarterdteSmtpFilter.logger : Logger.new("/dev/null")
78
78
  @port = CarterdteSmtpFilter::Config::return_port
79
79
  @host = CarterdteSmtpFilter::Config::return_host
80
80
  super
data/test/test_message.rb CHANGED
@@ -18,6 +18,13 @@ class TestMessage < Minitest::Test
18
18
  @return_stmp.stop
19
19
  end
20
20
 
21
+ def test_message_generate_qid_should_generate_a_valid_qid_in_hex_value
22
+ raw_mail = File.open("./test/fixtures/mail.tmp", "rb").read
23
+ message = CarterdteSmtpFilter::Message.new raw_mail
24
+ qid = message.generate_qid
25
+ assert(qid =~ /^[0-9A-F]+$/ , "Failure message.")
26
+ end
27
+
21
28
  def test_message_should_save_server_response_when_returning_email
22
29
  raw_mail = File.open("./test/fixtures/mail.tmp", "rb").read
23
30
  message = CarterdteSmtpFilter::Message.new raw_mail
@@ -29,17 +36,18 @@ class TestMessage < Minitest::Test
29
36
  raw_mail = File.open("./test/fixtures/mail_with_dte.eml", "rb").read
30
37
  message = CarterdteSmtpFilter::Message.new raw_mail
31
38
  message.return_email
32
- assert_equal(message.response.string.split(/\s+/).last, message.qid)
39
+ assert_equal(message.response.string.split(/\s+/).last, message.return_qid)
33
40
  end
34
41
 
35
42
  def test_message_to_json_should_return_json_object_with_message_metada
36
43
  message = CarterdteSmtpFilter::Message.new File.open("./test/fixtures/mail_with_dte.eml", "rb").read
37
44
  assert(JSON.parse(message.to_json), "No JSON")
38
45
  json = JSON.parse(message.to_json)
39
- assert_equal(message.email.to, json["to"])
40
- assert_equal(message.email.from, json["from"])
41
- assert_equal(message.email.date.to_s, json["date"])
42
- assert_equal("96529310-8", json["dte"]["rut_emisor"])
46
+ assert_equal(message.email.to.first, json["message"]["to"])
47
+ assert_equal(message.email.from.first, json["message"]["from"])
48
+ assert_equal(message.email.date.to_s, json["message"]["sent_date"])
49
+ assert_equal("96529310-8", json["message"]["dte_attributes"]["rut_emisor"])
50
+ assert(json["message"]["qid"])
43
51
  end
44
52
 
45
53
  def test_extract_dte_should_return_false_if_no_attachments
@@ -69,5 +77,18 @@ class TestMessage < Minitest::Test
69
77
  assert(File.file?("/tmp/carterdte_smtp_filter/#{message.email.message_id}.eml"), "Failure message.")
70
78
  end
71
79
 
80
+ def test_message_queue_file_should_return_the_full_path_of_the_queue_file
81
+ message = CarterdteSmtpFilter::Message.new File.open("./test/fixtures/mail_with_dte.eml", "rb").read
82
+ full_path = "#{CarterdteSmtpFilter::Config::spool_directory}/#{message.qid.split(//).first}/#{message.qid}"
83
+ assert_equal(full_path, message.queue_file)
84
+ end
85
+
86
+ def test_message_enqueue_must_create_a_file_with_qid_as_name
87
+ CarterdteSmtpFilter::Spool.directory_setup
88
+ message = CarterdteSmtpFilter::Message.new File.open("./test/fixtures/mail_with_dte.eml", "rb").read
89
+ message.enqueue
90
+ assert(File.file?("#{message.queue_file}"), "Failure message.")
91
+ end
92
+
72
93
 
73
94
  end
@@ -0,0 +1,42 @@
1
+ require 'test_helper'
2
+
3
+ class TestSpool < Minitest::Test
4
+
5
+ def setup
6
+ CarterdteSmtpFilter::Config.parse("./test/fixtures/config.yml")
7
+ @directory = CarterdteSmtpFilter::Config::spool_directory
8
+ end
9
+
10
+ def teardown
11
+ FileUtils.rm_r @directory if File.directory? @directory
12
+ end
13
+
14
+ def test_should_create_spool_directory
15
+ CarterdteSmtpFilter::Spool.build_spool
16
+ assert(File.directory?(@directory), "No se creo el spool")
17
+ end
18
+
19
+ def test_should_not_creatre_the_spool_directory_if_already_exists
20
+ FileUtils.mkdir_p @directory
21
+ assert(CarterdteSmtpFilter::Spool.build_spool)
22
+ end
23
+
24
+ def test_should_create_the_inside_structure
25
+ CarterdteSmtpFilter::Spool.build_spool_structure
26
+ (("A".."F").to_a + (0..9).to_a).each do |d|
27
+ base_dir = "#{@directory}/#{d.to_s}"
28
+ assert(File.directory?(base_dir), "#{base_dir} no dir" )
29
+ end
30
+ end
31
+
32
+ def test_setup_spool_should_do_everything
33
+ CarterdteSmtpFilter::Spool.directory_setup
34
+ (("A".."F").to_a + (0..9).to_a).each do |d|
35
+ base_dir = "#{@directory}/#{d.to_s}"
36
+ assert(File.directory?(base_dir), "#{base_dir} no dir" )
37
+ end
38
+ end
39
+
40
+
41
+
42
+ end
@@ -0,0 +1,66 @@
1
+ require 'test_helper'
2
+
3
+ class TestSpoolManager < Minitest::Test
4
+ require 'sucker_punch/testing/inline'
5
+
6
+ def setup
7
+ @api_host = CarterdteSmtpFilter::Config::api_host
8
+ @api_url = "http://#{@api_host}"
9
+ stub_request(:any, /#{@api_host}/).to_rack(FakeApi)
10
+ CarterdteSmtpFilter::Config.parse("./test/fixtures/config.yml")
11
+ CarterdteSmtpFilter::Spool.directory_setup
12
+ @directory = CarterdteSmtpFilter::Config::spool_directory
13
+ @spool_manager = CarterdteSmtpFilter::SpoolManager.new
14
+ end
15
+
16
+ def teardown
17
+ FileUtils.rm_r @directory if File.directory? @directory
18
+ end
19
+
20
+ def test_queued_files_should_return_an_array_queue_files
21
+ assert_equal(Array, @spool_manager.queued_files.class)
22
+ end
23
+
24
+ def test_queued_files_elements_should_be_file_names_paths
25
+ messages = []
26
+ 5.times do
27
+ message = CarterdteSmtpFilter::Message.new File.open("./test/fixtures/mail_with_dte.eml", "rb").read
28
+ message.enqueue
29
+ messages << message
30
+ end
31
+ queued_files = @spool_manager.queued_files
32
+ assert(queued_files.include?(messages.sample.queue_file), "Failure message.")
33
+ end
34
+
35
+ def test_resend_message_should_resend_the_message_and_erase_the_file
36
+ message = CarterdteSmtpFilter::Message.new File.open("./test/fixtures/mail_with_dte.eml", "rb").read
37
+ message.enqueue
38
+ queued_file = @spool_manager.queued_files.first
39
+ r_message = @spool_manager.resend_message(queued_file)
40
+ assert(r_message, "Failure message.")
41
+ assert(!File.file?("#{message.queue_file}"), "Failure message.")
42
+ end
43
+
44
+ def test_resend_message_should_resend_the_message_and_erase_the_file
45
+ message = CarterdteSmtpFilter::Message.new File.open("./test/fixtures/mail_with_dte.eml", "rb").read
46
+ message.enqueue
47
+ queued_file = @spool_manager.queued_files.first
48
+ r_message = @spool_manager.resend_message(queued_file)
49
+ assert(r_message, "Failure message.")
50
+ assert(!File.file?("#{message.queue_file}"), "Failure message.")
51
+ assert(message.qid == r_message.qid, "Failure message.")
52
+ end
53
+
54
+ def test_sucker_punch_must_do_all_of_this
55
+ messages = []
56
+ 5.times do
57
+ message = CarterdteSmtpFilter::Message.new File.open("./test/fixtures/mail_with_dte.eml", "rb").read
58
+ message.enqueue
59
+ messages << message
60
+ end
61
+ @spool_manager.async.perform
62
+ assert_equal([], @spool_manager.queued_files)
63
+ end
64
+
65
+
66
+ end
metadata CHANGED
@@ -1,181 +1,209 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carterdte_smtp_filter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3.6
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patricio Bruna
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-12 00:00:00.000000000 Z
11
+ date: 2015-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mail
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '2.6'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.6'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: midi-smtp-server
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '2.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '2.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rest-client
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '1.7'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.7'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: json
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: '1.8'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.8'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: bundler
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ~>
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
75
  version: '1.6'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ~>
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '1.6'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: nokogiri
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ~>
87
+ - - "~>"
88
88
  - !ruby/object:Gem::Version
89
89
  version: '1.6'
90
90
  type: :runtime
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ~>
94
+ - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '1.6'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sucker_punch
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rufus-scheduler
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.0'
97
125
  - !ruby/object:Gem::Dependency
98
126
  name: rake
99
127
  requirement: !ruby/object:Gem::Requirement
100
128
  requirements:
101
- - - ~>
129
+ - - "~>"
102
130
  - !ruby/object:Gem::Version
103
131
  version: '10.4'
104
132
  type: :development
105
133
  prerelease: false
106
134
  version_requirements: !ruby/object:Gem::Requirement
107
135
  requirements:
108
- - - ~>
136
+ - - "~>"
109
137
  - !ruby/object:Gem::Version
110
138
  version: '10.4'
111
139
  - !ruby/object:Gem::Dependency
112
140
  name: guard
113
141
  requirement: !ruby/object:Gem::Requirement
114
142
  requirements:
115
- - - ~>
143
+ - - "~>"
116
144
  - !ruby/object:Gem::Version
117
145
  version: '2.12'
118
146
  type: :development
119
147
  prerelease: false
120
148
  version_requirements: !ruby/object:Gem::Requirement
121
149
  requirements:
122
- - - ~>
150
+ - - "~>"
123
151
  - !ruby/object:Gem::Version
124
152
  version: '2.12'
125
153
  - !ruby/object:Gem::Dependency
126
154
  name: guard-minitest
127
155
  requirement: !ruby/object:Gem::Requirement
128
156
  requirements:
129
- - - ~>
157
+ - - "~>"
130
158
  - !ruby/object:Gem::Version
131
159
  version: '2.4'
132
160
  type: :development
133
161
  prerelease: false
134
162
  version_requirements: !ruby/object:Gem::Requirement
135
163
  requirements:
136
- - - ~>
164
+ - - "~>"
137
165
  - !ruby/object:Gem::Version
138
166
  version: '2.4'
139
167
  - !ruby/object:Gem::Dependency
140
168
  name: minitest-reporters
141
169
  requirement: !ruby/object:Gem::Requirement
142
170
  requirements:
143
- - - ~>
171
+ - - "~>"
144
172
  - !ruby/object:Gem::Version
145
173
  version: '1.0'
146
174
  type: :development
147
175
  prerelease: false
148
176
  version_requirements: !ruby/object:Gem::Requirement
149
177
  requirements:
150
- - - ~>
178
+ - - "~>"
151
179
  - !ruby/object:Gem::Version
152
180
  version: '1.0'
153
181
  - !ruby/object:Gem::Dependency
154
182
  name: sinatra
155
183
  requirement: !ruby/object:Gem::Requirement
156
184
  requirements:
157
- - - ~>
185
+ - - "~>"
158
186
  - !ruby/object:Gem::Version
159
187
  version: '1.4'
160
188
  type: :development
161
189
  prerelease: false
162
190
  version_requirements: !ruby/object:Gem::Requirement
163
191
  requirements:
164
- - - ~>
192
+ - - "~>"
165
193
  - !ruby/object:Gem::Version
166
194
  version: '1.4'
167
195
  - !ruby/object:Gem::Dependency
168
196
  name: webmock
169
197
  requirement: !ruby/object:Gem::Requirement
170
198
  requirements:
171
- - - ~>
199
+ - - "~>"
172
200
  - !ruby/object:Gem::Version
173
201
  version: '1.20'
174
202
  type: :development
175
203
  prerelease: false
176
204
  version_requirements: !ruby/object:Gem::Requirement
177
205
  requirements:
178
- - - ~>
206
+ - - "~>"
179
207
  - !ruby/object:Gem::Version
180
208
  version: '1.20'
181
209
  description: Description - Postfix SMTP Filter to parse DTE files to CarterDTE Platform
@@ -186,7 +214,7 @@ executables:
186
214
  extensions: []
187
215
  extra_rdoc_files: []
188
216
  files:
189
- - .gitignore
217
+ - ".gitignore"
190
218
  - Gemfile
191
219
  - LICENSE.txt
192
220
  - README.md
@@ -199,6 +227,8 @@ files:
199
227
  - lib/carterdte_smtp_filter/dte.rb
200
228
  - lib/carterdte_smtp_filter/message.rb
201
229
  - lib/carterdte_smtp_filter/smtp_server.rb
230
+ - lib/carterdte_smtp_filter/spool.rb
231
+ - lib/carterdte_smtp_filter/spool_manager.rb
202
232
  - lib/carterdte_smtp_filter/version.rb
203
233
  - o-ri/bundle
204
234
  - o-ri/bundler
@@ -222,6 +252,8 @@ files:
222
252
  - test/test_helper.rb
223
253
  - test/test_message.rb
224
254
  - test/test_smtp_server.rb
255
+ - test/test_spool.rb
256
+ - test/test_spool_manager.rb
225
257
  homepage: https://github.com/ZBoxApp/carterdte_smtp_filter
226
258
  licenses:
227
259
  - MIT
@@ -232,17 +264,17 @@ require_paths:
232
264
  - lib
233
265
  required_ruby_version: !ruby/object:Gem::Requirement
234
266
  requirements:
235
- - - '>='
267
+ - - ">="
236
268
  - !ruby/object:Gem::Version
237
269
  version: '0'
238
270
  required_rubygems_version: !ruby/object:Gem::Requirement
239
271
  requirements:
240
- - - '>='
272
+ - - ">="
241
273
  - !ruby/object:Gem::Version
242
274
  version: '0'
243
275
  requirements: []
244
276
  rubyforge_project:
245
- rubygems_version: 2.0.2
277
+ rubygems_version: 2.2.2
246
278
  signing_key:
247
279
  specification_version: 4
248
280
  summary: Postfix SMTP Filter to parse DTE files to CarterDTE Platform
@@ -267,4 +299,6 @@ test_files:
267
299
  - test/test_helper.rb
268
300
  - test/test_message.rb
269
301
  - test/test_smtp_server.rb
302
+ - test/test_spool.rb
303
+ - test/test_spool_manager.rb
270
304
  has_rdoc: