raise BadKey in StoredFile#each if file has been opened without a password
authorLunar <lunar@anargeek.net>
Tue, 6 Mar 2012 09:50:45 +0000 (10:50 +0100)
committerLunar <lunar@anargeek.net>
Fri, 23 Mar 2012 17:11:28 +0000 (18:11 +0100)
lib/coquelicot/stored_file.rb
spec/coquelicot/stored_file_spec.rb

index ed5fc5a..e5e8335 100644 (file)
@@ -84,6 +84,8 @@ module Coquelicot
 
     # used by Rack streaming mechanism
     def each
+      raise BadKey.new if @cipher.nil?
+
       # output content
       yield @initial_content
       @initial_content = nil
index 68be702..1b701ba 100644 (file)
@@ -307,13 +307,23 @@ module Coquelicot
     end
 
     describe '#each' do
-      let(:stored_file) { StoredFile.open(stored_file_path, 'secret') }
-      it 'should output the whole content with several yields' do
-        buf = ''
-        stored_file.each do |data|
-          buf << data
+      context 'when the right pass has been given' do
+        let(:stored_file) { StoredFile.open(stored_file_path, 'secret') }
+        it 'should output the whole content with several yields' do
+          buf = ''
+          stored_file.each do |data|
+            buf << data
+          end
+          buf.should == reference['Content']
+        end
+      end
+      context 'when no password has been given' do
+        let(:stored_file) { StoredFile.open(stored_file_path) }
+        it 'should raise BadKey' do
+          expect {
+            stored_file.each
+          }.to raise_error(BadKey)
         end
-        buf.should == reference['Content']
       end
     end