summaryrefslogtreecommitdiff
path: root/spec/unit/python
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-03-18 20:43:11 -0600
committermo khan <mo.khan@gmail.com>2020-03-18 20:43:11 -0600
commit39341b67f226590a30f2b70fe3692e6e064d2cdb (patch)
tree5982b5d1cda550edfeb03436cdc41dde361418c3 /spec/unit/python
parent0309a0968564f269b7fb4e00a3c23ce0af3d4cdb (diff)
Extract python namespace
Diffstat (limited to 'spec/unit/python')
-rw-r--r--spec/unit/python/pypi_spec.rb80
1 files changed, 80 insertions, 0 deletions
diff --git a/spec/unit/python/pypi_spec.rb b/spec/unit/python/pypi_spec.rb
new file mode 100644
index 0000000..aa87025
--- /dev/null
+++ b/spec/unit/python/pypi_spec.rb
@@ -0,0 +1,80 @@
+# frozen_string_literal: true
+
+RSpec.describe Spandx::Python::PyPI do
+ describe '#definition_for' do
+ subject { described_class.new }
+
+ let(:source) { 'pypi.org' }
+ let(:package) { 'six' }
+ let(:version) { '1.13.0' }
+ let(:successful_response_body) do
+ JSON.generate(
+ info: {
+ name: package,
+ version: version
+ }
+ )
+ end
+
+ context 'when the default source is reachable' do
+ before do
+ stub_request(:get, "https://#{source}/pypi/#{package}/#{version}/json")
+ .to_return(status: 200, body: successful_response_body)
+ end
+
+ specify do
+ expect(subject.definition_for(package, version)).to include(
+ 'name' => package,
+ 'version' => version
+ )
+ end
+ end
+
+ context 'when the response redirects to a different location' do
+ let(:redirect_url) { "https://#{source}/pypi/#{SecureRandom.uuid}" }
+
+ before do
+ stub_request(:get, "https://#{source}/pypi/#{package}/#{version}/json")
+ .to_return(status: 301, headers: { 'Location' => redirect_url })
+
+ stub_request(:get, redirect_url)
+ .to_return(status: 200, body: successful_response_body)
+ end
+
+ specify do
+ expect(subject.definition_for(package, version)).to include(
+ 'name' => package,
+ 'version' => version
+ )
+ end
+ end
+
+ context 'when stuck in an infinite redirect loop' do
+ before do
+ url = "https://#{source}/pypi/#{package}/#{version}/json"
+
+ 11.times do |n|
+ redirect_url = "#{url}#{n}"
+ stub_request(:get, url)
+ .to_return(status: 301, headers: { 'Location' => redirect_url })
+ url = redirect_url
+ end
+ end
+
+ it 'gives up after `n` attempts' do
+ expect(subject.definition_for(package, version)).to be_empty
+ end
+ end
+
+ context 'when the source is not reachable' do
+ before do
+ stub_request(:get, "https://#{source}/pypi/#{package}/#{version}/json")
+ .to_timeout
+ end
+
+ it 'fails gracefully' do
+ expect(subject.definition_for(package, version)).to be_empty
+ end
+ end
+ end
+end