nightcrawler_swift 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/Changelog.md +11 -0
  3. data/Gemfile.lock +1 -1
  4. data/README.md +40 -25
  5. data/bin/nswift +8 -0
  6. data/lib/nightcrawler_swift/cli.rb +208 -0
  7. data/lib/nightcrawler_swift/command.rb +12 -2
  8. data/lib/nightcrawler_swift/{delete.rb → commands/delete.rb} +1 -1
  9. data/lib/nightcrawler_swift/{download.rb → commands/download.rb} +1 -1
  10. data/lib/nightcrawler_swift/{list.rb → commands/list.rb} +0 -0
  11. data/lib/nightcrawler_swift/{sync.rb → commands/sync.rb} +2 -2
  12. data/lib/nightcrawler_swift/commands/upload.rb +25 -0
  13. data/lib/nightcrawler_swift/connection.rb +44 -23
  14. data/lib/nightcrawler_swift/exceptions.rb +1 -0
  15. data/lib/nightcrawler_swift/tasks/asset_sync.rake +7 -1
  16. data/lib/nightcrawler_swift/version.rb +1 -1
  17. data/lib/nightcrawler_swift.rb +26 -9
  18. data/spec/fixtures/auth_success.json +6 -6
  19. data/spec/lib/nightcrawler_swift/cli_spec.rb +293 -0
  20. data/spec/lib/nightcrawler_swift/command_spec.rb +55 -14
  21. data/spec/lib/nightcrawler_swift/{delete_spec.rb → commands/delete_spec.rb} +15 -8
  22. data/spec/lib/nightcrawler_swift/{download_spec.rb → commands/download_spec.rb} +16 -7
  23. data/spec/lib/nightcrawler_swift/{list_spec.rb → commands/list_spec.rb} +12 -8
  24. data/spec/lib/nightcrawler_swift/{sync_spec.rb → commands/sync_spec.rb} +10 -1
  25. data/spec/lib/nightcrawler_swift/{upload_spec.rb → commands/upload_spec.rb} +23 -8
  26. data/spec/lib/nightcrawler_swift/connection_spec.rb +32 -20
  27. data/spec/lib/nightcrawler_swift/tasks/asset_sync_spec.rb +19 -3
  28. data/spec/lib/nightcrawler_swift_spec.rb +46 -15
  29. metadata +23 -18
  30. data/lib/nightcrawler_swift/upload.rb +0 -17
@@ -7,20 +7,62 @@ describe NightcrawlerSwift::Command do
7
7
  end
8
8
 
9
9
  before do
10
+ NightcrawlerSwift.configure
10
11
  allow(NightcrawlerSwift).to receive(:connection).and_return(connection)
12
+ allow(connection).to receive(:token_id).and_return(token)
13
+ allow(connection).to receive(:expires_at).and_return(expires_at)
11
14
  allow(RestClient::Resource).to receive(:new).and_return(restclient)
12
15
  end
13
16
 
14
- let(:connection) { double(:connection, token_id: token) }
17
+ let(:connection) { NightcrawlerSwift::Connection.new }
15
18
  let(:restclient) { double(:restclient, put: response, get: response, delete: response) }
16
19
  let(:response) { double(:response) }
20
+ let(:url) { "http://url.com" }
21
+ let(:token) { "token" }
22
+ let(:expires_at) { (DateTime.now + 60).to_time }
23
+
24
+ shared_examples "resource configured with NightcrawlerSwift.options" do
25
+ it "uses the configured verify_ssl" do
26
+ NightcrawlerSwift.configure verify_ssl: true
27
+ execute_http
28
+ expect(RestClient::Resource).to have_received(:new).with(url, hash_including(verify_ssl: true))
29
+ end
17
30
 
18
- let :url do
19
- "http://url.com"
31
+ it "uses the configured timeout" do
32
+ NightcrawlerSwift.configure timeout: 10
33
+ execute_http
34
+ expect(RestClient::Resource).to have_received(:new).with(url, hash_including(timeout: 10))
35
+ end
20
36
  end
21
37
 
22
- let :token do
23
- "token"
38
+ describe "#connection" do
39
+ context "when disconnected" do
40
+ before do
41
+ allow(NightcrawlerSwift.connection).to receive(:connected?).and_return(false)
42
+ end
43
+
44
+ it "connects and then return the connection" do
45
+ expect(NightcrawlerSwift.connection).to receive(:connect!)
46
+ expect(subject.connection).to eql(connection)
47
+ end
48
+ end
49
+
50
+ context "when connected" do
51
+ before do
52
+ allow(NightcrawlerSwift.connection).to receive(:connected?).and_return(true)
53
+ end
54
+
55
+ it "returns the connection" do
56
+ expect(NightcrawlerSwift.connection).to_not receive(:connect!)
57
+ expect(subject.connection).to eql(connection)
58
+ end
59
+ end
60
+ end
61
+
62
+ describe "#options" do
63
+ it "returns NightcrawlerSwift.options" do
64
+ expect(subject.options).to eql(NightcrawlerSwift.options)
65
+ end
24
66
  end
25
67
 
26
68
  describe "#get" do
@@ -28,10 +70,8 @@ describe NightcrawlerSwift::Command do
28
70
  subject.send :get, url, headers: {content_type: :json}
29
71
  end
30
72
 
31
- it "uses url to initialize RestClient" do
32
- get
33
- expect(RestClient::Resource).to have_received(:new).with(url, verify_ssl: false)
34
- end
73
+ let(:execute_http) { get }
74
+ it_behaves_like "resource configured with NightcrawlerSwift.options"
35
75
 
36
76
  it "sends headers with token" do
37
77
  get
@@ -48,10 +88,8 @@ describe NightcrawlerSwift::Command do
48
88
  subject.send :delete, url, headers: {content_type: :json}
49
89
  end
50
90
 
51
- it "uses url to initialize RestClient" do
52
- delete
53
- expect(RestClient::Resource).to have_received(:new).with(url, verify_ssl: false)
54
- end
91
+ let(:execute_http) { delete }
92
+ it_behaves_like "resource configured with NightcrawlerSwift.options"
55
93
 
56
94
  it "sends headers with token" do
57
95
  delete
@@ -68,6 +106,9 @@ describe NightcrawlerSwift::Command do
68
106
  subject.send(:put, url, body: 'content', headers: {a: 1})
69
107
  end
70
108
 
109
+ let(:execute_http) { put }
110
+ it_behaves_like "resource configured with NightcrawlerSwift.options"
111
+
71
112
  it "returns RestClient response" do
72
113
  expect(put).to eql(response)
73
114
  end
@@ -84,7 +125,7 @@ describe NightcrawlerSwift::Command do
84
125
 
85
126
  it "uses url to initialize RestClient" do
86
127
  put
87
- expect(RestClient::Resource).to have_received(:new).with(url, verify_ssl: false)
128
+ expect(RestClient::Resource).to have_received(:new).with(url, verify_ssl: false, timeout: nil)
88
129
  end
89
130
  end
90
131
 
@@ -2,27 +2,34 @@ require 'spec_helper'
2
2
 
3
3
  describe NightcrawlerSwift::Delete do
4
4
 
5
+ let(:connection) { NightcrawlerSwift::Connection.new }
6
+ let(:token) { "token" }
7
+ let(:expires_at) { (DateTime.now + 60).to_time }
8
+ let(:upload_url) { "server-url" }
9
+
5
10
  subject do
6
11
  NightcrawlerSwift::Delete.new
7
12
  end
8
13
 
9
- describe "#execute" do
10
- let :connection do
11
- double :connection, upload_url: "server-url"
12
- end
14
+ before do
15
+ allow(NightcrawlerSwift).to receive(:connection).and_return(connection)
16
+ allow(connection).to receive(:token_id).and_return(token)
17
+ allow(connection).to receive(:expires_at).and_return(expires_at)
18
+ allow(connection).to receive(:upload_url).and_return(upload_url)
19
+ end
13
20
 
21
+ describe "#execute" do
14
22
  let :execute do
15
23
  subject.execute "file_path"
16
24
  end
17
25
 
18
26
  before do
19
- allow(NightcrawlerSwift).to receive(:connection).and_return(connection)
20
27
  allow(subject).to receive(:delete).and_return(response)
21
28
  end
22
29
 
23
30
  context "success" do
24
31
  let :response do
25
- double(:response, code: 200, body: {a: 1}.to_json)
32
+ double(:response, code: 200)
26
33
  end
27
34
 
28
35
  it "deletes using upload url" do
@@ -30,8 +37,8 @@ describe NightcrawlerSwift::Delete do
30
37
  expect(subject).to have_received(:delete).with("server-url/file_path", headers: {accept: :json})
31
38
  end
32
39
 
33
- it "returns the parsed json" do
34
- expect(execute).to eql "a" => 1
40
+ it "returns true" do
41
+ expect(execute).to eql true
35
42
  end
36
43
  end
37
44
 
@@ -2,21 +2,30 @@ require 'spec_helper'
2
2
 
3
3
  describe NightcrawlerSwift::Download do
4
4
 
5
+ let(:connection) { NightcrawlerSwift::Connection.new }
6
+ let(:token) { "token" }
7
+ let(:expires_at) { (DateTime.now + 60).to_time }
8
+ let(:public_url) { "server-url" }
9
+ let(:bucket) { "rogue" }
10
+
5
11
  subject do
6
12
  NightcrawlerSwift::Download.new
7
13
  end
8
14
 
9
- describe "#execute" do
10
- let :connection do
11
- double :connection, public_url: "server-url"
12
- end
15
+ before do
16
+ allow(NightcrawlerSwift).to receive(:connection).and_return(connection)
17
+ allow(connection).to receive(:token_id).and_return(token)
18
+ allow(connection).to receive(:expires_at).and_return(expires_at)
19
+ allow(connection).to receive(:public_url).and_return(public_url)
20
+ NightcrawlerSwift.configure bucket: bucket
21
+ end
13
22
 
23
+ describe "#execute" do
14
24
  let :execute do
15
25
  subject.execute "file_path"
16
26
  end
17
27
 
18
28
  before do
19
- allow(NightcrawlerSwift).to receive(:connection).and_return(connection)
20
29
  allow(subject).to receive(:get).and_return(response)
21
30
  end
22
31
 
@@ -25,9 +34,9 @@ describe NightcrawlerSwift::Download do
25
34
  double(:response, code: 200, body: "content")
26
35
  end
27
36
 
28
- it "gets using upload url" do
37
+ it "gets using public url" do
29
38
  execute
30
- expect(subject).to have_received(:get).with("server-url/file_path")
39
+ expect(subject).to have_received(:get).with("server-url/#{bucket}/file_path")
31
40
  end
32
41
 
33
42
  it "returns body" do
@@ -2,19 +2,23 @@ require "spec_helper"
2
2
 
3
3
  describe NightcrawlerSwift::List do
4
4
 
5
+ let(:connection) { NightcrawlerSwift::Connection.new }
6
+ let(:token) { "token" }
7
+ let(:expires_at) { (DateTime.now + 60).to_time }
8
+ let(:upload_url) { "server-url" }
9
+
5
10
  subject do
6
11
  NightcrawlerSwift::List.new
7
12
  end
8
13
 
9
- describe "#execute" do
10
- let :connection do
11
- double :connection, upload_url: "server-url"
12
- end
13
-
14
- before do
15
- allow(NightcrawlerSwift).to receive(:connection).and_return(connection)
16
- end
14
+ before do
15
+ allow(NightcrawlerSwift).to receive(:connection).and_return(connection)
16
+ allow(connection).to receive(:token_id).and_return(token)
17
+ allow(connection).to receive(:expires_at).and_return(expires_at)
18
+ allow(connection).to receive(:upload_url).and_return(upload_url)
19
+ end
17
20
 
21
+ describe "#execute" do
18
22
  context "success" do
19
23
  let :json_response do
20
24
  [{"name" => "file"}]
@@ -2,12 +2,21 @@ require "spec_helper"
2
2
 
3
3
  describe NightcrawlerSwift::Sync do
4
4
 
5
+ let(:connection) { NightcrawlerSwift::Connection.new }
6
+ let(:token) { "token" }
7
+ let(:expires_at) { (DateTime.now + 60).to_time }
8
+ let(:upload_url) { "server-url" }
9
+
5
10
  subject do
6
11
  NightcrawlerSwift::Sync.new
7
12
  end
8
13
 
9
14
  before do
10
15
  NightcrawlerSwift.logger = Logger.new(StringIO.new)
