# encoding: utf-8

load File.expand_path('../../spec_helper.rb', File.dirname(__FILE__))

describe Imap::Backup::Serializer::Directory do

  context '#initialize' do

    it 'should fail if download path file permissions are to lax' do
      stat = stub('File::Stat', :mode => 0345)
      File.should_receive(:stat).with('/base/path').and_return(stat)

      expect do
        Imap::Backup::Serializer::Directory.new('/base/path', 'my_folder')
      end.to raise_error(RuntimeError, "Permissions on '/base/path' should be 0700, not 0345")
    end

  end

  context 'with object' do

    before :each do
      stat = stub('File::Stat', :mode => 0700)
      File.stub!(:stat).with('/base/path').and_return(stat)
      FileUtils.stub!(:mkdir_p).with('/base/path/my_folder')
      FileUtils.stub!(:chmod_R).with('g-wrx,o-wrx', '/base/path/my_folder')
    end

    subject { Imap::Backup::Serializer::Directory.new('/base/path', 'my_folder') }

    context '#uids' do

      it 'should return the backed-up uids' do
        files = ['00000123.json', '000001.json']

        File.should_receive(:exist?).with('/base/path/my_folder').and_return(true)
        Dir.should_receive(:open).with('/base/path/my_folder').and_return(files)

        subject.uids.should == [1, 123]
      end

      it 'should return an empty Array if the directory does not exist' do
        File.should_receive(:exist?).with('/base/path/my_folder').and_return(false)

        subject.uids.should == []
      end

    end

    context '#exist?' do

      it 'should check if the file exists' do
        File.should_receive(:exist?).with(%r{/base/path/my_folder/0+123.json}).and_return(true)

        subject.exist?(123).should be_true
      end

    end

    context '#save' do

      before :each do
        File.stub!(:exist?).with(%r{/base/path/my_folder/0+1234.json}).and_return(true)
        FileUtils.stub!(:chmod).with(0600, /0+1234.json$/)
        @message = {
          'RFC822' => 'the body',
          'other'  => 'xxx'
        }
        @file = stub('File', :write => nil)
        File.stub!(:open) do |&block|
          block.call @file
        end
      end

      it 'should save messages' do
        File.should_receive(:open) do |&block|
          block.call @file
        end
        @file.should_receive(:write).with(/the body/)

        subject.save('1234', @message)
      end

      it 'should JSON encode messages' do
        @message.should_receive(:to_json)

        subject.save('1234', @message)
      end

      it 'should set file permissions' do
        FileUtils.should_receive(:chmod).with(0600, /0+1234.json$/)

        subject.save(1234, @message)
      end
    end

  end

end