Commit d29827433d77b9f9353fcae396ca321dd9016771
1 parent
b44e9a08
Exists in
master
and in
4 other branches
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
Showing
3 changed files
with
44 additions
and
27 deletions
Show diff stats
app/roles/account.rb
1 | -module Account | 1 | +module Account |
2 | + # Returns a string for use as a Gitolite user identifier | ||
3 | + # | ||
4 | + # Note that Gitolite 2.x requires the following pattern for users: | ||
5 | + # | ||
6 | + # ^@?[0-9a-zA-Z][0-9a-zA-Z._\@+-]*$ | ||
2 | def identifier | 7 | def identifier |
3 | - email.gsub /[^[:alnum:]]/, "_" | 8 | + # Replace non-word chars with underscores, then make sure it starts with |
9 | + # valid chars | ||
10 | + email.gsub(/\W/, '_').gsub(/\A([\W\_])+/, '') | ||
4 | end | 11 | end |
5 | 12 | ||
6 | def is_admin? | 13 | def is_admin? |
spec/factories.rb
@@ -28,7 +28,7 @@ FactoryGirl.define do | @@ -28,7 +28,7 @@ FactoryGirl.define do | ||
28 | email { Faker::Internet.email } | 28 | email { Faker::Internet.email } |
29 | name | 29 | name |
30 | password "123456" | 30 | password "123456" |
31 | - password_confirmation "123456" | 31 | + password_confirmation { password } |
32 | 32 | ||
33 | trait :admin do | 33 | trait :admin do |
34 | admin true | 34 | admin true |
spec/models/user_spec.rb
@@ -31,36 +31,46 @@ describe User do | @@ -31,36 +31,46 @@ describe User do | ||
31 | it { should respond_to(:private_token) } | 31 | it { should respond_to(:private_token) } |
32 | end | 32 | end |
33 | 33 | ||
34 | - it "should return valid identifier" do | ||
35 | - user = User.new(email: "test@mail.com") | ||
36 | - user.identifier.should == "test_mail_com" | ||
37 | - end | 34 | + describe '#identifier' do |
35 | + it "should return valid identifier" do | ||
36 | + user = build(:user, email: "test@mail.com") | ||
37 | + user.identifier.should == "test_mail_com" | ||
38 | + end | ||
38 | 39 | ||
39 | - it "should return identifier without + sign" do | ||
40 | - user = User.new(email: "test+foo@mail.com") | ||
41 | - user.identifier.should == "test_foo_mail_com" | ||
42 | - end | 40 | + it "should return identifier without + sign" do |
41 | + user = build(:user, email: "test+foo@mail.com") | ||
42 | + user.identifier.should == "test_foo_mail_com" | ||
43 | + end | ||
43 | 44 | ||
44 | - it "should execute callback when force_random_password specified" do | ||
45 | - user = User.new(email: "test@mail.com", force_random_password: true) | ||
46 | - user.should_receive(:generate_password) | ||
47 | - user.save | 45 | + it "should conform to Gitolite's required identifier pattern" do |
46 | + user = build(:user, email: "_test@example.com") | ||
47 | + user.identifier.should == 'test_example_com' | ||
48 | + end | ||
48 | end | 49 | end |
49 | 50 | ||
50 | - it "should not generate password by default" do | ||
51 | - user = Factory(:user, password: 'abcdefg', password_confirmation: 'abcdefg') | ||
52 | - user.password.should == 'abcdefg' | ||
53 | - end | 51 | + describe '#generate_password' do |
52 | + it "should execute callback when force_random_password specified" do | ||
53 | + user = build(:user, force_random_password: true) | ||
54 | + user.should_receive(:generate_password) | ||
55 | + user.save | ||
56 | + end | ||
57 | + | ||
58 | + it "should not generate password by default" do | ||
59 | + user = create(:user, password: 'abcdefg') | ||
60 | + user.password.should == 'abcdefg' | ||
61 | + end | ||
54 | 62 | ||
55 | - it "should generate password when forcing random password" do | ||
56 | - Devise.stub(:friendly_token).and_return('123456789') | ||
57 | - user = User.create(email: "test1@mail.com", force_random_password: true) | ||
58 | - user.password.should == user.password_confirmation | ||
59 | - user.password.should == '12345678' | 63 | + it "should generate password when forcing random password" do |
64 | + Devise.stub(:friendly_token).and_return('123456789') | ||
65 | + user = create(:user, password: 'abcdefg', force_random_password: true) | ||
66 | + user.password.should == '12345678' | ||
67 | + end | ||
60 | end | 68 | end |
61 | 69 | ||
62 | - it "should have authentication token" do | ||
63 | - user = Factory(:user) | ||
64 | - user.authentication_token.should_not == "" | 70 | + describe 'authentication token' do |
71 | + it "should have authentication token" do | ||
72 | + user = Factory(:user) | ||
73 | + user.authentication_token.should_not be_blank | ||
74 | + end | ||
65 | end | 75 | end |
66 | end | 76 | end |