aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models
diff options
context:
space:
mode:
authorGareth Rees <gareth@mysociety.org>2015-06-24 11:19:43 +0100
committerGareth Rees <gareth@mysociety.org>2015-06-24 11:19:43 +0100
commit2cce1794a4d9d2c42b83bab8a693900e8ca23ebc (patch)
tree7408a04d5ac0963ec2defbbf7d4955cff7cd62b5 /spec/models
parented6b256539e0dcaa3764951d90e2dc599a8acddd (diff)
parent54ba7a4fa232ad3b57310551b9a5e19d72060abe (diff)
Merge branch 'develop' into release-22-develop
Diffstat (limited to 'spec/models')
-rw-r--r--spec/models/censor_rule_spec.rb58
-rw-r--r--spec/models/foi_attachment_spec.rb74
-rw-r--r--spec/models/public_body_spec.rb38
3 files changed, 158 insertions, 12 deletions
diff --git a/spec/models/censor_rule_spec.rb b/spec/models/censor_rule_spec.rb
index 314b060d2..d308ac1b9 100644
--- a/spec/models/censor_rule_spec.rb
+++ b/spec/models/censor_rule_spec.rb
@@ -64,19 +64,35 @@ describe CensorRule, "substituting things" do
@censor_rule.replacement = "hello"
end
- it 'should do basic text substitution' do
- body = "I don't know why you say goodbye"
- @censor_rule.apply_to_text!(body)
- body.should == "I don't know why you say hello"
+ describe :apply_to_text do
+
+ it 'should do basic text substitution' do
+ body = "I don't know why you say goodbye"
+ @censor_rule.apply_to_text!(body)
+ body.should == "I don't know why you say hello"
+ end
+
end
- it 'should keep size same for binary substitution' do
- body = "I don't know why you say goodbye"
- orig_body = body.dup
- @censor_rule.apply_to_binary!(body)
- body.size.should == orig_body.size
- body.should == "I don't know why you say xxxxxxx"
- body.should_not == orig_body # be sure duplicated as expected
+ describe :apply_to_binary do
+
+ it 'should keep size same for binary substitution' do
+ body = "I don't know why you say goodbye"
+ orig_body = body.dup
+ @censor_rule.apply_to_binary!(body)
+ body.size.should == orig_body.size
+ body.should == "I don't know why you say xxxxxxx"
+ body.should_not == orig_body # be sure duplicated as expected
+ end
+
+ it 'should handle a UTF-8 rule and ASCII-8BIT text' do
+ body = "I don't know why you say g‘oodbye"
+ body.force_encoding("ASCII-8BIT") if String.method_defined?(:encode)
+ @censor_rule.text = 'g‘oodbye'
+ @censor_rule.apply_to_binary!(body)
+ body.should == "I don't know why you say xxxxxxxxxx"
+ end
+
end
end
@@ -121,6 +137,26 @@ xxxxxxxxx
BODY
end
+ it "handles a UTF-8 rule with ASCII-8BIT text" do
+ @censor_rule.text = "--PRIVATE.*--P‘RIVATE"
+ @body =
+<<BODY
+Some public information
+--PRIVATE
+Some private information
+--P‘RIVATE
+BODY
+ @body.force_encoding('ASCII-8BIT') if String.method_defined?(:encode)
+ @censor_rule.apply_to_binary!(@body)
+ @body.should ==
+<<BODY
+Some public information
+xxxxxxxxx
+xxxxxxxxxxxxxxxxxxxxxxxx
+xxxxxxxxxxxx
+BODY
+ end
+
end
end
diff --git a/spec/models/foi_attachment_spec.rb b/spec/models/foi_attachment_spec.rb
index 9583f4c76..b383e5d09 100644
--- a/spec/models/foi_attachment_spec.rb
+++ b/spec/models/foi_attachment_spec.rb
@@ -50,6 +50,80 @@ describe FoiAttachment do
end
+ describe :body do
+
+ it 'returns a binary encoded string when newly created' do
+ foi_attachment = FactoryGirl.create(:body_text)
+ if String.method_defined?(:encode)
+ expect(foi_attachment.body.encoding.to_s).to eq('ASCII-8BIT')
+ end
+ end
+
+
+ it 'returns a binary encoded string when saved' do
+ foi_attachment = FactoryGirl.create(:body_text)
+ foi_attachment = FoiAttachment.find(foi_attachment)
+ if String.method_defined?(:encode)
+ expect(foi_attachment.body.encoding.to_s).to eq('ASCII-8BIT')
+ end
+ end
+
+ end
+
+ describe :body_as_text do
+
+ it 'has a valid UTF-8 string when newly created' do
+ foi_attachment = FactoryGirl.create(:body_text)
+ if String.method_defined?(:encode)
+ expect(foi_attachment.body_as_text.string.encoding.to_s).to eq('UTF-8')
+ expect(foi_attachment.body_as_text.string.valid_encoding?).to be_true
+ end
+ end
+
+ it 'has a valid UTF-8 string when saved' do
+ foi_attachment = FactoryGirl.create(:body_text)
+ foi_attachment = FoiAttachment.find(foi_attachment)
+ if String.method_defined?(:encode)
+ expect(foi_attachment.body_as_text.string.encoding.to_s).to eq('UTF-8')
+ expect(foi_attachment.body_as_text.string.valid_encoding?).to be_true
+ end
+ end
+
+
+ it 'has a true scrubbed? value if the body has been coerced to valid UTF-8' do
+ foi_attachment = FactoryGirl.create(:body_text)
+ foi_attachment.body = "\x0FX\x1C\x8F\xA4\xCF\xF6\x8C\x9D\xA7\x06\xD9\xF7\x90lo"
+ expect(foi_attachment.body_as_text.scrubbed?).to be_true
+ end
+
+ it 'has a false scrubbed? value if the body has not been coerced to valid UTF-8' do
+ foi_attachment = FactoryGirl.create(:body_text)
+ foi_attachment.body = "κόσμε"
+ expect(foi_attachment.body_as_text.scrubbed?).to be_false
+ end
+
+ end
+
+ describe :default_body do
+
+ it 'returns valid UTF-8 for a text attachment' do
+ foi_attachment = FactoryGirl.create(:body_text)
+ if String.method_defined?(:encode)
+ expect(foi_attachment.default_body.encoding.to_s).to eq('UTF-8')
+ expect(foi_attachment.default_body.valid_encoding?).to be_true
+ end
+ end
+
+ it 'returns binary for a PDF attachment' do
+ foi_attachment = FactoryGirl.create(:pdf_attachment)
+ if String.method_defined?(:encode)
+ expect(foi_attachment.default_body.encoding.to_s).to eq('ASCII-8BIT')
+ end
+ end
+
+ end
+
+
describe :ensure_filename! do
it 'should create a filename for an instance with a blank filename' do
diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb
index ca94c59a8..3d14127f4 100644
--- a/spec/models/public_body_spec.rb
+++ b/spec/models/public_body_spec.rb
@@ -102,8 +102,44 @@ describe PublicBody do
end
end
end
-end
+ describe :set_api_key do
+
+ it 'generates and sets an API key' do
+ SecureRandom.stub(:base64).and_return('APIKEY')
+ body = PublicBody.new
+ body.set_api_key
+ expect(body.api_key).to eq('APIKEY')
+ end
+
+ it 'does not overwrite an existing API key' do
+ SecureRandom.stub(:base64).and_return('APIKEY')
+ body = PublicBody.new(:api_key => 'EXISTING')
+ body.set_api_key
+ expect(body.api_key).to eq('EXISTING')
+ end
+
+ end
+
+ describe :set_api_key! do
+
+ it 'generates and sets an API key' do
+ SecureRandom.stub(:base64).and_return('APIKEY')
+ body = PublicBody.new
+ body.set_api_key!
+ expect(body.api_key).to eq('APIKEY')
+ end
+
+ it 'overwrites an existing API key' do
+ SecureRandom.stub(:base64).and_return('APIKEY')
+ body = PublicBody.new(:api_key => 'EXISTING')
+ body.set_api_key!
+ expect(body.api_key).to eq('APIKEY')
+ end
+
+ end
+
+end
describe PublicBody, " using tags" do
before do