govuk_content_models 42.0.0 → 42.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/app/traits/attachable.rb +2 -2
  4. data/govuk_content_models.gemspec +1 -2
  5. data/lib/govuk_content_models/version.rb +1 -1
  6. metadata +3 -83
  7. data/test/fixtures/uploads/image.jpg +0 -0
  8. data/test/models/action_test.rb +0 -13
  9. data/test/models/artefact_action_test.rb +0 -130
  10. data/test/models/artefact_external_link_test.rb +0 -32
  11. data/test/models/artefact_test.rb +0 -482
  12. data/test/models/business_support/business_size_test.rb +0 -25
  13. data/test/models/business_support/business_type_test.rb +0 -25
  14. data/test/models/business_support/location_test.rb +0 -25
  15. data/test/models/business_support/purpose_test.rb +0 -29
  16. data/test/models/business_support/sector_test.rb +0 -25
  17. data/test/models/business_support/stage_test.rb +0 -25
  18. data/test/models/business_support/support_type_test.rb +0 -25
  19. data/test/models/business_support_edition_test.rb +0 -282
  20. data/test/models/campaign_edition_test.rb +0 -91
  21. data/test/models/completed_transaction_edition_test.rb +0 -56
  22. data/test/models/downtime_test.rb +0 -93
  23. data/test/models/edition_scheduled_for_publishing_test.rb +0 -91
  24. data/test/models/edition_test.rb +0 -1185
  25. data/test/models/help_page_edition_test.rb +0 -43
  26. data/test/models/licence_edition_test.rb +0 -105
  27. data/test/models/local_service_test.rb +0 -6
  28. data/test/models/local_transaction_edition_test.rb +0 -37
  29. data/test/models/overview_dashboard_test.rb +0 -46
  30. data/test/models/parted_test.rb +0 -26
  31. data/test/models/prerendered_entity_tests.rb +0 -46
  32. data/test/models/rendered_manual_test.rb +0 -10
  33. data/test/models/simple_smart_answer_edition_test.rb +0 -211
  34. data/test/models/simple_smart_answer_node_test.rb +0 -134
  35. data/test/models/simple_smart_answer_option_test.rb +0 -97
  36. data/test/models/time_zone_test.rb +0 -48
  37. data/test/models/transaction_edition_test.rb +0 -36
  38. data/test/models/travel_advice_edition_test.rb +0 -469
  39. data/test/models/user_test.rb +0 -155
  40. data/test/models/video_edition_test.rb +0 -64
  41. data/test/models/workflow_test.rb +0 -498
  42. data/test/test_helper.rb +0 -61
  43. data/test/traits/attachable_test.rb +0 -244
  44. data/test/validators/link_validator_test.rb +0 -86
  45. data/test/validators/safe_html_validator_test.rb +0 -83
  46. data/test/validators/slug_validator_test.rb +0 -109
data/test/test_helper.rb DELETED
@@ -1,61 +0,0 @@
1
- ENV["RACK_ENV"] = "test"
2
-
3
- require "bundler/setup"
4
-
5
- require "active_support/test_case"
6
-
7
- # NOTE: this test_helper does not introduce enough of rails to make
8
- # require_dependency to be available, requiring "active_support/dependencies"
9
- # causes the tests to fail, so we introduce our own require_dependency
10
- # that simply passes through to require
11
- def require_dependency(*args)
12
- require(*args)
13
- end
14
-
15
- require "shoulda/context"
16
- require "minitest/autorun"
17
- require "mocha/mini_test"
18
- require "mongoid"
19
- require "govuk_content_models/require_all"
20
- require "database_cleaner"
21
- require "gds_api/test_helpers/panopticon"
22
- require "webmock/minitest"
23
- require "govuk_content_models/test_helpers/factories"
24
- require 'govuk_content_models/test_helpers/action_processor_helpers'
25
- require "timecop"
26
- require "byebug"
27
-
28
- # The models depend on a zone being set, so tests will fail if we don't
29
- Time.zone = "London"
30
-
31
- Mongoid.load! File.expand_path("../../config/mongoid.yml", __FILE__)
32
- Mongoid::Tasks::Database.create_indexes
33
- WebMock.disable_net_connect!
34
-
35
- DatabaseCleaner.strategy = :truncation
36
- # initial clean
37
- DatabaseCleaner.clean
38
-
39
- class ActiveSupport::TestCase
40
- PROJECT_ROOT = File.expand_path("../..", __FILE__)
41
-
42
- include GdsApi::TestHelpers::Panopticon
43
- include GovukContentModels::TestHelpers::ActionProcessorHelpers
44
-
45
- def without_metadata_denormalisation(*klasses, &block)
46
- klasses.each {|klass| klass.any_instance.stubs(:denormalise_metadata).returns(true) }
47
- result = yield
48
- klasses.each {|klass| klass.any_instance.unstub(:denormalise_metadata) }
49
- result
50
- end
51
-
52
- def clean_db
53
- DatabaseCleaner.clean
54
- end
55
- set_callback :teardown, :before, :clean_db
56
-
57
- def timecop_return
58
- Timecop.return
59
- end
60
- set_callback :teardown, :before, :timecop_return
61
- end
@@ -1,244 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ModelWithAttachments
4
- include Attachable
5
- include Mongoid::Document
6
-
7
- field :title, type: String
8
- attaches :image
9
- end
10
-
11
- class ModelWithAttachmentsAndUrl
12
- include Attachable
13
- include Mongoid::Document
14
-
15
- field :title, type: String
16
- attaches :image, with_url_field: true
17
- end
18
-
19
- class ModelWithUpdatableAttachments
20
- include Attachable
21
- include Mongoid::Document
22
-
23
- field :title, type: String
24
- attaches :image, update_existing: true, with_url_field: true
25
- end
26
-
27
- class AttachableTest < ActiveSupport::TestCase
28
-
29
- setup do
30
- @edition = ModelWithAttachments.new
31
- @edition_with_url_field = ModelWithAttachmentsAndUrl.new
32
- @edition_with_update_option = ModelWithUpdatableAttachments.new
33
- @previous_api_client = Attachable.asset_api_client
34
- @mock_asset_api = mock("mock_asset_api")
35
- Attachable.asset_api_client = @mock_asset_api
36
- end
37
-
38
- teardown do
39
- Attachable.asset_api_client = @previous_api_client
40
- end
41
-
42
- context "retreiving assets from the api" do
43
- should "raise an exception if there is no api client present" do
44
- Attachable.asset_api_client = nil
45
-
46
- @edition.image_id = "an_image_id"
47
- assert_raise Attachable::ApiClientNotPresent do
48
- @edition.image.file_url
49
- end
50
- end
51
-
52
- should "make the request to the asset api" do
53
- @edition.image_id = "an_image_id"
54
-
55
- asset = OpenStruct.new(:file_url => "/path/to/image")
56
- @mock_asset_api.expects(:asset).with("an_image_id").returns(asset)
57
-
58
- assert_equal "/path/to/image", @edition.image.file_url
59
- end
60
-
61
- should "cache the asset from the api" do
62
- @edition.image_id = "an_image_id"
63
-
64
- asset = OpenStruct.new(:something => "one", :something_else => "two")
65
- @mock_asset_api.expects(:asset).once.with("an_image_id").returns(asset)
66
-
67
- assert_equal "one", @edition.image.something
68
- assert_equal "two", @edition.image.something_else
69
- end
70
-
71
- should "assign a file and detect it has changed" do
72
- file = File.open(File.expand_path("../../fixtures/uploads/image.jpg", __FILE__))
73
- @edition.image = file
74
- assert @edition.image_has_changed?
75
- end
76
- end
77
-
78
- context "saving an edition without update_existing set" do
79
- setup do
80
- @file = File.open(File.expand_path("../../fixtures/uploads/image.jpg", __FILE__))
81
- @asset = OpenStruct.new(
82
- id: 'http://asset-manager.dev.gov.uk/assets/an_image_id',
83
- file_url: 'http://asset-manager.dev.gov.uk/media/an_image_id/image.jpg'
84
- )
85
- end
86
-
87
- should "create another asset even if an asset already exists" do
88
- @edition.image_id = "foo"
89
- @mock_asset_api.expects(:create_asset).returns(@asset)
90
-
91
- @edition.image = @file
92
- @edition.save!
93
- end
94
-
95
- should "create an asset when one does not exist" do
96
- @mock_asset_api.expects(:create_asset).with({ :file => @file }).returns(@asset)
97
-
98
- @edition.image = @file
99
- @edition.save!
100
- end
101
-
102
- should "not upload an asset if it has not changed" do
103
- @edition.save!
104
- end
105
-
106
- should "assign the asset id to the attachment id attribute" do
107
- @mock_asset_api.expects(:create_asset).with({ :file => @file }).returns(@asset)
108
-
109
- @edition.image = @file
110
- @edition.save!
111
-
112
- assert_equal "an_image_id", @edition.image_id
113
- end
114
-
115
- should "assign the asset url to the attachment url attribute if requested" do
116
- @mock_asset_api.expects(:create_asset).with({ :file => @file }).returns(@asset)
117
-
118
- @edition_with_url_field.image = @file
119
- @edition_with_url_field.save!
120
-
121
- assert_equal 'http://asset-manager.dev.gov.uk/media/an_image_id/image.jpg', @edition_with_url_field.image_url
122
- end
123
-
124
- should "not create the attachment url attribute if not requested" do
125
- @mock_asset_api.expects(:create_asset).with({ :file => @file }).returns(@asset)
126
-
127
- @edition.image = @file
128
- @edition.save!
129
-
130
- refute @edition.respond_to?(:image_url)
131
- end
132
-
133
- should "raise an exception if there is no api client present" do
134
- Attachable.asset_api_client = nil
135
-
136
- @edition.image = @file
137
- assert_raise Attachable::ApiClientNotPresent do
138
- @edition.save!
139
- end
140
- end
141
-
142
- should "catch any errors raised by the api client" do
143
- @mock_asset_api.expects(:create_asset).raises(StandardError)
144
-
145
- assert_nothing_raised do
146
- @edition.image = @file
147
- @edition.save!
148
- end
149
-
150
- assert_equal ["could not be uploaded"], @edition.errors[:image_id]
151
- end
152
-
153
- should "not stop the edition from being saved when an uploading error is raised" do
154
- @mock_asset_api.expects(:create_asset).raises(StandardError)
155
-
156
- @edition.image = @file
157
- @edition.title = "foo"
158
- @edition.save!
159
-
160
- @edition.reload
161
- assert_equal "foo", @edition.title
162
- end
163
- end
164
-
165
- context "removing an asset" do
166
- should "remove an asset when remove_* set to true" do
167
- @edition.image_id = 'an_image_id'
168
- @edition.remove_image = true
169
- @edition.save!
170
-
171
- assert_nil @edition.image_id
172
- end
173
-
174
- should "not remove an asset when remove_* set to false or empty" do
175
- @edition.image_id = 'an_image_id'
176
- @edition.remove_image = false
177
- @edition.remove_image = ""
178
- @edition.remove_image = nil
179
- @edition.save!
180
-
181
- assert_equal "an_image_id", @edition.image_id
182
- end
183
- end
184
-
185
- context "with update_existing option set" do
186
- setup do
187
- @file = File.open(File.expand_path("../../fixtures/uploads/image.jpg", __FILE__))
188
-
189
- @asset_id = 'an_image_id'
190
-
191
- @asset_response = OpenStruct.new(
192
- id: "http://asset-manager.dev.gov.uk/assets/#{@asset_id}",
193
- file_url: 'http://asset-manager.dev.gov.uk/media/an_image_id/image.jpg'
194
- )
195
- end
196
-
197
- context "saving an edition without an existing asset" do
198
- should "create a new asset" do
199
- @mock_asset_api.expects(:create_asset).with(:file => @file).returns(@asset_response)
200
-
201
- @edition_with_update_option.image = @file
202
- @edition_with_update_option.save!
203
- end
204
-
205
- should "assign the asset id and file url" do
206
- @mock_asset_api.stubs(:create_asset).returns(@asset_response)
207
-
208
- @edition_with_update_option.image = @file
209
- @edition_with_update_option.save!
210
-
211
- assert_equal @asset_id, @edition_with_update_option.image_id
212
- assert_equal @asset_response.file_url, @edition_with_update_option.image_url
213
- end
214
- end
215
-
216
- context "saving an edition with and existing asset" do
217
- setup do
218
- @existing_asset = OpenStruct.new(
219
- id: "http://asset-manager.dev.gov.uk/assets/#{@asset_id}",
220
- file_url: 'http://asset-manager.dev.gov.uk/media/an_image_id/old_image.jpg'
221
- )
222
-
223
- @edition_with_update_option.image_id = @asset_id
224
- @edition_with_update_option.image_url = @existing_asset.file_url
225
- end
226
-
227
- should "update the asset on save" do
228
- @mock_asset_api.expects(:update_asset).with(@asset_id, :file => @file).returns(@asset_response)
229
-
230
- @edition_with_update_option.image = @file
231
- @edition_with_update_option.save!
232
- end
233
-
234
- should "update the file url for the asset" do
235
- @mock_asset_api.stubs(:update_asset).returns(@asset_response)
236
-
237
- @edition_with_update_option.image = @file
238
- @edition_with_update_option.save!
239
-
240
- assert_equal @asset_response.file_url, @edition_with_update_option.image_url
241
- end
242
- end
243
- end
244
- end
@@ -1,86 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'test_helper'
4
-
5
- class LinkValidatorTest < ActiveSupport::TestCase
6
- class Dummy
7
- include Mongoid::Document
8
-
9
- field "body", type: String
10
- field "assignee", type: String
11
- GOVSPEAK_FIELDS = [:body]
12
-
13
- validates_with LinkValidator
14
- end
15
-
16
- context "links" do
17
- should "not be verified for blank govspeak fields" do
18
- doc = Dummy.new(body: nil)
19
-
20
- assert_nothing_raised do
21
- doc.valid?
22
- end
23
- assert_empty doc.errors
24
- end
25
-
26
- should "not contain empty array for errors on fields" do
27
- doc = Dummy.new(body: "Nothing is invalid")
28
-
29
- assert doc.valid?
30
- assert_empty doc.errors[:body]
31
- end
32
-
33
- should "start with http[s]://, mailto: or /" do
34
- doc = Dummy.new(body: "abc [external](external.com)")
35
- assert doc.invalid?
36
- assert_includes doc.errors.keys, :body
37
-
38
- doc = Dummy.new(body: "abc [external](http://external.com)")
39
- assert doc.valid?
40
-
41
- doc = Dummy.new(body: "abc [internal](/internal)")
42
- assert doc.valid?
43
- end
44
-
45
- should "not contain hover text" do
46
- doc = Dummy.new(body: 'abc [foobar](http://foobar.com "hover")')
47
- assert doc.invalid?
48
- assert_includes doc.errors.keys, :body
49
- end
50
-
51
- should "validate smart quotes as normal quotes" do
52
- doc = Dummy.new(body: %q<abc [foobar](http://foobar.com “hover”)>)
53
- assert doc.invalid?
54
- assert_includes doc.errors.keys, :body
55
- end
56
-
57
- should "not set rel=external" do
58
- doc = Dummy.new(body: 'abc [foobar](http://foobar.com){:rel="external"}')
59
- assert doc.invalid?
60
- assert_includes doc.errors.keys, :body
61
- end
62
-
63
- should "show multiple errors" do
64
- doc = Dummy.new(body: 'abc [foobar](foobar.com "bar"){:rel="external"}')
65
- assert doc.invalid?
66
- assert_equal 3, doc.errors[:body].first.length
67
- end
68
-
69
- should "only show each error once" do
70
- doc = Dummy.new(body: 'abc [link1](foobar.com), ghi [link2](bazquux.com)')
71
- assert doc.invalid?
72
- assert_equal 1, doc.errors[:body].first.length
73
- end
74
-
75
- should "be validated when any attribute of the document changes" do
76
- # already published document having link validation errors
77
- doc = Dummy.new(body: 'abc [link1](foobar.com), ghi [link2](bazquux.com)')
78
- doc.save(validate: false)
79
-
80
- doc.assignee = "4fdef0000000000000000001"
81
- assert doc.invalid?
82
-
83
- assert_equal 1, doc.errors[:body].first.length
84
- end
85
- end
86
- end
@@ -1,83 +0,0 @@
1
- require 'test_helper'
2
-
3
- class SafeHtmlTest < ActiveSupport::TestCase
4
- class Dummy
5
- include Mongoid::Document
6
-
7
- field :i_am_govspeak, type: String
8
-
9
- GOVSPEAK_FIELDS = [:i_am_govspeak]
10
-
11
- validates_with SafeHtml
12
-
13
- embeds_one :dummy_embedded_single, class_name: 'SafeHtmlTest::DummyEmbeddedSingle'
14
- end
15
-
16
- class DummyEmbeddedSingle
17
- include Mongoid::Document
18
-
19
- embedded_in :dummy, class_name: 'SafeHtmlTest::Dummy'
20
-
21
- field :i_am_govspeak, type: String
22
-
23
- GOVSPEAK_FIELDS = [:i_am_govspeak]
24
-
25
- validates_with SafeHtml
26
- end
27
-
28
- context "we don't quite trust mongoid (2)" do
29
- should "validate embedded documents automatically" do
30
- embedded = DummyEmbeddedSingle.new(i_am_govspeak: "<script>")
31
- dummy = Dummy.new(i_am_govspeak: embedded)
32
- # Can't invoke embedded.valid? because that would run the validations
33
- assert dummy.invalid?
34
- assert_includes dummy.errors.keys, :i_am_govspeak
35
- end
36
- end
37
-
38
- context "what to validate" do
39
- should "allow clean content in nested fields" do
40
- dummy = Dummy.new(i_am_govspeak: { "clean" => ["plain text"] })
41
- assert dummy.valid?
42
- end
43
-
44
- should "disallow images not hosted by us" do
45
- dummy = Dummy.new(i_am_govspeak: '<img src="http://evil.com/trollface"/>')
46
- assert dummy.invalid?
47
- assert_includes dummy.errors.keys, :i_am_govspeak
48
- end
49
-
50
- should "allow images hosted by us" do
51
- dummy = Dummy.new(i_am_govspeak: '<img src="http://www.dev.gov.uk/trollface"/>')
52
- assert dummy.valid?
53
- end
54
-
55
- should "allow plain text" do
56
- dummy = Dummy.new(i_am_govspeak: "foo bar")
57
- assert dummy.valid?
58
- end
59
-
60
- should "check only specified fields as Govspeak" do
61
- nasty_govspeak = %q{[Numberwang](script:nasty(); "Wangernum")}
62
- assert ! Govspeak::Document.new(nasty_govspeak).valid?, "expected this to be identified as bad"
63
-
64
- dummy = Dummy.new(i_am_govspeak: nasty_govspeak)
65
- assert dummy.invalid?
66
- end
67
-
68
- should "all models that have govspeak fields should use this validator" do
69
- models_dir = File.expand_path("../../app/models/*", File.dirname(__FILE__))
70
-
71
- classes = Dir[models_dir]
72
- .map { |file|
73
- File.basename(file, ".rb").camelize.constantize
74
- }
75
- .select { |klass|
76
- klass.included_modules.include?(Mongoid::Document) && klass.const_defined?(:GOVSPEAK_FIELDS)
77
- }
78
- .each { |klass|
79
- assert_includes klass.validators.map(&:class), SafeHtml, "#{klass} must be validated with SafeHtml"
80
- }
81
- end
82
- end
83
- end