Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test graphql image urls #1469

Merged
merged 4 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ group :test do
# gem 'simplecov', require: false
gem 'vcr'
gem 'webmock'
gem 'graphql-client'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ GEM
railties
sprockets-rails
graphql (2.0.9)
graphql-client (0.18.0)
activesupport (>= 3.0)
graphql
groupdate (6.1.0)
activesupport (>= 5.2)
guard (2.18.0)
Expand Down Expand Up @@ -555,6 +558,7 @@ DEPENDENCIES
geocoder (~> 1.6.0)
graphiql-rails
graphql
graphql-client
groupdate
guard
guard-livereload (~> 2.5)
Expand Down
12 changes: 10 additions & 2 deletions app/graphql/types/article_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ class ArticleType < Types::BaseObject
method: :body

field :image, String,
description: 'The image of the article (a URL)',
method: :highres_image
description: 'The image of the article (a URL)' #,
ivan-kocienski-gfsc marked this conversation as resolved.
Show resolved Hide resolved

field :date_published, GraphQL::Types::ISO8601DateTime,
description: 'Date that the article was published on PlaceCal',
Expand All @@ -50,5 +49,14 @@ class ArticleType < Types::BaseObject

# creativeWorkStatus: string, from is_draft
# author: person, from user

def image
return nil unless object.article_image_url.present?

url = URI::HTTP.build(Rails.application.default_url_options)
url.path = object.article_image_url

url.to_s
end
end
end
Binary file added test/fixtures/files/good-cat-picture.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
141 changes: 141 additions & 0 deletions test/system/graphql/partner_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
require_relative "../application_system_test_case"

=begin

This is basically a stand alone script that pokes the GraphQL endpoint
running on the server through a network connection (just as how users
will use the endpoint on staging/production). It does not use any
internal code like /test/integration/graphql/*

We could be being overly paranoid here but it may become useful for
catching the kind of bugs that only show up on a real network
connection.

NOTE: we are mainly interested in testing the endpoints that
the Trans Dimension elm app uses.
=end

require 'graphql/client'
require 'graphql/client/http'


module PlaceCalApi
extend self

PING_QUERY_STRING =
"query { ping }"

ARTICLE_CONNECTION_QUERY_STRING = <<-QUERY
query { articleConnection { edges {
node {
articleBody
datePublished
headline
providers { id }
image
} } } }
QUERY

PARTNERS_BY_TAG_QUERY_STRING = <<-QUERY
query($tagId: ID!) {
partnersByTag(tagId: $tagId) {
id
name
description
summary
contact { email, telephone }
url
address { streetAddress, postalCode, addressRegion, geo { latitude, longitude } }
areasServed { name abbreviatedName }
logo
} }
QUERY


def configure(default_url_options)
@url = URI::HTTP::build(default_url_options)
@url.path = '/api/v1/graphql'

@http = GraphQL::Client::HTTP.new(@url.to_s)
@schema = GraphQL::Client.load_schema(@http)
@client = GraphQL::Client.new(schema: @schema, execute: @http)

define_constant 'PingQuery', @client.parse(PING_QUERY_STRING)

define_constant 'ArticleConnectionQuery', @client.parse(ARTICLE_CONNECTION_QUERY_STRING)

define_constant 'PartnersByTagQuery', @client.parse(PARTNERS_BY_TAG_QUERY_STRING)
end

# the API endpoints
def do_ping_query
@client.query PingQuery
end

def do_article_connection_query
@client.query ArticleConnectionQuery
end

def do_partners_by_tag_query(tag_id)
@client.query PartnersByTagQuery, variables: { 'tagId': tag_id }
end

private

def define_constant(name, value)
return if const_defined?(name)
const_set name, value
end
end


class GraphQLPartnerTest < ApplicationSystemTestCase
include ActionDispatch::TestProcess::FixtureFile

setup do
create_default_site

@server = Capybara.current_session.server

app_routes = Rails.application.routes
app_routes.default_url_options[:host] = @server.host
app_routes.default_url_options[:port] = @server.port

PlaceCalApi.configure app_routes.default_url_options
end

test "ping works" do
result = PlaceCalApi.do_ping_query
assert result.errors.empty?
assert result.data.ping =~ /^Hello World! The time is \d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/,
'missing PING data response'
end

test 'news article connection with image url' do
upload = fixture_file_upload('good-cat-picture.jpg')
article = create(:article, article_image: upload)

result = PlaceCalApi.do_article_connection_query

article = result.data.article_connection.edges.first.node.to_h
assert article, 'article node not found'

url = article['image']
assert url =~ %r{\Ahttp://#{@server.host}:#{@server.port}/}, 'article image is not full URL'
end

test 'partners by tag has correct logo url' do
upload = fixture_file_upload('good-cat-picture.jpg')
partner = create(:partner, image: upload)
tag = create(:tag)
partner.tags << tag

result = PlaceCalApi.do_partners_by_tag_query(tag.id)
partner = result.data.partners_by_tag.first.to_h
assert partner

url = partner['logo']
assert url =~ %r{\Ahttp://#{@server.host}:#{@server.port}/}, 'partner logo is not full URL'
end
end