scalingo 3.0.0.beta.2 → 3.0.0.beta.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +8 -0
  3. data/CHANGELOG.md +11 -0
  4. data/README.md +4 -14
  5. data/lib/scalingo/api/client.rb +38 -10
  6. data/lib/scalingo/api/endpoint.rb +12 -2
  7. data/lib/scalingo/api/response.rb +7 -1
  8. data/lib/scalingo/auth/keys.rb +4 -4
  9. data/lib/scalingo/auth/scm_integrations.rb +4 -4
  10. data/lib/scalingo/auth/tokens.rb +6 -6
  11. data/lib/scalingo/auth/two_factor_auth.rb +4 -4
  12. data/lib/scalingo/auth/user.rb +2 -2
  13. data/lib/scalingo/bearer_token.rb +15 -0
  14. data/lib/scalingo/billing/profile.rb +3 -3
  15. data/lib/scalingo/client.rb +26 -113
  16. data/lib/scalingo/configuration.rb +7 -36
  17. data/lib/scalingo/core_client.rb +106 -0
  18. data/lib/scalingo/regional/addons.rb +21 -8
  19. data/lib/scalingo/regional/apps.rb +8 -8
  20. data/lib/scalingo/regional/collaborators.rb +4 -4
  21. data/lib/scalingo/regional/containers.rb +4 -4
  22. data/lib/scalingo/regional/deployments.rb +3 -3
  23. data/lib/scalingo/regional/domains.rb +5 -5
  24. data/lib/scalingo/regional/environment.rb +6 -6
  25. data/lib/scalingo/regional/events.rb +4 -4
  26. data/lib/scalingo/regional/logs.rb +2 -2
  27. data/lib/scalingo/regional/metrics.rb +2 -2
  28. data/lib/scalingo/regional/notifiers.rb +7 -7
  29. data/lib/scalingo/regional/operations.rb +1 -1
  30. data/lib/scalingo/regional/scm_repo_links.rb +8 -8
  31. data/lib/scalingo/token_holder.rb +31 -0
  32. data/lib/scalingo/version.rb +1 -1
  33. data/samples/regional/addons/token-200.json +49 -0
  34. data/samples/regional/addons/token-404.json +24 -0
  35. data/scalingo.gemspec +1 -1
  36. data/spec/scalingo/api/client_spec.rb +58 -11
  37. data/spec/scalingo/api/endpoint_spec.rb +3 -2
  38. data/spec/scalingo/api/response_spec.rb +16 -16
  39. data/spec/scalingo/auth_spec.rb +1 -1
  40. data/spec/scalingo/billing_spec.rb +11 -0
  41. data/spec/scalingo/configuration_spec.rb +32 -30
  42. data/spec/scalingo/regional/addons_spec.rb +16 -0
  43. data/spec/scalingo/regional_spec.rb +1 -1
  44. metadata +8 -2
@@ -12,7 +12,7 @@ module Scalingo
12
12
  &block
13
13
  )
14
14
 
15
- unpack(response, key: :variables)
15
+ unpack(:variables) { response }
16
16
  end
17
17
 
18
18
  def create(app_id, payload = {}, headers = nil, &block)
@@ -25,7 +25,7 @@ module Scalingo
25
25
  &block
26
26
  )
27
27
 
28
- unpack(response, key: :variable)
28
+ unpack(:variable) { response }
29
29
  end
30
30
 
31
31
  def update(app_id, variable_id, value, headers = nil, &block)
@@ -38,7 +38,7 @@ module Scalingo
38
38
  &block
39
39
  )
40
40
 
41
- unpack(response, key: :variable)
41
+ unpack(:variable) { response }
42
42
  end
43
43
 
44
44
  def destroy(app_id, variable_id, headers = nil, &block)
@@ -51,7 +51,7 @@ module Scalingo
51
51
  &block
52
52
  )
53
53
 
54
- unpack(response)
54
+ unpack { response }
55
55
  end
56
56
 
57
57
  def bulk_update(app_id, variables, headers = nil, &block)
@@ -64,7 +64,7 @@ module Scalingo
64
64
  &block
65
65
  )
66
66
 
67
- unpack(response, key: :variables)
67
+ unpack(:variables) { response }
68
68
  end
69
69
 
70
70
  def bulk_destroy(app_id, variable_ids, headers = nil, &block)
@@ -77,7 +77,7 @@ module Scalingo
77
77
  &block
