From eee01c9e15728d8a47904e8735b1a456fec05aa5 Mon Sep 17 00:00:00 2001 From: Adam Cooke Date: Mon, 8 May 2017 16:39:32 +0100 Subject: [PATCH] a little more testing --- lib/postal/rspec_helpers.rb | 22 ++++++++ .../models/outgoing_message_prototype_spec.rb | 23 ++++++++ spec/app/models/server_spec.rb | 14 +++++ spec/config/postal.yml | 9 +++- spec/factories/domain_factory.rb | 50 ++++++++++++++++++ spec/factories/server_factory.rb | 52 +++++++++++++++++++ spec/factories/track_domain_factory.rb | 33 ++++++++++++ spec/lib/postal/message_db/database.rb | 17 ++++++ spec/lib/postal/message_parser_spec.rb | 24 ++++++++- spec/rails_helper.rb | 17 ++++++ 10 files changed, 257 insertions(+), 4 deletions(-) create mode 100644 lib/postal/rspec_helpers.rb create mode 100644 spec/app/models/outgoing_message_prototype_spec.rb create mode 100644 spec/app/models/server_spec.rb create mode 100644 spec/factories/domain_factory.rb create mode 100644 spec/factories/server_factory.rb create mode 100644 spec/factories/track_domain_factory.rb create mode 100644 spec/lib/postal/message_db/database.rb diff --git a/lib/postal/rspec_helpers.rb b/lib/postal/rspec_helpers.rb new file mode 100644 index 0000000..5ec44ea --- /dev/null +++ b/lib/postal/rspec_helpers.rb @@ -0,0 +1,22 @@ +module Postal + module RspecHelpers + + def with_global_server(&block) + server = Server.find(GLOBAL_SERVER.id) + block.call(server) + ensure + server.message_db.provisioner.clean + end + + def create_plain_text_message(server, text, to = 'test@example.com', override_attributes = {}) + domain = create(:domain, :owner => server) + attributes = {:from => "test@#{domain.name}", :subject => "Test Plain Text Message"}.merge(override_attributes) + attributes[:to] = to + attributes[:plain_body] = text + message = OutgoingMessagePrototype.new(server, '127.0.0.1', 'testsuite', attributes) + result = message.create_message(to) + server.message_db.message(result[:id]) + end + + end +end diff --git a/spec/app/models/outgoing_message_prototype_spec.rb b/spec/app/models/outgoing_message_prototype_spec.rb new file mode 100644 index 0000000..a86fc2b --- /dev/null +++ b/spec/app/models/outgoing_message_prototype_spec.rb @@ -0,0 +1,23 @@ +require 'rails_helper' + +describe OutgoingMessagePrototype do + + it "should create a new message" do + with_global_server do |server| + domain = create(:domain, :owner => server) + prototype = OutgoingMessagePrototype.new(server, '127.0.0.1', 'TestSuite', { + :from => "test@#{domain.name}", + :to => "test@example.com", + :subject => "Test Message", + :plain_body => "A plain body!" + }) + + expect(prototype.valid?).to be true + message = prototype.create_message('test@example.com') + expect(message).to be_a Hash + expect(message[:id]).to be_a Integer + expect(message[:token]).to be_a String + end + end + +end diff --git a/spec/app/models/server_spec.rb b/spec/app/models/server_spec.rb new file mode 100644 index 0000000..91a8b99 --- /dev/null +++ b/spec/app/models/server_spec.rb @@ -0,0 +1,14 @@ +require 'rails_helper' + +describe Server do + + context "model" do + subject(:server) { create(:server) } + + it "should have a UUID" do + expect(server.uuid).to be_a String + expect(server.uuid.length).to eq 36 + end + end + +end diff --git a/spec/config/postal.yml b/spec/config/postal.yml index c8acfb3..ec06fbf 100644 --- a/spec/config/postal.yml +++ b/spec/config/postal.yml @@ -3,8 +3,10 @@ web: protocol: https fast_server: - enabled: false - bind_address: + enabled: true + bind_address: 0.0.0.0 + port: 5010 + ssl_port: 5011 general: use_ip_pools: false @@ -27,6 +29,9 @@ rabbitmq: password: guest vhost: +smtp_server: + port: 2525 + dns: mx_records: - mx.postal.example.com diff --git a/spec/factories/domain_factory.rb b/spec/factories/domain_factory.rb new file mode 100644 index 0000000..ca04721 --- /dev/null +++ b/spec/factories/domain_factory.rb @@ -0,0 +1,50 @@ +# == Schema Information +# +# Table name: domains +# +# id :integer not null, primary key +# server_id :integer +# uuid :string(255) +# name :string(255) +# verification_token :string(255) +# verification_method :string(255) +# verified_at :datetime +# dkim_private_key :text(65535) +# created_at :datetime +# updated_at :datetime +# dns_checked_at :datetime +# spf_status :string(255) +# spf_error :string(255) +# dkim_status :string(255) +# dkim_error :string(255) +# mx_status :string(255) +# mx_error :string(255) +# return_path_status :string(255) +# return_path_error :string(255) +# outgoing :boolean default(TRUE) +# incoming :boolean default(TRUE) +# owner_type :string(255) +# owner_id :integer +# dkim_identifier_string :string(255) +# use_for_any :boolean +# +# Indexes +# +# index_domains_on_server_id (server_id) +# index_domains_on_uuid (uuid) +# + +FactoryGirl.define do + + factory :domain do + association :owner, :factory => :user + sequence(:name) { |n| "example#{n}.com" } + verification_method 'DNS' + verified_at Time.now + end + + factory :organization_domain, :parent => :domain do + association :owner, :factory => :organization + end + +end diff --git a/spec/factories/server_factory.rb b/spec/factories/server_factory.rb new file mode 100644 index 0000000..08dba60 --- /dev/null +++ b/spec/factories/server_factory.rb @@ -0,0 +1,52 @@ +# == Schema Information +# +# Table name: servers +# +# id :integer not null, primary key +# organization_id :integer +# uuid :string(255) +# name :string(255) +# mode :string(255) +# ip_pool_id :integer +# created_at :datetime +# updated_at :datetime +# permalink :string(255) +# send_limit :integer +# deleted_at :datetime +# message_retention_days :integer +# raw_message_retention_days :integer +# raw_message_retention_size :integer +# allow_sender :boolean default(FALSE) +# token :string(255) +# send_limit_approaching_at :datetime +# send_limit_approaching_notified_at :datetime +# send_limit_exceeded_at :datetime +# send_limit_exceeded_notified_at :datetime +# spam_threshold :decimal(8, 2) +# spam_failure_threshold :decimal(8, 2) +# postmaster_address :string(255) +# suspended_at :datetime +# outbound_spam_threshold :decimal(8, 2) +# domains_not_to_click_track :text(65535) +# suspension_reason :string(255) +# log_smtp_data :boolean default(FALSE) +# +# Indexes +# +# index_servers_on_organization_id (organization_id) +# index_servers_on_permalink (permalink) +# index_servers_on_token (token) +# index_servers_on_uuid (uuid) +# + +FactoryGirl.define do + + factory :server do + association :organization + name "Mail Server" + mode "Live" + provision_database false + sequence(:permalink) { |n| "server#{n}" } + end + +end diff --git a/spec/factories/track_domain_factory.rb b/spec/factories/track_domain_factory.rb new file mode 100644 index 0000000..affb416 --- /dev/null +++ b/spec/factories/track_domain_factory.rb @@ -0,0 +1,33 @@ +# == Schema Information +# +# Table name: track_domains +# +# id :integer not null, primary key +# uuid :string(255) +# server_id :integer +# domain_id :integer +# name :string(255) +# dns_checked_at :datetime +# dns_status :string(255) +# dns_error :string(255) +# created_at :datetime not null +# updated_at :datetime not null +# ssl_enabled :boolean default(TRUE) +# track_clicks :boolean default(TRUE) +# track_loads :boolean default(TRUE) +# excluded_click_domains :text(65535) +# + +FactoryGirl.define do + + factory :track_domain do + name "click" + dns_status 'OK' + association :server + + after(:build) do |track_domain| + track_domain.domain ||= create(:domain, :owner => track_domain.server) + end + end + +end diff --git a/spec/lib/postal/message_db/database.rb b/spec/lib/postal/message_db/database.rb new file mode 100644 index 0000000..a1aa80c --- /dev/null +++ b/spec/lib/postal/message_db/database.rb @@ -0,0 +1,17 @@ +require 'rails_helper' + +describe Postal::MessageDB::Database do + + context "when provisioned" do + subject(:database) { GLOBAL_SERVER.message_db } + + it "should be a message db" do + expect(database).to be_a Postal::MessageDB::Database + end + + it "should return the current schema version" do + expect(database.schema_version).to be_a Integer + end + end + +end diff --git a/spec/lib/postal/message_parser_spec.rb b/spec/lib/postal/message_parser_spec.rb index 25e240a..b3bb557 100644 --- a/spec/lib/postal/message_parser_spec.rb +++ b/spec/lib/postal/message_parser_spec.rb @@ -1,7 +1,27 @@ require 'rails_helper' describe Postal::MessageParser do - it "should be true" do - expect(Rails.env).to eq 'test' + + it "should not do anything when there are no tracking domains" do + with_global_server do |server| + expect(server.track_domains.size).to eq 0 + message = create_plain_text_message(server, 'Hello world!', 'test@example.com') + parser = Postal::MessageParser.new(message) + expect(parser.actioned?).to be false + expect(parser.tracked_links).to eq 0 + expect(parser.tracked_images).to eq 0 + end end + + it "should replace links in messages" do + with_global_server do |server| + message = create_plain_text_message(server, 'Hello world! http://github.com/atech/postal', 'test@example.com') + track_domain = create(:track_domain, :server => server, :domain => message.domain) + parser = Postal::MessageParser.new(message) + expect(parser.actioned?).to be true + expect(parser.new_body).to match(/\AHello world! http:\/\/click\.#{message.domain.name}/) + expect(parser.tracked_links).to eq 1 + end + end + end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index b9f2b47..1fd5d7a 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -14,13 +14,30 @@ RSpec.configure do |config| config.use_transactional_fixtures = true config.infer_spec_type_from_file_location! config.include FactoryGirl::Syntax::Methods + config.include Postal::RspecHelpers config.before(:suite) do + # Test that the factories are working as they should and then clean up before getting started on + # the rest of the suite. begin DatabaseCleaner.start FactoryGirl.lint(FactoryGirl.factories.select { |f| !FACTORIES_EXCLUDED_FROM_LINT.include?(f.name.to_sym) }) ensure DatabaseCleaner.clean end + + # We're going to create a global server that can be used by any tests. + # Because the mail databases don't use any transactions, all data left in the + # database will be left there unless removed. + DatabaseCleaner.start + GLOBAL_SERVER = FactoryGirl.create(:server, :provision_database => true) end + + config.after(:suite) do + # Remove the global server after the suite has finished running and then + # clean the database in case it left anything lying around. + GLOBAL_SERVER.destroy + DatabaseCleaner.clean + end + end