From 979518c3ebc085f1ea65d71324a0ee2c636bb906 Mon Sep 17 00:00:00 2001 From: Jay Joshua Date: Thu, 27 Feb 2025 13:34:07 +0100 Subject: [PATCH] Add more tests for channel in explainer/fact checks --- .../controllers/graphql_controller_12_test.rb | 90 +++++++++++++++++++ test/models/explainer_test.rb | 23 +++-- 2 files changed, 106 insertions(+), 7 deletions(-) diff --git a/test/controllers/graphql_controller_12_test.rb b/test/controllers/graphql_controller_12_test.rb index 112d6e637..b6d6d20e8 100644 --- a/test/controllers/graphql_controller_12_test.rb +++ b/test/controllers/graphql_controller_12_test.rb @@ -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 diff --git a/test/models/explainer_test.rb b/test/models/explainer_test.rb index a0aba9e13..5115d8762 100644 --- a/test/models/explainer_test.rb +++ b/test/models/explainer_test.rb @@ -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