78
78
  )
79
79
 
80
- unpack(response)
80
+ unpack { response }
81
81
  end
82
82
  end
83
83
  end
@@ -10,7 +10,7 @@ module Scalingo
10
10
  &block
11
11
  )
12
12
 
13
- unpack(response, key: :events)
13
+ unpack(:events) { response }
14
14
  end
15
15
 
16
16
  def for(app_id, payload = {}, headers = nil, &block)
@@ -23,7 +23,7 @@ module Scalingo
23
23
  &block
24
24
  )
25
25
 
26
- unpack(response, key: :events)
26
+ unpack(:events) { response }
27
27
  end
28
28
 
29
29
  def types(headers = nil, &block)
@@ -36,7 +36,7 @@ module Scalingo
36
36
  &block
37
37
  )
38
38
 
39
- unpack(response, key: :event_types)
39
+ unpack(:event_types) { response }
40
40
  end
41
41
 
42
42
  def categories(headers = nil, &block)
@@ -49,7 +49,7 @@ module Scalingo
49
49
  &block
50
50
  )
51
51
 
52
- unpack(response, key: :event_categories)
52
+ unpack(:event_categories) { response }
53
53
  end
54
54
  end
55
55
  end
@@ -10,7 +10,7 @@ module Scalingo
10
10
  &block
11
11
  )
12
12
 
13
- unpack(response)
13
+ unpack { response }
14
14
  end
15
15
 
16
16
  def archives(app_id, headers = nil, &block)
@@ -23,7 +23,7 @@ module Scalingo
23
23
  &block
24
24
  )
25
25
 
26
- unpack(response, key: :archives)
26
+ unpack(:archives) { response }
27
27
  end
28
28
 
29
29
  ## Helper method to avoid having to manually chain two operations
@@ -22,7 +22,7 @@ module Scalingo
22
22
  &block
23
23
  )
24
24
 
25
- unpack(response)
25
+ unpack { response }
26
26
  end
27
27
 
28
28
  def types(headers = nil, &block)
@@ -35,7 +35,7 @@ module Scalingo
35
35
  &block
36
36
  )
37
37
 
38
- unpack(response, key: :metrics)
38
+ unpack(:metrics) { response }
39
39
  end
40
40
  end
41
41
  end
@@ -12,7 +12,7 @@ module Scalingo
12
12
  &block
13
13
  )
14
14
 
15
- unpack(response, key: :notifiers)
15
+ unpack(:notifiers) { response }
16
16
  end
17
17
 
18
18
  def find(app_id, notifier_id, headers = nil, &block)
@@ -25,7 +25,7 @@ module Scalingo
25
25
  &block
26
26
  )
27
27
 
28
- unpack(response, key: :notifier)
28
+ unpack(:notifier) { response }
29
29
  end
30
30
 
31
31
  def create(app_id, payload = {}, headers = nil, &block)
@@ -38,7 +38,7 @@ module Scalingo
38
38
  &block
39
39
  )
40
40
 
41
- unpack(response, key: :notifier)
41
+ unpack(:notifier) { response }
42
42
  end
43
43
 
44
44
  def update(app_id, notifier_id, payload = {}, headers = nil, &block)
@@ -51,7 +51,7 @@ module Scalingo
51
51
  &block
52
52
  )
53
53
 
54
- unpack(response, key: :notifier)
54
+ unpack(:notifier) { response }
55
55
  end
56
56
 
57
57
  def destroy(app_id, notifier_id, headers = nil, &block)
@@ -64,7 +64,7 @@ module Scalingo
64
64
  &block
65
65
  )
66
66
 
67
- unpack(response)
67
+ unpack { response }
68
68
  end
69
69
 
70
70
  def test(app_id, notifier_id, headers = nil, &block)
@@ -77,7 +77,7 @@ module Scalingo
77
77
  &block
78
78
  )
79
79
 
80
- unpack(response)
80
+ unpack { response }
81
81
  end
82
82
 
83
83
  def platforms(headers = nil, &block)
@@ -90,7 +90,7 @@ module Scalingo
90
90
  &block
91
91
  )
92
92
 
93
- unpack(response, key: :notification_platforms)
93
+ unpack(:notification_platforms) { response }
94
94
  end
95
95
  end
96
96
  end
@@ -20,7 +20,7 @@ module Scalingo
20
20
  &block
