fastly 2.4.0 → 3.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9a2ede9580b2a60043debfc3a95545c6ec6d88dce770e53f626975d508922230
4
- data.tar.gz: 88471ec1a779279a3b35bbf42c2882caef0adc6fd54161eb0f0793180a1f9e0a
3
+ metadata.gz: d0331b2215d245985194128508980192db08a0ae94c0da70c1f3b417a4309df7
4
+ data.tar.gz: d3ab806682ed8e8141a301886fb2bf21b91102f478866dee281b3be2c7480a5c
5
5
  SHA512:
6
- metadata.gz: d66d4a875d04d5889dd0f7d094073abc575cc9ef3f8c698d03050e35a1236ce3f2d94356503d9391f494a95f7cc47ff0dc581e04a342db79e0689b8038d4ed74
7
- data.tar.gz: 257ca1ae2413695c47394fecd8b9a77e9be2d073232ea0a2ea186045c7356e96b562a1d66cafb9b7e2a71fe2760b52c5f121aacae98d99184cd768c10f4aa5b2
6
+ metadata.gz: 4e738dcf185c95ff25b675f2795fbdd62154894c1c00098dccd48f39d1dfb854491f8f007b5a1c1bef1c0689aad5392b0b5c3facb22ed0a956b501a7300017c3
7
+ data.tar.gz: f76512046a815ab57aa559abc71f9ab9108246defb81f2c710579f0d11296eb2f422897bb97440aa8a18f9c57b363ae1570db95257e50e9f1ed12736a7a959ac
data/.env.example ADDED
@@ -0,0 +1,3 @@
1
+ FASTLY_TEST_USER=
2
+ FASTLY_TEST_PASSWORD=
3
+ FASTLY_TEST_API_KEY=
data/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Change Log
2
2
 
3
+ ## [v3.0.0](https://github.com/fastly/fastly-ruby/tree/v3.0.0) (2021-02-03)
4
+
5
+ [Full Changelog](https://github.com/fastly/fastly-ruby/compare/v2.5.3...v3.0.0)
6
+
7
+ **Merged pull requests:**
8
+
9
+ - Removes session authentication [\#170](https://github.com/fastly/fastly-ruby/pull/170) ([conniechu929](https://github.com/conniechu929))
10
+
11
+ ## [v2.5.0](https://github.com/fastly/fastly-ruby/tree/v2.5.0) (2020-01-16)
12
+
13
+ [Full Changelog](https://github.com/fastly/fastly-ruby/compare/v2.4.0...v2.5.0)
14
+
15
+ **Merged pull requests:**
16
+
17
+ - Add tokens.rb [\#150](https://github.com/fastly/fastly-ruby/pull/150) ([malachy-mcconnell](https://github.com/malachy-mcconnell))
18
+
3
19
  ## [v2.4.0](https://github.com/fastly/fastly-ruby/tree/v2.4.0) (2019-07-29)
4
20
  [Full Changelog](https://github.com/fastly/fastly-ruby/compare/v2.3.1...v2.4.0)
5
21
 
data/Gemfile CHANGED
@@ -4,10 +4,14 @@ gemspec
4
4
 
5
5
  group :development, :test do
6
6
  gem 'appraisal', '~> 2.1'
7
- gem 'rake', '~> 10.3.2'
7
+ gem 'rake', '>= 12.3.3'
8
8
  gem 'rdoc', '~> 4.1.1'
9
9
  gem 'minitest', '~> 5.3.4'
10
10
  gem 'pry'
11
11
  gem 'rubocop', '~> 0.57.2', require: false
12
12
  gem 'webmock'
13
13
  end
14
+
15
+ group :test do
16
+ gem 'dotenv'
17
+ end
data/README.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  Client library for interacting with the Fastly web acceleration service [API](http://docs.fastly.com/api)
4
4
 
5
+ ### A Note About Authentication
6
+
7
+ Authenticating with a username/password is deprecated and will no longer be available starting September 2020.
8
+
9
+ Authenticating with an API Token is shown in the example section below. For more information on API Tokens, please see [Fastly's API Token documentation](https://developer.fastly.com/reference/api/auth/). For more information about authenticating to our API, please see our [Authentication section](https://developer.fastly.com/reference/api/#authentication).
10
+
5
11
  ## Examples
6
12
 
7
13
  Add fastly to your Gemfile:
@@ -13,7 +19,8 @@ Create a fastly client:
13
19
 
14
20
  ```ruby
15
21
  # some_file.rb
16
- # use {api_key: 'your-key'} or {user: 'username', password: 'password'} as login options
22
+ # username/password authentication is deprecated and will not be available
23
+ # starting September 2020; use {api_key: 'your-key'} as the login option
17
24
  fastly = Fastly.new(login_opts)
18
25
 
19
26
  current_user = fastly.current_user
@@ -202,19 +209,23 @@ This option should not be used in a production setting as all HTTP headers, requ
202
209
 
203
210
  ### Notes for testing
204
211
 
205
- The test suite requires the following `ENV` variables to be set:
212
+ The test suite tests create and delete three services in sequence, so you may want to create an account just for these tests.
213
+
214
+ To run the test suite:
215
+
216
+ 1. Generate a personal token for these tests: https://manage.fastly.com/account/personal/tokens
217
+
218
+ 2. Copy `.env.example` to `.env` and add the values for the variables:
206
219
 
207
- * `FASTLY_TEST_USER` - Your user email
208
- * `FASTLY_TEST_PASSWORD` - Your account password
209
- * `FASTLY_TEST_API_KEY` - Your API key (found at https://app.fastly.com/#account)
220
+ * `FASTLY_TEST_USER` - Your user email
221
+ * `FASTLY_TEST_PASSWORD` - Your account password
222
+ * `FASTLY_TEST_API_KEY` - Your personal token
210
223
 
211
- While the test suite is safe to be run on all accounts and isn't harmful to your
212
- data, the tests will create and delete 3 services in sequence so you may want
213
- to create an account just for tests.
224
+ 3. Run the tests via `bundle exec rake test:unit`
214
225
 
215
226
  ## Copyright
216
227
 
217
- Copyright 2011-2014 - Fastly Inc
228
+ Copyright 2011-2020 - Fastly, Inc.
218
229
 
219
230
  ## Redistribution
220
231
 
data/lib/fastly.rb CHANGED
@@ -34,6 +34,7 @@ require 'fastly/snippet'
34
34
  require 'fastly/dynamic_snippet'
35
35
  require 'fastly/sumologic_logging'
36
36
  require 'fastly/syslog'
37
+ require 'fastly/token'
37
38
  require 'fastly/s3_logging'
38
39
  require 'fastly/gcs_logging'
39
40
  require 'fastly/user'
@@ -54,8 +55,8 @@ class Fastly
54
55
  #
55
56
  # Some methods require full username and password rather than just auth token.
56
57
  def initialize(opts)
57
- if opts[:api_key].nil? && (opts[:password].nil? || opts[:user].nil?)
58
- raise ArgumentError, "Required options missing. Please pass either ':api_key' or both ':user' and ':password'."
58
+ if opts[:api_key].nil?
59
+ raise ArgumentError, "Required option missing. Please pass ':api_key'."
59
60
  end
60
61
 
61
62
  client(opts)
@@ -151,7 +152,7 @@ class Fastly
151
152
  client.get_stats('/stats/regions')
152
153
  end
153
154
 
154
- [ACL, ACLEntry, User, Customer, Backend, CacheSetting, Condition, Dictionary, DictionaryItem, Director, Domain, Header, Healthcheck, Gzip, Match, PapertrailLogging, RequestSetting, ResponseObject, Service, Snippet, S3Logging, Syslog, VCL, Version].each do |klass|
155
+ [ACL, ACLEntry, User, Customer, Backend, CacheSetting, Condition, Dictionary, DictionaryItem, Director, Domain, Header, Healthcheck, Gzip, Match, PapertrailLogging, RequestSetting, ResponseObject, Service, Snippet, S3Logging, Syslog, Token, VCL, Version].each do |klass|
155
156
  type = Util.class_to_path(klass)
156
157
 
157
158
  if klass.respond_to?(:pluralize)
@@ -16,9 +16,9 @@ class Fastly
16
16
  end
17
17
 
18
18
  # Get the number of the Version this belongs to
19
- def version_number # rubocop:disable all
20
- @version
21
- end # rubocop:enable all
19
+ def version_number
20
+ @version ||= nil
21
+ end
22
22
 
23
23
  # :nodoc:
24
24
  def as_hash
data/lib/fastly/client.rb CHANGED
@@ -11,7 +11,7 @@ class Fastly
11
11
 
12
12
  DEFAULT_URL = 'https://api.fastly.com'.freeze
13
13
 
14
- attr_accessor :api_key, :base_url, :debug, :user, :password, :cookie, :customer
14
+ attr_accessor :api_key, :base_url, :debug, :user, :password, :customer
15
15
 
16
16
  def initialize(opts)
17
17
  @api_key = opts.fetch(:api_key, nil)
@@ -25,15 +25,12 @@ class Fastly
25
25
  Concurrent::ThreadLocalVar.new { build_http_client }
26
26
  end
27
27
 
28
- return self unless fully_authed?
29
-
30
- # If full auth creds (user/pass) then log in and set a cookie
31
- resp = http.post('/login', make_params(user: user, password: password))
32
-
33
- if resp.kind_of?(Net::HTTPSuccess)
34
- @cookie = resp['Set-Cookie']
35
- else
36
- fail Unauthorized, "Invalid auth credentials. Check username/password."
28
+ warn("DEPRECATION WARNING: Username/password authentication is deprecated
29
+ and will not be available starting September 2020;
30
+ please migrate to API tokens as soon as possible.")
31
+
32
+ if api_key.nil?
33
+ fail Unauthorized, "Invalid auth credentials. Check api_key."
37
34
  end
38
35
 
39
36
  self
@@ -59,8 +56,9 @@ class Fastly
59
56
 
60
57
  def get(path, params = {})
61
58
  extras = params.delete(:headers) || {}
59
+ include_auth = params.key?(:include_auth) ? params.delete(:include_auth) : true
62
60
  path += "?#{make_params(params)}" unless params.empty?
63
- resp = http.get(path, headers(extras))
61
+ resp = http.get(path, headers(extras, include_auth))
64
62
  fail Error, resp.body unless resp.kind_of?(Net::HTTPSuccess)
65
63
  JSON.parse(resp.body)
66
64
  end
@@ -86,7 +84,8 @@ class Fastly
86
84
 
87
85
  def delete(path, params = {})
88
86
  extras = params.delete(:headers) || {}
89
- resp = http.delete(path, headers(extras))
87
+ include_auth = params.key?(:include_auth) ? params.delete(:include_auth) : true
88
+ resp = http.delete(path, headers(extras, include_auth))
90
89
  resp.kind_of?(Net::HTTPSuccess)
91
90
  end
92
91
 
@@ -132,16 +131,18 @@ class Fastly
132
131
 
133
132
  def post_and_put(method, path, params = {})
134
133
  extras = params.delete(:headers) || {}
134
+ include_auth = params.key?(:include_auth) ? params.delete(:include_auth) : true
135
135
  query = make_params(params)
136
- resp = http.send(method, path, query, headers(extras).merge('Content-Type' => 'application/x-www-form-urlencoded'))
136
+ resp = http.send(method, path, query, headers(extras, include_auth).merge('Content-Type' => 'application/x-www-form-urlencoded'))
137
137
  fail Error, resp.body unless resp.kind_of?(Net::HTTPSuccess)
138
138
  JSON.parse(resp.body)
139
139
  end
140
140
 
141
- def headers(extras={})
142
- headers = fully_authed? ? { 'Cookie' => cookie } : {}
143
- headers['Fastly-Key'] = api_key if api_key
144
-
141
+ def headers(extras={}, include_auth=true)
142
+ headers = {}
143
+ if include_auth
144
+ headers['Fastly-Key'] = api_key if api_key
145
+ end
145
146
  headers.merge('Content-Accept' => 'application/json', 'User-Agent' => "fastly-ruby-v#{Fastly::VERSION}").merge(extras.keep_if {|k,v| !v.nil? })
146
147
  end
147
148
 
@@ -1,4 +1,4 @@
1
1
  # The current version of the library
2
2
  class Fastly
3
- VERSION = "2.4.0"
3
+ VERSION = "3.0.0"
4
4
  end
@@ -2,7 +2,8 @@
2
2
  class Fastly
3
3
  # Represents something you want to serve - this can be, for example, a whole web site, a Wordpress site, or just your image servers
4
4
  class Service < Base
5
- attr_accessor :id, :customer_id, :name, :comment, :versions
5
+ attr_accessor :id, :customer_id, :name, :comment
6
+ attr_writer :versions
6
7
 
7
8
  @versions = []
8
9
 
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Token object
4
+ class Fastly
5
+ class Token < Base
6
+ attr_accessor :id, :access_token, :user_id, :services, :name, :scope, :created_at, :last_used_at, :expires_at, :ip, :user_agent
7
+
8
+ private
9
+
10
+ def self.get_path(*_args)
11
+ '/tokens'
12
+ end
13
+
14
+ def self.post_path(*_args)
15
+ '/tokens'
16
+ end
17
+
18
+ def self.delete_path(opts)
19
+ "/tokens/#{opts.id}"
20
+ end
21
+ end
22
+
23
+ def new_token(opts)
24
+ if client.fully_authed?
25
+ opts[:username] = client.user
26
+ opts[:password] = client.password
27
+ opts[:include_auth] = false
28
+
29
+ token = create(Token, opts)
30
+ token.nil? ? nil : token
31
+ else
32
+ raise ArgumentError, "Required options missing. Please pass :api_key, :user and :password."
33
+ end
34
+ end
35
+
36
+ def customer_tokens(opts)
37
+ hash = client.get("/customer/#{opts[:customer_id]}/tokens")
38
+ hash.map { |token_hash| Token.new(token_hash, Fastly::Fetcher) }
39
+ end
40
+ end
data/test/api_key_test.rb CHANGED
@@ -50,7 +50,7 @@ class Fastly
50
50
  end
51
51
  end
52
52
 
53
- describe 'with cookie and api key' do
53
+ describe 'with username/password and api key' do
54
54
  before do
55
55
  @opts = login_opts(:both)
56
56
  @client = Fastly::Client.new(@opts)
@@ -75,26 +75,6 @@ class Fastly
75
75
  assert_equal 'ok', response['status']
76
76
  end
77
77
  end
78
-
79
- describe 'with cookie only' do
80
- before do
81
- @opts = login_opts(:full)
82
- @client = Fastly::Client.new(@opts)
83
- @fastly = Fastly.new(@opts)
84
- service_name = "fastly-test-service-#{random_string}"
85
- @service = @fastly.create_service(:name => service_name)
86
- end
87
-
88
- after do
89
- @fastly.delete_service(@service)
90
- end
91
-
92
- it 'does not allow purging' do
93
- assert_raises Fastly::KeyAuthRequired do
94
- @service.purge_by_key('somekey')
95
- end
96
- end
97
- end
98
78
  end
99
79
  end
100
80
  end
@@ -12,7 +12,7 @@ describe Fastly::Client do
12
12
  }
13
13
  end
14
14
 
15
- it 'does not log in if user/pass are not provided' do
15
+ it 'does not set the user/pass if they are not provided' do
16
16
  client = Fastly::Client.new(api_key: api_key)
17
17
 
18
18
  assert_equal api_key, client.api_key
@@ -20,13 +20,17 @@ describe Fastly::Client do
20
20
  assert_equal nil, client.password
21
21
  end
22
22
 
23
+ it 'raises Unauthorized if api_key is not passed in the options' do
24
+ assert_raises(Fastly::Unauthorized) { Fastly::Client.new(user: user, password: password)}
25
+ end
26
+
23
27
  it 'raises Unauthorized if user/pass provided but are invalid' do
24
28
  stub_request(:any, /api.fastly.com/).to_return(status: 400)
25
29
 
26
30
  e = assert_raises(Fastly::Unauthorized) {
27
- Fastly::Client.new(user: user, password: pass)
31
+ Fastly::Client.new(user: user, password: password)
28
32
  }
29
- assert_equal "Invalid auth credentials. Check username/password.", e.message
33
+ assert_equal "Invalid auth credentials. Check api_key.", e.message
30
34
  end
31
35
 
32
36
  it 'initializes an http client' do
@@ -38,23 +42,13 @@ describe Fastly::Client do
38
42
  assert client.http.use_ssl?
39
43
  end
40
44
 
41
- it 'sets a cookie when auth with valid user/pass' do
42
- stub_request(:any, /api.fastly.com/).
43
- to_return(body: JSON.generate(i: "dont care"), status: 200, headers: { 'Set-Cookie' => 'tasty!' })
44
-
45
- client = Fastly::Client.new(user: user, password: pass)
46
- assert_equal "tasty!", client.cookie
47
- end
48
-
49
45
  it 'raises an Error if username is used in place of user as an option' do
50
46
  stub_request(:any, /api.fastly.com/).
51
- to_return(body: JSON.generate(i: "dont care"), status: 200, headers: { 'Set-Cookie' => 'tasty!' })
47
+ to_return(body: JSON.generate(i: "dont care"), status: 200)
52
48
 
53
- assert_raises(ArgumentError) { Fastly.new(username: user, password: pass) }
49
+ assert_raises(ArgumentError) { Fastly.new(username: user, password: password) }
54
50
 
55
- Fastly.new(user: user, password: pass)
56
- Fastly.new(api_key: api_key)
57
- Fastly.new(api_key: api_key, user: user, password: pass)
51
+ Fastly.new(api_key: api_key, user: user, password: password)
58
52
  end
59
53
  end
60
54
 
@@ -98,6 +92,25 @@ describe Fastly::Client do
98
92
  assert_equal resp.class, Hash
99
93
  assert_includes resp, "i"
100
94
  end
95
+
96
+ it 'can make a POST without auth if asked to do so' do
97
+ stub_request(:post, /api.fastly.com/).
98
+ with(headers: {
99
+ 'Accept'=>'*/*',
100
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
101
+ 'Content-Accept'=>'application/json',
102
+ 'Content-Type'=>'application/x-www-form-urlencoded',
103
+ #'Fastly-Key'=>'notasecreteither',
104
+ 'User-Agent'=> /fastly-ruby/
105
+ }).
106
+ to_return(body: JSON.generate(i: "dont care"), status: 200)
107
+
108
+ resp = client.post(
109
+ '/service/blah',
110
+ {include_auth: false}
111
+ )
112
+ end
113
+
101
114
  end
102
115
 
103
116
  describe 'get_stats' do
@@ -7,7 +7,7 @@ describe Fastly::Customer do
7
7
  let(:owner_id) { SecureRandom.hex(6) }
8
8
 
9
9
  let(:customer) do
10
- stub_request(:post, "#{Fastly::Client::DEFAULT_URL}/login").to_return(body: '{}', status: 200, headers: { 'Set-Cookie' => 'tasty!' })
10
+ stub_request(:post, "#{Fastly::Client::DEFAULT_URL}/login").to_return(body: '{}', status: 200)
11
11
 
12
12
  customer_body = JSON.dump(
13
13
  'id' => customer_id,
@@ -2,13 +2,13 @@ require_relative '../test_helper'
2
2
 
3
3
  describe Fastly::Dictionary do
4
4
 
5
- let(:client) { Fastly.new(user: 'test@example.com', password: 'password') }
5
+ let(:client) { Fastly.new(api_key: 'notasecrettestkey') }
6
6
  let(:service_id) { SecureRandom.hex(6) }
7
7
  let(:version) { 1 }
8
8
  let(:dictionary) { Fastly::Dictionary.new({id: SecureRandom.hex(6), service_id: service_id, version: 1}, client) }
9
9
 
10
10
  before {
11
- stub_request(:post, "#{Fastly::Client::DEFAULT_URL}/login").to_return(body: '{}', status: 200, headers: { 'Set-Cookie' => 'tasty!' })
11
+ stub_request(:post, "#{Fastly::Client::DEFAULT_URL}/login").to_return(body: '{}', status: 200)
12
12
  }
13
13
 
14
14
  describe '#item' do
@@ -2,13 +2,13 @@ require_relative '../test_helper'
2
2
 
3
3
  describe Fastly::Syslog do
4
4
 
5
- let(:client) { Fastly.new(user: 'test@example.com', password: 'password') }
5
+ let(:client) { Fastly.new(api_key: 'notasecrettestkey', user: 'test@example.com', password: 'password') }
6
6
  let(:service_id) { SecureRandom.hex(6) }
7
7
  let(:version) { 1 }
8
8
  let(:syslog) { Fastly::Syslog.new({ name: 'test_syslog', service_id: service_id, version: 1 }, client) }
9
9
 
10
10
  before {
11
- stub_request(:post, "#{Fastly::Client::DEFAULT_URL}/login").to_return(body: '{}', status: 200, headers: { 'Set-Cookie' => 'tasty!' })
11
+ stub_request(:post, "#{Fastly::Client::DEFAULT_URL}/login").to_return(body: '{}', status: 200)
12
12
  }
13
13
 
14
14
  describe '#item' do
@@ -0,0 +1,127 @@
1
+ require_relative '../test_helper'
2
+
3
+ describe Fastly::Token do
4
+ let(:fastly) { Fastly.new(api_key:'my_api_key', user: 'test@example.com', password: 'password') }
5
+
6
+ before {
7
+ stub_request(:post, "#{Fastly::Client::DEFAULT_URL}/login").to_return(body: '{}', status: 200)
8
+ }
9
+
10
+ describe '#fastly' do
11
+ it 'cannot create itself because POST /tokens must have no auth headers' do
12
+ stub_request(:post, "https://api.fastly.com/tokens").
13
+ with(
14
+ body: {"name"=>"name_of_token", "scope"=>"token_scope such_as purge_all purge_select", "services"=>"service_id_that_token_can_access"},
15
+ headers: {
16
+ 'Accept'=>'*/*',
17
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
18
+ 'Content-Accept'=>'application/json',
19
+ 'Content-Type'=>'application/x-www-form-urlencoded',
20
+ 'User-Agent'=> /fastly-ruby/
21
+ }).
22
+ to_return(status: 403, body: '{"msg":"You must POST /sudo to access this endpoint"}', headers: {})
23
+
24
+ assert_raises(Fastly::Error,'{"msg":"You must POST /sudo to access this endpoint"}') do
25
+ fastly.create_token(
26
+ name: 'name_of_token',
27
+ services: 'service_id_that_token_can_access',
28
+ scope: 'token_scope such_as purge_all purge_select'
29
+ )
30
+ end
31
+ end
32
+
33
+ it 'can create a new token only if there are no auth headers' do
34
+ response_body = %q(
35
+ {
36
+ "id": "5Yo3XXnrQpjc20u0ybrf2g",
37
+ "access_token": "YOUR_FASTLY_TOKEN",
38
+ "user_id": "4y5K5trZocEAQYkesWlk7M",
39
+ "services": ["service_id_that_token_can_access"],
40
+ "name": "name_of_token",
41
+ "scope": "optional token_scope such_as purge_all purge_select",
42
+ "created_at": "2016-06-22T03:19:48+00:00",
43
+ "last_used_at": "2016-06-22T03:19:48+00:00",
44
+ "expires_at": "2016-07-28T19:24:50+00:00",
45
+ "ip": "127.17.202.173",
46
+ "user_agent": "fastly-ruby-v2.4.0"
47
+ }
48
+ )
49
+
50
+ stub_request(:post, "https://api.fastly.com/tokens").
51
+ with(
52
+ body: {"name"=>"name_of_token", "password"=>"password", "scope"=>"optional token_scope such_as purge_all purge_select", "services"=>"service_id_that_token_can_access", "username"=>"test@example.com"},
53
+ headers: {
54
+ 'Accept'=>'*/*',
55
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
56
+ 'Content-Accept'=>'application/json',
57
+ 'Content-Type'=>'application/x-www-form-urlencoded',
58
+ 'User-Agent'=> /fastly-ruby/
59
+ }).
60
+ to_return(status: 200, body: response_body, headers: {})
61
+
62
+ token = fastly.new_token(
63
+ name: 'name_of_token',
64
+ services: 'service_id_that_token_can_access',
65
+ scope: 'optional token_scope such_as purge_all purge_select'
66
+ )
67
+ assert_equal token.id, '5Yo3XXnrQpjc20u0ybrf2g'
68
+ assert_equal token.user_id, '4y5K5trZocEAQYkesWlk7M'
69
+ assert_equal token.services[0], 'service_id_that_token_can_access'
70
+ assert_equal token.name, 'name_of_token'
71
+ assert_equal token.scope, 'optional token_scope such_as purge_all purge_select'
72
+ assert_equal token.created_at, '2016-06-22T03:19:48+00:00'
73
+ assert_equal token.last_used_at, '2016-06-22T03:19:48+00:00'
74
+ assert_equal token.expires_at, '2016-07-28T19:24:50+00:00'
75
+ assert_equal token.ip, '127.17.202.173'
76
+ assert_equal token.user_agent, 'fastly-ruby-v2.4.0'
77
+ assert_equal token.access_token, 'YOUR_FASTLY_TOKEN'
78
+ end
79
+
80
+ it 'would delete a token' do
81
+ stub_request(:delete, "https://api.fastly.com/tokens/").
82
+ with(
83
+ headers: {
84
+ 'Accept'=>'*/*',
85
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
86
+ 'Content-Accept'=>'application/json',
87
+ 'Fastly-Key'=>'my_api_key',
88
+ 'User-Agent'=> /fastly-ruby/
89
+ }).
90
+ to_return(status: 204, body: "", headers: {})
91
+
92
+ token = Fastly::Token.new({acess_token: 'my_api_key'}, Fastly::Fetcher)
93
+ fastly.delete_token(token)
94
+ end
95
+
96
+ it 'would list all the tokens belonging to a token' do
97
+ stub_request(:get, "https://api.fastly.com/tokens").
98
+ with(
99
+ headers: {
100
+ 'Accept'=>'*/*',
101
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
102
+ 'Content-Accept'=>'application/json',
103
+ 'Fastly-Key'=>'my_api_key',
104
+ 'User-Agent'=> /fastly-ruby/
105
+ }).
106
+ to_return(status: 200, body: "[]", headers: {})
107
+
108
+ fastly.list_tokens()
109
+ end
110
+
111
+ it 'would list all the tokens belonging to a customer' do
112
+ stub_request(:get, "https://api.fastly.com/customer/customer_account_number/tokens").
113
+ with(
114
+ headers: {
115
+ 'Accept'=>'*/*',
116
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
117
+ 'Content-Accept'=>'application/json',
118
+ 'Fastly-Key'=>'my_api_key',
119
+ 'User-Agent'=> /fastly-ruby/
120
+ }).
121
+ to_return(status: 200, body: "[]", headers: {})
122
+
123
+ fastly.customer_tokens({customer_id: 'customer_account_number'})
124
+ end
125
+
126
+ end
127
+ end
@@ -3,7 +3,7 @@ require 'helper'
3
3
  # Test username/password login access to user and customer objects
4
4
  class Fastly
5
5
  describe 'FullLoginTest' do
6
- let(:opts) { login_opts(:full) }
6
+ let(:opts) { login_opts(:both) }
7
7
  let(:client) { Client.new(opts) }
8
8
  let(:fastly) { Fastly.new(opts) }
9
9
  let(:current_user) { fastly.current_user }
data/test/helper.rb CHANGED
@@ -2,6 +2,7 @@ require 'common'
2
2
  require 'fastly'
3
3
  require 'minitest/autorun'
4
4
  require 'pry'
5
+ require 'dotenv/load'
5
6
 
6
7
  class Fastly
7
8
  class TestCase < Minitest::Test; end
@@ -0,0 +1,64 @@
1
+ require 'helper'
2
+
3
+ # Testing client components related to tokens
4
+ class Fastly
5
+ describe 'TokenTest' do
6
+ let(:opts) { login_opts(:both) }
7
+ let(:fastly) { Fastly.new(opts) }
8
+
9
+ let(:service_name) { "fastly-test-service-#{random_string}" }
10
+ let(:service) { fastly.create_service(:name => service_name) }
11
+ let(:current_customer) { fastly.current_customer }
12
+
13
+ def create_a_test_token(name, service, scope)
14
+ fastly.new_token(
15
+ name: name,
16
+ services: service,
17
+ scope: scope
18
+ )
19
+ end
20
+
21
+ it 'creates a new token' do
22
+ token = create_a_test_token('name_vi',service.id,'purge_all purge_select')
23
+ assert_equal token.name, 'name_vi'
24
+ assert_equal token.services[0], service.id
25
+ assert_equal token.scope, 'purge_all purge_select'
26
+ fastly.delete_token(token)
27
+ end
28
+
29
+ it 'deletes a token by passed-in token' do
30
+ token = create_a_test_token('name_vi',service.id,'purge_all purge_select')
31
+
32
+ tokens = fastly.customer_tokens({customer_id: current_customer.id})
33
+ before_count = tokens.count
34
+
35
+ fastly.delete_token(token)
36
+
37
+ tokens = fastly.customer_tokens({customer_id: current_customer.id})
38
+ after_count = tokens.count
39
+
40
+ assert_equal 1, before_count - after_count
41
+ end
42
+
43
+ it 'returns a list of tokens belonging to the customer or to the auth token' do
44
+ @token_i = create_a_test_token('name_i',service.id,'purge_all purge_select')
45
+ @token_ii = create_a_test_token('name_ii',service.id,'purge_all purge_select')
46
+ @token_iii = create_a_test_token('name_iii',service.id,'purge_all purge_select')
47
+ @token_iv = create_a_test_token('name_iv',service.id,'purge_all purge_select')
48
+ @token_v = create_a_test_token('name_v',service.id,'purge_all purge_select')
49
+
50
+ tokens = fastly.customer_tokens({customer_id: current_customer.id})
51
+ assert tokens.count > 4
52
+
53
+ tokens = fastly.list_tokens
54
+ assert tokens.count > 4
55
+
56
+ fastly.delete_token(@token_i)
57
+ fastly.delete_token(@token_ii)
58
+ fastly.delete_token(@token_iii)
59
+ fastly.delete_token(@token_iv)
60
+ fastly.delete_token(@token_v)
61
+ fastly.delete_service(service)
62
+ end
63
+ end
64
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fastly
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fastly
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-29 00:00:00.000000000 Z
11
+ date: 2021-02-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Client library for the Fastly acceleration system
14
14
  email:
@@ -21,6 +21,7 @@ executables:
21
21
  extensions: []
22
22
  extra_rdoc_files: []
23
23
  files:
24
+ - ".env.example"
24
25
  - ".gitignore"
25
26
  - ".rubocop.yml"
26
27
  - ".travis.yml"
@@ -68,6 +69,7 @@ files:
68
69
  - lib/fastly/snippet.rb
69
70
  - lib/fastly/sumologic_logging.rb
70
71
  - lib/fastly/syslog.rb
72
+ - lib/fastly/token.rb
71
73
  - lib/fastly/user.rb
72
74
  - lib/fastly/util.rb
73
75
  - lib/fastly/vcl.rb
@@ -81,12 +83,14 @@ files:
81
83
  - test/fastly/customer_test.rb
82
84
  - test/fastly/dictionary_test.rb
83
85
  - test/fastly/syslog_test.rb
86
+ - test/fastly/token_test.rb
84
87
  - test/fastly/util_test.rb
85
88
  - test/full_login_test.rb
86
89
  - test/helper.rb
87
90
  - test/missing_api_key_test.rb
88
91
  - test/stats_test.rb
89
92
  - test/test_helper.rb
93
+ - test/token_test.rb
90
94
  homepage: http://github.com/fastly/fastly-ruby
91
95
  licenses:
92
96
  - MIT
@@ -106,8 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
110
  - !ruby/object:Gem::Version
107
111
  version: '0'
108
112
  requirements: []
109
- rubyforge_project:
110
- rubygems_version: 2.7.6.2
113
+ rubygems_version: 3.0.1
111
114
  signing_key:
112
115
  specification_version: 4
113
116
  summary: Client library for the Fastly acceleration system
@@ -121,9 +124,11 @@ test_files:
121
124
  - test/fastly/customer_test.rb
122
125
  - test/fastly/dictionary_test.rb
123
126
  - test/fastly/syslog_test.rb
127
+ - test/fastly/token_test.rb
124
128
  - test/fastly/util_test.rb
125
129
  - test/full_login_test.rb
126
130
  - test/helper.rb
127
131
  - test/missing_api_key_test.rb
128
132
  - test/stats_test.rb
129
133
  - test/test_helper.rb
134
+ - test/token_test.rb