diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/factories/snippets.rb | 4 | ||||
-rw-r--r-- | spec/helpers/snippets_helper_spec.rb | 88 | ||||
-rw-r--r-- | spec/lib/gitlab/visibility_level_spec.rb | 12 | ||||
-rw-r--r-- | spec/models/snippet_spec.rb | 40 |
4 files changed, 136 insertions, 8 deletions
diff --git a/spec/factories/snippets.rb b/spec/factories/snippets.rb index 9c3a0fbe9b3..0b606f3daa1 100644 --- a/spec/factories/snippets.rb +++ b/spec/factories/snippets.rb @@ -12,6 +12,10 @@ FactoryBot.define do visibility_level Snippet::PUBLIC end + trait :secret do + visibility_level Snippet::SECRET + end + trait :internal do visibility_level Snippet::INTERNAL end diff --git a/spec/helpers/snippets_helper_spec.rb b/spec/helpers/snippets_helper_spec.rb index ce5e037f88d..8f54605e8e8 100644 --- a/spec/helpers/snippets_helper_spec.rb +++ b/spec/helpers/snippets_helper_spec.rb @@ -3,7 +3,91 @@ require 'spec_helper' describe SnippetsHelper do include IconsHelper - describe '#embedded_snippet_raw_button' do + describe '.reliable_snippet_path' do + context 'personal snippets' do + context 'public' do + it 'gives a full path' do + snippet = create(:personal_snippet, :public) + + expect(reliable_snippet_path(snippet)).to eq("/snippets/#{snippet.id}") + end + end + + context 'secret' do + it 'gives a full path, including secret word' do + snippet = create(:personal_snippet, :secret) + + expect(reliable_snippet_path(snippet)).to match(%r{/snippets/#{snippet.id}\?secret=\w+}) + end + end + end + + context 'project snippets' do + it 'gives a full path' do + snippet = create(:project_snippet, :public) + + expect(reliable_snippet_path(snippet)).to eq("/#{snippet.project.full_path}/snippets/#{snippet.id}") + end + end + end + + describe '.reliable_snippet_url' do + context 'personal snippets' do + context 'public' do + it 'gives a full url' do + snippet = create(:personal_snippet, :public) + + expect(reliable_snippet_url(snippet)).to eq("http://test.host/snippets/#{snippet.id}") + end + end + + context 'secret' do + it 'gives a full url, including secret word' do + snippet = create(:personal_snippet, :secret) + + expect(reliable_snippet_url(snippet)).to match(%r{http://test.host/snippets/#{snippet.id}\?secret=\w+}) + end + end + end + + context 'project snippets' do + it 'gives a full url' do + snippet = create(:project_snippet, :public) + + expect(reliable_snippet_url(snippet)).to eq("http://test.host/#{snippet.project.full_path}/snippets/#{snippet.id}") + end + end + end + + describe '.shareable_snippets_link' do + context 'personal snippets' do + context 'public' do + it 'gives a full link' do + snippet = create(:personal_snippet, :public) + + expect(reliable_snippet_url(snippet)).to eq("http://test.host/snippets/#{snippet.id}") + end + end + + context 'secret' do + it 'gives a full link, including secret word' do + snippet = create(:personal_snippet, :secret) + + expect(reliable_snippet_url(snippet)).to match(%r{http://test.host/snippets/#{snippet.id}\?secret=\w+}) + end + end + end + + context 'project snippets' do + it 'gives a full link' do + snippet = create(:project_snippet, :public) + + expect(reliable_snippet_url(snippet)).to eq("http://test.host/#{snippet.project.full_path}/snippets/#{snippet.id}") + end + end + end + + describe '.embedded_snippet_raw_button' do it 'gives view raw button of embedded snippets for project snippets' do @snippet = create(:project_snippet, :public) @@ -17,7 +101,7 @@ describe SnippetsHelper do end end - describe '#embedded_snippet_download_button' do + describe '.embedded_snippet_download_button' do it 'gives download button of embedded snippets for project snippets' do @snippet = create(:project_snippet, :public) diff --git a/spec/lib/gitlab/visibility_level_spec.rb b/spec/lib/gitlab/visibility_level_spec.rb index 82116b8f57e..4950636021d 100644 --- a/spec/lib/gitlab/visibility_level_spec.rb +++ b/spec/lib/gitlab/visibility_level_spec.rb @@ -182,12 +182,12 @@ describe Gitlab::VisibilityLevel do describe '.string_values' do it 'returns an Array of const values (including Secret)' do expect(described_class.string_values) - .to eq([ - 'private', - 'secret', - 'internal', - 'public' - ]) + .to eq(%w{ + private + secret + internal + public + }) end end end diff --git a/spec/models/snippet_spec.rb b/spec/models/snippet_spec.rb index 3524cdae3b8..0d47041ded4 100644 --- a/spec/models/snippet_spec.rb +++ b/spec/models/snippet_spec.rb @@ -462,4 +462,44 @@ describe Snippet do end end end + + describe '#ensure_secret_added_if_needed' do + let(:snippet) { create(:snippet) } + + context 'visibility_level is NOT SECRET' do + it 'assigns a random hex value' do + snippet.visibility_level = Gitlab::VisibilityLevel::PUBLIC + snippet.save + expect(snippet.secret).to be_nil + end + end + + context 'visibility_level is SECRET' do + it 'assigns a random hex value' do + snippet.visibility_level = Gitlab::VisibilityLevel::SECRET + snippet.save + expect(snippet.secret).not_to be_nil + end + end + end + + describe '#visibility_secret?' do + let(:snippet) { create(:snippet) } + + context 'for a Snippet that is not Secret' do + it 'returns false' do + snippet.visibility_level = Gitlab::VisibilityLevel::PUBLIC + snippet.save + expect(snippet.visibility_secret?).to be_falsey + end + end + + context 'for a Snippet that is Secret' do + it 'returns true' do + snippet.visibility_level = Gitlab::VisibilityLevel::SECRET + snippet.save + expect(snippet.visibility_secret?).to be_truthy + end + end + end end |