require 'spec_helper' class ExamplePackage < Kosmos::Package title 'Example' aliases 'specimen', 'illustration' url 'http://www.example.com/releases/release-0-1.zip' end describe Kosmos::Package do let(:example_url) { 'http://www.example.com/releases/release-0-1.zip' } subject { ExamplePackage } it 'has a full name' do expect(subject.title).to eq 'Example' end describe 'downloading' do before { FakeFS.activate! } after { FakeFS.deactivate! } let(:example_zip) do Zip::File.open('example', Zip::File::CREATE) {} File.read('example') end let(:redirected_url) { 'http://example.com/latest' } let(:schemeless_url) { '//example.com/latest' } before do stub_request(:get, example_url).to_return(body: example_zip) stub_request(:get, redirected_url). to_return(status: 301, headers: {'Location' => example_url}) ExamplePackage.url redirected_url end it 'downloads from the url' do download_file = subject.send(:download!) expect(File.read(download_file)).to eq example_zip end it 'unzips the contents' do Zip::File.should_receive(:open) subject.unzip! end it 'falls back to http if no scheme specified' do ExamplePackage.url schemeless_url download_file = subject.download! expect(File.read(download_file)).to eq example_zip end end describe '#normalize_for_find' do it 'converts all to lowercase' do expect(Kosmos::Package.normalize_for_find('eXaMpLe')).to eq 'example' end it 'converts spaces to dashes' do expect(Kosmos::Package.normalize_for_find('many words')). to eq 'many-words' end end describe '#find' do it 'finds a package by name' do expect(Kosmos::Package.find('Example')).to eq ExamplePackage end it 'finds a package by alias' do expect(Kosmos::Package.find('specimen')).to eq ExamplePackage end end end