Skip to content

Commit

Permalink
fix: webhook registration key check
Browse files Browse the repository at this point in the history
  • Loading branch information
macite committed Jul 26, 2024
1 parent 527b810 commit 70f095c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
5 changes: 3 additions & 2 deletions app/models/turn_it_in/tii_action_register_webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ def need_to_register_webhook?
end

def register_webhook
raise "TCA_SIGNING_KEY is not set" if data.signing_secret.nil?
key = ENV.fetch('TCA_SIGNING_KEY', nil)
raise "TCA_SIGNING_KEY is not set" if key.nil?

data = TCAClient::WebhookWithSecret.new(
signing_secret: Base64.encode64(ENV.fetch('TCA_SIGNING_KEY', nil)).tr("\n", ''),
signing_secret: Base64.encode64(key).tr("\n", ''),
url: TurnItIn.webhook_url,
event_types: %w[
SIMILARITY_COMPLETE
Expand Down
12 changes: 11 additions & 1 deletion test/models/task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,7 @@ def test_accept_files_checks_they_all_exist

task.task_status = TaskStatus.not_started
task.save!
task.reload

# Now... lets upload a submission with no files
task.accept_submission user, [], user, self, nil, 'ready_for_feedback', nil
Expand All @@ -860,7 +861,16 @@ def test_accept_files_checks_they_all_exist

# Now... lets upload a submission with too many files
begin
task.accept_submission user, [], user, self, nil, 'ready_for_feedback', nil
task.accept_submission user,
[
{
id: 'file0',
name: 'Document 1',
type: 'document',
filename: 'file0.pdf',
"tempfile" => File.new(test_file_path('submissions/1.2P.pdf'))
}
], user, self, nil, 'ready_for_feedback', nil
assert false, 'Should have raised an error with too many files submitted'
rescue StandardError => e
assert_equal :not_started, task.status
Expand Down
10 changes: 8 additions & 2 deletions test/sidekiq/tii_webhooks_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ class TiiWebhooksJobTest < ActiveSupport::TestCase
include TestHelpers::TiiTestHelper

def test_register_webhooks
setup_tii_eula
setup_tii_features_enabled

Doubtfire::Application.config.tii_register_webhook = true

# Will ask for current webhooks
Expand Down Expand Up @@ -32,14 +35,17 @@ def test_register_webhooks
]
)
].to_json,
headers: {})
headers: {}
)

ENV['TCA_SIGNING_KEY'] = 'TESTING'

# and will register the webhooks
register_webhooks_stub = stub_request(:post, "https://#{ENV['TCA_HOST']}/api/v1/webhooks")
.with(tii_headers)
.with(
body: TCAClient::WebhookWithSecret.new(
signing_secret: Base64.encode64(ENV.fetch('TCA_SIGNING_KEY', nil)).tr("\n", '' ),
signing_secret: Base64.encode64(ENV.fetch('TCA_SIGNING_KEY', nil)).tr("\n", ''),
url: TurnItIn.webhook_url,
event_types: [
'SIMILARITY_COMPLETE',
Expand Down

0 comments on commit 70f095c

Please sign in to comment.