Skip to content

Instantly share code, notes, and snippets.

@hieuk09
Last active December 26, 2015 11:13
Show Gist options
  • Save hieuk09/a31e8f8c5fcdd2d7e9bf to your computer and use it in GitHub Desktop.
Save hieuk09/a31e8f8c5fcdd2d7e9bf to your computer and use it in GitHub Desktop.
describe "validations" do
it "validates presence of github_username" do
acceptance = build(:acceptance, github_username: "")
expect(acceptance).to be_invalid
expect(acceptance.errors[:github_username]).to eq(["can't be blank"])
end
it "bubbles up user validations" do
acceptance = build(:acceptance, password: "")
expect(acceptance).to be_invalid
expect(acceptance.errors[:password]).to eq(["can't be blank"])
end
it "requires user password if user is pre-existing" do
user = build_stubbed(:user, :with_github, password: "password")
allow(user).to receive(:authenticated?).and_return(false)
allow(User).to receive(:find_by).and_return(user)
acceptance = build(:acceptance, email: user.email, password: "other")
expect(acceptance).to be_invalid
expect(acceptance.errors[:password]).to eq(["password is incorrect"])
end
it "sets user password if user doesn't exist" do
acceptance = build(:acceptance, password: "other")
expect(acceptance).to be_valid
expect(acceptance.user.password).to eq("other")
end
it "ensures the invitation has not been accepted" do
invitation = build_stubbed(:invitation)
allow(invitation).to receive(:accepted?).and_return(true)
acceptance = build(:acceptance, invitation: invitation)
expect(acceptance).to be_invalid
expect(acceptance.errors[:invitation]).to eq(["has already been accepted"])
end
end
describe 'validations' do
let(:acceptance) { build(acceptance, attributes) }
context 'when github user name is blank' do
let(:attributes) { { github_username: '' } }
it 'returns error' do
expect(acceptance).to be_invalid
expect(acceptance.errors[:github_username]).to eq(["can't be blank"])
end
end
context 'when password is blank' do
let(:attributes) { { password: '' } }
it 'returns error' do
expect(acceptance).to be_invalid
expect(acceptance.errors[:password]).to eq(["can't be blank"])
end
end
context 'when user is pre-existing' do
let(:attributes) { { email: user.email, password: "other" } }
let(:user) { build_stubbed(:user, :with_github, password: "password") }
before do
allow(user).to receive(:authenticated?).and_return(false)
allow(User).to receive(:find_by).and_return(user)
end
it 'requires correct password' do
expect(acceptance).to be_invalid
expect(acceptance.errors[:password]).to eq(["password is incorrect"])
end
end
context 'when user is not pre-existing' do
let(:attributes) { { password: "other" } }
it 'sets password' do
expect(acceptance).to be_valid
expect(acceptance.user.password).to eq("other")
end
end
context 'when invitation has not been accepted' do
let(:invitation) { build_stubbed(:invitation) }
let(:attributes) { build(:acceptance, invitation: invitation) }
before do
allow(invitation).to receive(:accepted?).and_return(true)
end
it 'raises error' do
expect(acceptance).to be_invalid
expect(acceptance.errors[:invitation]).to eq(["has already been accepted"])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment