This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
/
attachment_content_type_validator_test.rb
140 lines (118 loc) · 3.98 KB
/
attachment_content_type_validator_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
require './test/helper'
class AttachmentContentTypeValidatorTest < Test::Unit::TestCase
def setup
rebuild_model
@dummy = Dummy.new
end
def build_validator(options)
@validator = Paperclip::Validators::AttachmentContentTypeValidator.new(options.merge(
:attributes => :avatar
))
end
context "with a nil content type" do
setup do
build_validator :content_type => "image/jpg"
@dummy.stubs(:avatar_content_type => nil)
@validator.validate(@dummy)
end
should "not set an error message" do
assert @dummy.errors[:avatar_content_type].blank?
end
end
context "with an allowed type" do
context "as a string" do
setup do
build_validator :content_type => "image/jpg"
@dummy.stubs(:avatar_content_type => "image/jpg")
@validator.validate(@dummy)
end
should "not set an error message" do
assert @dummy.errors[:avatar_content_type].blank?
end
end
context "as an regexp" do
setup do
build_validator :content_type => /^image\/.*/
@dummy.stubs(:avatar_content_type => "image/jpg")
@validator.validate(@dummy)
end
should "not set an error message" do
assert @dummy.errors[:avatar_content_type].blank?
end
end
context "as a list" do
setup do
build_validator :content_type => ["image/png", "image/jpg", "image/jpeg"]
@dummy.stubs(:avatar_content_type => "image/jpg")
@validator.validate(@dummy)
end
should "not set an error message" do
assert @dummy.errors[:avatar_content_type].blank?
end
end
end
context "with a disallowed type" do
context "as a string" do
setup do
build_validator :content_type => "image/png"
@dummy.stubs(:avatar_content_type => "image/jpg")
@validator.validate(@dummy)
end
should "set a correct default error message" do
assert @dummy.errors[:avatar_content_type].present?
assert_includes @dummy.errors[:avatar_content_type], "is invalid"
end
end
context "as a regexp" do
setup do
build_validator :content_type => /^text\/.*/
@dummy.stubs(:avatar_content_type => "image/jpg")
@validator.validate(@dummy)
end
should "set a correct default error message" do
assert @dummy.errors[:avatar_content_type].present?
assert_includes @dummy.errors[:avatar_content_type], "is invalid"
end
end
context "with :message option" do
context "without interpolation" do
setup do
build_validator :content_type => "image/png", :message => "should be a PNG image"
@dummy.stubs(:avatar_content_type => "image/jpg")
@validator.validate(@dummy)
end
should "set a correct error message" do
assert_includes @dummy.errors[:avatar_content_type], "should be a PNG image"
end
end
context "with interpolation" do
setup do
build_validator :content_type => "image/png", :message => "should have content type %{types}"
@dummy.stubs(:avatar_content_type => "image/jpg")
@validator.validate(@dummy)
end
should "set a correct error message" do
assert_includes @dummy.errors[:avatar_content_type], "should have content type image/png"
end
end
end
end
context "using the helper" do
setup do
Dummy.validates_attachment_content_type :avatar, :content_type => "image/jpg"
end
should "add the validator to the class" do
assert Dummy.validators_on(:avatar).any?{ |validator| validator.kind == :attachment_content_type }
end
end
context "given options" do
should "raise argument error if no required argument was given" do
assert_raises(ArgumentError) do
build_validator :message => "Some message"
end
end
should "not raise arguemnt error if :content_type was given" do
build_validator :content_type => "image/jpg"
end
end
end