debian-mirror-gitlab/spec/lib/gitlab/graphql/markdown_field_spec.rb

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

111 lines
4.1 KiB
Ruby
Raw Permalink Normal View History

2019-09-30 21:07:59 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Graphql::MarkdownField do
2021-01-03 14:25:43 +05:30
include Gitlab::Routing
2022-07-23 23:45:48 +05:30
include GraphqlHelpers
2021-01-03 14:25:43 +05:30
2019-09-30 21:07:59 +05:30
describe '.markdown_field' do
it 'creates the field with some default attributes' do
field = class_with_markdown_field(:test_html, null: true, method: :hello).fields['testHtml']
expect(field.name).to eq('testHtml')
2023-04-23 21:23:45 +05:30
expect(field.description).to eq('GitLab Flavored Markdown rendering of `hello`')
2021-10-27 15:23:28 +05:30
expect(field.type).to eq(GraphQL::Types::String)
2022-05-07 20:08:51 +05:30
expect(field.complexity).to eq(5)
2019-09-30 21:07:59 +05:30
end
context 'developer warnings' do
2021-01-03 14:25:43 +05:30
let_it_be(:expected_error) { /Only `method` is allowed to specify the markdown field/ }
2019-09-30 21:07:59 +05:30
it 'raises when passing a resolver' do
expect { class_with_markdown_field(:test_html, null: true, resolver: 'not really') }
.to raise_error(expected_error)
end
end
context 'resolving markdown' do
2021-01-03 14:25:43 +05:30
let_it_be(:note) { build(:note, note: '# Markdown!') }
let_it_be(:expected_markdown) { '<h1 data-sourcepos="1:1-1:11" dir="auto">Markdown!</h1>' }
2022-07-23 23:45:48 +05:30
let_it_be(:query) { GraphQL::Query.new(empty_schema, document: nil, context: {}, variables: {}) }
2021-01-03 14:25:43 +05:30
let_it_be(:context) { GraphQL::Query::Context.new(query: query, values: {}, object: nil) }
let(:type_class) { class_with_markdown_field(:note_html, null: false) }
let(:type_instance) { type_class.authorized_new(note, context) }
let(:field) { type_class.fields['noteHtml'] }
2019-09-30 21:07:59 +05:30
it 'renders markdown from the same property as the field name without the `_html` suffix' do
2022-05-07 20:08:51 +05:30
expect(field.resolve(type_instance, {}, context)).to eq(expected_markdown)
2021-01-03 14:25:43 +05:30
end
context 'when a `method` argument is passed' do
let(:type_class) { class_with_markdown_field(:test_html, null: false, method: :note) }
let(:field) { type_class.fields['testHtml'] }
2019-09-30 21:07:59 +05:30
2021-01-03 14:25:43 +05:30
it 'renders markdown from a specific property' do
2022-05-07 20:08:51 +05:30
expect(field.resolve(type_instance, {}, context)).to eq(expected_markdown)
2021-01-03 14:25:43 +05:30
end
2019-09-30 21:07:59 +05:30
end
2022-07-23 23:45:48 +05:30
context 'when a block is passed for the resolved object' do
let(:type_class) do
class_with_markdown_field(:note_html, null: false) do |resolved_object|
resolved_object.object
end
end
let(:type_instance) { type_class.authorized_new(class_wrapped_object(note), context) }
it 'renders markdown from the same property as the field name without the `_html` suffix' do
expect(field.resolve(type_instance, {}, context)).to eq(expected_markdown)
end
end
2021-01-03 14:25:43 +05:30
describe 'basic verification that references work' do
let_it_be(:project) { create(:project, :public) }
2021-04-29 21:17:54 +05:30
2021-01-03 14:25:43 +05:30
let(:issue) { create(:issue, project: project) }
let(:note) { build(:note, note: "Referencing #{issue.to_reference(full: true)}") }
it 'renders markdown correctly' do
2022-05-07 20:08:51 +05:30
expect(field.resolve(type_instance, {}, context)).to include(issue_path(issue))
2021-01-03 14:25:43 +05:30
end
context 'when the issue is not publicly accessible' do
let_it_be(:project) { create(:project, :private) }
it 'hides the references from users that are not allowed to see the reference' do
2022-05-07 20:08:51 +05:30
expect(field.resolve(type_instance, {}, context)).not_to include(issue_path(issue))
2021-01-03 14:25:43 +05:30
end
it 'shows the reference to users that are allowed to see it' do
2022-04-04 11:22:00 +05:30
context = GraphQL::Query::Context.new(query: query, values: { current_user: project.first_owner }, object: nil)
2021-01-03 14:25:43 +05:30
type_instance = type_class.authorized_new(note, context)
2019-09-30 21:07:59 +05:30
2022-05-07 20:08:51 +05:30
expect(field.resolve(type_instance, {}, context)).to include(issue_path(issue))
2021-01-03 14:25:43 +05:30
end
end
2019-09-30 21:07:59 +05:30
end
end
end
2022-07-23 23:45:48 +05:30
def class_with_markdown_field(name, **args, &blk)
2021-01-03 14:25:43 +05:30
Class.new(Types::BaseObject) do
2019-09-30 21:07:59 +05:30
prepend Gitlab::Graphql::MarkdownField
2021-01-03 14:25:43 +05:30
graphql_name 'MarkdownFieldTest'
2019-09-30 21:07:59 +05:30
2022-07-23 23:45:48 +05:30
markdown_field name, **args, &blk
2019-09-30 21:07:59 +05:30
end
end
2022-07-23 23:45:48 +05:30
def class_wrapped_object(object)
Class.new do
def initialize(object)
@object = object
end
attr_accessor :object
end.new(object)
end
2019-09-30 21:07:59 +05:30
end