fog-brightbox 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +8 -0
  3. data/lib/fog/brightbox.rb +1 -0
  4. data/lib/fog/brightbox/config.rb +102 -2
  5. data/lib/fog/brightbox/core.rb +1 -0
  6. data/lib/fog/brightbox/models/compute/api_client.rb +3 -1
  7. data/lib/fog/brightbox/models/storage/directories.rb +45 -0
  8. data/lib/fog/brightbox/models/storage/directory.rb +53 -0
  9. data/lib/fog/brightbox/models/storage/file.rb +166 -0
  10. data/lib/fog/brightbox/models/storage/files.rb +104 -0
  11. data/lib/fog/brightbox/oauth2.rb +140 -136
  12. data/lib/fog/brightbox/requests/storage/copy_object.rb +27 -0
  13. data/lib/fog/brightbox/requests/storage/delete_container.rb +22 -0
  14. data/lib/fog/brightbox/requests/storage/delete_multiple_objects.rb +67 -0
  15. data/lib/fog/brightbox/requests/storage/delete_object.rb +23 -0
  16. data/lib/fog/brightbox/requests/storage/delete_static_large_object.rb +43 -0
  17. data/lib/fog/brightbox/requests/storage/get_container.rb +44 -0
  18. data/lib/fog/brightbox/requests/storage/get_containers.rb +33 -0
  19. data/lib/fog/brightbox/requests/storage/get_object.rb +29 -0
  20. data/lib/fog/brightbox/requests/storage/get_object_http_url.rb +21 -0
  21. data/lib/fog/brightbox/requests/storage/get_object_https_url.rb +85 -0
  22. data/lib/fog/brightbox/requests/storage/head_container.rb +28 -0
  23. data/lib/fog/brightbox/requests/storage/head_containers.rb +25 -0
  24. data/lib/fog/brightbox/requests/storage/head_object.rb +23 -0
  25. data/lib/fog/brightbox/requests/storage/post_set_meta_temp_url_key.rb +37 -0
  26. data/lib/fog/brightbox/requests/storage/put_container.rb +27 -0
  27. data/lib/fog/brightbox/requests/storage/put_dynamic_obj_manifest.rb +43 -0
  28. data/lib/fog/brightbox/requests/storage/put_object.rb +42 -0
  29. data/lib/fog/brightbox/requests/storage/put_object_manifest.rb +16 -0
  30. data/lib/fog/brightbox/requests/storage/put_static_obj_manifest.rb +57 -0
  31. data/lib/fog/brightbox/storage.rb +166 -0
  32. data/lib/fog/brightbox/storage/authentication_request.rb +52 -0
  33. data/lib/fog/brightbox/storage/config.rb +23 -0
  34. data/lib/fog/brightbox/storage/connection.rb +83 -0
  35. data/lib/fog/brightbox/storage/errors.rb +11 -0
  36. data/lib/fog/brightbox/version.rb +1 -1
  37. data/spec/fog/brightbox/config_spec.rb +62 -1
  38. data/spec/fog/brightbox/storage/authentication_request_spec.rb +77 -0
  39. data/spec/fog/brightbox/storage/config_spec.rb +40 -0
  40. data/spec/fog/brightbox/storage/connection_errors_spec.rb +54 -0
  41. data/spec/fog/brightbox/storage/connection_spec.rb +120 -0
  42. data/spec/fog/storage/brightbox_spec.rb +290 -0
  43. metadata +40 -3
@@ -0,0 +1,11 @@
1
+ module Fog
2
+ module Brightbox
3
+ module Storage
4
+ class ManagementUrlUnknown < Fog::Errors::Error
5
+ end
6
+
7
+ class AuthenticationRequired < Fog::Errors::Error
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,5 +1,5 @@
1
1
  module Fog
2
2
  module Brightbox
3
- VERSION = "0.3.0"
3
+ VERSION = "0.4.0"
4
4
  end
5
5
  end
@@ -97,6 +97,25 @@ describe Fog::Brightbox::Config do
97
97
  end
98
98
  end
99
99
 