21
21
  )
22
22
 
23
- unpack(response, key: :operation)
23
+ unpack(:operation) { response }
24
24
  end
25
25
  end
26
26
  end
@@ -12,7 +12,7 @@ module Scalingo
12
12
  &block
13
13
  )
14
14
 
15
- unpack(response, key: :scm_repo_link)
15
+ unpack(:scm_repo_link) { response }
16
16
  end
17
17
 
18
18
  def create(app_id, payload = {}, headers = nil, &block)
@@ -25,7 +25,7 @@ module Scalingo
25
25
  &block
26
26
  )
27
27
 
28
- unpack(response, key: :scm_repo_link)
28
+ unpack(:scm_repo_link) { response }
29
29
  end
30
30
 
31
31
  def update(app_id, payload = {}, headers = nil, &block)
@@ -38,7 +38,7 @@ module Scalingo
38
38
  &block
39
39
  )
40
40
 
41
- unpack(response, key: :scm_repo_link)
41
+ unpack(:scm_repo_link) { response }
42
42
  end
43
43
 
44
44
  def destroy(app_id, headers = nil, &block)
@@ -51,7 +51,7 @@ module Scalingo
51
51
  &block
52
52
  )
53
53
 
54
- unpack(response, key: :scm_repo_link)
54
+ unpack(:scm_repo_link) { response }
55
55
  end
56
56
 
57
57
  def deploy(app_id, branch, headers = nil, &block)
@@ -64,7 +64,7 @@ module Scalingo
64
64
  &block
65
65
  )
66
66
 
67
- unpack(response, key: :deployment)
67
+ unpack(:deployment) { response }
68
68
  end
69
69
 
70
70
  def review_app(review_app, pull_request_id, headers = nil, &block)
@@ -77,7 +77,7 @@ module Scalingo
77
77
  &block
78
78
  )
79
79
 
80
- unpack(response, key: :review_app)
80
+ unpack(:review_app) { response }
81
81
  end
82
82
 
83
83
  def branches(app_id, headers = nil, &block)
@@ -90,7 +90,7 @@ module Scalingo
90
90
  &block
91
91
  )
92
92
 
93
- unpack(response, key: :branches)
93
+ unpack(:branches) { response }
94
94
  end
95
95
 
96
96
  def pulls(app_id, headers = nil, &block)
@@ -103,7 +103,7 @@ module Scalingo
103
103
  &block
104
104
  )
105
105
 
106
- unpack(response, key: :pulls)
106
+ unpack(:pulls) { response }
107
107
  end
108
108
  end
109
109
  end
@@ -0,0 +1,31 @@
1
+ require "scalingo/bearer_token"
2
+
3
+ module Scalingo
4
+ module TokenHolder
5
+ def self.included(base)
6
+ base.attr_reader :token
7
+ end
8
+
9
+ def token=(input)
10
+ @token = input.is_a?(BearerToken) ? input : BearerToken.new(input.to_s, raise_on_expired: config.raise_on_expired_token)
11
+ end
12
+
13
+ def authenticated?
14
+ token.present? && !token.expired?
15
+ end
16
+
17
+ def authenticate_with_bearer_token(bearer_token, expires_at:, raise_on_expired_token:)
18
+ self.token = if expires_at
19
+ token = bearer_token.is_a?(BearerToken) ? bearer_token.value : bearer_token.to_s
20
+
21
+ BearerToken.new(
22
+ token,
23
+ expires_at: expires_at,
24
+ raise_on_expired: raise_on_expired_token,
25
+ )
26
+ else
27
+ bearer_token
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,3 +1,3 @@
1
1
  module Scalingo
2
- VERSION = "3.0.0.beta.2"
2
+ VERSION = "3.0.0.beta.3"
3
3
  end
