diff options
-rw-r--r-- | app/models/spam_address.rb | 11 | ||||
-rw-r--r-- | db/migrate/20140325120619_create_spam_addresses.rb | 9 | ||||
-rw-r--r-- | spec/factories/spam_addresses.rb | 5 | ||||
-rw-r--r-- | spec/models/spam_address_spec.rb | 49 |
4 files changed, 74 insertions, 0 deletions
diff --git a/app/models/spam_address.rb b/app/models/spam_address.rb new file mode 100644 index 000000000..15c9d1ab8 --- /dev/null +++ b/app/models/spam_address.rb @@ -0,0 +1,11 @@ +class SpamAddress < ActiveRecord::Base + attr_accessible :email + + validates_presence_of :email, :message => _('Please enter the email address to mark as spam') + validates_uniqueness_of :email, :message => _('This address is already marked as spam') + + def self.spam?(email_address) + exists?(:email => email_address) + end + +end diff --git a/db/migrate/20140325120619_create_spam_addresses.rb b/db/migrate/20140325120619_create_spam_addresses.rb new file mode 100644 index 000000000..7c730a5c7 --- /dev/null +++ b/db/migrate/20140325120619_create_spam_addresses.rb @@ -0,0 +1,9 @@ +class CreateSpamAddresses < ActiveRecord::Migration + def change + create_table :spam_addresses do |t| + t.string :email, :null => false + + t.timestamps + end + end +end diff --git a/spec/factories/spam_addresses.rb b/spec/factories/spam_addresses.rb new file mode 100644 index 000000000..bafb7cd50 --- /dev/null +++ b/spec/factories/spam_addresses.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :spam_address do + sequence(:email) { |n| "spam-#{ n }@example.org" } + end +end diff --git a/spec/models/spam_address_spec.rb b/spec/models/spam_address_spec.rb new file mode 100644 index 000000000..79a1afd11 --- /dev/null +++ b/spec/models/spam_address_spec.rb @@ -0,0 +1,49 @@ +require 'spec_helper' + +describe SpamAddress do + + describe :new do + + it 'requres an email address' do + SpamAddress.new().should_not be_valid + SpamAddress.new(:email => 'spam@example.org').should be_valid + end + + it 'must have a unique email address' do + existing = FactoryGirl.create(:spam_address) + SpamAddress.new(:email => existing.email).should_not be_valid + end + + end + + describe '.spam?' do + + before(:each) do + @spam_address = FactoryGirl.create(:spam_address) + end + + it 'is a spam address if the address is stored' do + SpamAddress.spam?(@spam_address.email).should be_true + end + + it 'is not a spam address if the adress is not stored' do + SpamAddress.spam?('genuine-email@example.com').should be_false + end + + describe 'when accepting an array of emails' do + + it 'is spam if any of the emails are stored' do + emails = ['genuine-email@example.com', @spam_address.email] + SpamAddress.spam?(emails).should be_true + end + + it 'is not spam if none of the emails are stored' do + emails = ['genuine-email@example.com', 'genuine-email@example.org'] + SpamAddress.spam?(emails).should be_false + end + + end + + end + +end |