discourse/spec/controllers/email_controller_spec.rb
Andy Waite 3e50313fdc Prepare for separation of RSpec helper files
Since rspec-rails 3, the default installation creates two helper files:
* `spec_helper.rb`
* `rails_helper.rb`

`spec_helper.rb` is intended as a way of running specs that do not
require Rails, whereas `rails_helper.rb` loads Rails (as Discourse's
current `spec_helper.rb` does).

For more information:

https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#default-helper-files

In this commit, I've simply replaced all instances of `spec_helper` with
`rails_helper`, and renamed the original `spec_helper.rb`.

This brings the Discourse project closer to the standard usage of RSpec
in a Rails app.

At present, every spec relies on loading Rails, but there are likely
many that don't need to. In a future pull request, I hope to introduce a
separate, minimal `spec_helper.rb` which can be used in tests which
don't rely on Rails.
2015-12-01 20:39:42 +00:00

113 lines
2.5 KiB
Ruby

require 'rails_helper'
describe EmailController do
context '.preferences_redirect' do
it 'requires you to be logged in' do
expect { get :preferences_redirect }.to raise_error(Discourse::NotLoggedIn)
end
context 'when logged in' do
let!(:user) { log_in }
it 'redirects to your user preferences' do
get :preferences_redirect
expect(response).to redirect_to("/users/#{user.username}/preferences")
end
end
end
context '.resubscribe' do
let(:user) { Fabricate(:user, email_digests: false) }
let(:key) { DigestUnsubscribeKey.create_key_for(user) }
context 'with a valid key' do
before do
get :resubscribe, key: key
user.reload
end
it 'subscribes the user' do
expect(user.email_digests).to eq(true)
end
end
end
context '.unsubscribe' do
let(:user) { Fabricate(:user) }
let(:key) { DigestUnsubscribeKey.create_key_for(user) }
context 'with a valid key' do
before do
get :unsubscribe, key: key
user.reload
end
it 'unsubscribes the user' do
expect(user.email_digests).to eq(false)
end
it "sets the appropriate instance variables" do
expect(assigns(:success)).to be_present
end
end
context "with an expired key or invalid key" do
before do
get :unsubscribe, key: 'watwatwat'
end
it "sets the appropriate instance variables" do
expect(assigns(:success)).to be_blank
end
end
context 'when logged in as a different user' do
let!(:logged_in_user) { log_in(:coding_horror) }
before do
get :unsubscribe, key: key
user.reload
end
it 'does not unsubscribe the user' do
expect(user.email_digests).to eq(true)
end
it 'sets the appropriate instance variables' do
expect(assigns(:success)).to be_blank
expect(assigns(:different_user)).to be_present
end
end
context 'when logged in as the keyed user' do
before do
log_in_user(user)
get :unsubscribe, key: DigestUnsubscribeKey.create_key_for(user)
user.reload
end
it 'unsubscribes the user' do
expect(user.email_digests).to eq(false)
end
it 'sets the appropriate instance variables' do
expect(assigns(:success)).to be_present
end
end
it "sets not_found when the key didn't match anything" do
get :unsubscribe, key: 'asdfasdf'
expect(assigns(:not_found)).to eq(true)
end
end
end