@@ -0,0 +1,49 @@
1
+ {
2
+ "path": "/apps/5ed10967884fef000f5e4fff/addons/ad-6733873b-1628-4b8e-8e74-c30f1c3f3b63/token",
3
+ "method": "post",
4
+ "request": {
5
+ "headers": {
6
+ "Authorization": "Bearer the-bearer-token"
7
+ }
8
+ },
9
+ "response": {
10
+ "status": 200,
11
+ "headers": {
12
+ "Date": "Fri, 29 May 2020 13:09:00 GMT",
13
+ "Etag": "W/\"ece1ff1263aad47584f356e20127cf3b\"",
14
+ "Content-Type": "application/json; charset=utf-8",
15
+ "Transfer-Encoding": "chunked",
16
+ "Connection": "keep-alive",
17
+ "Cache-Control": "max-age=0, private, must-revalidate",
18
+ "Referrer-Policy": "strict-origin-when-cross-origin"
19
+ },
20
+ "json_body": {
21
+ "addon": {
22
+ "id": "ad-6733873b-1628-4b8e-8e74-c30f1c3f3b63",
23
+ "app_id": "5ed10967884fef000f5e4fff",
24
+ "resource_id": "some_example_5629",
25
+ "addon_provider": {
26
+ "id": "postgresql",
27
+ "name": "PostgreSQL",
28
+ "logo_url": "//cdn.scalingo.com/addons/Scalingo_Postgresql.svg"
29
+ },
30
+ "plan": {
31
+ "id": "5d1e03873e6b3b000eefa827",
32
+ "name": "postgresql-sandbox",
33
+ "display_name": "Sandbox",
34
+ "price": 0.0,
35
+ "description": "<p>Pouet</p>\n",
36
+ "position": null,
37
+ "on_demand": false,
38
+ "disabled": false,
39
+ "disabled_alternative_plan_id": null,
40
+ "sku": "ovh-st-fr1-postgresql-sandbox"
41
+ },
42
+ "provisioned_at": "2020-05-29T13:08:58.103Z",
43
+ "deprovisioned_at": null,
44
+ "status": "provisioning",
45
+ "token": "eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJTY2FsaW5nbyIsImlhdCI6MTU5Mjg0MjQ3OSwidXVpZCI6InVzLTY3NmJiMGMxLTFjYTktNDY4Mi04ZWUxLTI1NzQyMmU5ZWFiNSIsInJuZCI6IjlhZGZlMzNlNDUxNzNmZTBiNjg4ZjQxZTY4OTg1OWQ0IiwiZXhwIjoxNTkyODQ2MDc5LCJhZGRvbl91dWlkIjoiYWQtNTRlYzczMzItYjViOS00MGUyLTg2NWQtYmNkYTg1Yzc2Y2I2In0.-rYijDhFRWcycSFLcCbdQD3tg9rh1MhXE_rTvlOtQt-48BJcPmxH9Wpazzp7tkspYrx8jfIrWrALoujvW33WVw"
46
+ }
47
+ }
48
+ }
49
+ }
@@ -0,0 +1,24 @@
1
+ {
2
+ "path": "/apps/5ed10967884fef000f5e4fff/addons/wrong-addon-id/token",
3
+ "method": "post",
4
+ "request": {
5
+ "headers": {
6
+ "Authorization": "Bearer the-bearer-token"
7
+ }
8
+ },
9
+ "response": {
10
+ "status": 404,
11
+ "headers": {
12
+ "Date": "Fri, 29 May 2020 13:08:59 GMT",
13
+ "Content-Type": "application/json; charset=utf-8",
14
+ "Transfer-Encoding": "chunked",
15
+ "Connection": "keep-alive",
16
+ "Cache-Control": "no-cache",
17
+ "Referrer-Policy": "strict-origin-when-cross-origin"
18
+ },
19
+ "json_body": {
20
+ "resource": "addon",
21
+ "error": "not found"
22
+ }
23
+ }
24
+ }
@@ -19,7 +19,7 @@ Gem::Specification.new do |s|
19
19
  "changelog_uri" => "https://github.com/Scalingo/scalingo-ruby-api/blob/master/CHANGELOG.md",
20
20
  "documentation_uri" => "https://developers.scalingo.com/",
21
21
  "homepage_uri" => "https://www.scalingo.com/",
22
- "source_code_uri" => "https://github.com/Scalingo/scalingo-ruby-api"
22
+ "source_code_uri" => "https://github.com/Scalingo/scalingo-ruby-api",
23
23
  }
24
24
 
25
25
  # Specify which files should be added to the gem when it is released.
@@ -3,14 +3,61 @@ require "spec_helper"
3
3
  RSpec.describe Scalingo::API::Client do
4
4
  let(:url) { "http://localhost" }
5
5
 
6
- subject { described_class.new(scalingo, url) }
6
+ subject { described_class.new(url, scalingo: scalingo) }
7
7
 
8
8
  describe "initialize" do
