2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-02-01 12:40:48 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
require "file_store/s3_store"
|
|
|
|
|
|
|
|
describe Jobs::UpdateS3Inventory do
|
|
|
|
before do
|
2020-09-14 19:32:25 +08:00
|
|
|
setup_s3
|
|
|
|
SiteSetting.s3_upload_bucket = "special-bucket"
|
2019-02-01 12:40:48 +08:00
|
|
|
SiteSetting.enable_s3_inventory = true
|
|
|
|
|
|
|
|
store = FileStore::S3Store.new
|
|
|
|
@client = Aws::S3::Client.new(stub_responses: true)
|
|
|
|
store.s3_helper.stubs(:s3_client).returns(@client)
|
|
|
|
Discourse.stubs(:store).returns(store)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "updates the bucket policy and inventory configuration in S3" do
|
|
|
|
id = "original"
|
2019-02-06 23:21:28 +08:00
|
|
|
path = File.join(S3Inventory::INVENTORY_PREFIX, S3Inventory::INVENTORY_VERSION)
|
|
|
|
|
2019-02-01 12:40:48 +08:00
|
|
|
@client.expects(:put_bucket_policy).with(
|
2020-09-14 19:32:25 +08:00
|
|
|
bucket: "special-bucket",
|
|
|
|
policy: %Q|{"Version":"2012-10-17","Statement":[{"Sid":"InventoryAndAnalyticsPolicy","Effect":"Allow","Principal":{"Service":"s3.amazonaws.com"},"Action":["s3:PutObject"],"Resource":["arn:aws:s3:::special-bucket/#{path}/*"],"Condition":{"ArnLike":{"aws:SourceArn":"arn:aws:s3:::special-bucket"},"StringEquals":{"s3:x-amz-acl":"bucket-owner-full-control"}}}]}|
|
2019-02-01 12:40:48 +08:00
|
|
|
)
|
|
|
|
@client.expects(:put_bucket_inventory_configuration)
|
|
|
|
@client.expects(:put_bucket_inventory_configuration).with(
|
2020-09-14 19:32:25 +08:00
|
|
|
bucket: "special-bucket",
|
2019-02-01 12:40:48 +08:00
|
|
|
id: id,
|
|
|
|
inventory_configuration: {
|
|
|
|
destination: {
|
|
|
|
s3_bucket_destination: {
|
2020-09-14 19:32:25 +08:00
|
|
|
bucket: "arn:aws:s3:::special-bucket",
|
2019-02-06 23:21:28 +08:00
|
|
|
prefix: path,
|
2019-02-01 12:40:48 +08:00
|
|
|
format: "CSV"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
filter: {
|
|
|
|
prefix: id
|
|
|
|
},
|
|
|
|
is_enabled: true,
|
|
|
|
id: id,
|
|
|
|
included_object_versions: "Current",
|
|
|
|
optional_fields: ["ETag"],
|
|
|
|
schedule: { frequency: "Daily" }
|
|
|
|
},
|
|
|
|
use_accelerate_endpoint: false
|
|
|
|
)
|
|
|
|
|
|
|
|
described_class.new.execute(nil)
|
|
|
|
end
|
|
|
|
|
2020-01-16 20:57:57 +08:00
|
|
|
it "doesn't update the policy with s3_configure_inventory_policy disabled" do
|
|
|
|
SiteSetting.s3_configure_inventory_policy = false
|
|
|
|
|
|
|
|
@client.expects(:put_bucket_policy).never
|
|
|
|
@client.expects(:put_bucket_inventory_configuration).never
|
|
|
|
|
|
|
|
described_class.new.execute(nil)
|
|
|
|
end
|
2019-02-01 12:40:48 +08:00
|
|
|
end
|