bonanza 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rspec +2 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/CHANGELOG.md +188 -0
  7. data/Gemfile +5 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +95 -0
  10. data/Rakefile +9 -0
  11. data/VERSION +1 -0
  12. data/bonanza.gemspec +27 -0
  13. data/config/locales/de.yml +7 -0
  14. data/config/locales/en.yml +7 -0
  15. data/lib/bonanza.rb +5 -0
  16. data/lib/bonanza/call_link_helpers.rb +24 -0
  17. data/lib/bonanza/engine.rb +15 -0
  18. data/lib/bonanza/link_helpers.rb +65 -0
  19. data/lib/bonanza/link_strategies.rb +14 -0
  20. data/lib/bonanza/link_strategies/base.rb +45 -0
  21. data/lib/bonanza/link_strategies/billing.rb +24 -0
  22. data/lib/bonanza/link_strategies/kuba.rb +11 -0
  23. data/lib/bonanza/link_strategies/mojito.rb +11 -0
  24. data/lib/bonanza/link_strategies/ninevirt.rb +11 -0
  25. data/lib/bonanza/link_strategies/openshift.rb +29 -0
  26. data/lib/bonanza/link_strategies/otrs.rb +25 -0
  27. data/lib/bonanza/link_strategies/redmine.rb +11 -0
  28. data/lib/bonanza/link_strategies/stats.rb +17 -0
  29. data/lib/bonanza/link_strategies/wiki.rb +15 -0
  30. data/lib/bonanza/link_strategies/yimin.rb +15 -0
  31. data/lib/bonanza/urn_link_helpers.rb +40 -0
  32. data/lib/bonanza/validators.rb +3 -0
  33. data/lib/bonanza/validators/domain_name_validator.rb +16 -0
  34. data/lib/bonanza/validators/otrs_number_validator.rb +13 -0
  35. data/lib/bonanza/validators/urn_validator.rb +14 -0
  36. data/spec/bonanza/call_link_helpers_spec.rb +50 -0
  37. data/spec/bonanza/link_helpers_spec.rb +126 -0
  38. data/spec/bonanza/link_strategies/base_spec.rb +50 -0
  39. data/spec/bonanza/link_strategies/billing_spec.rb +35 -0
  40. data/spec/bonanza/link_strategies/openshift_spec.rb +26 -0
  41. data/spec/bonanza/urn_link_helpers_spec.rb +80 -0
  42. data/spec/bonanza/validators/domain_name_validator_spec.rb +59 -0
  43. data/spec/bonanza/validators/otrs_number_validator_spec.rb +53 -0
  44. data/spec/bonanza/validators/urn_validator_spec.rb +64 -0
  45. data/spec/spec_helper.rb +90 -0
  46. metadata +182 -0
