fortnox-api 0.5.2 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/.rubocop.yml +9 -1104
- data/.travis.yml +22 -2
- data/Gemfile +3 -1
- data/Guardfile +4 -2
- data/README.md +2 -2
- data/Rakefile +3 -1
- data/bin/console +17 -3
- data/fortnox-api.gemspec +32 -27
- data/lib/fortnox/api.rb +17 -16
- data/lib/fortnox/api/circular_queue.rb +13 -7
- data/lib/fortnox/api/mappers.rb +29 -25
- data/lib/fortnox/api/mappers/article.rb +7 -6
- data/lib/fortnox/api/mappers/base.rb +18 -16
- data/lib/fortnox/api/mappers/base/from_json.rb +37 -30
- data/lib/fortnox/api/mappers/base/to_json.rb +39 -37
- data/lib/fortnox/api/mappers/customer.rb +7 -7
- data/lib/fortnox/api/mappers/default_delivery_types.rb +4 -2
- data/lib/fortnox/api/mappers/default_templates.rb +5 -4
- data/lib/fortnox/api/mappers/edi_information.rb +5 -4
- data/lib/fortnox/api/mappers/email_information.rb +5 -4
- data/lib/fortnox/api/mappers/invoice.rb +10 -8
- data/lib/fortnox/api/mappers/invoice_row.rb +12 -7
- data/lib/fortnox/api/mappers/order.rb +10 -7
- data/lib/fortnox/api/mappers/order_row.rb +13 -6
- data/lib/fortnox/api/mappers/project.rb +6 -4
- data/lib/fortnox/api/mappers/terms_of_payments.rb +17 -0
- data/lib/fortnox/api/mappers/unit.rb +17 -0
- data/lib/fortnox/api/models.rb +9 -5
- data/lib/fortnox/api/models/article.rb +68 -67
- data/lib/fortnox/api/models/base.rb +52 -36
- data/lib/fortnox/api/models/customer.rb +95 -94
- data/lib/fortnox/api/models/document.rb +186 -0
- data/lib/fortnox/api/models/invoice.rb +44 -44
- data/lib/fortnox/api/models/label.rb +5 -3
- data/lib/fortnox/api/models/order.rb +8 -8
- data/lib/fortnox/api/models/project.rb +10 -8
- data/lib/fortnox/api/models/terms_of_payments.rb +28 -0
- data/lib/fortnox/api/models/unit.rb +24 -0
- data/lib/fortnox/api/repositories.rb +9 -5
- data/lib/fortnox/api/repositories/article.rb +6 -4
- data/lib/fortnox/api/repositories/base.rb +49 -46
- data/lib/fortnox/api/repositories/base/loaders.rb +32 -36
- data/lib/fortnox/api/repositories/base/savers.rb +23 -21
- data/lib/fortnox/api/repositories/customer.rb +6 -4
- data/lib/fortnox/api/repositories/invoice.rb +6 -4
- data/lib/fortnox/api/repositories/order.rb +6 -4
- data/lib/fortnox/api/repositories/project.rb +6 -4
- data/lib/fortnox/api/repositories/terms_of_payments.rb +17 -0
- data/lib/fortnox/api/repositories/unit.rb +17 -0
- data/lib/fortnox/api/request_handling.rb +12 -12
- data/lib/fortnox/api/types.rb +62 -11
- data/lib/fortnox/api/types/default_delivery_types.rb +5 -3
- data/lib/fortnox/api/types/default_templates.rb +3 -1
- data/lib/fortnox/api/types/defaulted.rb +2 -2
- data/lib/fortnox/api/types/document_row.rb +41 -42
- data/lib/fortnox/api/types/edi_information.rb +10 -8
- data/lib/fortnox/api/types/email_information.rb +10 -8
- data/lib/fortnox/api/types/enums.rb +60 -47
- data/lib/fortnox/api/types/invoice_row.rb +8 -8
- data/lib/fortnox/api/types/model.rb +14 -10
- data/lib/fortnox/api/types/nullable.rb +7 -7
- data/lib/fortnox/api/types/order_row.rb +5 -5
- data/lib/fortnox/api/types/required.rb +5 -5
- data/lib/fortnox/api/types/sized.rb +14 -6
- data/lib/fortnox/api/version.rb +3 -1
- data/spec/fortnox/api/mappers/article_spec.rb +3 -1
- data/spec/fortnox/api/mappers/base/from_json_spec.rb +15 -13
- data/spec/fortnox/api/mappers/base/to_json_spec.rb +4 -3
- data/spec/fortnox/api/mappers/base_spec.rb +40 -35
- data/spec/fortnox/api/mappers/contexts/json_conversion.rb +12 -11
- data/spec/fortnox/api/mappers/customer_spec.rb +3 -1
- data/spec/fortnox/api/mappers/default_delivery_types_spec.rb +3 -1
- data/spec/fortnox/api/mappers/edi_information_spec.rb +4 -2
- data/spec/fortnox/api/mappers/email_information_spec.rb +4 -2
- data/spec/fortnox/api/mappers/examples/mapper.rb +17 -9
- data/spec/fortnox/api/mappers/invoice_row_spec.rb +8 -3
- data/spec/fortnox/api/mappers/invoice_spec.rb +4 -1
- data/spec/fortnox/api/mappers/order_row_spec.rb +9 -2
- data/spec/fortnox/api/mappers/order_spec.rb +4 -1
- data/spec/fortnox/api/mappers/project_spec.rb +3 -1
- data/spec/fortnox/api/mappers/terms_of_payments_spec.rb +16 -0
- data/spec/fortnox/api/mappers/unit_spec.rb +57 -0
- data/spec/fortnox/api/models/article_spec.rb +2 -0
- data/spec/fortnox/api/models/base_spec.rb +46 -38
- data/spec/fortnox/api/models/customer_spec.rb +2 -0
- data/spec/fortnox/api/models/examples/document_base.rb +5 -3
- data/spec/fortnox/api/models/examples/model.rb +12 -9
- data/spec/fortnox/api/models/invoice_spec.rb +2 -0
- data/spec/fortnox/api/models/order_spec.rb +2 -0
- data/spec/fortnox/api/models/project_spec.rb +2 -0
- data/spec/fortnox/api/models/terms_of_payments_spec.rb +9 -0
- data/spec/fortnox/api/models/unit_spec.rb +31 -0
- data/spec/fortnox/api/repositories/article_spec.rb +7 -5
- data/spec/fortnox/api/repositories/base_spec.rb +121 -100
- data/spec/fortnox/api/repositories/customer_spec.rb +7 -5
- data/spec/fortnox/api/repositories/examples/all.rb +5 -4
- data/spec/fortnox/api/repositories/examples/find.rb +50 -41
- data/spec/fortnox/api/repositories/examples/only.rb +14 -13
- data/spec/fortnox/api/repositories/examples/save.rb +23 -29
- data/spec/fortnox/api/repositories/examples/save_with_nested_model.rb +11 -11
- data/spec/fortnox/api/repositories/examples/save_with_specially_named_attribute.rb +11 -10
- data/spec/fortnox/api/repositories/examples/search.rb +15 -15
- data/spec/fortnox/api/repositories/invoice_spec.rb +8 -6
- data/spec/fortnox/api/repositories/order_spec.rb +8 -6
- data/spec/fortnox/api/repositories/project_spec.rb +7 -5
- data/spec/fortnox/api/repositories/terms_of_payments_spec.rb +30 -0
- data/spec/fortnox/api/repositories/unit_spec.rb +34 -0
- data/spec/fortnox/api/types/account_number_spec.rb +12 -9
- data/spec/fortnox/api/types/default_delivery_types_spec.rb +6 -4
- data/spec/fortnox/api/types/edi_information_spec.rb +9 -7
- data/spec/fortnox/api/types/email_information_spec.rb +8 -6
- data/spec/fortnox/api/types/email_spec.rb +27 -14
- data/spec/fortnox/api/types/enums_spec.rb +3 -1
- data/spec/fortnox/api/types/examples/document_row.rb +10 -8
- data/spec/fortnox/api/types/examples/enum.rb +29 -22
- data/spec/fortnox/api/types/examples/types.rb +8 -4
- data/spec/fortnox/api/types/housework_types_spec.rb +68 -0
- data/spec/fortnox/api/types/invoice_row_spec.rb +3 -1
- data/spec/fortnox/api/types/model_spec.rb +12 -9
- data/spec/fortnox/api/types/nullable_spec.rb +8 -6
- data/spec/fortnox/api/types/order_row_spec.rb +4 -2
- data/spec/fortnox/api/types/required_spec.rb +4 -2
- data/spec/fortnox/api/types/sized_spec.rb +18 -16
- data/spec/fortnox/api_spec.rb +45 -43
- data/spec/spec_helper.rb +9 -6
- data/spec/support/helpers.rb +3 -1
- data/spec/support/helpers/configuration_helper.rb +2 -0
- data/spec/support/helpers/dummy_class_helper.rb +7 -8
- data/spec/support/helpers/repository_helper.rb +2 -0
- data/spec/support/helpers/when_performing_helper.rb +3 -1
- data/spec/support/matchers.rb +2 -0
- data/spec/support/matchers/type.rb +3 -1
- data/spec/support/matchers/type/attribute_matcher.rb +15 -14
- data/spec/support/matchers/type/enum_matcher.rb +9 -7
- data/spec/support/matchers/type/have_account_number_matcher.rb +11 -9
- data/spec/support/matchers/type/have_country_code_matcher.rb +6 -4
- data/spec/support/matchers/type/have_currency_matcher.rb +4 -2
- data/spec/support/matchers/type/have_customer_type_matcher.rb +6 -4
- data/spec/support/matchers/type/have_default_delivery_type_matcher.rb +4 -2
- data/spec/support/matchers/type/have_discount_type_matcher.rb +4 -2
- data/spec/support/matchers/type/have_email_matcher.rb +11 -9
- data/spec/support/matchers/type/have_housework_type_matcher.rb +9 -0
- data/spec/support/matchers/type/have_nullable_date_matcher.rb +29 -29
- data/spec/support/matchers/type/have_nullable_matcher.rb +23 -21
- data/spec/support/matchers/type/have_nullable_string_matcher.rb +30 -32
- data/spec/support/matchers/type/have_sized_float_matcher.rb +4 -2
- data/spec/support/matchers/type/have_sized_integer_matcher.rb +4 -2
- data/spec/support/matchers/type/have_sized_string_matcher.rb +16 -15
- data/spec/support/matchers/type/have_vat_type_matcher.rb +4 -2
- data/spec/support/matchers/type/numeric_matcher.rb +25 -23
- data/spec/support/matchers/type/require_attribute_matcher.rb +36 -37
- data/spec/support/matchers/type/type_matcher.rb +18 -16
- data/spec/support/vcr_setup.rb +2 -0
- data/spec/vcr_cassettes/orders/{house_work_type_babysitting.yml → housework_type_babysitting.yml} +9 -10
- data/spec/vcr_cassettes/orders/{house_work_type_cleaning.yml → housework_type_cleaning.yml} +9 -10
- data/spec/vcr_cassettes/orders/{house_work_type_construction.yml → housework_type_construction.yml} +9 -10
- data/spec/vcr_cassettes/orders/{house_work_type_cooking.yml → housework_type_cooking.yml} +7 -7
- data/spec/vcr_cassettes/orders/{house_work_type_electricity.yml → housework_type_electricity.yml} +9 -10
- data/spec/vcr_cassettes/orders/{house_work_type_gardening.yml → housework_type_gardening.yml} +9 -10
- data/spec/vcr_cassettes/orders/{house_work_type_glassmetalwork.yml → housework_type_glassmetalwork.yml} +9 -10
- data/spec/vcr_cassettes/orders/{house_work_type_grounddrainagework.yml → housework_type_grounddrainagework.yml} +9 -10
- data/spec/vcr_cassettes/orders/{house_work_type_hvac.yml → housework_type_hvac.yml} +9 -10
- data/spec/vcr_cassettes/orders/{house_work_type_masonry.yml → housework_type_masonry.yml} +9 -10
- data/spec/vcr_cassettes/orders/{house_work_type_othercare.yml → housework_type_othercare.yml} +9 -10
- data/spec/vcr_cassettes/orders/{house_work_type_othercosts.yml → housework_type_othercosts.yml} +9 -10
- data/spec/vcr_cassettes/orders/{house_work_type_paintingwallpapering.yml → housework_type_paintingwallpapering.yml} +9 -10
- data/spec/vcr_cassettes/orders/{house_work_type_snowplowing.yml → housework_type_snowplowing.yml} +9 -10
- data/spec/vcr_cassettes/orders/{house_work_type_textileclothing.yml → housework_type_textileclothing.yml} +9 -10
- data/spec/vcr_cassettes/orders/{house_work_type_tutoring.yml → housework_type_tutoring.yml} +7 -7
- data/spec/vcr_cassettes/termsofpayments/all.yml +51 -0
- data/spec/vcr_cassettes/termsofpayments/find_failure.yml +45 -0
- data/spec/vcr_cassettes/termsofpayments/find_id_1.yml +46 -0
- data/spec/vcr_cassettes/termsofpayments/find_new.yml +46 -0
- data/spec/vcr_cassettes/termsofpayments/save_new.yml +45 -0
- data/spec/vcr_cassettes/termsofpayments/save_old.yml +46 -0
- data/spec/vcr_cassettes/units/all.yml +48 -0
- data/spec/vcr_cassettes/units/find_failure.yml +45 -0
- data/spec/vcr_cassettes/units/find_id_1.yml +46 -0
- data/spec/vcr_cassettes/units/find_new.yml +48 -0
- data/spec/vcr_cassettes/units/save_new.yml +47 -0
- data/spec/vcr_cassettes/units/save_old.yml +48 -0
- data/spec/vcr_cassettes/units/save_with_specially_named_attribute.yml +47 -0
- metadata +127 -88
- data/docs/account.rb +0 -59
- data/docs/extraction_script.js +0 -23
- data/docs/generation_script.rb +0 -43
- data/docs/json/account.json +0 -77
- data/lib/fortnox/api/models/document_base.rb +0 -189
- data/spec/fortnox/api/types/house_work_types_spec.rb +0 -64
- data/spec/support/matchers/type/have_house_work_type_matcher.rb +0 -7
@@ -1,19 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
shared_examples_for 'a model' do |unique_id|
|
2
|
-
let(
|
3
|
-
let(
|
4
|
+
let(:required_attributes) { described_class::STUB.dup }
|
5
|
+
let(:unique_id_attribute) { described_class::UNIQUE_ID }
|
4
6
|
|
5
7
|
it 'can be initialized' do
|
6
|
-
expect{ described_class.new(
|
8
|
+
expect { described_class.new(required_attributes) }.not_to raise_error
|
7
9
|
end
|
8
10
|
|
9
11
|
describe '.unique_id' do
|
10
|
-
subject{ model.unique_id }
|
11
|
-
|
12
|
-
before{ expect(model.send(unique_id_attribute)).to eq unique_id }
|
12
|
+
subject { model.unique_id }
|
13
13
|
|
14
|
-
let(
|
15
|
-
|
14
|
+
let(:model) do
|
15
|
+
described_class.new(
|
16
|
+
required_attributes.merge(unique_id_attribute => unique_id)
|
17
|
+
)
|
18
|
+
end
|
16
19
|
|
17
|
-
it{ is_expected.to eq unique_id }
|
20
|
+
it { is_expected.to eq unique_id }
|
18
21
|
end
|
19
22
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'fortnox/api/models/unit'
|
5
|
+
|
6
|
+
module Fortnox
|
7
|
+
module API
|
8
|
+
# Shhh Rubocop, we don't need a comment here ... Really
|
9
|
+
module Model
|
10
|
+
describe Unit, type: :model do
|
11
|
+
context 'when created from empty hash' do
|
12
|
+
subject { -> { described_class.new } }
|
13
|
+
|
14
|
+
it { is_expected.to raise_error(Fortnox::API::MissingAttributeError, /Missing attribute.*:code/) }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'when created from stub' do
|
18
|
+
subject { described_class.stub }
|
19
|
+
|
20
|
+
it { is_expected.to have_attributes(code: '', description: nil) }
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'when created with all attributes' do
|
24
|
+
subject { described_class.new(code: 'lbs', description: 'Pounds') }
|
25
|
+
|
26
|
+
it { is_expected.to have_attributes(code: 'lbs', description: 'Pounds') }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
require 'fortnox/api'
|
3
5
|
require 'fortnox/api/mappers'
|
@@ -11,9 +13,9 @@ require 'fortnox/api/repositories/examples/search'
|
|
11
13
|
describe Fortnox::API::Repository::Article, order: :defined, integration: true do
|
12
14
|
include Helpers::Configuration
|
13
15
|
|
14
|
-
before{ set_api_test_configuration }
|
16
|
+
before { set_api_test_configuration }
|
15
17
|
|
16
|
-
subject(:repository){ described_class.new }
|
18
|
+
subject(:repository) { described_class.new }
|
17
19
|
|
18
20
|
include_examples '.save',
|
19
21
|
:description,
|
@@ -27,10 +29,10 @@ describe Fortnox::API::Repository::Article, order: :defined, integration: true d
|
|
27
29
|
include_examples '.all', 12
|
28
30
|
|
29
31
|
include_examples '.find', '1' do
|
30
|
-
let(
|
31
|
-
let(
|
32
|
+
let(:find_by_hash_failure) { { description: 'Not Found' } }
|
33
|
+
let(:single_param_find_by_hash) { { find_hash: { articlenumber: 1 }, matches: 3 } }
|
32
34
|
|
33
|
-
let(
|
35
|
+
let(:multi_param_find_by_hash) do
|
34
36
|
{ find_hash: { articlenumber: 1, description: 'Cykelpump' }, matches: 1 }
|
35
37
|
end
|
36
38
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
require 'fortnox/api'
|
3
5
|
|
@@ -15,79 +17,85 @@ describe Fortnox::API::Repository::Base do
|
|
15
17
|
|
16
18
|
require 'dry/container/stub'
|
17
19
|
Fortnox::API::Registry.enable_stubs!
|
18
|
-
Fortnox::API::Registry.stub(
|
20
|
+
Fortnox::API::Registry.stub(:test, Model::Test)
|
19
21
|
end
|
20
22
|
|
21
|
-
let(:access_token){ '3f08d038-f380-4893-94a0-a08f6e60e67a' }
|
22
|
-
let(:access_token2){ '89feajou-sif8-8f8u-29ja-xdfniokeniod' }
|
23
|
-
let(:client_secret){ 'P5K5vE3Kun' }
|
24
|
-
let(:repository){ Repository::Test.new }
|
25
|
-
let(:application_json){}
|
23
|
+
let(:access_token) { '3f08d038-f380-4893-94a0-a08f6e60e67a' }
|
24
|
+
let(:access_token2) { '89feajou-sif8-8f8u-29ja-xdfniokeniod' }
|
25
|
+
let(:client_secret) { 'P5K5vE3Kun' }
|
26
|
+
let(:repository) { Repository::Test.new }
|
27
|
+
let(:application_json) {}
|
26
28
|
let(:headers) do
|
27
29
|
{
|
28
30
|
'Access-Token' => access_token,
|
29
31
|
'Client-Secret' => client_secret,
|
30
32
|
'Content-Type' => 'application/json',
|
31
|
-
'Accept' => 'application/json'
|
33
|
+
'Accept' => 'application/json'
|
32
34
|
}
|
33
35
|
end
|
34
36
|
|
35
37
|
describe 'creation' do
|
36
38
|
shared_examples_for 'missing configuration' do
|
37
|
-
subject{ ->{ repository } }
|
39
|
+
subject { -> { repository } }
|
38
40
|
|
39
|
-
let(:error){ Fortnox::API::MissingConfiguration }
|
41
|
+
let(:error) { Fortnox::API::MissingConfiguration }
|
40
42
|
|
41
|
-
it{ is_expected.to raise_error(
|
43
|
+
it { is_expected.to raise_error(error, /#{message}/) }
|
42
44
|
end
|
43
45
|
|
44
46
|
context 'without base url' do
|
45
47
|
include_examples 'missing configuration' do
|
46
|
-
before{ Fortnox::API.configure{ |conf| conf.base_url = nil } }
|
47
|
-
let(:message){ 'have to provide a base url' }
|
48
|
+
before { Fortnox::API.configure { |conf| conf.base_url = nil } }
|
49
|
+
let(:message) { 'have to provide a base url' }
|
48
50
|
end
|
49
51
|
end
|
50
52
|
|
51
53
|
context 'without client secret' do
|
52
54
|
include_examples 'missing configuration' do
|
53
|
-
before{ Fortnox::API.configure{ |conf| conf.client_secret = nil } }
|
54
|
-
let(:message){ 'have to provide your client secret' }
|
55
|
+
before { Fortnox::API.configure { |conf| conf.client_secret = nil } }
|
56
|
+
let(:message) { 'have to provide your client secret' }
|
55
57
|
end
|
56
58
|
end
|
57
59
|
end
|
58
60
|
|
59
61
|
describe '#next_access_token' do
|
60
|
-
before{ Fortnox::API.configure{ |conf| conf.client_secret = client_secret } }
|
62
|
+
before { Fortnox::API.configure { |conf| conf.client_secret = client_secret } }
|
61
63
|
|
62
64
|
context 'with default token store' do
|
63
65
|
context 'with one access token' do
|
64
|
-
subject{ repository.next_access_token }
|
66
|
+
subject { repository.next_access_token }
|
67
|
+
|
68
|
+
before { Fortnox::API.configure { |conf| conf.access_token = access_token } }
|
65
69
|
|
66
|
-
|
67
|
-
|
70
|
+
it { is_expected.to eql(access_token) }
|
71
|
+
end
|
68
72
|
|
69
|
-
|
70
|
-
|
73
|
+
context 'with one access token and two subsequent requests' do
|
74
|
+
subject { repository.next_access_token }
|
75
|
+
|
76
|
+
before do
|
77
|
+
Fortnox::API.configure { |conf| conf.access_token = access_token }
|
78
|
+
repository.next_access_token
|
79
|
+
end
|
71
80
|
|
72
|
-
|
73
|
-
|
74
|
-
end
|
81
|
+
it 'still uses the same token' do
|
82
|
+
is_expected.to eql(access_token)
|
75
83
|
end
|
76
84
|
end
|
77
85
|
|
78
86
|
context 'with multiple access tokens' do
|
79
|
-
subject{ access_tokens }
|
87
|
+
subject { access_tokens }
|
80
88
|
|
81
|
-
before{ Fortnox::API.configure{ |conf| conf.access_tokens = access_tokens } }
|
82
|
-
let(
|
89
|
+
before { Fortnox::API.configure { |conf| conf.access_tokens = access_tokens } }
|
90
|
+
let(:access_tokens) { [access_token, access_token2] }
|
83
91
|
|
84
|
-
it{ is_expected.to include(
|
92
|
+
it { is_expected.to include(repository.next_access_token) }
|
85
93
|
|
86
94
|
it 'changes token on next request' do
|
87
95
|
token1 = repository.next_access_token
|
88
96
|
token2 = repository.next_access_token
|
89
97
|
|
90
|
-
expect(
|
98
|
+
expect(token1).not_to eql(token2)
|
91
99
|
end
|
92
100
|
|
93
101
|
it 'circulates tokens' do
|
@@ -95,78 +103,83 @@ describe Fortnox::API::Repository::Base do
|
|
95
103
|
repository.next_access_token
|
96
104
|
token3 = repository.next_access_token
|
97
105
|
|
98
|
-
expect(
|
106
|
+
expect(token1).to eql(token3)
|
99
107
|
end
|
100
108
|
end
|
101
109
|
end
|
102
110
|
|
103
111
|
context 'with multiple stores' do
|
104
|
-
subject{ repository.next_access_token }
|
112
|
+
subject { repository.next_access_token }
|
105
113
|
|
106
|
-
before do
|
114
|
+
before do
|
107
115
|
Fortnox::API.configure do |config|
|
108
116
|
config.access_tokens = { store1: access_token, store2: access_token2 }
|
109
117
|
end
|
110
118
|
end
|
111
119
|
|
112
120
|
describe 'first token store' do
|
113
|
-
let(
|
121
|
+
let(:repository) { Repository::Test.new(token_store: :store1) }
|
114
122
|
|
115
|
-
it{ is_expected.to eql access_token }
|
123
|
+
it { is_expected.to eql access_token }
|
116
124
|
end
|
117
125
|
|
118
126
|
describe 'second token store' do
|
119
|
-
let(
|
127
|
+
let(:repository) { Repository::Test.new(token_store: :store2) }
|
120
128
|
|
121
|
-
it{ is_expected.to eql access_token2 }
|
129
|
+
it { is_expected.to eql access_token2 }
|
122
130
|
end
|
123
131
|
end
|
124
132
|
end
|
125
133
|
|
126
|
-
describe '#
|
127
|
-
subject(
|
134
|
+
describe '#access_tokens' do
|
135
|
+
subject(:access_tokens) { repository.access_tokens }
|
128
136
|
|
129
|
-
before{ Fortnox::API.configure{ |conf| conf.client_secret = client_secret } }
|
137
|
+
before { Fortnox::API.configure { |conf| conf.client_secret = client_secret } }
|
130
138
|
|
131
|
-
let(
|
132
|
-
|
139
|
+
let(:token_store_not_present) do
|
140
|
+
"There is no token store named #{token_store.inspect}. Available stores are #{available_stores}."
|
141
|
+
end
|
142
|
+
|
143
|
+
let(:error) { Fortnox::API::MissingConfiguration }
|
133
144
|
|
134
145
|
context 'with non existing token store' do
|
135
|
-
subject{ ->{
|
146
|
+
subject { -> { access_tokens } }
|
136
147
|
|
137
148
|
before do
|
138
|
-
Fortnox::API.configure{ |conf| conf.access_tokens = { some_store: [access_token] } }
|
149
|
+
Fortnox::API.configure { |conf| conf.access_tokens = { some_store: [access_token] } }
|
139
150
|
end
|
140
151
|
|
141
|
-
let(
|
142
|
-
let(
|
152
|
+
let(:repository) { Repository::Test.new(token_store: token_store) }
|
153
|
+
let(:token_store) { :non_existing_store }
|
154
|
+
let(:available_stores) { [:some_store] }
|
143
155
|
|
144
|
-
it{ is_expected.to raise_error(
|
156
|
+
it { is_expected.to raise_error(error, token_store_not_present) }
|
145
157
|
end
|
146
158
|
|
147
159
|
context 'with no tokens set' do
|
148
|
-
subject{ ->{
|
160
|
+
subject { -> { access_tokens } }
|
149
161
|
|
150
|
-
before{ Fortnox::API.configure{ |conf| conf.access_tokens = {} } }
|
151
|
-
let(
|
162
|
+
before { Fortnox::API.configure { |conf| conf.access_tokens = {} } }
|
163
|
+
let(:token_store) { :default }
|
164
|
+
let(:available_stores) { [] }
|
152
165
|
|
153
|
-
it{ is_expected.to raise_error(
|
166
|
+
it { is_expected.to raise_error(error, token_store_not_present) }
|
154
167
|
end
|
155
168
|
|
156
169
|
context 'with one access token in token store' do
|
157
|
-
before{ Fortnox::API.configure{ |conf| conf.access_token = access_token } }
|
158
|
-
let(
|
170
|
+
before { Fortnox::API.configure { |conf| conf.access_token = access_token } }
|
171
|
+
let(:token_store) { :default }
|
159
172
|
|
160
|
-
it{ is_expected.to eql(
|
173
|
+
it { is_expected.to eql(access_token) }
|
161
174
|
end
|
162
175
|
|
163
176
|
context 'with multiple access tokens' do
|
164
177
|
before do
|
165
|
-
Fortnox::API.configure{ |conf| conf.access_tokens = [access_token, access_token2] }
|
178
|
+
Fortnox::API.configure { |conf| conf.access_tokens = [access_token, access_token2] }
|
166
179
|
end
|
167
|
-
let(
|
180
|
+
let(:token_store) { :default }
|
168
181
|
|
169
|
-
it{ is_expected.to eql(
|
182
|
+
it { is_expected.to eql([access_token, access_token2]) }
|
170
183
|
end
|
171
184
|
|
172
185
|
context 'with multiple token stores' do
|
@@ -175,64 +188,64 @@ describe Fortnox::API::Repository::Base do
|
|
175
188
|
conf.access_tokens = { store_a: store_a_tokens, store_b: store_b_token }
|
176
189
|
end
|
177
190
|
end
|
178
|
-
let(
|
179
|
-
let(
|
191
|
+
let(:store_a_tokens) { %w[token_a1 token_a2] }
|
192
|
+
let(:store_b_token) { 'token_b1' }
|
180
193
|
|
181
194
|
context 'with valid store name' do
|
182
|
-
let(
|
195
|
+
let(:repository) { Repository::Test.new(token_store: :store_a) }
|
183
196
|
|
184
|
-
it{ is_expected.to eql(
|
197
|
+
it { is_expected.to eql(store_a_tokens) }
|
185
198
|
end
|
186
199
|
|
187
200
|
context 'with non collection' do
|
188
|
-
let(
|
201
|
+
let(:repository) { Repository::Test.new(token_store: :store_b) }
|
189
202
|
|
190
|
-
it{ is_expected.to eql(
|
203
|
+
it { is_expected.to eql(store_b_token) }
|
191
204
|
end
|
192
205
|
|
193
206
|
context 'with invalid store name' do
|
194
|
-
subject{ ->{
|
207
|
+
subject { -> { access_tokens } }
|
195
208
|
|
196
|
-
let(
|
209
|
+
let(:repository) { Repository::Test.new(token_store: :nonsence_store) }
|
197
210
|
|
198
|
-
it{ is_expected.to raise_error(
|
211
|
+
it { is_expected.to raise_error(error) }
|
199
212
|
end
|
200
213
|
end
|
201
214
|
end
|
202
215
|
|
203
216
|
describe '#check_access_tokens!' do
|
204
|
-
subject{ ->{ repository.check_access_tokens!(tokens) } }
|
217
|
+
subject { -> { repository.check_access_tokens!(tokens) } }
|
205
218
|
|
206
|
-
before{ Fortnox::API.configure{ |conf| conf.client_secret = client_secret } }
|
207
|
-
let(
|
208
|
-
let(
|
209
|
-
let(
|
219
|
+
before { Fortnox::API.configure { |conf| conf.client_secret = client_secret } }
|
220
|
+
let(:error) { Fortnox::API::MissingConfiguration }
|
221
|
+
let(:message) { "not provided any access tokens in token store #{token_store.inspect}" }
|
222
|
+
let(:token_store) { :default }
|
210
223
|
|
211
224
|
context 'with nil' do
|
212
|
-
let(
|
225
|
+
let(:tokens) { nil }
|
213
226
|
|
214
|
-
it{ is_expected.to raise_error(
|
227
|
+
it { is_expected.to raise_error(error, /#{message}/) }
|
215
228
|
end
|
216
229
|
|
217
230
|
context 'with empty array' do
|
218
|
-
let(
|
231
|
+
let(:tokens) { [] }
|
219
232
|
|
220
|
-
it{ is_expected.to raise_error(
|
233
|
+
it { is_expected.to raise_error(error, /#{message}/) }
|
221
234
|
end
|
222
235
|
|
223
236
|
context 'with an empty, non default, token store' do
|
224
|
-
before{ Fortnox::API.configure{ |conf| conf.access_tokens = { token_store => tokens } } }
|
225
|
-
let(
|
226
|
-
let(
|
227
|
-
let(
|
237
|
+
before { Fortnox::API.configure { |conf| conf.access_tokens = { token_store => tokens } } }
|
238
|
+
let(:repository) { Repository::Test.new(token_store: token_store) }
|
239
|
+
let(:tokens) { [] }
|
240
|
+
let(:token_store) { :store1 }
|
228
241
|
|
229
|
-
it{ is_expected.to raise_error(
|
242
|
+
it { is_expected.to raise_error(error, /#{message}/) }
|
230
243
|
end
|
231
244
|
|
232
245
|
context 'with valid tokens' do
|
233
|
-
let(
|
246
|
+
let(:tokens) { %w[12345 abcde] }
|
234
247
|
|
235
|
-
it{ is_expected.not_to raise_error }
|
248
|
+
it { is_expected.not_to raise_error }
|
236
249
|
end
|
237
250
|
end
|
238
251
|
|
@@ -245,7 +258,7 @@ describe Fortnox::API::Repository::Base do
|
|
245
258
|
|
246
259
|
stub_request(
|
247
260
|
:get,
|
248
|
-
'https://api.fortnox.se/3/test'
|
261
|
+
'https://api.fortnox.se/3/test'
|
249
262
|
).with(
|
250
263
|
headers: headers
|
251
264
|
).to_return(
|
@@ -253,9 +266,9 @@ describe Fortnox::API::Repository::Base do
|
|
253
266
|
)
|
254
267
|
end
|
255
268
|
|
256
|
-
subject{ repository.get(
|
269
|
+
subject { repository.get('/test', body: '') }
|
257
270
|
|
258
|
-
it{ is_expected.to be_nil }
|
271
|
+
it { is_expected.to be_nil }
|
259
272
|
end
|
260
273
|
|
261
274
|
describe 'making requests with multiple access tokens' do
|
@@ -267,13 +280,13 @@ describe Fortnox::API::Repository::Base do
|
|
267
280
|
|
268
281
|
stub_request(
|
269
282
|
:get,
|
270
|
-
'https://api.fortnox.se/3/test'
|
283
|
+
'https://api.fortnox.se/3/test'
|
271
284
|
).with(
|
272
285
|
headers: {
|
273
286
|
'Access-Token' => access_token,
|
274
287
|
'Client-Secret' => client_secret,
|
275
288
|
'Content-Type' => 'application/json',
|
276
|
-
'Accept' => 'application/json'
|
289
|
+
'Accept' => 'application/json'
|
277
290
|
}
|
278
291
|
).to_return(
|
279
292
|
status: 200,
|
@@ -282,13 +295,13 @@ describe Fortnox::API::Repository::Base do
|
|
282
295
|
|
283
296
|
stub_request(
|
284
297
|
:get,
|
285
|
-
'https://api.fortnox.se/3/test'
|
298
|
+
'https://api.fortnox.se/3/test'
|
286
299
|
).with(
|
287
300
|
headers: {
|
288
301
|
'Access-Token' => access_token2,
|
289
302
|
'Client-Secret' => client_secret,
|
290
303
|
'Content-Type' => 'application/json',
|
291
|
-
'Accept' => 'application/json'
|
304
|
+
'Accept' => 'application/json'
|
292
305
|
}
|
293
306
|
).to_return(
|
294
307
|
status: 200,
|
@@ -297,23 +310,26 @@ describe Fortnox::API::Repository::Base do
|
|
297
310
|
end
|
298
311
|
|
299
312
|
context 'with subsequent requests on same object' do
|
300
|
-
let!(:response1){ repository.get(
|
301
|
-
let!(:response2){ repository.get(
|
302
|
-
let!(:response3){ repository.get(
|
313
|
+
let!(:response1) { repository.get('/test', body: '') }
|
314
|
+
let!(:response2) { repository.get('/test', body: '') }
|
315
|
+
let!(:response3) { repository.get('/test', body: '') }
|
303
316
|
|
304
317
|
# rubocop:disable RSpec/MultipleExpectations
|
305
318
|
# All these checks must be run in same it-statement because
|
306
319
|
# of the random starting index.
|
307
320
|
it 'works' do
|
308
|
-
expect(response1).not_to eq(
|
309
|
-
expect(response1).to eq(
|
310
|
-
expect(response3).not_to eq(
|
321
|
+
expect(response1).not_to eq(response2)
|
322
|
+
expect(response1).to eq(response3)
|
323
|
+
expect(response3).not_to eq(response2)
|
311
324
|
end
|
312
325
|
# rubocop:enable RSpec/MultipleExpectations
|
313
326
|
end
|
314
327
|
end
|
315
328
|
|
316
329
|
context 'when raising error from remote server' do
|
330
|
+
error_message = 'Räkenskapsår finns inte upplagt. '\
|
331
|
+
'För att kunna skapa en faktura krävs det att det finns ett räkenskapsår'
|
332
|
+
|
317
333
|
before do
|
318
334
|
Fortnox::API.configure do |conf|
|
319
335
|
conf.client_secret = client_secret
|
@@ -322,18 +338,23 @@ describe Fortnox::API::Repository::Base do
|
|
322
338
|
|
323
339
|
stub_request(
|
324
340
|
:post,
|
325
|
-
'https://api.fortnox.se/3/test'
|
341
|
+
'https://api.fortnox.se/3/test'
|
326
342
|
).to_return(
|
327
343
|
status: 500,
|
328
|
-
body: {
|
329
|
-
|
344
|
+
body: {
|
345
|
+
'ErrorInformation' => {
|
346
|
+
'error' => 1,
|
347
|
+
'message' => error_message
|
348
|
+
}
|
349
|
+
}.to_json,
|
350
|
+
headers: { 'Content-Type' => 'application/json' }
|
330
351
|
)
|
331
352
|
end
|
332
353
|
|
333
|
-
subject{ ->{ repository.post(
|
354
|
+
subject { -> { repository.post('/test', body: '') } }
|
334
355
|
|
335
|
-
it{ is_expected.to raise_error(
|
336
|
-
it{ is_expected.to raise_error(
|
356
|
+
it { is_expected.to raise_error(Fortnox::API::RemoteServerError) }
|
357
|
+
it { is_expected.to raise_error(error_message) }
|
337
358
|
|
338
359
|
context 'with debugging enabled' do
|
339
360
|
around do |example|
|
@@ -342,7 +363,7 @@ describe Fortnox::API::Repository::Base do
|
|
342
363
|
Fortnox::API.config.debugging = false
|
343
364
|
end
|
344
365
|
|
345
|
-
it{ is_expected.to raise_error(
|
366
|
+
it { is_expected.to raise_error(/\<HTTParty\:\:Request\:0x/) }
|
346
367
|
end
|
347
368
|
end
|
348
369
|
end
|