9
- it "stores the scalingo client and the url" do
10
- instance = described_class.new(:scalingo, :url)
9
+ let(:config) { {default_region: :test} }
11
10
 
12
- expect(instance.scalingo).to eq(:scalingo)
13
- expect(instance.url).to eq(:url)
11
+ describe "with only url" do
12
+ subject { described_class.new(:url) }
13
+
14
+ it "stores the url" do
15
+ expect(subject.url).to eq(:url)
16
+ end
17
+
18
+ it "uses itself as token holder" do
19
+ expect(subject.token_holder).to eq(subject)
20
+ end
21
+
22
+ it "configuration is the global one" do
23
+ expect(Scalingo::Configuration).to receive(:new).with({}, Scalingo.config).and_return(:config).once
24
+
25
+ expect(subject.config).to eq(:config)
26
+ end
27
+ end
28
+
29
+ describe "with scalingo client supplied" do
30
+ subject { described_class.new(:url, scalingo: scalingo) }
31
+
32
+ it "uses the scalingo client as token holder" do
33
+ expect(subject.token_holder).to eq(scalingo)
34
+ end
35
+
36
+ it "configuration is herited from the scalingo client" do
37
+ expect(Scalingo::Configuration).to receive(:new).with({}, scalingo.config).once
38
+
39
+ subject
40
+ end
41
+ end
42
+
43
+ describe "with config supplied" do
44
+ subject { described_class.new(:url, config: config) }
45
+
46
+ it "configuration is herited from the global one" do
47
+ expect(Scalingo::Configuration).to receive(:new).with(config, Scalingo.config).and_return(:config).once
48
+
49
+ expect(subject.config).to eq(:config)
50
+ end
51
+ end
52
+
53
+ describe "with both supplied" do
54
+ subject { described_class.new(:url, scalingo: scalingo, config: config) }
55
+
56
+ it "configuration is herited from the scalingo one" do
57
+ expect(Scalingo::Configuration).to receive(:new).with(config, scalingo.config).once
58
+
59
+ subject
60
+ end
14
61
  end
15
62
  end
16
63
 
@@ -26,7 +73,7 @@ RSpec.describe Scalingo::API::Client do
26
73
  mock = double
27
74
 
28
75
  described_class.register_handler!(:handler, mock)
29
- instance = described_class.new(:scalingo, :url)
76
+ instance = described_class.new(:url, scalingo: scalingo)
30
77
 
31
78
  # Only 1 instanciation should be done, no matter how many calls are done below
32
79
  expect(mock).to receive(:new).with(instance).and_return("1st").once
@@ -55,20 +102,20 @@ RSpec.describe Scalingo::API::Client do
55
102
  proc { {"X-Another" => "another"} }
56
103
  }
57
104
 
58
- it "only returns the user agent if nothing else is configured" do
59
- expect(subject.headers).to eq("User-Agent" => user_agent)
105
+ it "only returns the user agent and accept if nothing else is configured" do
106
+ expect(subject.headers).to eq("Accept" => "application/json", "User-Agent" => user_agent)
60
107
  end
61
108
 
62
109
  it "allows additional headers to be globally configured" do
63
110
  expect(scalingo.config).to receive(:additional_headers).and_return(extra_hash)
64
111
 
65
- expect(subject.headers).to eq("User-Agent" => user_agent, "X-Other" => "other")
112
+ expect(subject.headers).to eq("Accept" => "application/json", "User-Agent" => user_agent, "X-Other" => "other")
66
113
  end
67
114
 
68
115
  it "additional headers can be a block" do
69
116
  expect(scalingo.config).to receive(:additional_headers).and_return(extra_block)
70
117
 
71
- expect(subject.headers).to eq("User-Agent" => user_agent, "X-Another" => "another")
118
+ expect(subject.headers).to eq("Accept" => "application/json", "User-Agent" => user_agent, "X-Another" => "another")
72
119
  end
73
120
  end
74
121
 
@@ -118,7 +165,7 @@ RSpec.describe Scalingo::API::Client do
118
165
 
119
166
  context "with bearer token" do
120
167
  it "has an authentication header set with a bearer scheme" do
121
- expect(subject.connection.headers["Authorization"]).to eq "Bearer #{subject.scalingo.token.value}"
168
+ expect(subject.connection.headers["Authorization"]).to eq "Bearer #{subject.token_holder.token.value}"
122
169
  end
123
170
  end
124
171
  end