2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-31 23:40:48 -05:00
|
|
|
require "file_store/s3_store"
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe Jobs::UpdateS3Inventory do
|
2019-01-31 23:40:48 -05:00
|
|
|
before do
|
2020-09-14 07:32:25 -04:00
|
|
|
setup_s3
|
|
|
|
SiteSetting.s3_upload_bucket = "special-bucket"
|
2019-01-31 23:40:48 -05: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 10:21:28 -05:00
|
|
|
path = File.join(S3Inventory::INVENTORY_PREFIX, S3Inventory::INVENTORY_VERSION)
|
|
|
|
|
2019-01-31 23:40:48 -05:00
|
|
|
@client.expects(:put_bucket_policy).with(
|
2020-09-14 07:32:25 -04: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-01-31 23:40:48 -05:00
|
|
|
)
|
|
|
|
@client.expects(:put_bucket_inventory_configuration)
|
|
|
|
@client.expects(:put_bucket_inventory_configuration).with(
|
2020-09-14 07:32:25 -04:00
|
|
|
bucket: "special-bucket",
|
2019-01-31 23:40:48 -05:00
|
|
|
id: id,
|
|
|
|
inventory_configuration: {
|
|
|
|
destination: {
|
|
|
|
s3_bucket_destination: {
|
2020-09-14 07:32:25 -04:00
|
|
|
bucket: "arn:aws:s3:::special-bucket",
|
2019-02-06 10:21:28 -05:00
|
|
|
prefix: path,
|
2019-01-31 23:40:48 -05: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 07:57:57 -05: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-01-31 23:40:48 -05:00
|
|
|
end
|