2012-02-25 16:30:42 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe User do
|
|
|
|
|
2014-02-11 16:49:22 +00:00
|
|
|
it "is not dummy by default" do
|
|
|
|
expect(described_class.new).to_not be_dummy
|
|
|
|
end
|
|
|
|
|
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
|
2014-04-10 20:14:20 +00:00
|
|
|
let!(:existing_user) { create(:user, username: 'the-user-name') }
|
2014-02-11 16:49:22 +00:00
|
|
|
let(:user) { described_class.new }
|
|
|
|
|
2014-02-12 19:51:01 +00:00
|
|
|
it { should validate_presence_of(:username) }
|
2014-02-11 16:49:22 +00:00
|
|
|
|
|
|
|
context "when user is dummy" do
|
|
|
|
before do
|
|
|
|
user.dummy = true
|
|
|
|
end
|
|
|
|
|
2014-04-10 20:03:21 +00:00
|
|
|
it "doesn't check username uniqueness" do
|
2014-02-12 19:51:01 +00:00
|
|
|
user.username = existing_user.username
|
2014-02-11 16:49:22 +00:00
|
|
|
user.valid?
|
2014-02-12 19:51:01 +00:00
|
|
|
expect(user.errors[:username]).to be_empty
|
2014-02-11 16:49:22 +00:00
|
|
|
end
|
|
|
|
|
2014-04-10 20:03:21 +00:00
|
|
|
it "doesn't check email presence" do
|
2014-02-11 16:49:22 +00:00
|
|
|
user.email = nil
|
|
|
|
user.valid?
|
|
|
|
expect(user.errors[:email]).to be_empty
|
|
|
|
end
|
|
|
|
|
2014-04-10 20:03:21 +00:00
|
|
|
it "doesn't check email uniqueness" do
|
2014-02-11 16:49:22 +00:00
|
|
|
user.email = existing_user.email
|
|
|
|
user.valid?
|
|
|
|
expect(user.errors[:email]).to be_empty
|
|
|
|
end
|
2012-03-05 23:25:21 +00:00
|
|
|
end
|
2013-10-22 15:52:04 +00:00
|
|
|
|
2014-02-11 16:49:22 +00:00
|
|
|
context "when user is real" do
|
|
|
|
before do
|
|
|
|
user.dummy = false
|
|
|
|
end
|
|
|
|
|
2014-04-10 20:03:21 +00:00
|
|
|
it { should allow_value('sickill').for(:username) }
|
|
|
|
it { should allow_value('sick-ill').for(:username) }
|
2014-04-10 20:22:33 +00:00
|
|
|
it { should allow_value('ab').for(:username) }
|
2014-04-10 20:03:21 +00:00
|
|
|
it { should allow_value('s' * 16).for(:username) }
|
2014-04-10 20:14:20 +00:00
|
|
|
it { should allow_value('Sickill').for(:username) }
|
2014-04-10 20:03:21 +00:00
|
|
|
it { should_not allow_value('sick.ill').for(:username) }
|
|
|
|
it { should_not allow_value('-sickill').for(:username) }
|
|
|
|
it { should_not allow_value('sickill-').for(:username) }
|
2014-04-10 20:22:33 +00:00
|
|
|
it { should_not allow_value('a').for(:username) }
|
2014-04-10 20:03:21 +00:00
|
|
|
it { should_not allow_value('s' * 17).for(:username) }
|
|
|
|
|
|
|
|
it "checks username uniqueness" do
|
2014-04-10 20:14:20 +00:00
|
|
|
user.username = 'The-User-Name'
|
2014-02-11 16:49:22 +00:00
|
|
|
user.valid?
|
2014-02-12 19:51:01 +00:00
|
|
|
expect(user.errors[:username]).to_not be_empty
|
2014-02-11 16:49:22 +00:00
|
|
|
end
|
|
|
|
|
2014-04-10 20:03:21 +00:00
|
|
|
it "checks email presence" do
|
2014-02-11 16:49:22 +00:00
|
|
|
user.email = nil
|
|
|
|
user.valid?
|
|
|
|
expect(user.errors[:email]).to_not be_empty
|
|
|
|
end
|
|
|
|
|
2014-04-10 20:03:21 +00:00
|
|
|
it "checks email uniqueness" do
|
2014-02-11 16:49:22 +00:00
|
|
|
user.email = existing_user.email
|
|
|
|
user.valid?
|
|
|
|
expect(user.errors[:email]).to_not be_empty
|
|
|
|
end
|
|
|
|
end
|
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
|
|
|
|
|
2014-02-11 16:49:22 +00:00
|
|
|
describe '.for_api_token' do
|
2014-02-15 20:37:05 +00:00
|
|
|
subject { described_class.for_api_token(token) }
|
2014-02-11 16:49:22 +00:00
|
|
|
|
|
|
|
let(:token) { 'f33e6188-f53c-11e2-abf4-84a6c827e88b' }
|
|
|
|
|
2014-02-15 20:37:05 +00:00
|
|
|
context "when token exists" do
|
|
|
|
let!(:existing_token) { create(:api_token, token: token) }
|
2014-02-11 16:49:22 +00:00
|
|
|
|
2014-02-20 22:14:36 +00:00
|
|
|
it { should eq(existing_token.user) }
|
2014-02-15 20:37:05 +00:00
|
|
|
end
|
2014-02-11 16:49:22 +00:00
|
|
|
|
2014-02-20 22:14:36 +00:00
|
|
|
context "when token doesn't exist" do
|
2014-02-15 20:37:05 +00:00
|
|
|
it { should be(nil) }
|
|
|
|
end
|
2014-02-20 22:14:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '.for_auth_token' do
|
|
|
|
subject { described_class.for_auth_token(auth_token) }
|
|
|
|
|
|
|
|
context "when user with given token exists" do
|
|
|
|
let(:auth_token) { user.auth_token }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
it { should eq(user) }
|
|
|
|
end
|
2014-02-11 16:49:22 +00:00
|
|
|
|
2014-02-20 22:14:36 +00:00
|
|
|
context "when user with given token doesn't exist" do
|
|
|
|
let(:auth_token) { 'Km3u8ZsAZ_Qo0qgBT0rE0g' }
|
2014-02-11 16:49:22 +00:00
|
|
|
|
2014-02-15 20:37:05 +00:00
|
|
|
it { should be(nil) }
|
|
|
|
end
|
|
|
|
end
|
2014-02-11 16:49:22 +00:00
|
|
|
|
2014-02-15 20:37:05 +00:00
|
|
|
describe '.create_dummy' do
|
|
|
|
subject { described_class.create_dummy(token, username) }
|
2014-02-11 16:49:22 +00:00
|
|
|
|
2014-02-15 20:37:05 +00:00
|
|
|
let(:token) { 'f33e6188-f53c-11e2-abf4-84a6c827e88b' }
|
|
|
|
let(:username) { 'somerandomguy' }
|
2014-02-11 16:49:22 +00:00
|
|
|
|
2014-02-15 20:37:05 +00:00
|
|
|
it "returns a persisted user record" do
|
|
|
|
expect(subject.id).not_to be(nil)
|
2014-02-11 16:49:22 +00:00
|
|
|
end
|
|
|
|
|
2014-02-15 20:37:05 +00:00
|
|
|
it "assigns given username to the user" do
|
|
|
|
expect(subject.username).to eq(username)
|
|
|
|
end
|
2014-02-11 16:49:22 +00:00
|
|
|
|
2014-02-15 20:37:05 +00:00
|
|
|
it "assigns given api token to the user" do
|
|
|
|
expect(subject.api_tokens.reload.first.token).to eq(token)
|
2014-02-11 16:49:22 +00:00
|
|
|
end
|
|
|
|
|
2014-02-15 20:37:05 +00:00
|
|
|
context "when token is blank" do
|
|
|
|
let(:token) { '' }
|
2014-02-11 16:49:22 +00:00
|
|
|
|
|
|
|
it { should be(nil) }
|
|
|
|
end
|
|
|
|
|
2014-02-15 20:37:05 +00:00
|
|
|
context "when username is nil" do
|
|
|
|
let(:username) { nil }
|
2014-02-11 16:49:22 +00:00
|
|
|
|
2014-02-15 20:37:05 +00:00
|
|
|
it "returns a persisted user record" do
|
|
|
|
expect(subject.id).not_to be(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "assigns 'anonymous' as username to the user" do
|
|
|
|
expect(subject.username).to eq('anonymous')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when username is an empty string" do
|
|
|
|
let(:username) { nil }
|
|
|
|
|
|
|
|
it "returns a persisted user record" do
|
|
|
|
expect(subject.id).not_to be(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "assigns 'anonymous' as username to the user" do
|
|
|
|
expect(subject.username).to eq('anonymous')
|
|
|
|
end
|
2014-02-11 16:49:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-12 19:51:01 +00:00
|
|
|
describe '#username=' do
|
2013-10-22 15:56:14 +00:00
|
|
|
it 'strips the whitespace' do
|
2014-02-12 19:51:01 +00:00
|
|
|
user = described_class.new(username: ' sickill ')
|
2013-10-22 15:56:14 +00:00
|
|
|
|
2014-02-12 19:51:01 +00:00
|
|
|
expect(user.username).to eq('sickill')
|
2013-10-22 15:56:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#email=' do
|
|
|
|
it 'strips the whitespace' do
|
2014-02-11 16:49:22 +00:00
|
|
|
user = described_class.new(email: ' foo@bar.com ')
|
2013-10-22 15:56:14 +00:00
|
|
|
|
|
|
|
expect(user.email).to eq('foo@bar.com')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-12 16:41:06 +00:00
|
|
|
describe '#assign_api_token' do
|
|
|
|
subject { user.assign_api_token(token) }
|
2013-10-20 16:26:14 +00:00
|
|
|
|
2014-02-12 16:41:06 +00:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:token) { 'a33e6188-f53c-11e2-abf4-84a6c827e88b' }
|
2012-03-04 19:29:19 +00:00
|
|
|
|
2014-02-12 16:41:06 +00:00
|
|
|
before do
|
|
|
|
allow(ApiToken).to receive(:for_token).with(token) { api_token }
|
|
|
|
end
|
2012-03-04 19:29:19 +00:00
|
|
|
|
2014-02-12 16:41:06 +00:00
|
|
|
context "when given token doesn't exist" do
|
|
|
|
let(:api_token) { nil }
|
|
|
|
|
|
|
|
it { should be_kind_of(ApiToken) }
|
|
|
|
it { should be_persisted }
|
|
|
|
specify { expect(subject.token).to eq(token) }
|
2012-03-04 19:29:19 +00:00
|
|
|
end
|
|
|
|
|
2014-02-12 16:41:06 +00:00
|
|
|
context "when given token already exists" do
|
|
|
|
let(:api_token) { double('api_token', reassign_to: nil) }
|
2012-03-04 19:29:19 +00:00
|
|
|
|
2014-02-12 16:41:06 +00:00
|
|
|
it "reassigns it to the user" do
|
|
|
|
subject
|
|
|
|
expect(api_token).to have_received(:reassign_to).with(user)
|
2012-03-04 19:29:19 +00:00
|
|
|
end
|
2014-02-12 16:41:06 +00:00
|
|
|
|
|
|
|
it { should be(api_token) }
|
2012-03-04 19:29:19 +00:00
|
|
|
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
|
|
|
|
|
2014-01-18 10:44:13 +00:00
|
|
|
describe '#asciicasts_excluding' do
|
|
|
|
subject { user.asciicasts_excluding(asciicast, 1) }
|
|
|
|
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:asciicast) { create(:asciicast, user: user) }
|
|
|
|
|
|
|
|
it "returns other asciicasts by user excluding the given one" do
|
|
|
|
other = create(:asciicast, user: user)
|
|
|
|
expect(subject).to eq([other])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-01 00:16:28 +00:00
|
|
|
describe '#editable_by?' do
|
|
|
|
subject { user.editable_by?(other) }
|
|
|
|
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
context "when it's the same user" do
|
|
|
|
let(:other) { user }
|
|
|
|
|
|
|
|
it { should be(true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when it's a different user" do
|
|
|
|
let(:other) { create(:user) }
|
|
|
|
|
|
|
|
it { should be(false) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-12 16:41:06 +00:00
|
|
|
describe '#merge_to' do
|
|
|
|
subject { user.merge_to(target_user) }
|
|
|
|
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:target_user) { create(:user) }
|
|
|
|
let!(:api_token_1) { create(:api_token, user: user) }
|
|
|
|
let!(:api_token_2) { create(:api_token, user: user) }
|
|
|
|
let!(:asciicast_1) { create(:asciicast, user: user) }
|
|
|
|
let!(:asciicast_2) { create(:asciicast, user: user) }
|
2014-04-10 19:08:22 +00:00
|
|
|
let(:updated_at) { 1.hour.from_now }
|
2014-02-12 16:41:06 +00:00
|
|
|
|
|
|
|
before do
|
2014-04-10 19:08:22 +00:00
|
|
|
Timecop.freeze(updated_at) do
|
|
|
|
subject
|
|
|
|
end
|
2014-02-12 16:41:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reassigns all user api tokens to the target user" do
|
|
|
|
api_token_1.reload
|
|
|
|
api_token_2.reload
|
|
|
|
|
|
|
|
expect(api_token_1.user).to eq(target_user)
|
|
|
|
expect(api_token_2.user).to eq(target_user)
|
2014-04-10 19:08:22 +00:00
|
|
|
expect(api_token_1.updated_at.to_i).to eq(updated_at.to_i)
|
|
|
|
expect(api_token_2.updated_at.to_i).to eq(updated_at.to_i)
|
2014-02-12 16:41:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reassigns all user asciicasts to the target user" do
|
|
|
|
asciicast_1.reload
|
|
|
|
asciicast_2.reload
|
|
|
|
|
|
|
|
expect(asciicast_1.user).to eq(target_user)
|
|
|
|
expect(asciicast_2.user).to eq(target_user)
|
2014-04-10 19:08:22 +00:00
|
|
|
expect(asciicast_1.updated_at.to_i).to eq(updated_at.to_i)
|
|
|
|
expect(asciicast_2.updated_at.to_i).to eq(updated_at.to_i)
|
2014-02-12 16:41:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "removes the source user" do
|
|
|
|
expect(user).to be_destroyed
|
|
|
|
end
|
|
|
|
end
|
2012-02-25 16:30:42 +00:00
|
|
|
end
|