Update User#identifier to conform to Gitolite 2.x's user pattern
Also modifies the specs a bit because I can't help myself. Closes #480
This commit is contained in:
parent
b44e9a08d5
commit
d29827433d
3 changed files with 47 additions and 30 deletions
|
@ -1,6 +1,13 @@
|
||||||
module Account
|
module Account
|
||||||
|
# Returns a string for use as a Gitolite user identifier
|
||||||
|
#
|
||||||
|
# Note that Gitolite 2.x requires the following pattern for users:
|
||||||
|
#
|
||||||
|
# ^@?[0-9a-zA-Z][0-9a-zA-Z._\@+-]*$
|
||||||
def identifier
|
def identifier
|
||||||
email.gsub /[^[:alnum:]]/, "_"
|
# Replace non-word chars with underscores, then make sure it starts with
|
||||||
|
# valid chars
|
||||||
|
email.gsub(/\W/, '_').gsub(/\A([\W\_])+/, '')
|
||||||
end
|
end
|
||||||
|
|
||||||
def is_admin?
|
def is_admin?
|
||||||
|
|
|
@ -28,7 +28,7 @@ FactoryGirl.define do
|
||||||
email { Faker::Internet.email }
|
email { Faker::Internet.email }
|
||||||
name
|
name
|
||||||
password "123456"
|
password "123456"
|
||||||
password_confirmation "123456"
|
password_confirmation { password }
|
||||||
|
|
||||||
trait :admin do
|
trait :admin do
|
||||||
admin true
|
admin true
|
||||||
|
|
|
@ -31,36 +31,46 @@ describe User do
|
||||||
it { should respond_to(:private_token) }
|
it { should respond_to(:private_token) }
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return valid identifier" do
|
describe '#identifier' do
|
||||||
user = User.new(email: "test@mail.com")
|
it "should return valid identifier" do
|
||||||
user.identifier.should == "test_mail_com"
|
user = build(:user, email: "test@mail.com")
|
||||||
|
user.identifier.should == "test_mail_com"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return identifier without + sign" do
|
||||||
|
user = build(:user, email: "test+foo@mail.com")
|
||||||
|
user.identifier.should == "test_foo_mail_com"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should conform to Gitolite's required identifier pattern" do
|
||||||
|
user = build(:user, email: "_test@example.com")
|
||||||
|
user.identifier.should == 'test_example_com'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return identifier without + sign" do
|
describe '#generate_password' do
|
||||||
user = User.new(email: "test+foo@mail.com")
|
it "should execute callback when force_random_password specified" do
|
||||||
user.identifier.should == "test_foo_mail_com"
|
user = build(:user, force_random_password: true)
|
||||||
|
user.should_receive(:generate_password)
|
||||||
|
user.save
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not generate password by default" do
|
||||||
|
user = create(:user, password: 'abcdefg')
|
||||||
|
user.password.should == 'abcdefg'
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should generate password when forcing random password" do
|
||||||
|
Devise.stub(:friendly_token).and_return('123456789')
|
||||||
|
user = create(:user, password: 'abcdefg', force_random_password: true)
|
||||||
|
user.password.should == '12345678'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should execute callback when force_random_password specified" do
|
describe 'authentication token' do
|
||||||
user = User.new(email: "test@mail.com", force_random_password: true)
|
it "should have authentication token" do
|
||||||
user.should_receive(:generate_password)
|
user = Factory(:user)
|
||||||
user.save
|
user.authentication_token.should_not be_blank
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should not generate password by default" do
|
|
||||||
user = Factory(:user, password: 'abcdefg', password_confirmation: 'abcdefg')
|
|
||||||
user.password.should == 'abcdefg'
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should generate password when forcing random password" do
|
|
||||||
Devise.stub(:friendly_token).and_return('123456789')
|
|
||||||
user = User.create(email: "test1@mail.com", force_random_password: true)
|
|
||||||
user.password.should == user.password_confirmation
|
|
||||||
user.password.should == '12345678'
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have authentication token" do
|
|
||||||
user = Factory(:user)
|
|
||||||
user.authentication_token.should_not == ""
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue