Skip to content

Commit

Permalink
Add more tests for channel in explainer/fact checks
Browse files Browse the repository at this point in the history
  • Loading branch information
jayjay-w committed Feb 27, 2025
1 parent 1adacec commit 979518c
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 7 deletions.
90 changes: 90 additions & 0 deletions test/controllers/graphql_controller_12_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -906,4 +906,94 @@ def teardown
assert_response 400
assert_equal 'This item already exists', JSON.parse(@response.body).dig('errors', 0, 'message')
end

test "should return fact_check channel in team articles query when filtered by channel" do
# Create two fact-checks with different channel values.
pm1 = create_project_media team: @t
cd1 = create_claim_description(project_media: pm1)
fc_manual = create_fact_check(claim_description: cd1, channel: "manual", trashed: false)

pm2 = create_project_media team: @t
cd2 = create_claim_description(project_media: pm2)
fc_api = create_fact_check(claim_description: cd2, channel: "api", trashed: false)

authenticate_with_user(@u)

query = <<-GRAPHQL
query {
team(slug: "#{@t.slug}") {
articles(article_type: "fact-check", channel: "manual") {
edges {
node {
... on FactCheck {
dbid
channel
}
}
}
}
}
}
GRAPHQL
post :create, params: { query: query, team: @t.slug }
assert_response :success
articles = JSON.parse(@response.body)['data']['team']['articles']['edges']
articles.each do |edge|
assert_equal "manual", edge['node']['channel']
end
end

test "should create explainer with default channel via GraphQL mutation" do
# When a regular user creates an explainer without an explicit channel,
# the Article concern callback should set it to "manual".
authenticate_with_user(@u)
query = <<-GRAPHQL
mutation {
createExplainer(input: {
title: "GraphQL Explainer Default",
description: "Test description",
url: "http://test.com",
language: "en"
}) {
explainer {
dbid
channel
}
}
}
GRAPHQL
post :create, params: { query: query, team: @t.slug }
assert_response :success
result = JSON.parse(@response.body)['data']['createExplainer']['explainer']
assert_equal "manual", result["channel"]
end

test "should create fact_check with explicit channel via GraphQL mutation" do
authenticate_with_user(@u)
# Create a project media and claim description for the FactCheck
pm = create_project_media(team: @t)
cd = create_claim_description(project_media: pm)

query = <<-GRAPHQL
mutation {
createFactCheck(input: {
title: "GraphQL FactCheck Explicit",
summary: "Test summary",
language: "en",
url: "http://test.com",
channel: "zapier",
claim_description_id: #{cd.id}
}) {
fact_check {
dbid
channel
}
}
}
GRAPHQL
post :create, params: { query: query, team: @t.slug }
assert_response :success, @response.body
result = JSON.parse(@response.body)['data']['createFactCheck']['fact_check']
assert_equal "zapier", result["channel"]
end
end
23 changes: 16 additions & 7 deletions test/models/explainer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,43 +179,52 @@ def teardown

test "should assign default channel 'manual' for a regular user" do
user = create_user
# Ensure the user has permission by making them an admin of the team
create_team_user(team: @t, user: user, role: 'admin')
User.current = user
# When channel is not provided, it should default to "manual"
ex = create_explainer(channel: nil)
ex = create_explainer(team: @t, channel: nil)
assert_equal "manual", ex.channel
User.current = nil
end

test "should assign default channel 'api' for a BotUser" do
bot = create_bot_user(default: true, approved: true)
# Prevent duplicate installation errors by stubbing out the default bot callback if needed.
Team.any_instance.stubs(:add_default_bots_to_team)
create_team_user(team: @t, user: bot, role: 'admin')
User.current = bot
ex = create_explainer(channel: nil)
ex = create_explainer(team: @t, channel: nil)
assert_equal "api", ex.channel
User.current = nil
end

test "should allow explicit override of channel" do
user = create_user
create_team_user(team: @t, user: user, role: 'admin')
User.current = user
# Even if the default would be "manual", explicitly providing a valid value should keep it.
ex = create_explainer(channel: "imported")
# Explicitly providing a valid channel value ("imported") should be preserved.
ex = create_explainer(team: @t, channel: "imported")
assert_equal "imported", ex.channel
User.current = nil
end

test "should not allow an invalid channel value" do
user = create_user
create_team_user(team: @t, user: user, role: 'admin')
User.current = user
assert_raises ActiveRecord::RecordInvalid do
create_explainer(channel: "invalid")
# Enum assignment raises ArgumentError when an invalid value is given.
assert_raises(ArgumentError) do
create_explainer(team: @t, channel: "invalid")
end
User.current = nil
end

test "should not change channel on update if already set" do
user = create_user
create_team_user(team: @t, user: user, role: 'admin')
User.current = user
ex = create_explainer(channel: "imported")
ex = create_explainer(team: @t, channel: "imported")
ex.title = "Updated Title"
ex.save!
assert_equal "imported", ex.reload.channel
Expand Down

0 comments on commit 979518c

Please sign in to comment.