100
+ describe "when account was passed but changed" do
101
+ it "returns the new account" do
102
+ @options = { :brightbox_account => "acc-12345" }
103
+ @config = Fog::Brightbox::Config.new(@options)
104
+ @config.change_account("acc-abcde")
105
+ assert_equal "acc-abcde", @config.account
106
+ end
107
+ end
108
+
109
+ describe "when account was passed, changed and reset" do
110
+ it "returns the original account" do
111
+ @options = { :brightbox_account => "acc-12345" }
112
+ @config = Fog::Brightbox::Config.new(@options)
113
+ @config.change_account("acc-abcde")
114
+ @config.reset_account
115
+ assert_equal "acc-12345", @config.account
116
+ end
117
+ end
118
+
100
119
  describe "when connection options are passed" do
101
120
  it "returns the settings" do
102
121
  @connection_settings = {
@@ -135,7 +154,7 @@ describe Fog::Brightbox::Config do
135
154
  end
136
155
 
137
156
  describe "when cached OAuth tokens were passed" do
138
- it "returns the settings" do
157
+ before do
139
158
  @access_token = "1234567890abcdefghijklmnopqrstuvwxyz"
140
159
  @refresh_token = "1234567890abcdefghijklmnopqrstuvwxyz"
141
160
  @options = {
@@ -143,8 +162,26 @@ describe Fog::Brightbox::Config do
143
162
  :brightbox_refresh_token => @refresh_token
144
163
  }
145
164
  @config = Fog::Brightbox::Config.new(@options)
165
+ end
166
+
167
+ it "returns the access token" do
146
168
  assert_equal @access_token, @config.cached_access_token
169
+ assert_equal @access_token, @config.latest_access_token
170
+ end
171
+
172
+ it "returns the refresh token" do
147
173
  assert_equal @refresh_token, @config.cached_refresh_token
174
+ assert_equal @refresh_token, @config.latest_refresh_token
175
+ end
176
+
177
+ it "does not need to authenticate" do
178
+ refute @config.must_authenticate?
179
+ end
180
+
181
+ it "can expire the tokens" do
182
+ @config.expire_tokens!
183
+ assert_nil @config.latest_access_token
184
+ assert_nil @config.latest_refresh_token
148
185
  end
149
186
  end
150
187
 
@@ -177,4 +214,28 @@ describe Fog::Brightbox::Config do
177
214
  assert_nil @config.default_image_id
178
215
  end
179
216
  end
217
+
218
+ describe "when username and password are given" do
219
+ it "user_credentials? returns true" do
220
+ @options = {
221
+ :brightbox_client_id => "app-12345",
222
+ :brightbox_secret => "12345",
223
+ :brightbox_username => "user@example.com",
224
+ :brightbox_password => "12345"
225
+ }
226
+ @config = Fog::Brightbox::Config.new(@options)
227
+ assert @config.user_credentials?
228
+ end
229
+ end
230
+
231
+ describe "when no username is given" do
232
+ it "user_credentials? returns false" do
233
+ @options = {
234
+ :brightbox_client_id => "cli-12345",
235
+ :brightbox_secret => "12345"
236
+ }
237
+ @config = Fog::Brightbox::Config.new(@options)
238
+ refute @config.user_credentials?
239
+ end
240
+ end
180
241
  end
@@ -0,0 +1,77 @@
1
+ require "minitest/autorun"
2
+ require "webmock/minitest"
3
+ require "fog/brightbox"
4
+
5
+ describe Fog::Brightbox::Storage::AuthenticationRequest do
6
+ describe "when initialised with blank config" do
7
+ before do
8
+ stub_request(:get, "https://files.gb1.brightbox.com/v1").
9
+ with(:headers => {
10
+ "Host" => "files.gb1.brightbox.com:443",
11
+ "X-Auth-User" => "",
12
+ "X-Auth-Key" => ""
13
+ }).to_return(:status => 412, :body => "Bad URL", :headers => {})
14
+ end
15
+
16
+ it "fails" do
17
+ settings = {}
18
+ @config = Fog::Brightbox::Config.new(settings)
19
+ @request = Fog::Brightbox::Storage::AuthenticationRequest.new(@config)
20
+ refute @request.authenticate
21
+ end
22
+ end
23
+
24
+ describe "when initialised with API client details" do
25
+ before do
26
+ stub_request(:get, "https://files.gb1.brightbox.com/v1").
27
+ with(:headers => {
28
+ "Host" => "files.gb1.brightbox.com:443",
29
+ "X-Auth-User" => "cli-12345",
30
+ "X-Auth-Key" => "12345"
31
+ }).to_return(:status => 200, :body => "Authenticated", :headers => {
32
+ "X-Storage-Url" => "https://files.gb1.brightbox.com/v1/acc-12345",
33
+ "X-Storage-Token" => "abcdefghijklmnopqrstuvwxyz1234567890",
34
+ "X-Auth-Token" => "abcdefghijklmnopqrstuvwxyz1234567890",
35
+ "Content-Type" => "text/plain"
36
+ })
37
+ end
38
+
39
+ it "authenticates correctly" do
40
+ settings = {
41
+ :brightbox_client_id => "cli-12345",
42
+ :brightbox_secret => "12345"
43
+ }
44
+ @config = Fog::Brightbox::Config.new(settings)
45
+ @request = Fog::Brightbox::Storage::AuthenticationRequest.new(@config)
46
+ assert @request.authenticate
47
+ end
48
+ end
49
+
50
+ describe "when initialised with user details" do
51
+ before do
52
+ stub_request(:get, "https://files.gb1.brightbox.com/v1").
53
+ with(:headers => {
54
+ "Host" => "files.gb1.brightbox.com:443",
55
+ "X-Auth-User" => "user@example.com",
56
+ "X-Auth-Key" => "abcde"
57
+ }).to_return(:status => 200, :body => "Authenticated", :headers => {
58
+ "X-Storage-Url" => "https://files.gb1.brightbox.com/v1/acc-12345",
59
+ "X-Storage-Token" => "abcdefghijklmnopqrstuvwxyz1234567890",
60
+ "X-Auth-Token" => "abcdefghijklmnopqrstuvwxyz1234567890",
61
+ "Content-Type" => "text/plain"
62
+ })
63
+ end
64
+
65
+ it "authenticates correctly" do
66
+ settings = {
67
+ :brightbox_client_id => "app-12345",
68
+ :brightbox_secret => "12345",
69
+ :brightbox_username => "user@example.com",
70
+ :brightbox_password => "abcde"
71
+ }
72
+ @config = Fog::Brightbox::Config.new(settings)
73
+ @request = Fog::Brightbox::Storage::AuthenticationRequest.new(@config)
74
+ assert @request.authenticate
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,40 @@
1
+ require "minitest/autorun"
2
+ require "fog/brightbox"
3
+
4
+ describe Fog::Brightbox::Storage::Config do
5
+ describe "when required arguments are included" do
6
+ it "nothing is raised" do
7
+ settings = {
8
+ :brightbox_client_id => "cli-12345",
9
+ :brightbox_secret => "1234567890"
10
+ }
11
+ config = Fog::Brightbox::Config.new(settings)
12
+ Fog::Brightbox::Storage::Config.new(config)
13
+ pass
14
+ end
15
+ end
16
+
17
+ describe "when client_id is not in configuration" do
18
+ it "raises ArgumentError" do
19
+ settings = {
20
+ :brightbox_secret => "1234567890"
21
+ }
22
+ config = Fog::Brightbox::Config.new(settings)
23
+ assert_raises ArgumentError do
24
+ Fog::Brightbox::Storage::Config.new(config)
25
+ end
26
+ end
27
+ end
28
+
29
+ describe "when client_secret is not in configuration" do
30
+ it "raises ArgumentError" do
31
+ settings = {
32
+ :brightbox_client_id => "cli-12345"
33
+ }
34
+ config = Fog::Brightbox::Config.new(settings)
35
+ assert_raises ArgumentError do
36
+ Fog::Brightbox::Storage::Config.new(config)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,54 @@
1
+ require "minitest/autorun"
2
+ require "webmock/minitest"
3
+ require "fog/brightbox"
4
+
5
+ describe Fog::Brightbox::Storage::Connection do
6
+ let(:config) { Fog::Brightbox::Config.new(settings) }
7
+ let(:connection) { Fog::Brightbox::Storage::Connection.new(config) }
8
+ let(:params) do
9
+ { :path => "fnord", :expects => [200] }
10
+ end
11
+ let(:settings) do
12
+ {
13
+ :brightbox_client_id => "app-12345",
14
+ :brightbox_secret => "1234567890",
15
+ :brightbox_storage_management_url => "https://files.gb2.brightbox.com/v1/acc-12345"
16
+ }
17
+ end
18
+ let(:valid_auth_token) { "01234567890abcdefghijklmnopqrstuvwxyz" }
19
+
20
+ {
21
+ 400 => Excon::Errors::BadRequest,
22
+ 401 => Fog::Brightbox::Storage::AuthenticationRequired,
23
+ 402 => Excon::Errors::PaymentRequired,
24
+ 403 => Excon::Errors::Forbidden,
25
+ 404 => Fog::Storage::Brightbox::NotFound,
26
+ 405 => Excon::Errors::MethodNotAllowed,
27
+ 406 => Excon::Errors::NotAcceptable,
28
+ 407 => Excon::Errors::ProxyAuthenticationRequired,
29
+ 408 => Excon::Errors::RequestTimeout,
30
+ 409 => Excon::Errors::Conflict,
31
+ 410 => Excon::Errors::Gone,
32
+ 411 => Excon::Errors::LengthRequired,
33
+ 412 => Excon::Errors::PreconditionFailed,
34
+ 413 => Excon::Errors::RequestEntityTooLarge,
35
+ 414 => Excon::Errors::RequestURITooLong,
36
+ 415 => Excon::Errors::UnsupportedMediaType,
37
+ 416 => Excon::Errors::RequestedRangeNotSatisfiable,
38
+ 417 => Excon::Errors::ExpectationFailed,
39
+ 422 => Excon::Errors::UnprocessableEntity,
40
+ 500 => Excon::Errors::InternalServerError,
41
+ 501 => Excon::Errors::NotImplemented,
42
+ 502 => Excon::Errors::BadGateway,
43
+ 503 => Excon::Errors::ServiceUnavailable,
44
+ 504 => Excon::Errors::GatewayTimeout
45
+ }.each do |status, error|
46
+ describe "when request responds with #{status}" do
47
+ it "raises #{error}" do
48
+ stub_request(:get, "https://files.gb2.brightbox.com/v1/acc-12345/fnord").
49
+ to_return(:status => status)
50
+ assert_raises(error) { connection.request(params) }
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,120 @@
1
+ require "minitest/autorun"
2
+ require "webmock/minitest"
3
+ require "fog/brightbox"
4
+
5
+ describe Fog::Brightbox::Storage::Connection do
6
+ let(:config) { Fog::Brightbox::Config.new(settings) }
7
+ let(:connection) { Fog::Brightbox::Storage::Connection.new(config) }
8
+ let(:params) do
9
+ { :path => "fnord", :expects => [200] }
10
+ end
11
+ let(:settings) do
12
+ {
13
+ :brightbox_client_id => "app-12345",
14
+ :brightbox_secret => "1234567890",
15
+ :brightbox_storage_management_url => "https://files.gb2.brightbox.com/v1/acc-12345"
16
+ }
17
+ end
18
+ let(:valid_auth_token) { "01234567890abcdefghijklmnopqrstuvwxyz" }
19
+
20
+ describe "when management URL is not available" do
21
+ let(:settings) do
22
+ {
23
+ :brightbox_client_id => "app-12345",
24
+ :brightbox_secret => "1234567890"
25
+ }
26
+ end
27
+
28
+ it "raises Fog::Brightbox::Storage::ManagementUrlUnknown" do
29
+ assert_raises(Fog::Brightbox::Storage::ManagementUrlUnknown) { connection.request(params) }
30
+ end
31
+ end
32
+
33
+ describe "when parameters are nil" do
34
+ let(:params) {}
35
+
36
+ it "raises ArgumentError" do
37
+ assert_raises(ArgumentError) { connection.request(params) }
38
+ end
39
+ end
40
+
41
+ describe "when parameters are empty" do
42
+ let(:params) { {} }
43
+
44
+ before do
45
+ stub_request(:get, "https://files.gb2.brightbox.com/v1/acc-12345").
46
+ with(:headers => {
47
+ "Accept" => "application/json",
48
+ "Content-Type" => "application/json",
49
+ "X-Auth-Token" => valid_auth_token
50
+ }).to_return(:status => 200, :body => "{}", :headers => {
51
+ "Content-Type" => "application/json"
52
+ })
53
+ end
54
+
55
+ it "completes successfully" do
56
+ config.stub :latest_access_token, valid_auth_token do
57
+ connection.request(params)
58
+ pass
59
+ end
60
+ end
61
+ end
62
+
63
+ describe "when request should succeed" do
64
+ before do
65
+ stub_request(:get, "https://files.gb2.brightbox.com/v1/acc-12345/fnord").
66
+ with(:headers => {
67
+ "Accept" => "application/json",
68
+ "Content-Type" => "application/json",
69
+ "X-Auth-Token" => valid_auth_token
70
+ }).to_return(:status => 200, :body => "{}", :headers => {
71
+ "Content-Type" => "application/json"
72
+ })
73
+ end
74
+
75
+ it "completes successfully" do
76
+ config.stub :latest_access_token, valid_auth_token do
77
+ connection.request(params)
78
+ pass
79
+ end
80
+ end
81
+ end
82
+
83
+ describe "when custom headers are passed" do
84
+ let(:params) do
85
+ { :headers => { "X-Test" => "present" }, :path => "fnord" }
86
+ end
87
+
88
+ it "completes successfully" do
89
+ stub_request(:get, "https://files.gb2.brightbox.com/v1/acc-12345/fnord").
90
+ with(:headers => { "X-Test" => "present" }).to_return(:status => 200)
91
+
92
+ connection.request(params)
93
+ pass
94
+ end
95
+ end
96
+
97
+ describe "when container is not found" do
98
+ it "raises Fog::Storage::Brightbox::NotFound" do
99
+ stub_request(:get, "https://files.gb2.brightbox.com/v1/acc-12345/fnord").
100
+ to_return(:status => 404,
101
+ :body => "<html><h1>Not Found</h1><p>The resource could not be found.</p></html>",
102
+ :headers => {
103
+ "Content-Type" => "text/html"
104
+ })
105
+ assert_raises(Fog::Storage::Brightbox::NotFound) { connection.request(params) }
106
+ end
107
+ end
108
+
109
+ describe "when request is not authenticated" do
110
+ it "raises Fog::Brightbox::Storage::AuthenticationRequired" do
111
+ stub_request(:get, "https://files.gb2.brightbox.com/v1/acc-12345/fnord").
112
+ to_return(:status => 401,
113
+ :body => "Authentication required",
114
+ :headers => {
115
+ "Content-Type" => "text/plain"
116
+ })
117
+ assert_raises(Fog::Brightbox::Storage::AuthenticationRequired) { connection.request(params) }
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,290 @@
1
+ require "minitest/autorun"
2
+ require "webmock/minitest"
3
+ require "fog/brightbox"
4
+
5
+ describe Fog::Storage::Brightbox do
6
+ let(:valid_auth_response) do
7
+ {
8
+ :status => 200,
9
+ :body => "Authenticated",
10
+ :headers => {
11
+ "X-Storage-Url"=>"https://files.gb1.brightbox.com:443/v1/acc-12345",
12
+ "Content-Length"=>"13",
13
+ "X-Storage-Token"=>"c1236c8c34d668df497c06075d8a76a79c6fdd0d",
14
+ "Content-Type"=>"text/plain",
15
+ "X-Auth-Token"=>"c1236c8c34d668df497c06075d8a76a79c6fdd0d",
16
+ "X-Trans-Id"=>"txcb228b8b9bfe4d5184828-0053568313",
17
+ "Date"=>"Tue, 22 Apr 2014 14:56:20 GMT"
18
+ }
19
+ }
20
+ end
21
+ let(:config) { Fog::Brightbox::Config.new(settings) }
22
+ let(:service) { Fog::Storage::Brightbox.new(config) }
23
+
24
+ describe "when created without required arguments" do
25
+ it "raises an error" do
26
+ Fog.stub :credentials, {} do
27
+ assert_raises ArgumentError do
28
+ Fog::Storage::Brightbox.new({})
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ describe "when created with a Config object" do
35
+ let(:settings) do
36
+ {
37
+ :brightbox_client_id => "cli-12345",
38
+ :brightbox_secret => "1234567890"
39
+ }
40
+ end
41
+
42
+ it "does not error" do
43
+ service
44
+ pass
45
+ end
46
+ end
47
+
48
+ describe "when created with Config missing required settings" do
49
+ let(:settings) { {} }
50
+
51
+ it "raises ArgumentError" do
52
+ assert_raises ArgumentError do
53
+ Fog::Storage::Brightbox.new(config)
54
+ end
55
+ end
56
+ end
57
+
58
+ describe "when created with a viable config" do
59
+ let(:settings) do
60
+ {
61
+ :brightbox_client_id => "cli-12345",
62
+ :brightbox_secret => "fdkls"
63
+ }
64
+ end
65
+
66
+ before do
67
+ stub_request(:get, "https://files.gb1.brightbox.com/v1").to_return(valid_auth_response)
68
+ end
69
+
70
+ it "requires a call to authenticate" do
71
+ assert service.needs_to_authenticate?
72
+ end
73
+
74
+ it "requires a call to discover management_url" do
75
+ assert_nil service.management_url
76
+ end
77
+
78
+ it "can authenticate" do
79
+ service.authenticate
80
+ assert_equal "https://files.gb1.brightbox.com/v1/acc-12345", service.management_url.to_s
81
+ end
82
+ end
83
+
84
+ describe "when created with bad credentials" do
85
+ let(:settings) do
86
+ {
87
+ :brightbox_client_id => "cli-12345",
88
+ :brightbox_secret => "wrong"
89
+ }
90
+ end
91
+
92
+ it "fails to authenticate" do
93
+ response = {
94
+ :body=>"Bad URL",
95
+ :headers=>
96
+ {"Content-Length"=>"7",
97
+ "Content-Type"=>"text/html; charset=UTF-8",
98
+ "X-Trans-Id"=>"txab03ff4864ff42989f4a1-0053568282",
99
+ "Date"=>"Tue, 22 Apr 2014 14:53:54 GMT"},
100
+ :status=>412
101
+ }
102
+ stub_request(:get, "https://files.gb1.brightbox.com/v1").to_return(response)
103
+
104
+ service.authenticate
105
+ assert_nil service.management_url
106
+ end
107
+ end
108
+
109
+ describe "when configured scoped to a specific account" do
110
+ let(:settings) do
111
+ {
112
+ :brightbox_client_id => "app-12345",
113
+ :brightbox_secret => "12345",
114
+ :brightbox_username => "user@example.com",
115
+ :brightbox_password => "abcde",
116
+ :brightbox_account => "acc-abcde"
117
+ }
118
+ end
119
+
120
+ before do
121
+ stub_request(:get, "https://files.gb1.brightbox.com/v1").to_return(valid_auth_response)
122
+ end
123
+
124
+ it "uses the configured account" do
125
+ assert service.authenticate
126
+ assert_equal "acc-abcde", service.account
127
+ end
128
+ end
129
+
130
+ describe "when account is not configured" do
131
+ let(:settings) do
132
+ {
133
+ :brightbox_client_id => "app-12345",
134
+ :brightbox_secret => "12345",
135
+ :brightbox_username => "user@example.com",
136
+ :brightbox_password => "abcde"
137
+ }
138
+ end
139
+
140
+ before do
141
+ stub_request(:get, "https://files.gb1.brightbox.com/v1").to_return(valid_auth_response)
142
+ end
143
+
144
+ it "extracts the account from the management URL" do
145
+ assert service.authenticate
146
+ assert_equal "acc-12345", service.account
147
+ end
148
+ end
149
+
150
+ describe "when configured with existing token" do
151
+ let(:settings) do
152
+ {
153
+ :brightbox_client_id => "app-12345",
154
+ :brightbox_secret => "12345",
155
+ :brightbox_access_token => "1234567890abcdefghijklmnopqrstuvwxyz",
156
+ :brightbox_refresh_token => "1234567890abcdefghijklmnopqrstuvwxyz"
157
+ }
158
+ end
159
+
160
+ it "does not need to authenticate" do
161
+ refute service.needs_to_authenticate?
162
+ end
163
+
164
+ it "requires a call to discover management_url" do
165
+ assert_nil service.management_url
166
+ end
167
+ end
168
+
169
+ describe "when configured with tokens and management_url" do
170
+ let(:settings) do
171
+ {
172
+ :brightbox_client_id => "app-12345",
173
+ :brightbox_secret => "12345",
174
+ :brightbox_access_token => "1234567890abcdefghijklmnopqrstuvwxyz",
175
+ :brightbox_refresh_token => "1234567890abcdefghijklmnopqrstuvwxyz",
176
+ :brightbox_storage_management_url => "https://files.gb2.brightbox.com/v1/acc-12345"
177
+ }
178
+ end
179
+
180
+ it "does not need to authenticate" do
181
+ refute service.needs_to_authenticate?
182
+ end
183
+
184
+ it "uses configured management_url" do
185
+ assert_equal "https://files.gb2.brightbox.com/v1/acc-12345", service.management_url.to_s
186
+ end
187
+
188
+ it "keeps setting after authentication" do
189
+ stub_request(:get, "https://files.gb1.brightbox.com/v1").to_return(valid_auth_response)
190
+ config.expire_tokens!
191
+ service.authenticate
192
+ assert_equal "https://files.gb2.brightbox.com/v1/acc-12345", service.management_url.to_s
193
+ end
194
+ end
195
+
196
+ describe "when configured with expired tokens" do
197
+ let(:settings) do
198
+ {
199
+ :brightbox_client_id => "app-12345",
200
+ :brightbox_secret => "12345",
201
+ :brightbox_access_token => "1234567890abcdefghijklmnopqrstuvwxyz",
202
+ :brightbox_refresh_token => "1234567890abcdefghijklmnopqrstuvwxyz",
203
+ :brightbox_storage_management_url => "https://files.gb2.brightbox.com/v1/acc-12345",
204
+ :brightbox_token_management => false
205
+ }
206
+ end
207
+
208
+ before do
209
+ # Ongoing request but tokens are expired
210
+ stub_request(:get, "https://files.gb2.brightbox.com/v1/acc-12345/fnord").
211
+ to_return(:status => 401,
212
+ :body => "Authentication required",
213
+ :headers => { "Content-Type" => "text/plain" })
214
+ end
215
+
216
+ let(:params) { { :expects => [200], :path => "fnord" } }
217
+
218
+ it "raises Fog::Brightbox::Storage::AuthenticationRequired" do
219
+ assert_raises(Fog::Brightbox::Storage::AuthenticationRequired) { service.request(params) }
220
+ end
221
+ end
222
+
223
+ describe "when configured with user details and expired tokens" do
224
+ let(:settings) do
225
+ {
226
+ :brightbox_client_id => "app-12345",
227
+ :brightbox_secret => "12345",
228
+ :brightbox_username => "user@example.com",
229
+ :brightbox_password => "12345",
230
+ :brightbox_access_token => "1234567890abcdefghijklmnopqrstuvwxyz",
231
+ :brightbox_refresh_token => "1234567890abcdefghijklmnopqrstuvwxyz",
232
+ :brightbox_storage_url => "https://files.gb2.brightbox.com",
233
+ :brightbox_storage_management_url => "https://files.gb2.brightbox.com/v1/acc-12345"
234
+ }
235
+ end
236
+
237
+ before do
238
+ # Ongoing request but tokens are expired
239
+ stub_request(:get, "https://files.gb2.brightbox.com/v1/acc-12345/fnord").
240
+ with(:headers => { "X-Auth-Token" => "1234567890abcdefghijklmnopqrstuvwxyz" }).
241
+ to_return(:status => 401,
242
+ :body => "Authentication required",
243
+ :headers => { "Content-Type" => "text/plain" })
244
+
245
+ # The reauthentication
246
+ stub_request(:get, "https://files.gb2.brightbox.com/v1").
247
+ with(:headers => { "X-Auth-User" => "user@example.com", "X-Auth-Key" => "12345" }).
248
+ to_return(valid_auth_response)
249
+
250
+ # Repeated request
251
+ stub_request(:get, "https://files.gb2.brightbox.com/v1/acc-12345/fnord").
252
+ with(:headers => { "X-Auth-Token" => "c1236c8c34d668df497c06075d8a76a79c6fdd0d" }).
253
+ to_return(:status => 200)
254
+ end
255
+
256
+ let(:params) { { :expects => [200], :path => "fnord" } }
257
+
258
+ it "authenticates again and retries" do
259
+ service.request(params)
260
+ pass
261
+ end
262
+ end
263
+
264
+ describe "when configured with client credentials" do
265
+ let(:settings) do
266
+ {
267
+ :brightbox_client_id => "cli-12345",
268
+ :brightbox_secret => "12345"
269
+ }
270
+ end
271
+
272
+ before do
273
+ # Initial authentication
274
+ stub_request(:get, "https://files.gb1.brightbox.com/v1").
275
+ with(:headers => {"X-Auth-Key" => "12345", "X-Auth-User" => "cli-12345"}).
276
+ to_return(valid_auth_response)
277
+
278
+ stub_request(:get, "https://files.gb1.brightbox.com/v1/acc-12345/fnord").
279
+ with(:headers => { "X-Auth-Token" => "c1236c8c34d668df497c06075d8a76a79c6fdd0d" }).
280
+ to_return(:status => 200)
281
+ end
282
+
283
+ let(:params) { { :expects => [200], :path => "fnord" } }
284
+
285
+ it "authenticates again and retries" do
286
+ service.request(params)
287
+ pass
288
+ end
289
+ end
290
+ end