diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/admin_spam_addresses_controller_spec.rb | 91 | ||||
-rw-r--r-- | spec/controllers/track_controller_spec.rb | 6 | ||||
-rw-r--r-- | spec/factories.rb | 6 | ||||
-rw-r--r-- | spec/factories/spam_addresses.rb | 5 | ||||
-rw-r--r-- | spec/factories/track_things.rb | 26 | ||||
-rw-r--r-- | spec/helpers/track_helper_spec.rb | 204 | ||||
-rw-r--r-- | spec/mailers/request_mailer_spec.rb | 10 | ||||
-rw-r--r-- | spec/models/spam_address_spec.rb | 49 | ||||
-rw-r--r-- | spec/models/track_thing_spec.rb | 9 |
9 files changed, 390 insertions, 16 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/controllers/track_controller_spec.rb b/spec/controllers/track_controller_spec.rb index 40865d2b9..d2b45b6bf 100644 --- a/spec/controllers/track_controller_spec.rb +++ b/spec/controllers/track_controller_spec.rb @@ -5,7 +5,7 @@ describe TrackController, "when making a new track on a request" do @ir = mock_model(InfoRequest, :url_title => 'myrequest', :title => 'My request') @track_thing = mock_model(TrackThing, :save! => true, - :params => {:list_description => 'list description'}, + :params => {}, :track_medium= => nil, :tracking_user_id= => nil) TrackThing.stub!(:create_track_for_request).and_return(@track_thing) @@ -58,7 +58,7 @@ end describe TrackController, "when unsubscribing from a track" do before do - @track_thing = FactoryGirl.create(:track_thing) + @track_thing = FactoryGirl.create(:search_track) end it 'should destroy the track thing' do @@ -78,7 +78,7 @@ describe TrackController, "when unsubscribing from a track" do end it 'should not redirect to a url on another site' do - track_thing = FactoryGirl.create(:track_thing) + track_thing = FactoryGirl.create(:search_track) get :update, {:track_id => @track_thing.id, :track_medium => 'delete', :r => 'http://example.com/'}, diff --git a/spec/factories.rb b/spec/factories.rb deleted file mode 100644 index 7dec41aaf..000000000 --- a/spec/factories.rb +++ /dev/null @@ -1,6 +0,0 @@ -# Factories are defined in spec/factories/*.rb -# -# You can use this file to define shared sequences -FactoryGirl.define do - -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/factories/track_things.rb b/spec/factories/track_things.rb index d1a937c02..cf76b00b3 100644 --- a/spec/factories/track_things.rb +++ b/spec/factories/track_things.rb @@ -2,9 +2,29 @@ FactoryGirl.define do factory :track_thing do association :tracking_user, :factory => :user - track_medium 'email_daily' - track_type 'search_query' - track_query 'Example Query' + factory :search_track do + track_medium 'email_daily' + track_type 'search_query' + track_query 'Example Query' + end + factory :user_track do + association :tracked_user, :factory => :user + track_type 'user_updates' + end + factory :public_body_track do + association :public_body, :factory => :public_body + track_type 'public_body_updates' + end + factory :request_update_track do + association :info_request, :factory => :info_request + track_type 'request_updates' + end + factory :successful_request_track do + track_type 'all_successful_requests' + end + factory :new_request_track do + track_type 'all_new_requests' + end end end diff --git a/spec/helpers/track_helper_spec.rb b/spec/helpers/track_helper_spec.rb new file mode 100644 index 000000000..80857067b --- /dev/null +++ b/spec/helpers/track_helper_spec.rb @@ -0,0 +1,204 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe TrackHelper do + + include TrackHelper + include LinkToHelper + + describe 'when displaying notices for a search track' do + + before do + @track_thing = FactoryGirl.build(:search_track) + end + + it 'should create an already subscribed_notice' do + expected = %Q(You are already subscribed to <a href="/search/Example%20Query/newest/advanced">this search</a>) + already_subscribed_notice(@track_thing).should == expected + end + + it 'should create an email subscription notice' do + expected = %Q(You will now be emailed updates about <a href="/search/Example%20Query/newest/advanced">this search</a>) + subscribe_email_notice(@track_thing).should == expected + end + + it 'should create a following subscription notice' do + expected = %Q(You are now <a href="#{show_user_wall_path(:url_name => @track_thing.tracking_user.url_name)}">following</a> updates about <a href="/search/Example%20Query/newest/advanced">this search</a>) + subscribe_follow_notice(@track_thing).should == expected + end + + it 'should create an unsubscribe notice' do + expected = %Q(You are no longer following <a href="/search/Example%20Query/newest/advanced">this search</a>) + unsubscribe_notice(@track_thing).should == expected + end + + it 'should create a description of the track' do + expected = %Q(<a href="/search/Example%20Query/newest/advanced">anything matching text 'Example Query'</a>) + track_description(@track_thing).should == expected + end + + end + + describe 'when displaying notices for a user track' do + + before do + @track_thing = FactoryGirl.build(:user_track) + end + + it 'should create an already subscribed_notice' do + expected = %Q(You are already subscribed to '#{user_link(@track_thing.tracked_user)}', a person) + already_subscribed_notice(@track_thing).should == expected + end + + it 'should create an email subscription notice' do + expected = %Q(You will now be emailed updates about '#{user_link(@track_thing.tracked_user)}', a person) + subscribe_email_notice(@track_thing).should == expected + end + + it 'should create a following subscription notice' do + expected = %Q(You are now <a href="#{show_user_wall_path(:url_name => @track_thing.tracking_user.url_name)}">following</a> updates about '#{user_link(@track_thing.tracked_user)}', a person) + subscribe_follow_notice(@track_thing).should == expected + end + + it 'should create an unsubscribe notice' do + expected = %Q(You are no longer following '#{user_link(@track_thing.tracked_user)}', a person) + unsubscribe_notice(@track_thing).should == expected + end + + it 'should create a description of the track' do + expected = %Q('#{user_link(@track_thing.tracked_user)}', a person) + track_description(@track_thing).should == expected + end + + end + + describe 'when displaying notices for a public body track' do + + before do + @track_thing = FactoryGirl.build(:public_body_track) + end + + it 'should create an already subscribed_notice' do + expected = %Q(You are already subscribed to '#{public_body_link(@track_thing.public_body)}', a public authority) + already_subscribed_notice(@track_thing).should == expected + end + + it 'should create an email subscription notice' do + expected = %Q(You will now be emailed updates about '#{public_body_link(@track_thing.public_body)}', a public authority) + subscribe_email_notice(@track_thing).should == expected + end + + it 'should create a following subscription notice' do + expected = %Q(You are now <a href="#{show_user_wall_path(:url_name => @track_thing.tracking_user.url_name)}">following</a> updates about '#{public_body_link(@track_thing.public_body)}', a public authority) + subscribe_follow_notice(@track_thing).should == expected + end + + it 'should create an unsubscribe notice' do + expected = %Q(You are no longer following '#{public_body_link(@track_thing.public_body)}', a public authority) + unsubscribe_notice(@track_thing).should == expected + end + + it 'should create a description of the track' do + expected = %Q('#{public_body_link(@track_thing.public_body)}', a public authority) + track_description(@track_thing).should == expected + end + end + + describe 'when displaying notices for a successful request track' do + + before do + @track_thing = FactoryGirl.build(:successful_request_track) + end + + it 'should create an already subscribed_notice' do + expected = %Q(You are already subscribed to any <a href="/list/successful">successful requests</a>) + already_subscribed_notice(@track_thing).should == expected + end + + it 'should create an email subscription notice' do + expected = %Q(You will now be emailed updates about <a href="/list/successful">successful requests</a>) + subscribe_email_notice(@track_thing).should == expected + end + + it 'should create a following subscription notice' do + expected = %Q(You are now <a href="#{show_user_wall_path(:url_name => @track_thing.tracking_user.url_name)}">following</a> updates about <a href="/list/successful">successful requests</a>) + subscribe_follow_notice(@track_thing).should == expected + end + + it 'should create an unsubscribe notice' do + expected = %Q(You are no longer following <a href="/list/successful">successful requests</a>) + unsubscribe_notice(@track_thing).should == expected + end + + it 'should create a description of the track' do + expected = %Q(<a href="/list/successful">successful requests</a>) + track_description(@track_thing).should == expected + end + end + + describe 'when displaying notices for a new request track' do + + before do + @track_thing = FactoryGirl.build(:new_request_track) + end + + it 'should create an already subscribed_notice' do + expected = %Q(You are already subscribed to any <a href="/list">new requests</a>) + already_subscribed_notice(@track_thing).should == expected + end + + it 'should create an email subscription notice' do + expected = %Q(You will now be emailed updates about any <a href="/list">new requests</a>) + subscribe_email_notice(@track_thing).should == expected + end + + it 'should create a following subscription notice' do + expected = %Q(You are now <a href="#{show_user_wall_path(:url_name => @track_thing.tracking_user.url_name)}">following</a> updates about <a href="/list">new requests</a>) + subscribe_follow_notice(@track_thing).should == expected + end + + it 'should create an unsubscribe notice' do + expected = %Q(You are no longer following <a href="/list">new requests</a>) + unsubscribe_notice(@track_thing).should == expected + end + + it 'should create a description of the track' do + expected = %Q(<a href="/list">new requests</a>) + track_description(@track_thing).should == expected + end + + end + + describe 'when displaying notices for a request update track' do + + before do + @track_thing = FactoryGirl.build(:request_update_track) + end + + it 'should create an already subscribed_notice' do + expected = %Q(You are already subscribed to '#{request_link(@track_thing.info_request)}', a request) + already_subscribed_notice(@track_thing).should == expected + end + + it 'should create an email subscription notice' do + expected = %Q(You will now be emailed updates about '#{request_link(@track_thing.info_request)}', a request) + subscribe_email_notice(@track_thing).should == expected + end + + it 'should create a following subscription notice' do + expected = %Q(You are now <a href="#{show_user_wall_path(:url_name => @track_thing.tracking_user.url_name)}">following</a> updates about '#{request_link(@track_thing.info_request)}', a request) + subscribe_follow_notice(@track_thing).should == expected + end + + it 'should create an unsubscribe notice' do + expected = %Q(You are no longer following '#{request_link(@track_thing.info_request)}', a request) + unsubscribe_notice(@track_thing).should == expected + end + + it 'should create a description of the track' do + expected = %Q('#{request_link(@track_thing.info_request)}', a request) + track_description(@track_thing).should == expected + end + + 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 diff --git a/spec/models/track_thing_spec.rb b/spec/models/track_thing_spec.rb index 1c582564b..3edf2d1ad 100644 --- a/spec/models/track_thing_spec.rb +++ b/spec/models/track_thing_spec.rb @@ -51,10 +51,11 @@ describe TrackThing, "when tracking changes" do end it "will make some sane descriptions of search-based tracks" do - tests = { 'bob variety:user' => "users matching text 'bob'", - 'bob (variety:sent OR variety:followup_sent OR variety:response OR variety:comment) (latest_status:successful OR latest_status:partially_successful OR latest_status:rejected OR latest_status:not_held)' => "comments or requests which are successful or unsuccessful matching text 'bob'", - '(latest_status:waiting_response OR latest_status:waiting_clarification OR waiting_classification:true)' => 'requests which are awaiting a response', - ' (variety:sent OR variety:followup_sent OR variety:response OR variety:comment)' => 'all requests or comments' } + tests = { ' (variety:sent OR variety:followup_sent OR variety:response OR variety:comment)' => 'all requests or comments', + 'bob (variety:sent OR variety:followup_sent OR variety:response OR variety:comment)' => "all requests or comments matching text 'bob'", + 'bob (latest_status:successful OR latest_status:partially_successful)' => "requests which are successful matching text 'bob'", + '(latest_status:successful OR latest_status:partially_successful)' => 'requests which are successful', + 'bob' => "anything matching text 'bob'" } tests.each do |query, description| track_thing = TrackThing.create_track_for_search_query(query) track_thing.track_query_description.should == description |