diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/admin_spam_addresses_controller_spec.rb | 91 | ||||
-rw-r--r-- | spec/factories/spam_addresses.rb | 5 | ||||
-rw-r--r-- | spec/mailers/request_mailer_spec.rb | 10 | ||||
-rw-r--r-- | spec/models/spam_address_spec.rb | 49 |
4 files changed, 155 insertions, 0 deletions
diff --git a/spec/controllers/admin_spam_addresses_controller_spec.rb b/spec/controllers/admin_spam_addresses_controller_spec.rb new file mode 100644 index 000000000..da1e9bb5a --- /dev/null +++ b/spec/controllers/admin_spam_addresses_controller_spec.rb @@ -0,0 +1,91 @@ +require 'spec_helper' + +describe AdminSpamAddressesController do + render_views + before { basic_auth_login @request } + + describe :index do + + it 'lists the spam addresses' do + 3.times { FactoryGirl.create(:spam_address) } + get :index + assigns(:spam_addresses).should == SpamAddress.all + end + + it 'creates a new spam address for the form' do + get :index + expect(assigns(:spam_address)).to be_a_new(SpamAddress) + end + + it 'renders the index template' do + get :index + expect(response).to render_template('index') + end + + end + + describe :create do + + let(:spam_params) { FactoryGirl.attributes_for(:spam_address) } + + it 'creates a new spam address with the given parameters' do + post :create, :spam_address => spam_params + assigns(:spam_address).email.should == spam_params[:email] + assigns(:spam_address).should be_persisted + end + + it 'redirects to the index action if successful' do + SpamAddress.any_instance.stub(:save).and_return(true) + post :create, :spam_address => spam_params + expect(response).to redirect_to(spam_addresses_path) + end + + it 'notifies the admin the spam address has been created' do + SpamAddress.any_instance.stub(:save).and_return(true) + post :create, :spam_address => spam_params + msg = "#{ spam_params[:email] } has been added to the spam addresses list" + flash[:notice].should == msg + end + + it 'renders the index action if the address could not be saved' do + SpamAddress.any_instance.stub(:save).and_return(false) + post :create, :spam_address => spam_params + expect(response).to render_template('index') + end + + it 'collects the spam addresses if the address could not be saved' do + 3.times { FactoryGirl.create(:spam_address) } + SpamAddress.any_instance.stub(:save).and_return(false) + post :create, :spam_address => spam_params + assigns(:spam_addresses).should == SpamAddress.all + end + + end + + describe :delete do + + before(:each) do + @spam = FactoryGirl.create(:spam_address) + delete :destroy, :id => @spam.id + end + + it 'finds the spam address to delete' do + assigns(:spam_address).should == @spam + end + + it 'destroys the spam address' do + assigns(:spam_address).should be_destroyed + end + + it 'tells the admin the spam address has been deleted' do + msg = "#{ @spam.email } has been removed from the spam addresses list" + flash[:notice].should == msg + end + + it 'redirects to the index action' do + expect(response).to redirect_to(spam_addresses_path) + 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/mailers/request_mailer_spec.rb b/spec/mailers/request_mailer_spec.rb index 516d13127..2c5d6e6a9 100644 --- a/spec/mailers/request_mailer_spec.rb +++ b/spec/mailers/request_mailer_spec.rb @@ -78,6 +78,16 @@ describe RequestMailer, " when receiving incoming mail" do deliveries.clear end + it "should ignore mail sent to known spam addresses" do + @spam_address = FactoryGirl.create(:spam_address) + + receive_incoming_mail('incoming-request-plain.email', @spam_address.email) + + deliveries = ActionMailer::Base.deliveries + deliveries.size.should == 0 + deliveries.clear + end + it "should return incoming mail to sender when a request is stopped fully for spam" do # mark request as anti-spam ir = info_requests(:fancy_dog_request) 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 |