debian-mirror-gitlab/spec/models/blob_viewer/package_json_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

147 lines
3.5 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
require 'spec_helper'
2023-06-20 00:43:36 +05:30
RSpec.describe BlobViewer::PackageJson, feature_category: :source_code_management do
2017-09-10 17:25:29 +05:30
include FakeBlobHelpers
let(:project) { build_stubbed(:project) }
let(:data) do
<<-SPEC.strip_heredoc
{
"name": "module-name",
"version": "10.3.1"
}
SPEC
end
2020-10-24 23:57:45 +05:30
2017-09-10 17:25:29 +05:30
let(:blob) { fake_blob(path: 'package.json', data: data) }
2020-03-13 15:44:24 +05:30
2017-09-10 17:25:29 +05:30
subject { described_class.new(blob) }
describe '#package_name' do
it 'returns the package name' do
expect(subject).to receive(:prepare!)
expect(subject.package_name).to eq('module-name')
end
end
2018-03-17 18:26:18 +05:30
2021-12-11 22:18:48 +05:30
context 'yarn' do
let(:data) do
<<-SPEC.strip_heredoc
{
"name": "module-name",
"version": "10.3.1",
"engines": {
"yarn": "^2.4.0"
}
}
SPEC
end
let(:blob) { fake_blob(path: 'package.json', data: data) }
subject { described_class.new(blob) }
describe '#package_url' do
it 'returns the package URL', :aggregate_failures do
expect(subject).to receive(:prepare!)
expect(subject.package_url).to eq("https://yarnpkg.com/package/#{subject.package_name}")
end
end
describe '#manager_url' do
it 'returns the manager URL', :aggregate_failures do
expect(subject).to receive(:prepare!)
expect(subject.manager_url).to eq("https://yarnpkg.com/")
end
end
2023-06-20 00:43:36 +05:30
context 'when json is an array' do
let(:data) { '[]' }
it 'does not raise an error', :aggregate_failures do
expect(subject).to receive(:prepare!)
expect { subject.yarn? }.not_to raise_error
expect(subject.yarn?).to be_falsey
end
end
2021-12-11 22:18:48 +05:30
end
context 'npm' do
describe '#package_url' do
it 'returns the package URL', :aggregate_failures do
expect(subject).to receive(:prepare!)
expect(subject.package_url).to eq("https://www.npmjs.com/package/#{subject.package_name}")
end
end
describe '#manager_url' do
it 'returns the manager URL', :aggregate_failures do
expect(subject).to receive(:prepare!)
2018-03-17 18:26:18 +05:30
2021-12-11 22:18:48 +05:30
expect(subject.manager_url).to eq("https://www.npmjs.com/")
end
2018-03-17 18:26:18 +05:30
end
end
describe '#package_type' do
it 'returns "package"' do
expect(subject).to receive(:prepare!)
expect(subject.package_type).to eq('package')
end
end
context 'when package.json has "private": true' do
2018-11-08 19:23:39 +05:30
let(:homepage) { 'http://example.com' }
2018-03-17 18:26:18 +05:30
let(:data) do
<<-SPEC.strip_heredoc
{
"name": "module-name",
"version": "10.3.1",
"private": true,
2018-11-08 19:23:39 +05:30
"homepage": #{homepage.to_json}
2018-03-17 18:26:18 +05:30
}
SPEC
end
2020-10-24 23:57:45 +05:30
2018-03-17 18:26:18 +05:30
let(:blob) { fake_blob(path: 'package.json', data: data) }
2020-03-13 15:44:24 +05:30
2018-03-17 18:26:18 +05:30
subject { described_class.new(blob) }
describe '#package_url' do
2018-11-08 19:23:39 +05:30
context 'when the homepage has a valid URL' do
it 'returns homepage URL' do
expect(subject).to receive(:prepare!)
expect(subject.package_url).to eq(homepage)
end
end
context 'when the homepage has an invalid URL' do
let(:homepage) { 'javascript:alert()' }
it 'returns nil' do
expect(subject).to receive(:prepare!)
2018-03-17 18:26:18 +05:30
2018-11-08 19:23:39 +05:30
expect(subject.package_url).to be_nil
end
2018-03-17 18:26:18 +05:30
end
end
describe '#package_type' do
it 'returns "private package"' do
expect(subject).to receive(:prepare!)
expect(subject.package_type).to eq('private package')
end
end
end
2017-09-10 17:25:29 +05:30
end