govuk_content_models 9.0.1 → 10.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 10.0.0
2
+
3
+ * Add validation for tag IDs
4
+
1
5
  ## 9.0.1
2
6
 
3
7
  * Bugfix: `whole_body` can return nil for CompletedTransactionEdition because the body field is no longer used.
data/app/models/tag.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "safe_html"
2
+ require 'tag_id_validator'
2
3
 
3
4
  class Tag
4
5
  include Mongoid::Document
@@ -17,6 +18,8 @@ class Tag
17
18
  index :tag_type
18
19
 
19
20
  validates_presence_of :tag_id, :title, :tag_type
21
+ validates_uniqueness_of :tag_id, scope: :tag_type
22
+ validates_with TagIdValidator
20
23
  validates_with SafeHtml
21
24
 
22
25
  class MissingTags < RuntimeError
@@ -0,0 +1,17 @@
1
+ class TagIdValidator < ActiveModel::Validator
2
+
3
+ def validate(record)
4
+ unless valid_tag_id?(record.tag_id)
5
+ record.errors[:tag_id] << "ID must be valid in a URL and have no more than one slash"
6
+ end
7
+ end
8
+
9
+ private
10
+
11
+ def valid_tag_id?(tag_id)
12
+ tag_id.to_s.match(/\A[a-z0-9\-\/]+\Z/) &&
13
+ tag_id.to_s.count('/') <= 1 &&
14
+ ! tag_id.to_s.end_with?('/')
15
+ end
16
+
17
+ end
@@ -1,4 +1,4 @@
1
1
  module GovukContentModels
2
2
  # Changing this causes Jenkins to tag and release the gem into the wild
3
- VERSION = "9.0.1"
3
+ VERSION = "10.0.0"
4
4
  end
@@ -101,4 +101,18 @@ class TagTest < ActiveSupport::TestCase
101
101
 
102
102
  assert_equal %w{Business Chips Crime Pie}, tags.map(&:title).sort
103
103
  end
104
+
105
+ test "should be invalid when tag id already exists for the tag type" do
106
+ Tag.create!(tag_id: "cars", tag_type: "vehicles", title: "Cars")
107
+ Tag.create!(tag_id: "cars", tag_type: "gary-numan-songs", title: "Cars")
108
+
109
+ tag = Tag.new(tag_id: "cars", tag_type: "vehicles")
110
+
111
+ refute tag.valid?
112
+ assert tag.errors.has_key?(:tag_id)
113
+ end
114
+
115
+ test "should validate with TagIdValidator" do
116
+ assert_includes Tag.validators.map(&:class), TagIdValidator
117
+ end
104
118
  end
@@ -0,0 +1,60 @@
1
+ require 'test_helper'
2
+ require 'tag_id_validator'
3
+
4
+ class TagIdValidatorTest < ActiveSupport::TestCase
5
+ class Dummy
6
+ include Mongoid::Document
7
+
8
+ field :tag_id, type: String
9
+ field :parent_id, type: String
10
+
11
+ validates_with TagIdValidator
12
+ end
13
+
14
+ should "permit a lower-case alphanumeric tag id" do
15
+ dummy = Dummy.new(tag_id: "a-good-tag-id")
16
+ assert dummy.valid?
17
+ end
18
+
19
+ should "not permit a tag id with spaces" do
20
+ dummy = Dummy.new(tag_id: "this tag has spaces")
21
+ refute dummy.valid?
22
+ assert dummy.errors.has_key?(:tag_id)
23
+ end
24
+
25
+ should "not permit a tag id with uppercase characters" do
26
+ dummy = Dummy.new(tag_id: "CLEAN-ALL-THE-THINGS")
27
+ refute dummy.valid?
28
+ assert dummy.errors.has_key?(:tag_id)
29
+ end
30
+
31
+ should "not permit a tag id with non-alphanumeric characters" do
32
+ dummy = Dummy.new(tag_id: "a-t@g-!d")
33
+ refute dummy.valid?
34
+ assert dummy.errors.has_key?(:tag_id)
35
+ end
36
+
37
+ should "not permit a tag id with underscores" do
38
+ dummy = Dummy.new(tag_id: "tag_id_with_underscores")
39
+ refute dummy.valid?
40
+ assert dummy.errors.has_key?(:tag_id)
41
+ end
42
+
43
+ should "permit a tag id containing a slash" do
44
+ dummy = Dummy.new(tag_id: "parent-tag-id/child-tag-id")
45
+ assert dummy.valid?
46
+ end
47
+
48
+ should "not permit more than one slash in a tag id" do
49
+ dummy = Dummy.new(tag_id: "parent-tag-id/more/than/one/slash")
50
+ refute dummy.valid?
51
+ assert dummy.errors.has_key?(:tag_id)
52
+ end
53
+
54
+ should "not permit a slash at the end of a tag id" do
55
+ dummy = Dummy.new(tag_id: "parent-tag-id/")
56
+ refute dummy.valid?
57
+ assert dummy.errors.has_key?(:tag_id)
58
+ end
59
+
60
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govuk_content_models
3
3
  version: !ruby/object:Gem::Version
4
- version: 9.0.1
4
+ version: 10.0.0
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-03-31 00:00:00.000000000 Z
12
+ date: 2014-04-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bson_ext
@@ -392,6 +392,7 @@ files:
392
392
  - app/traits/taggable.rb
393
393
  - app/validators/safe_html.rb
394
394
  - app/validators/slug_validator.rb
395
+ - app/validators/tag_id_validator.rb
395
396
  - config/mongoid.yml
396
397
  - govuk_content_models.gemspec
397
398
  - jenkins.sh
@@ -448,6 +449,7 @@ files:
448
449
  - test/traits/taggable_test.rb
449
450
  - test/validators/safe_html_validator_test.rb
450
451
  - test/validators/slug_validator_test.rb
452
+ - test/validators/tag_id_validator_test.rb
451
453
  homepage: https://github.com/alphagov/govuk_content_models
452
454
  licenses: []
453
455
  post_install_message:
@@ -463,7 +465,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
463
465
  version: '0'
464
466
  segments:
465
467
  - 0
466
- hash: 2591551071857125687
468
+ hash: -283248144793344771
467
469
  required_rubygems_version: !ruby/object:Gem::Requirement
468
470
  none: false
469
471
  requirements:
@@ -472,7 +474,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
472
474
  version: '0'
473
475
  segments:
474
476
  - 0
475
- hash: 2591551071857125687
477
+ hash: -283248144793344771
476
478
  requirements: []
477
479
  rubyforge_project:
478
480
  rubygems_version: 1.8.23
@@ -525,3 +527,4 @@ test_files:
525
527
  - test/traits/taggable_test.rb
526
528
  - test/validators/safe_html_validator_test.rb
527
529
  - test/validators/slug_validator_test.rb
530
+ - test/validators/tag_id_validator_test.rb