2012-02-25 16:30:42 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe User do
|
|
|
|
|
2013-10-22 17:16:18 +00:00
|
|
|
it 'gets an auth_token upon creation' do
|
|
|
|
attrs = attributes_for(:user)
|
|
|
|
attrs.delete(:auth_token)
|
|
|
|
user = described_class.create!(attrs)
|
|
|
|
|
|
|
|
expect(user.auth_token).to be_kind_of(String)
|
|
|
|
end
|
|
|
|
|
2013-10-22 15:52:04 +00:00
|
|
|
describe "#valid?" do
|
|
|
|
before do
|
|
|
|
create(:user)
|
2012-03-05 23:25:21 +00:00
|
|
|
end
|
2013-10-22 15:52:04 +00:00
|
|
|
|
|
|
|
it { should validate_uniqueness_of(:nickname) }
|
|
|
|
it { should validate_uniqueness_of(:email) }
|
2012-03-05 23:25:21 +00:00
|
|
|
end
|
|
|
|
|
2013-10-22 17:16:18 +00:00
|
|
|
describe '.generate_auth_token' do
|
|
|
|
it 'generates a string token' do
|
|
|
|
token = described_class.generate_auth_token
|
|
|
|
|
|
|
|
expect(token).to be_kind_of(String)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'generates unique token' do
|
|
|
|
token_1 = described_class.generate_auth_token
|
|
|
|
token_2 = described_class.generate_auth_token
|
|
|
|
|
|
|
|
expect(token_1).to_not eq(token_2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-20 16:51:35 +00:00
|
|
|
describe '.for_credentials' do
|
|
|
|
subject { described_class.for_credentials(credentials) }
|
|
|
|
|
|
|
|
let!(:user) { create(:user, provider: 'twitter', uid: '1') }
|
|
|
|
|
|
|
|
context "when there is matching record" do
|
|
|
|
let(:credentials) { double('credentials', provider: 'twitter', uid: '1') }
|
|
|
|
|
|
|
|
it { should eq(user) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when there isn't matching record" do
|
|
|
|
let(:credentials) { double('credentials', provider: 'twitter', uid: '2') }
|
|
|
|
|
|
|
|
it { should be(nil) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.for_email' do
|
|
|
|
subject { described_class.for_email(email) }
|
|
|
|
|
|
|
|
let!(:user) { create(:user, email: 'foo@bar.com') }
|
|
|
|
|
|
|
|
context "when there is matching record" do
|
|
|
|
let(:email) { 'foo@bar.com' }
|
|
|
|
|
|
|
|
it { should eq(user) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when there isn't matching record" do
|
|
|
|
let(:email) { 'qux@bar.com' }
|
|
|
|
|
|
|
|
it { should be(nil) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-22 15:56:14 +00:00
|
|
|
describe '#nickname=' do
|
|
|
|
it 'strips the whitespace' do
|
|
|
|
user = User.new(nickname: ' sickill ')
|
|
|
|
|
|
|
|
expect(user.nickname).to eq('sickill')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#email=' do
|
|
|
|
it 'strips the whitespace' do
|
|
|
|
user = User.new(email: ' foo@bar.com ')
|
|
|
|
|
|
|
|
expect(user.email).to eq('foo@bar.com')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-04 19:29:19 +00:00
|
|
|
describe '#add_user_token' do
|
2013-10-20 16:26:14 +00:00
|
|
|
let(:user) { build(:user) }
|
|
|
|
|
2012-03-04 19:29:19 +00:00
|
|
|
before { user.save }
|
|
|
|
|
|
|
|
context "when user doesn't have given token" do
|
2012-03-06 18:09:15 +00:00
|
|
|
let(:token) { FactoryGirl.attributes_for(:user_token)[:token] }
|
2012-03-04 19:29:19 +00:00
|
|
|
|
|
|
|
it 'returns created UserToken' do
|
|
|
|
ut = user.add_user_token(token)
|
|
|
|
ut.should be_kind_of(UserToken)
|
|
|
|
ut.id.should_not be(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user doesn't have given token" do
|
2012-12-01 17:45:06 +00:00
|
|
|
let(:existing_token) { FactoryGirl.create(:user_token, :user => user) }
|
2012-03-04 19:29:19 +00:00
|
|
|
let(:token) { existing_token.token }
|
|
|
|
|
|
|
|
it 'returns existing UserToken' do
|
|
|
|
ut = user.add_user_token(token)
|
|
|
|
ut.should == existing_token
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-10-22 15:52:04 +00:00
|
|
|
|
2013-11-18 18:06:56 +00:00
|
|
|
describe '#asciicast_count' do
|
|
|
|
subject { user.asciicast_count }
|
|
|
|
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
2.times { create(:asciicast, user: user) }
|
|
|
|
end
|
|
|
|
|
|
|
|
it { should eq(2) }
|
|
|
|
end
|
|
|
|
|
2012-02-25 16:30:42 +00:00
|
|
|
end
|