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
|
|
|
|
SiteSetting.enable_s3_uploads = true
|
|
|
|
SiteSetting.s3_access_key_id = "abc"
|
|
|
|
SiteSetting.s3_secret_access_key = "def"
|
|
|
|
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(
|
|
|
|
bucket: "bucket",
|
2020-01-16 20:57:57 +08:00
|
|
|
policy: %Q|{"Version":"2012-10-17","Statement":[{"Sid":"InventoryAndAnalyticsPolicy","Effect":"Allow","Principal":{"Service":"s3.amazonaws.com"},"Action":["s3:PutObject"],"Resource":["arn:aws:s3:::bucket/#{path}/*"],"Condition":{"ArnLike":{"aws:SourceArn":"arn:aws:s3:::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(
|
|
|
|
bucket: "bucket",
|
|
|
|
id: id,
|
|
|
|
inventory_configuration: {
|
|
|
|
destination: {
|
|
|
|
s3_bucket_destination: {
|
|
|
|
bucket: "arn:aws:s3:::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
|