2012-02-25 16:30:42 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe User do
|
|
|
|
|
2012-03-06 18:09:15 +00:00
|
|
|
let(:user) { FactoryGirl.build(:user) }
|
2012-02-25 16:30:42 +00:00
|
|
|
|
|
|
|
it "has valid factory" do
|
2012-03-06 18:09:15 +00:00
|
|
|
FactoryGirl.build(:user).should be_valid
|
2012-02-25 16:30:42 +00:00
|
|
|
end
|
|
|
|
|
2012-03-05 23:25:21 +00:00
|
|
|
describe "validation" do
|
2012-12-01 17:45:06 +00:00
|
|
|
let(:user) { FactoryGirl.create(:user) }
|
2012-03-05 23:25:21 +00:00
|
|
|
|
|
|
|
it "validates nickname uniqueness" do
|
2012-03-18 11:07:01 +00:00
|
|
|
new_user = FactoryGirl.build(:user)
|
2012-03-05 23:25:21 +00:00
|
|
|
new_user.nickname = user.nickname
|
|
|
|
|
|
|
|
new_user.should_not be_valid
|
|
|
|
new_user.should have(1).error_on(:nickname)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-25 16:30:42 +00:00
|
|
|
describe ".create_with_omniauth" do
|
|
|
|
let(:uid) { "123" }
|
|
|
|
let(:provider) { "twitter" }
|
2012-02-26 19:11:09 +00:00
|
|
|
let(:nickname) { "foo" }
|
|
|
|
let(:name) { "Foo Bar" }
|
2012-02-25 16:30:42 +00:00
|
|
|
|
|
|
|
let(:auth) do
|
|
|
|
{
|
|
|
|
"provider" => provider,
|
|
|
|
"uid" => uid,
|
2012-02-25 22:42:44 +00:00
|
|
|
"info" => {
|
2012-02-26 19:11:09 +00:00
|
|
|
"name" => name,
|
|
|
|
"nickname" => nickname }
|
2012-02-25 16:30:42 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-03-05 23:25:21 +00:00
|
|
|
context "valid data" do
|
|
|
|
let(:user) { User.create_with_omniauth(auth) }
|
|
|
|
|
|
|
|
it "creates user with valid attributes" do
|
|
|
|
user.persisted?.should be_true
|
|
|
|
|
|
|
|
user.provider.should == provider
|
|
|
|
user.uid.should == uid
|
|
|
|
user.nickname.should == nickname
|
|
|
|
user.name.should == name
|
|
|
|
user.avatar_url.should be_nil
|
|
|
|
end
|
|
|
|
|
2012-02-25 16:30:42 +00:00
|
|
|
end
|
2012-02-26 16:39:49 +00:00
|
|
|
|
2012-02-26 16:55:19 +00:00
|
|
|
context "when avatar available" do
|
2012-03-08 14:00:10 +00:00
|
|
|
let(:avatar_url) { "http://foo.bar/avatar.jpg" }
|
2012-02-26 16:55:19 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
OauthHelper.stub(:get_avatar_url).and_return(avatar_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "assigns avatar_url" do
|
|
|
|
user = User.create_with_omniauth(auth)
|
|
|
|
user.avatar_url.should == avatar_url
|
|
|
|
end
|
2012-03-05 23:25:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when nickname is taken" do
|
2012-12-01 17:45:06 +00:00
|
|
|
let!(:user) { FactoryGirl.create(:user) }
|
2012-03-05 23:25:21 +00:00
|
|
|
let(:nickname) { user.nickname }
|
|
|
|
|
|
|
|
it "doesn't create user" do
|
|
|
|
user = User.create_with_omniauth(auth)
|
|
|
|
user.persisted?.should be_false
|
|
|
|
user.valid?.should be_false
|
|
|
|
end
|
2012-02-26 16:55:19 +00:00
|
|
|
|
|
|
|
end
|
2012-02-25 16:30:42 +00:00
|
|
|
end
|
2012-03-04 19:29:19 +00:00
|
|
|
|
|
|
|
describe '#add_user_token' do
|
|
|
|
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
|
2012-02-25 16:30:42 +00:00
|
|
|
end
|