File: google_spec.rb

package info (click to toggle)
ruby-asset-sync 2.11.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 420 kB
  • sloc: ruby: 2,080; makefile: 3; javascript: 1
file content (215 lines) | stat: -rw-r--r-- 6,846 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
require File.dirname(__FILE__) + '/../spec_helper'

describe AssetSync do
  include_context "mock Rails without_yml"

  describe 'with initializer' do
    before(:each) do
      AssetSync.config = AssetSync::Config.new
      AssetSync.configure do |config|
        config.fog_provider = 'Google'
        config.fog_directory = 'mybucket'
        config.existing_remote_files = "keep"
      end
    end

    it "should configure provider as Google" do
      expect(AssetSync.config.fog_provider).to eq('Google')
      expect(AssetSync.config).to be_google
    end

    it "should should keep existing remote files" do
      expect(AssetSync.config.existing_remote_files?).to eq(true)
    end

    it "should configure fog_directory" do
      expect(AssetSync.config.fog_directory).to eq("mybucket")
    end

    it "should configure existing_remote_files" do
      expect(AssetSync.config.existing_remote_files).to eq("keep")
    end

    it "should default gzip_compression to false" do
      expect(AssetSync.config.gzip_compression).to be_falsey
    end

    it "should default manifest to false" do
      expect(AssetSync.config.manifest).to be_falsey
    end

    describe "when using S3 interop API" do
      before(:each) do
        AssetSync.configure do |config|
          config.google_storage_access_key_id = 'aaaa'
          config.google_storage_secret_access_key = 'bbbb'
        end
      end

      it "should configure google_storage_access_key_id" do
        expect(AssetSync.config.google_storage_access_key_id).to eq("aaaa")
      end

      it "should configure google_storage_secret_access_key" do
        expect(AssetSync.config.google_storage_secret_access_key).to eq("bbbb")
      end

      it "should return the correct fog_options" do
        expected_fog_options = { google_storage_access_key_id: "aaaa",
                                google_storage_secret_access_key: "bbbb",
                                provider: "Google"}
        expect(AssetSync.config.fog_options).to eq(expected_fog_options)
      end

      it "should not require that google_json_key_location be set" do
        expect(AssetSync.config.valid?).to eq(true)
      end

      it "should require that google_storage_secret_access_key or access_key_id be set" do

        AssetSync.configure do |config|
          config.google_storage_access_key_id = nil
          config.google_storage_secret_access_key = nil
        end

        expect(AssetSync.config.valid?).to eq(false)
      end
    end

    describe "when using service account" do
      before(:each) do
        AssetSync.configure do |config|
          config.google_json_key_location = '/path/to.json'
          config.google_project = 'a-google-project-name'
        end
      end

      it "should configure google_json_key_location" do
        expect(AssetSync.config.google_json_key_location).to eq("/path/to.json")
      end

      it "should return the correct fog_options" do
        expected_fog_options = { google_json_key_location: "/path/to.json",
                                 google_project: 'a-google-project-name',
                                provider: "Google"}
        expect(AssetSync.config.fog_options).to eq(expected_fog_options)
      end
      it "should not require that google_storage_secret_access_key or access_key_id be set" do
        expect(AssetSync.config.valid?).to eq(true)
      end
    end

  end

  describe 'from yml' do
    describe 'when using S3 interop API' do
      before(:each) do
        set_rails_root('google_with_yml')
        AssetSync.config = AssetSync::Config.new
      end

      it "should configure google_storage_access_key_id" do
        expect(AssetSync.config.google_storage_access_key_id).to eq("xxxx")
      end

      it "should configure google_storage_secret_access_key" do
        expect(AssetSync.config.google_storage_secret_access_key).to eq("zzzz")
      end

      it "should not configure google_json_key_location" do
        expect(AssetSync.config.google_json_key_location).to eq(nil)
      end

      it "should configure fog_directory" do
        expect(AssetSync.config.fog_directory).to eq("rails_app_test")
      end

      it "should configure existing_remote_files" do
        expect(AssetSync.config.existing_remote_files).to eq("keep")
      end

      it "should default gzip_compression to false" do
        expect(AssetSync.config.gzip_compression).to be_falsey
      end

      it "should default manifest to false" do
        expect(AssetSync.config.manifest).to be_falsey
      end
    end

    describe 'when using service account API' do
      before(:each) do
        set_rails_root('google_with_service_account_yml')
        AssetSync.config = AssetSync::Config.new
      end

      it "should configure google_json_key_location" do
        expect(AssetSync.config.google_json_key_location).to eq("gcs.json")
      end

      it "should not configure google_storage_secret_access_key" do
        expect(AssetSync.config.google_storage_secret_access_key).to eq(nil)
      end
    end
  end

  describe 'with no configuration' do
    before(:each) do
      AssetSync.config = AssetSync::Config.new
    end

    it "should be invalid" do
      expect{ AssetSync.sync }.to raise_error(::AssetSync::Config::Invalid)
    end
  end

  describe 'with fail_silent configuration' do
    before(:each) do
      allow(AssetSync).to receive(:stderr).and_return(StringIO.new)
      AssetSync.config = AssetSync::Config.new
      AssetSync.configure do |config|
        config.fail_silently = true
      end
    end

    it "should not raise an invalid exception" do
      expect{ AssetSync.sync }.not_to raise_error
    end
  end

  describe 'with gzip_compression enabled' do
    before(:each) do
      AssetSync.config = AssetSync::Config.new
      AssetSync.config.gzip_compression = true
    end

    it "config.gzip? should be true" do
      expect(AssetSync.config.gzip?).to be_truthy
    end
  end

  describe 'with manifest enabled' do
    before(:each) do
      AssetSync.config = AssetSync::Config.new
      AssetSync.config.manifest = true
    end

    it "config.manifest should be true" do
      expect(AssetSync.config.manifest).to be_truthy
    end

    it "config.manifest_path should default to public/assets.." do
      expect(AssetSync.config.manifest_path).to match(/public\/assets\/manifest.yml/)
    end

    it "config.manifest_path should default to public/assets.." do
      Rails.application.config.assets.manifest = "/var/assets"
      expect(AssetSync.config.manifest_path).to eq("/var/assets/manifest.yml")
    end

    it "config.manifest_path should default to public/custom_assets.." do
      Rails.application.config.assets.prefix = 'custom_assets'
      expect(AssetSync.config.manifest_path).to match(/public\/custom_assets\/manifest.yml/)
    end
  end
end