@@ -0,0 +1,126 @@
1
+ require 'spec_helper'
2
+ require 'bonanza/link_helpers'
3
+ require 'bonanza/link_strategies'
4
+
5
+ RSpec.describe Bonanza::LinkHelpers do
6
+ include Bonanza::LinkHelpers
7
+
8
+ let(:expected_url) { nine_app_url(app, entity, reference) }
9
+ let(:reference) { 1234 }
10
+
11
+ describe '.nine_app_url' do
12
+ it 'raises an error when the app name is invalid' do
13
+ expect do
14
+ nine_app_url(:foobar, 1)
15
+ end.to raise_exception 'Invalid app foobar'
16
+ end
17
+
18
+ it 'considers the Rails environment in staging' do
19
+ allow(ENV).to receive(:fetch).with('RAILS_ENV', 'production').and_return('staging')
20
+ expect(nine_app_url(:stats, :customer)).to start_with 'https://stats-staging.nine.ch'
21
+ end
22
+
23
+ it 'considers the Rails environment in development' do
24
+ allow(ENV).to receive(:fetch).with('RAILS_ENV', 'production').and_return('development')
25
+ expect(nine_app_url(:stats, :customer)).to start_with 'http://stats.dev'
26
+ end
27
+ end
28
+
29
+ describe '.link_to_otrs_ticket' do
30
+ let(:app) { :otrs }
31
+ let(:entity) { :ticket }
32
+
33
+ it 'creates a link to an otrs ticket' do
34
+ markup = link_to_otrs_ticket('OTRS', reference)
35
+ expect(markup).to include "href=\"#{expected_url}\""
36
+ expect(markup).to include "target=\"_blank\""
37
+ expect(markup).to include 'OTRS'
38
+ end
39
+ end
40
+
41
+ describe '.link_to_otrs_customer' do
42
+ let(:app) { :otrs }
43
+ let(:entity) { :customer }
44
+
45
+ it 'creates a link to an otrs customer profile' do
46
+ markup = link_to_otrs_customer('OTRS', reference)
47
+ expect(markup).to include "href=\"#{expected_url}\""
48
+ expect(markup).to include "target=\"_blank\""
49
+ expect(markup).to include 'OTRS'
50
+ end
51
+ end
52
+
53
+ describe '.link_to_billing' do
54
+ let(:app) { :billing }
55
+ let(:entity) { :debtor }
56
+
57
+ it 'creates a link to billing' do
58
+ markup = link_to_billing('Billing', reference)
59
+ expect(markup).to include "href=\"#{expected_url}\""
60
+ expect(markup).to include "target=\"_blank\""
61
+ expect(markup).to include 'Billing'
62
+ end
63
+ end
64
+
65
+ describe '.link_to_stats' do
66
+ let(:app) { :stats }
67
+ let(:entity) { :customer }
68
+
69
+ it 'creates a link to stats' do
70
+ markup = link_to_stats('Stats', reference)
71
+ expect(markup).to include "href=\"#{expected_url}\""
72
+ expect(markup).to include "target=\"_blank\""
73
+ expect(markup).to include 'Stats'
74
+ end
75
+ end
76
+
77
+ describe '.link_to_redmine' do
78
+ let(:app) { :redmine }
79
+ let(:entity) { :issue }
80
+
81
+ it 'creates a link to redmine' do
82
+ markup = link_to_redmine('Redmine', reference)
83
+ expect(markup).to include "href=\"#{expected_url}\""
84
+ expect(markup).to include "target=\"_blank\""
85
+ expect(markup).to include 'Redmine'
86
+ end
87
+ end
88
+
89
+ describe '.link_to_kuba_account' do
90
+ let(:app) { :kuba }
91
+ let(:entity) { :account }
92
+
93
+ it 'creates a link to a kuba account' do
94
+ markup = link_to_kuba_account('Kuba', reference)
95
+ expect(markup).to include "href=\"#{expected_url}\""
96
+ expect(markup).to include "target=\"_blank\""
97
+ expect(markup).to include 'Kuba'
98
+ end
99
+ end
100
+
101
+ describe '.link_to_wiki_customer' do
102
+ let(:app) { :wiki }
103
+ let(:entity) { :customer }
104
+ let(:reference) { 'prepress' }
105
+
106
+ it 'creates a link to a wiki customer doc' do
107
+ markup = link_to_wiki_customer('Wiki', reference)
108
+ expect(markup).to include "href=\"#{expected_url}\""
109
+ expect(markup).to include "target=\"_blank\""
110
+ expect(markup).to include 'Wiki'
111
+ end
112
+ end
113
+
114
+ describe '.link_to_migration_item' do
115
+ let(:app) { :yimin }
116
+ let(:entity) { :item }
117
+ let(:reference) { 12 }
118
+
119
+ it 'creates a link to a Yimin migration item' do
120
+ markup = link_to_migration_item('Migration', reference)
121
+ expect(markup).to include "href=\"#{expected_url}\""
122
+ expect(markup).to include "target=\"_blank\""
123
+ expect(markup).to include 'Migration'
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,50 @@
1
+ require 'bonanza/link_strategies/base'
2
+
3
+ RSpec.describe Bonanza::LinkStrategies::Base do
4
+ let(:strategy) { Bonanza::LinkStrategies::Base.new }
5
+ let(:environment) { 'production' }
6
+
7
+ before do
8
+ allow(strategy).to receive(:environment).and_return(environment)
9
+ end
10
+
11
+ describe '#app' do
12
+ it 'uses the class name as app name' do
13
+ expect(strategy.app).to eq 'base'
14
+ end
15
+ end
16
+
17
+ describe '#domain' do
18
+ subject { strategy.domain }
19
+
20
+ context 'for dev environment' do
21
+ let(:environment) { 'development' }
22
+ it { should eq 'http://base.dev' }
23
+ end
24
+
25
+ context 'for test environment' do
26
+ let(:environment) { 'test' }
27
+ it { should eq 'http://base.test' }
28
+ end
29
+
30
+ context 'for staging environment' do
31
+ let(:environment) { 'staging' }
32
+ it { should eq 'https://base-staging.nine.ch' }
33
+ end
34
+
35
+ context 'for production environment' do
36
+ let(:environment) { 'production' }
37
+ it { should eq 'https://base.nine.ch' }
38
+ end
39
+ end
40
+
41
+ describe '#url_to' do
42
+ before do
43
+ allow(strategy).to receive(:paths).and_return(account: '/accounts/%{reference}')
44
+ end
45
+
46
+ it 'constructs a correct url' do
47
+ expect(strategy.url_to(:account, '1234')).to eq 'https://base.nine.ch/accounts/1234'
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,35 @@
1
+ require 'bonanza/link_strategies/base'
2
+ require 'bonanza/link_strategies/billing'
3
+
4
+ RSpec.describe Bonanza::LinkStrategies::Billing do
5
+ let(:strategy) { described_class.new }
6
+ let(:environment) { 'production' }
7
+
8
+ before do
9
+ allow(strategy).to receive(:environment).and_return(environment)
10
+ end
11
+
12
+ describe '#domain' do
13
+ subject { strategy.domain }
14
+
15
+ context 'development' do
16
+ let(:environment) { 'development' }
17
+ it { should eq 'http://billing.dev' }
18
+ end
19
+
20
+ context 'test' do
21
+ let(:environment) { 'test' }
22
+ it { should eq 'http://billing.test' }
23
+ end
24
+
25
+ context 'staging' do
26
+ let(:environment) { 'staging' }
27
+ it { should eq 'http://billing.dev' }
28
+ end
29
+
30
+ context 'production' do
31
+ let(:environment) { 'production' }
32
+ it { should eq 'https://billingfw.nine.ch' }
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,26 @@
1
+ require 'bonanza/link_strategies/base'
2
+ require 'bonanza/link_strategies/openshift'
3
+
4
+ RSpec.describe Bonanza::LinkStrategies::Openshift do
5
+ let(:environment) { 'production' }
6
+
7
+ before do
8
+ allow(strategy).to receive(:environment).and_return(environment)
9
+ end
10
+
11
+ describe '#domain' do
12
+ subject { strategy.domain }
13
+
14
+ context 'no entity provided' do
15
+ let(:strategy) { described_class.new }
16
+
17
+ it { should eq 'https://aws-openshift.nine.ch:8443' }
18
+ end
19
+
20
+ context 'entity provided' do
21
+ let(:strategy) { described_class.new('openshift:project:es34') }
22
+
23
+ it { should eq 'https://openshift.nine.ch:8443' }
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+ require 'bonanza/urn_link_helpers'
3
+ require 'infrastructure_client/urn'
4
+
5
+ RSpec.describe Bonanza::UrnLinkHelpers do
6
+ include Bonanza::UrnLinkHelpers
7
+
8
+ let(:urn) { ::InfrastructureClient::Urn.new('urn:nine:vserver:1279') }
9
+
10
+ describe '.link_to_urn' do
11
+ let(:expected_url) { 'http://stats.test/admin/vserverdetail.php?id=1279' }
12
+
13
+ it 'creates a link to a resource identified by urn' do
14
+ markup = link_to_urn('My VServer', urn)
15
+ expect(markup).to include "href=\"#{expected_url}\""
16
+ expect(markup).to include "target=\"_blank\""
17
+ expect(markup).to include 'My VServer'
18
+ end
19
+
20
+ it 'works with a string just as well' do
21
+ markup = link_to_urn('My VServer', urn.to_s)
22
+ expect(markup).to include "href=\"#{expected_url}\""
23
+ expect(markup).to include "target=\"_blank\""
24
+ expect(markup).to include 'My VServer'
25
+ end
26
+
27
+ it 'assigns the urn name if no name is provided' do
28
+ markup = link_to_urn('', urn)
29
+ expect(markup).to include "href=\"#{expected_url}\""
30
+ expect(markup).to include "target=\"_blank\""
31
+ expect(markup).to include urn.short
32
+ end
33
+
34
+ it 'makes an empty link when urn is nil' do
35
+ markup = link_to_urn('My VServer', nil)
36
+ expect(markup).to include "href=\"\""
37
+ expect(markup).to include "target=\"_blank\""
38
+ expect(markup).to include 'My VServer'
39
+ end
40
+
41
+ context 'urn as a string' do
42
+ it 'makes an empty link when urn is invalid' do
43
+ markup = link_to_urn('My VServer', 'some:wrong:urn')
44
+ expect(markup).to include "href=\"\""
45
+ expect(markup).to include "target=\"_blank\""
46
+ expect(markup).to include 'My VServer'
47
+ end
48
+ end
49
+
50
+ context 'urn as an object' do
51
+ it 'makes an empty link when urn is invalid' do
52
+ markup = link_to_urn('My VServer', ::InfrastructureClient::Urn.new('aaa:bbb:ccc'))
53
+ expect(markup).to include "href=\"\""
54
+ expect(markup).to include "target=\"_blank\""
55
+ expect(markup).to include 'My VServer'
56
+ end
57
+ end
58
+
59
+ it 'makes an empty link when the urn type is not found in urls' do
60
+ urn = ::InfrastructureClient::Urn.new('urn:nine:foobar:1279')
61
+ markup = link_to_urn('My VServer', urn)
62
+ markup = link_to_urn('My VServer', urn.to_s)
63
+ expect(markup).to include "href=\"\""
64
+ expect(markup).to include "target=\"_blank\""
65
+ expect(markup).to include 'My VServer'
66
+ end
67
+
68
+ context 'when the urn type has multiple parts' do
69
+ let(:urn) { ::InfrastructureClient::Urn.new('urn:nine:ip:v4:subnet:1279') }
70
+ let(:expected_url) { 'http://stats.test/admin/changesubnet.php?version=4&id=1279' }
71
+
72
+ it 'creates a correct link' do
73
+ markup = link_to_urn('My VServer', urn)
74
+ expect(markup).to include "href=\"#{expected_url}\""
75
+ expect(markup).to include "target=\"_blank\""
76
+ expect(markup).to include 'My VServer'
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'bonanza/validators/domain_name_validator'
5
+
6
+ RSpec.describe Bonanza::Validators::DomainNameValidator do
7
+ class Domain
8
+ include ActiveModel::Validations
9
+ include Bonanza::Validators
10
+
11
+ attr_accessor :name
12
+ validates :name, domain_name: true
13
+ end
14
+
15
+ subject { Domain.new }
16
+
17
+ [
18
+ 'domain.com',
19
+ '4domain.com',
20
+ 'www.domain.ch',
21
+ 'sub.domain.domain.co.uk',
22
+ '1234.ch',
23
+ 'sasie.diamonds',
24
+ 'äää.com',
25
+ 'putzy-the-bear.co.uk',
26
+ 'موقع.وزارة-الاتصالات.مصر',
27
+ '🐩💩.com',
28
+ 'busi.katze', # we don't care about validity of tlds
29
+ ].each do |domain_name|
30
+ it "#{domain_name} is valid" do
31
+ subject.name = domain_name
32
+ expect(subject).to be_valid
33
+ end
34
+ end
35
+
36
+ describe 'invalid domain name' do
37
+ it 'displays the correct error message' do
38
+ subject.name = 'grosser bruder'
39
+
40
+ subject.valid?
41
+
42
+ expect(subject.errors[:name]).to match_array('not a valid domain name')
43
+ end
44
+
45
+ [
46
+ 'yolo',
47
+ 'grosser bruder',
48
+ 12345,
49
+ 'yolo.c',
50
+ '$$.com', # might not be valid but we don't care
51
+ ].each do |domain_name|
52
+ it "#{domain_name} is not valid" do
53
+ subject.name = domain_name
54
+
55
+ expect(subject).to_not be_valid
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+ require 'bonanza/validators/otrs_number_validator'
3
+
4
+ RSpec.describe Bonanza::Validators::OtrsNumberValidator do
5
+ class Order
6
+ include ActiveModel::Validations
7
+ include Bonanza::Validators
8
+
9
+ attr_accessor :otrs_ticket
10
+ validates :otrs_ticket, otrs_number: true
11
+ end
12
+
13
+ subject { Order.new }
14
+
15
+ it 'passes when the otrs ticket is a 16-character string of numbers' do
16
+ subject.otrs_ticket = '1234567812345678'
17
+ expect(subject).to be_valid
18
+ end
19
+
20
+ it 'passes when the otrs ticket is a 16-digit number' do
21
+ subject.otrs_ticket = 1234567812345678
22
+
23
+ expect(subject).to be_valid
24
+ end
25
+
26
+ describe 'invalid OTRS ticket' do
27
+ it 'displays the correct error message' do
28
+ subject.otrs_ticket = 'abcdefgh12345678'
29
+
30
+ subject.valid?
31
+
32
+ expect(subject.errors[:otrs_ticket]).to match_array('not an OTRS number')
33
+ end
34
+
35
+ it 'fails when it has invalid characters' do
36
+ subject.otrs_ticket = 'abcdefgh12345678'
37
+
38
+ expect(subject).to_not be_valid
39
+ end
40
+
41
+ it 'fails when it has too few numbers' do
42
+ subject.otrs_ticket = '12345678'
43
+
44
+ expect(subject).to_not be_valid
45
+ end
46
+
47
+ it 'fails when the ticket includes 16-digits but is longer' do
48
+ subject.otrs_ticket = '123456789123456789'
49
+
50
+ expect(subject).to_not be_valid
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+ require 'bonanza/validators/urn_validator'
3
+
4
+ RSpec.describe Bonanza::Validators::UrnValidator do
5
+ class Product
6
+ include ActiveModel::Validations
7
+ include Bonanza::Validators
8
+
9
+ attr_accessor :urn
10
+ validates :urn, urn: true
11
+ end
12
+
13
+ subject { Product.new }
14
+
15
+ it 'is valid with correct syntax and type' do
16
+ subject.urn = 'urn:nine:vserver:1234'
17
+
18
+ expect(subject).to be_valid
19
+ end
20
+
21
+ describe 'invalid URN' do
22
+ it 'displays the correct error message' do
23
+ subject.urn = 'urn:nine:serrrrrver:123'
24
+
25
+ subject.valid?
26
+
27
+ expect(subject.errors[:urn]).to match_array('invalid URN')
28
+ end
29
+
30
+ it 'is invalid if type is not correct' do
31
+ subject.urn = 'urn:nine:serrrrrver:123'
32
+
33
+ expect(subject).to be_invalid
34
+ end
35
+
36
+ it 'is invalid if syntax is garbage' do
37
+ subject.urn = 'garbage::syntax'
38
+
39
+ expect(subject).to be_invalid
40
+ end
41
+
42
+ it 'is invalid if empty' do
43
+ subject.urn = ''
44
+
45
+ expect(subject).to be_invalid
46
+ end
47
+ end
48
+
49
+ context 'allow_empty true' do
50
+ class OtherProduct
51
+ include ActiveModel::Validations
52
+ include Bonanza::Validators
53
+
54
+ attr_accessor :urn
55
+ validates :urn, urn: { allow_empty: true }
56
+ end
57
+
58
+ it 'is valid if empty' do
59
+ product = OtherProduct.new
60
+ product.urn = ''
61
+ expect(product).to be_valid
62
+ end
63
+ end
64
+ end