-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support importing original claim with fact check (#1928)
* Support importing original claim along with fact-check This update enhances the Check API by allowing the `createProjectMedia` GraphQL mutation to import original claims alongside fact-checks. The new functionality supports creating media from file URLs, regular URLs, and text. A new argument, `set_original_claim`, is introduced to handle the original claim data. - Added `set_original_claim` argument to the `Create` mutation in `project_media_mutations.rb`. - Declared `set_original_claim` as an accessor in `project_media.rb`. - Added `create_original_claim` callback before validation on create in `project_media.rb`. - Implemented `create_original_claim` method in `project_media_creators.rb` to handle URLs (creating `UploadedImage`, `UploadedVideo`, `UploadedAudio`, or `Link` media) and text (creating `Claim` media). - Added unit tests in `project_media_test.rb` to cover all media types: link, image, video, audio, and text. * Increase code coverage and fix whitespace issues Increase code coverage and fix whitespace issues flagged by CodeQL * Update GraphQL relay schema Update GraphQL relay schema * Add reviewer feedback * Split original claim tests into multiple tests * Print test name during setup and teardown * Move original claim tests to new files Move original claim tests to new files. Created graphql_controller_11_test.rb and project_media_7_test.rb
- Loading branch information
Showing
7 changed files
with
262 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
require_relative '../test_helper' | ||
|
||
class GraphqlController11Test < ActionController::TestCase | ||
def setup | ||
@controller = Api::V1::GraphqlController.new | ||
TestDynamicAnnotationTables.load! | ||
|
||
@u = create_user | ||
@t = create_team | ||
create_team_user team: @t, user: @u, role: 'admin' | ||
end | ||
|
||
def teardown | ||
User.unstub(:current) | ||
Team.unstub(:current) | ||
User.current = nil | ||
Team.current = nil | ||
end | ||
|
||
test "should create media with various types using set_original_claim" do | ||
p = create_project team: @t | ||
authenticate_with_user(@u) | ||
|
||
# Test for creating media with plain text original claim | ||
query_plain_text = <<~GRAPHQL | ||
mutation { | ||
createProjectMedia(input: { project_id: #{p.id}, set_original_claim: "This is an original claim" }) { | ||
project_media { | ||
id | ||
} | ||
} | ||
} | ||
GRAPHQL | ||
|
||
post :create, params: { query: query_plain_text, team: @t.slug } | ||
assert_response :success | ||
data = JSON.parse(response.body)['data']['createProjectMedia'] | ||
assert_not_nil data['project_media']['id'] | ||
|
||
# Prepare mock responses for URLs | ||
pender_url = CheckConfig.get('pender_url_private') + '/api/medias' | ||
url_types = { | ||
audio: 'http://example.com/audio.mp3', | ||
image: 'http://example.com/image.png', | ||
video: 'http://example.com/video.mp4', | ||
generic: 'http://example.com' | ||
} | ||
|
||
url_types.each do |type, url| | ||
response_body = '{"type":"media","data":{"url":"' + url + '","type":"item"}}' | ||
WebMock.stub_request(:get, pender_url).with({ query: { url: url } }).to_return(body: response_body) | ||
end | ||
|
||
# Test for creating media with audio URL original claim | ||
query_audio = <<~GRAPHQL | ||
mutation { | ||
createProjectMedia(input: { project_id: #{p.id}, set_original_claim: "#{url_types[:audio]}" }) { | ||
project_media { | ||
id | ||
} | ||
} | ||
} | ||
GRAPHQL | ||
|
||
post :create, params: { query: query_audio, team: @t.slug } | ||
assert_response :success | ||
data = JSON.parse(response.body)['data']['createProjectMedia'] | ||
assert_not_nil data['project_media']['id'] | ||
|
||
# Test for creating media with image URL original claim | ||
query_image = <<~GRAPHQL | ||
mutation { | ||
createProjectMedia(input: { project_id: #{p.id}, set_original_claim: "#{url_types[:image]}" }) { | ||
project_media { | ||
id | ||
} | ||
} | ||
} | ||
GRAPHQL | ||
|
||
post :create, params: { query: query_image, team: @t.slug } | ||
assert_response :success | ||
data = JSON.parse(response.body)['data']['createProjectMedia'] | ||
assert_not_nil data['project_media']['id'] | ||
|
||
# Test for creating media with video URL original claim | ||
query_video = <<~GRAPHQL | ||
mutation { | ||
createProjectMedia(input: { project_id: #{p.id}, set_original_claim: "#{url_types[:video]}" }) { | ||
project_media { | ||
id | ||
} | ||
} | ||
} | ||
GRAPHQL | ||
|
||
post :create, params: { query: query_video, team: @t.slug } | ||
assert_response :success | ||
data = JSON.parse(response.body)['data']['createProjectMedia'] | ||
assert_not_nil data['project_media']['id'] | ||
|
||
# Test for creating media with generic URL original claim | ||
query_generic = <<~GRAPHQL | ||
mutation { | ||
createProjectMedia(input: { project_id: #{p.id}, set_original_claim: "#{url_types[:generic]}" }) { | ||
project_media { | ||
id | ||
} | ||
} | ||
} | ||
GRAPHQL | ||
|
||
post :create, params: { query: query_generic, team: @t.slug } | ||
assert_response :success | ||
data = JSON.parse(response.body)['data']['createProjectMedia'] | ||
assert_not_nil data['project_media']['id'] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
require_relative '../test_helper' | ||
require 'tempfile' | ||
|
||
class ProjectMedia7Test < ActiveSupport::TestCase | ||
def setup | ||
require 'sidekiq/testing' | ||
Sidekiq::Testing.fake! | ||
super | ||
create_team_bot login: 'keep', name: 'Keep' | ||
create_verification_status_stuff | ||
end | ||
|
||
test "should create media from original claim URL as Link" do | ||
setup_elasticsearch | ||
|
||
# Mock Pender response for Link | ||
link_url = 'https://example.com' | ||
pender_url = CheckConfig.get('pender_url_private') + '/api/medias' | ||
link_response = { | ||
type: 'media', | ||
data: { | ||
url: link_url, | ||
type: 'item' | ||
} | ||
}.to_json | ||
WebMock.stub_request(:get, pender_url).with(query: { url: link_url }).to_return(body: link_response) | ||
pm_link = create_project_media(set_original_claim: link_url) | ||
assert_equal 'Link', pm_link.media.type | ||
assert_equal link_url, pm_link.media.url | ||
end | ||
|
||
test "should create media from original claim URL as UploadedImage" do | ||
Tempfile.create(['test_image', '.jpg']) do |file| | ||
file.write(File.read(File.join(Rails.root, 'test', 'data', 'rails.png'))) | ||
file.rewind | ||
image_url = "http://example.com/#{file.path.split('/').last}" | ||
WebMock.stub_request(:get, image_url).to_return(body: file.read, headers: { 'Content-Type' => 'image/jpeg' }) | ||
pm_image = create_project_media(set_original_claim: image_url) | ||
assert_equal 'UploadedImage', pm_image.media.type | ||
end | ||
end | ||
|
||
test "should create media from original claim URL as UploadedVideo" do | ||
Tempfile.create(['test_video', '.mp4']) do |file| | ||
file.write(File.read(File.join(Rails.root, 'test', 'data', 'rails.mp4'))) | ||
file.rewind | ||
video_url = "http://example.com/#{file.path.split('/').last}" | ||
WebMock.stub_request(:get, video_url).to_return(body: file.read, headers: { 'Content-Type' => 'video/mp4' }) | ||
pm_video = create_project_media(set_original_claim: video_url) | ||
assert_equal 'UploadedVideo', pm_video.media.type | ||
end | ||
end | ||
|
||
test "should create media from original claim URL as UploadedAudio" do | ||
Tempfile.create(['test_audio', '.mp3']) do |file| | ||
file.write(File.read(File.join(Rails.root, 'test', 'data', 'rails.mp3'))) | ||
file.rewind | ||
audio_url = "http://example.com/#{file.path.split('/').last}" | ||
WebMock.stub_request(:get, audio_url).to_return(body: file.read, headers: { 'Content-Type' => 'audio/mp3' }) | ||
pm_audio = create_project_media(set_original_claim: audio_url) | ||
assert_equal 'UploadedAudio', pm_audio.media.type | ||
end | ||
end | ||
|
||
test "should create media from original claim text as Claim" do | ||
pm_claim = create_project_media(set_original_claim: 'This is a claim.') | ||
assert_equal 'Claim', pm_claim.media.type | ||
assert_equal 'This is a claim.', pm_claim.media.quote | ||
end | ||
end |