discourse/spec/components/common_passwords/common_passwords_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

79 lines
2.7 KiB
Ruby

require "rails_helper"
require_dependency "common_passwords/common_passwords"
describe CommonPasswords do
it "the passwords file should exist" do
expect(File.exists?(described_class::PASSWORD_FILE)).to eq(true)
end
describe "#common_password?" do
before { described_class.stubs(:redis).returns(stub_everything) }
subject { described_class.common_password? @password }
it "returns false if password isn't in the common passwords list" do
described_class.stubs(:password_list).returns(stub_everything(:include? => false))
@password = 'uncommonPassword'
expect(subject).to eq(false)
end
it "returns false if password is nil" do
described_class.expects(:password_list).never
@password = nil
expect(subject).to eq(false)
end
it "returns false if password is blank" do
described_class.expects(:password_list).never
@password = ""
expect(subject).to eq(false)
end
it "returns true if password is in the common passwords list" do
described_class.stubs(:password_list).returns(stub_everything(:include? => true))
@password = "password"
expect(subject).to eq(true)
end
end
describe '#password_list' do
it "loads the passwords file if redis doesn't have it" do
mock_redis = mock("redis")
mock_redis.stubs(:exists).returns(false)
mock_redis.stubs(:scard).returns(0)
described_class.stubs(:redis).returns(mock_redis)
described_class.expects(:load_passwords).returns(['password'])
list = described_class.password_list
expect(list).to respond_to(:include?)
end
it "doesn't load the passwords file if redis has it" do
mock_redis = mock("redis")
mock_redis.stubs(:exists).returns(true)
mock_redis.stubs(:scard).returns(10000)
described_class.stubs(:redis).returns(mock_redis)
described_class.expects(:load_passwords).never
list = described_class.password_list
expect(list).to respond_to(:include?)
end
it "loads the passwords file if redis has an empty list" do
mock_redis = mock("redis")
mock_redis.stubs(:exists).returns(true)
mock_redis.stubs(:scard).returns(0)
described_class.stubs(:redis).returns(mock_redis)
described_class.expects(:load_passwords).returns(['password'])
list = described_class.password_list
expect(list).to respond_to(:include?)
end
end
context "missing password file" do
it "tolerates it" do
described_class.stubs(:redis).returns(stub_everything(sismember: false, exists: false, scard: 0))
File.stubs(:readlines).with(described_class::PASSWORD_FILE).raises(Errno::ENOENT)
expect(described_class.common_password?("password")).to eq(false)
end
end
end