16
+ allow(NightcrawlerSwift).to receive(:connection).and_return(connection)
17
+ allow(connection).to receive(:token_id).and_return(token)
18
+ allow(connection).to receive(:expires_at).and_return(expires_at)
19
+ allow(connection).to receive(:upload_url).and_return(upload_url)
11
20
  end
12
21
 
13
22
  describe "#execute" do
@@ -17,7 +26,7 @@ describe NightcrawlerSwift::Sync do
17
26
  end
18
27
 
19
28
  it "executes upload command for each file of a directory" do
20
- dir = File.expand_path(File.join(File.dirname(__FILE__), "../../fixtures/assets"))
29
+ dir = File.expand_path(File.join(File.dirname(__FILE__), "../../../fixtures/assets"))
21
30
 
22
31
  subject.instance_variable_set(:@upload, upload)
23
32
  expect(File).to receive(:open).with(File.join(dir, "css1.css"), "r").and_call_original
@@ -3,27 +3,36 @@ require 'spec_helper'
3
3
 
4
4
  describe NightcrawlerSwift::Upload do
5
5
 
6
+ let(:connection) { NightcrawlerSwift::Connection.new }
7
+ let(:token) { "token" }
8
+ let(:expires_at) { (DateTime.now + 60).to_time }
9
+ let(:upload_url) { "server-url" }
10
+
6
11
  subject do
7
12
  NightcrawlerSwift::Upload.new
8
13
  end
9
14
 
15
+ before do
16
+ NightcrawlerSwift.configure
17
+ allow(NightcrawlerSwift).to receive(:connection).and_return(connection)
18
+ allow(connection).to receive(:token_id).and_return(token)
19
+ allow(connection).to receive(:expires_at).and_return(expires_at)
20
+ allow(connection).to receive(:upload_url).and_return(upload_url)
21
+ end
22
+
10
23
  describe "#execute" do
11
24
  let(:path) { "file_name" }
12
25
  let(:file) do
13
- dir = File.expand_path(File.join(File.dirname(__FILE__), "../../fixtures/assets"))
26
+ dir = File.expand_path(File.join(File.dirname(__FILE__), "../../../fixtures/assets"))
14
27
  File.open(File.join(dir, "css1.css"))
15
28
  end
16
29
 
17
- let :connection do
18
- double :connection, upload_url: "server-url", opts: OpenStruct.new {}
19
- end
20
-
21
30
  let :response do
22
31
  double(:response, code: 201)
23
32
  end
24
33
 
25
34
  let :etag do
26
- '"%s"' % Digest::MD5.new.update(file.read).hexdigest
35
+ Digest::MD5.hexdigest(file.read)
27
36
  end
28
37
 
29
38
  let :max_age do
@@ -31,7 +40,6 @@ describe NightcrawlerSwift::Upload do
31
40
  end
32
41
 
33
42
  before do
34
- allow(NightcrawlerSwift).to receive(:connection).and_return(connection)
35
43
  allow(subject).to receive(:put).and_return(response)
36
44
  allow(file).to receive(:read).and_return("content")
37
45
  end
@@ -61,7 +69,7 @@ describe NightcrawlerSwift::Upload do
61
69
  end
62
70
 
63
71
  it "sends max_age into headers" do
64
- connection.opts.max_age = max_age
72
+ NightcrawlerSwift.configure max_age: max_age
65
73
  execute
66
74
  expect(subject).to have_received(:put).with(anything, hash_including(headers: { content_type: "text/css", etag: etag, cache_control: "max-age=#{max_age}" }))
67
75
  end
@@ -79,6 +87,13 @@ describe NightcrawlerSwift::Upload do
79
87
  let(:response) { double(:response, code: 500) }
80
88
  it { expect(execute).to be false }
81
89
  end
90
+
91
+ context "when rescue RestClient::UnprocessableEntity" do
92
+ it "wraps into NightcrawlerSwift::Exceptions::ValidationError" do
93
+ expect(subject).to receive(:put).and_raise(RestClient::UnprocessableEntity.new)
94
+ expect { execute }.to raise_error NightcrawlerSwift::Exceptions::ValidationError
95
+ end
96
+ end
82
97
  end
83
98
 
84
99
  end
@@ -8,8 +8,8 @@ describe NightcrawlerSwift::Connection do
8
8
  tenant_name: "tenant_username1",
