From 44adccc6e4d630aca1b1443c50987ead7c20590f Mon Sep 17 00:00:00 2001 From: Aditya Prakash Date: Thu, 16 Sep 2021 11:49:33 +0530 Subject: [PATCH] Add test for downloads controller and sqs worker lograge is ignored since the test for this was not making sense to me --- .../v1/versions/downloads_controller_test.rb | 21 +++++ test/test_helper.rb | 1 + test/unit/sqs_worker_test.rb | 78 +++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 test/functional/api/v1/versions/downloads_controller_test.rb create mode 100644 test/unit/sqs_worker_test.rb diff --git a/test/functional/api/v1/versions/downloads_controller_test.rb b/test/functional/api/v1/versions/downloads_controller_test.rb new file mode 100644 index 00000000000..d57545895fd --- /dev/null +++ b/test/functional/api/v1/versions/downloads_controller_test.rb @@ -0,0 +1,21 @@ +require "test_helper" + +class Api::V1::Versions::DownloadsControllerTest < ActionController::TestCase + context "on GET to index" do + setup do + @rubygem = create(:rubygem, number: "0.1.0") + get :index, params: { version_id: @rubygem.latest_version.number, format: "json" } + end + + should respond_with :gone + end + + context "on GET to search" do + setup do + @rubygem = create(:rubygem, number: "0.1.0") + get :search, params: { version_id: @rubygem.latest_version.number, format: "json" } + end + + should respond_with :gone + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 1efdd2f3250..7e7f66404bb 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,6 +1,7 @@ require "simplecov" SimpleCov.start "rails" do add_filter "lib/tasks" + add_filter "lib/lograge" end ENV["RAILS_ENV"] ||= "test" diff --git a/test/unit/sqs_worker_test.rb b/test/unit/sqs_worker_test.rb new file mode 100644 index 00000000000..abb284283bf --- /dev/null +++ b/test/unit/sqs_worker_test.rb @@ -0,0 +1,78 @@ +require "test_helper" +require_relative "../../lib/shoryuken/sqs_worker" + +class SqsWorkerTest < ActiveSupport::TestCase + setup do + @sqs_worker = SqsWorker.new + @body = { + "Records" => [{ + "eventVersion" => "2.2", + "eventSource" => "aws => s3", + "awsRegion" => "us-west-2", + "eventTime" => "The time, in ISO-8601 format, for example, 1970-01-01T00 => 00 => 00.000Z, when Amazon S3 finished processing the request", + "eventName" => "event-type", + "userIdentity" => { + "principalId" => "Amazon-customer-ID-of-the-user-who-caused-the-event" + }, + "requestParameters" => { + "sourceIPAddress" => "ip-address-where-request-came-from" + }, + "responseElements" => { + "x-amz-request-id" => "Amazon S3 generated request ID", + "x-amz-id-2" => "Amazon S3 host that processed the request" + }, + "s3" => { + "s3SchemaVersion" => "1.0", + "configurationId" => "ID found in the bucket notification configuration", + "bucket" => { + "name" => "bucket-name", + "ownerIdentity" => { + "principalId" => "Amazon-customer-ID-of-the-bucket-owner" + }, + "arn" => "bucket-ARN" + }, + "object" => { + "key" => "object-key", + "size" => "object-size in bytes", + "eTag" => "object eTag", + "versionId" => "object version if bucket is versioning-enabled, otherwise null", + "sequencer" => "a string representation of a hexadecimal value used to determine event sequence, only used with PUTs and DELETEs" + } + }, + "glacierEventData" => { + "restoreEventData" => { + "lifecycleRestorationExpiryTime" => "The time, in ISO-8601 format, for example, 1970-01-01T00 => 00 => 00.000Z, of Restore Expiry", + "lifecycleRestoreStorageClass" => "Source storage class for restore" + } + } + }] + } + end + + context "#perform" do + should "create Logticket" do + StatsD.expects(:increment).with("fastly_log_processor.s3_entry_fetched") + StatsD.expects(:increment).with("fastly_log_processor.enqueued") + assert_difference "Delayed::Job.count", 1 do + @sqs_worker.perform(nil, @body) + end + + log_ticket = LogTicket.last + assert_equal "bucket-name", log_ticket.directory + assert_equal "object-key", log_ticket.key + assert_equal "pending", log_ticket.status + end + + should "not create duplicate LogTicket" do + duplicate_record = @body["Records"].first + @body["Records"] << duplicate_record + + StatsD.expects(:increment).with("fastly_log_processor.s3_entry_fetched") + StatsD.expects(:increment).with("fastly_log_processor.enqueued").twice + StatsD.expects(:increment).with("fastly_log_processor.duplicated") + assert_difference "Delayed::Job.count", 1 do + @sqs_worker.perform(nil, @body) + end + end + end +end