has_public_id 1.1.0 → 1.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +8 -11
- data/lib/has_public_id/activerecord/mixin.rb +3 -0
- data/lib/has_public_id/util.rb +14 -6
- data/lib/has_public_id/version.rb +1 -1
- data/test/dummy/app/models/user.rb +1 -1
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/test.log +3059 -0
- data/test/dummy/test/models/user_test.rb +49 -3
- data/test/has_public_id_test.rb +43 -31
- metadata +2 -2
@@ -1,7 +1,53 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class UserTest < ActiveSupport::TestCase
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
fixtures :users
|
5
|
+
self.use_transactional_fixtures = true
|
6
|
+
|
7
|
+
test "identifier_initializes" do
|
8
|
+
assert User.new.ident.present?, "should have an id when initialized"
|
9
|
+
assert_equal User.new.ident.first(5), 'user-', "should have a 3 letter prefix from the class name"
|
10
|
+
end
|
11
|
+
|
12
|
+
test "identifier_doesnt_change" do
|
13
|
+
u = User.new
|
14
|
+
identifier = u.ident
|
15
|
+
u.save!
|
16
|
+
u.reload
|
17
|
+
assert_equal identifier, u.ident, "doesn't match after create"
|
18
|
+
u.save!
|
19
|
+
u.reload
|
20
|
+
assert_equal identifier, u.ident, "doesn't match after update"
|
21
|
+
end
|
22
|
+
|
23
|
+
test "to_param_matches_identifier" do
|
24
|
+
u = User.new
|
25
|
+
assert_equal u.ident, u.to_param, "should match to_param"
|
26
|
+
end
|
27
|
+
|
28
|
+
test "new_public_id" do
|
29
|
+
pid = User.new_public_id
|
30
|
+
assert pid.starts_with?('user-'), 'has the right prefix'
|
31
|
+
assert_equal 4 + 1 + 12, pid.length, 'has the right length'
|
32
|
+
end
|
33
|
+
|
34
|
+
test "find_by_public_id" do
|
35
|
+
u = User.create(name: 'joey')
|
36
|
+
assert_equal u, User.find_by_public_id(u.to_param), "Can't be looked up by #{u.to_param}"
|
37
|
+
assert_nil User.find_by_public_id('bad_key'), 'returns nil if not found'
|
38
|
+
end
|
39
|
+
|
40
|
+
test "find_by_public_id!" do
|
41
|
+
u = User.create(name: 'joey')
|
42
|
+
assert_equal u, User.find_by_public_id!(u.to_param), "Can't be looked up by #{u.to_param}"
|
43
|
+
assert_raises(ActiveRecord::RecordNotFound){ User.find_by_public_id!('bad_key') }
|
44
|
+
end
|
45
|
+
|
46
|
+
test "initialize_public_ids!" do
|
47
|
+
# From fixtures...
|
48
|
+
assert_equal 3, User.where(ident: nil).count
|
49
|
+
User.initialize_public_ids!
|
50
|
+
assert_equal User.where(ident: nil).count, 0
|
51
|
+
end
|
52
|
+
|
7
53
|
end
|
data/test/has_public_id_test.rb
CHANGED
@@ -1,39 +1,51 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class HasPublicIdTest < ActiveSupport::TestCase
|
4
|
-
|
5
|
-
|
4
|
+
test "Mixin" do
|
5
|
+
assert ActiveRecord::Base.respond_to?(:has_public_id), "should respond to the method we define"
|
6
|
+
end
|
7
|
+
|
8
|
+
end
|
6
9
|
|
7
|
-
|
8
|
-
|
10
|
+
class HasPublicId::UtilTest < ActiveSupport::TestCase
|
11
|
+
test "char_set.length" do
|
12
|
+
assert_equal 62, HasPublicId::Util.char_set.length
|
9
13
|
end
|
10
14
|
|
11
|
-
test "
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
assert_equal
|
32
|
-
end
|
33
|
-
test "
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
15
|
+
test "generate_random_suffix" do
|
16
|
+
keys = {}
|
17
|
+
1000.times do |i|
|
18
|
+
# 62 ^ 10 possibiliities
|
19
|
+
suffix = HasPublicId::Util.generate_random_suffix(10)
|
20
|
+
assert_nil keys[suffix], "#{suffix} is a duplicate!"
|
21
|
+
keys[suffix] = i
|
22
|
+
end
|
23
|
+
end
|
24
|
+
test "generate_random_suffix(length)" do
|
25
|
+
assert_equal 10, HasPublicId::Util.generate_random_suffix(10).length
|
26
|
+
assert_equal 15, HasPublicId::Util.generate_random_suffix(15).length
|
27
|
+
end
|
28
|
+
test "new_public_id(with_prefix)" do
|
29
|
+
key = HasPublicId::Util.new_public_id(User, length: 10, prefix: 'user')
|
30
|
+
assert_equal('user-', key.first(5), "prefix option")
|
31
|
+
end
|
32
|
+
test "new_public_id(with_false_prefix)" do
|
33
|
+
key = HasPublicId::Util.new_public_id(User, length: 10, prefix: false)
|
34
|
+
assert key.match(/^[a-zA-Z0-9]{10}$/), "#{key} doesn't match 10 random alphanumerics"
|
35
|
+
assert_equal(10, key.length, "prefix: false")
|
36
|
+
end
|
37
|
+
test "new_public_id(with_nil_prefix)" do
|
38
|
+
key = HasPublicId::Util.new_public_id(User, length: 10, prefix: nil)
|
39
|
+
assert key.match(/^[a-zA-Z0-9]{10}$/), "#{key} doesn't match 10 random alphanumerics"
|
40
|
+
assert_equal(10, key.length, "prefix: nil")
|
41
|
+
end
|
42
|
+
test "new_public_id(with_blank_prefix)" do
|
43
|
+
key = HasPublicId::Util.new_public_id(User, length: 10, prefix: '')
|
44
|
+
assert key.match(/^[a-zA-Z0-9]{10}$/), "#{key} doesn't match 10 random alphanumerics"
|
45
|
+
assert_equal(10, key.length, "prefix: ''")
|
46
|
+
end
|
47
|
+
test "new_public_id(with_join_with)" do
|
48
|
+
key = HasPublicId::Util.new_public_id(User, length: 10, join_with: '_')
|
49
|
+
assert_equal('use_', key.first(4))
|
38
50
|
end
|
39
51
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_public_id
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-01-
|
12
|
+
date: 2014-01-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|