9
9
  username: "username1",
10
10
  password: "some-pass",
11
- auth_url: "https://auth.url.com:123/v2.0/tokens",
12
- max_age: 31536000 #1 year
11
+ auth_url: "https://auth-url-com:123/v2.0/tokens",
12
+ max_age: 31536000 # 1 year
13
13
  }
14
14
  end
15
15
 
@@ -19,22 +19,14 @@ describe NightcrawlerSwift::Connection do
19
19
  end
20
20
 
21
21
  subject do
22
- NightcrawlerSwift::Connection.new opts
22
+ NightcrawlerSwift::Connection.new
23
23
  end
24
24
 
25
25
  before do
26
+ NightcrawlerSwift.configure opts
26
27
  NightcrawlerSwift.logger = Logger.new(StringIO.new)
27
28
  end
28
29
 
29
- describe "initialization" do
30
- it "creates the opts struct with the given values" do
31
- expect(subject.opts).to_not be_nil
32
- opts.keys.each do |key|
33
- expect(subject.opts.send(key)).to eql(opts[key])
34
- end
35
- end
36
- end
37
-
38
30
  describe "#connect!" do
39
31
  let :auth_json do
40
32
  {
@@ -55,12 +47,26 @@ describe NightcrawlerSwift::Connection do
55
47
  end
56
48
 
57
49
  describe "when it connects" do
50
+ let :resource do
51
+ RestClient::Resource.new(
52
+ opts[:auth_url],
53
+ verify_ssl: NightcrawlerSwift.options.verify_ssl,
54
+ timeout: NightcrawlerSwift.options.timeout
55
+ )
56
+ end
57
+
58
58
  before do
59
- allow(RestClient).to receive(:post).
60
- with(opts[:auth_url], auth_json, content_type: :json, accept: :json).
59
+ allow(RestClient::Resource).to receive(:new).and_return(resource)
60
+ allow(resource).to receive(:post).
61
+ with(auth_json, content_type: :json, accept: :json).
61
62
  and_return(auth_success_response)
62
63
  end
63
64
 
65
+ it "stores the auth_response" do
66
+ subject.connect!
67
+ expect(subject.auth_response).to eql(OpenStruct.new(auth_success_json))
68
+ end
69
+
64
70
  it "stores the token id" do
65
71
  subject.connect!
66
72
  expect(subject.token_id).to eql(auth_success_json["access"]["token"]["id"])
@@ -72,6 +78,11 @@ describe NightcrawlerSwift::Connection do
72
78
  expect(subject.expires_at).to eql(expires_at)
73
79
  end
74
80
 
81
+ it "stores the catalog" do
82
+ subject.connect!
83
+ expect(subject.catalog).to eql(auth_success_json["access"]["serviceCatalog"][0])
84
+ end
85
+
75
86
  it "stores the admin_url" do
76
87
  subject.connect!
77
88
  expect(subject.admin_url).to eql(auth_success_json["access"]["serviceCatalog"].first["endpoints"].first["adminURL"])
@@ -91,18 +102,19 @@ describe NightcrawlerSwift::Connection do
91
102
  expect(subject.connect!).to eql(subject)
92
103
  end
93
104
 
94
- context "max_age" do
95
- it "stores it" do
96
- expect(subject.opts.max_age).to eql(opts[:max_age])
105
+ context "and there isn't any catalog configured" do
106
+ before do
107
+ auth_success_json["access"]["serviceCatalog"] = []
108
+ allow(subject).to receive(:auth_response).and_return(OpenStruct.new(auth_success_json))
97
109
  end
98
110
 
99
- it "should be an integer" do
100
- expect { NightcrawlerSwift::Connection.new({max_age: "a string"}) }.to raise_error(NightcrawlerSwift::Exceptions::ConfigurationError)
111
+ it "raises NightcrawlerSwift::Exceptions::ConfigurationError" do
112
+ expect { subject.connect! }.to raise_error(NightcrawlerSwift::Exceptions::ConfigurationError)
101
113
  end
102
114
  end
103
115
  end
104
116
 
105
- describe "when some error happens" do
117
+ describe "when some error happens with the connection" do
106
118
  before do
107
119
  allow(RestClient).to receive(:post).
108
120
  with(opts[:auth_url], auth_json, content_type: :json, accept: :json).