hieraviz 0.1.2 → 0.2.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.
@@ -1,20 +1,19 @@
1
1
  require 'sinatra_helper'
2
2
 
3
3
  describe HieravizApp::ApiV1 do
4
-
5
- context "page not found" do
6
- describe "GET /v1/blahblah" do
7
- let(:expected) { { 'error' => "endpoint not found" } }
4
+ context 'page not found' do
5
+ describe 'GET /v1/blahblah' do
6
+ let(:expected) { { 'error' => 'Endpoint not found' } }
8
7
  before do
9
8
  get '/blahblah'
10
9
  end
11
10
  it { expect(last_response).not_to be_ok }
12
- it { expect(JSON.parse last_response.body).to eq expected }
11
+ it { expect(JSON.parse(last_response.body)).to eq expected }
13
12
  end
14
13
  end
15
14
 
16
- context "when no creds" do
17
- describe "GET /v1/nodes" do
15
+ context 'when no creds' do
16
+ describe 'GET /v1/nodes' do
18
17
  let(:expected) { 'http://example.org/v1/not_logged' }
19
18
  before do
20
19
  get '/nodes'
@@ -23,57 +22,58 @@ describe HieravizApp::ApiV1 do
23
22
  it { expect(last_response.header['Location']).to eq expected }
24
23
  end
25
24
  describe 'GET /v1/not_logged' do
26
- let(:expected) { { 'error' => "Not connected." } }
25
+ let(:expected) { { 'error' => 'Not connected.' } }
27
26
  before do
28
27
  get '/not_logged'
29
28
  end
30
29
  it { expect(last_response).to be_ok }
31
- it { expect(JSON.parse last_response.body).to eq expected }
30
+ it { expect(JSON.parse(last_response.body)).to eq expected }
32
31
  end
33
32
  end
34
33
 
35
- context "with creds but no perms" do
36
- describe "GET /v1/nodes" do
34
+ context 'with creds but no perms' do
35
+ describe 'GET /v1/nodes' do
37
36
  let(:expected) { 'http://example.org/v1/unauthorized' }
38
37
  before do
39
38
  current_session.rack_session[:access_token] = 'sada'
40
- allow(Hieraviz::Store).
41
- to receive(:get).
42
- with('sada', 3600).
43
- and_return(false)
39
+ allow(Hieraviz::Store)
40
+ .to receive(:get)
41
+ .with('sada', 3600)
42
+ .and_return(false)
44
43
  get '/nodes'
45
44
  end
46
45
  it { expect(last_response).not_to be_ok }
47
46
  it { expect(last_response.header['Location']).to eq expected }
48
47
  end
49
48
  describe 'GET /v1/unauthorized' do
50
- let(:expected) { { 'error' => "Unauthorized" } }
49
+ let(:expected) { { 'error' => 'Unauthorized' } }
51
50
  before do
52
51
  get '/unauthorized'
53
52
  end
54
53
  it { expect(last_response).to be_ok }
55
- it { expect(JSON.parse last_response.body).to eq expected }
54
+ it { expect(JSON.parse(last_response.body)).to eq expected }
56
55
  end
57
56
  end
58
57
 
59
- context "with proper creds" do
58
+ context 'with proper creds' do
60
59
  before do
61
60
  current_session.rack_session[:access_token] = 'sada'
62
- allow(Hieraviz::Store).
63
- to receive(:get).
64
- with('sada', 3600).
65
- and_return({})
61
+ allow(app.settings.store)
62
+ .to receive(:get)
63
+ .with('sada', 3600)
64
+ .and_return({'username' => 'toto'})
66
65
  end
67
- describe "GET /v1/nodes" do
66
+ describe 'GET /v1/nodes' do
68
67
  let(:expected) { ['node1.example.com'] }
69
68
  before do
70
69
  get '/nodes'
71
70
  end
72
71
  it { expect(last_response).to be_ok }
73
- it { expect(JSON.parse last_response.body).to eq expected }
72
+ it { expect(JSON.parse(last_response.body)).to eq expected }
74
73
  end
75
- describe "GET /v1/node/node1.example.com/info" do
76
- let(:expected) {
74
+
75
+ describe 'GET /v1/node/node1.example.com/info' do
76
+ let(:expected) do
77
77
  {
78
78
  'classes' => ['farm1'],
79
79
  'country' => 'fr',
@@ -81,110 +81,219 @@ describe HieravizApp::ApiV1 do
81
81
  'farm' => 'dev',
82
82
  'fqdn' => 'node1.example.com'
83
83
  }
84
- }
84
+ end
85
85
  before do
86
86
  get '/node/node1.example.com/info'
87
87
  end
88
88
  it { expect(last_response).to be_ok }
89
- it { expect(JSON.parse last_response.body).to eq expected }
89
+ it { expect(JSON.parse(last_response.body)).to eq expected }
90
90
  end
91
- describe "GET /v1/node/node1.example.com/params" do
92
- let(:expected) {
91
+
92
+ describe 'GET /v1/node/node1.example.com/params' do
93
+ let(:expected) do
93
94
  {
94
- "param1.subparam1" => {
95
- "value" => "value1",
96
- "file" => "params/nodes/node1.example.com.yaml",
97
- "overriden" => false,
98
- "found_in" => [
95
+ 'param1.subparam1' => {
96
+ 'value' => 'value1',
97
+ 'file' => 'params/nodes/node1.example.com.yaml',
98
+ 'overriden' => false,
99
+ 'found_in' => [
99
100
  {
100
- "value"=>"value1",
101
- "file"=>"params/nodes/node1.example.com.yaml"
101
+ 'value' => 'value1',
102
+ 'file' => 'params/nodes/node1.example.com.yaml'
102
103
  }
103
104
  ]
104
105
  }
105
106
  }
106
- }
107
+ end
107
108
  before do
108
109
  get '/node/node1.example.com/params'
109
110
  end
110
111
  it { expect(last_response).to be_ok }
111
- it { expect(JSON.parse last_response.body).to eq expected }
112
+ it { expect(JSON.parse(last_response.body)).to eq expected }
112
113
  end
113
- describe "GET /v1/node/node1.example.com" do
114
- let(:expected) {
114
+
115
+ describe 'GET /v1/node/node1.example.com' do
116
+ let(:expected) do
115
117
  {
116
- "param1.subparam1" => {
117
- "value" => "value1",
118
- "file" => "params/nodes/node1.example.com.yaml",
119
- "overriden" => false,
120
- "found_in" => [
118
+ 'param1.subparam1' => {
119
+ 'value' => 'value1',
120
+ 'file' => 'params/nodes/node1.example.com.yaml',
121
+ 'overriden' => false,
122
+ 'found_in' => [
121
123
  {
122
- "value"=>"value1",
123
- "file"=>"params/nodes/node1.example.com.yaml"
124
+ 'value' => 'value1',
125
+ 'file' => 'params/nodes/node1.example.com.yaml'
124
126
  }
125
127
  ]
126
128
  }
127
129
  }
128
- }
130
+ end
129
131
  before do
130
132
  get '/node/node1.example.com'
131
133
  end
132
134
  it { expect(last_response).to be_ok }
133
- it { expect(JSON.parse last_response.body).to eq expected }
134
- end
135
- describe "GET /v1/node/node1.example.com/allparams" do
136
- let(:expected) {
137
- {
138
- "param1.subparam1" => {
139
- "value" => "value1",
140
- "file" => "-",
141
- "overriden" => true,
142
- "found_in" => [
135
+ it { expect(JSON.parse(last_response.body)).to eq expected }
136
+ end
137
+
138
+ describe 'GET /v1/node/node1.example.com/allparams' do
139
+ let(:expected) do
140
+ {
141
+ 'param1.subparam1' => {
142
+ 'value' => 'value1',
143
+ 'file' => '-',
144
+ 'overriden' => true,
145
+ 'found_in' => [
143
146
  {
144
- "value" => "value1",
145
- "file" => "params/nodes/node1.example.com.yaml"
147
+ 'value' => 'value1',
148
+ 'file' => 'params/nodes/node1.example.com.yaml'
146
149
  },
147
150
  {
148
- "value" => "value common",
149
- "file"=>"params/common/common.yaml"
151
+ 'value' => 'value common',
152
+ 'file' => 'params/common/common.yaml'
150
153
  }
151
154
  ]
152
155
  },
153
- "param2.subparam2" => {
154
- "value" => "another value",
155
- "file"=>"params/common/common.yaml",
156
- "overriden" => false,
157
- "found_in" => [
156
+ 'param2.subparam2' => {
157
+ 'value' => 'another value',
158
+ 'file' => 'params/common/common.yaml',
159
+ 'overriden' => false,
160
+ 'found_in' => [
158
161
  {
159
- "value" => "another value",
160
- "file"=>"params/common/common.yaml"
162
+ 'value' => 'another value',
163
+ 'file' => 'params/common/common.yaml'
161
164
  }
162
165
  ]
163
166
  }
164
167
  }
165
- }
168
+ end
166
169
  before do
167
170
  get '/node/node1.example.com/allparams'
168
171
  end
169
172
  it { expect(last_response).to be_ok }
170
- it { expect(JSON.parse last_response.body).to eq expected }
173
+ it { expect(JSON.parse(last_response.body)).to eq expected }
174
+ end
175
+
176
+ describe 'GET /v1/node/node1.example.com/facts' do
177
+ context 'when no facts are recorded' do
178
+ let(:expected) { Hash.new }
179
+ before do
180
+ get '/node/node1.example.com/facts'
181
+ end
182
+ it { expect(last_response).to be_ok }
183
+ it { expect(JSON.parse(last_response.body)).to eq expected }
184
+ end
185
+ context 'when some facts are recorded' do
186
+ let(:tmpdir) { 'spec/files/tmp' }
187
+ let(:base) { '' }
188
+ let(:node) { 'node1.example.com' }
189
+ let(:user) { 'toto' }
190
+ let(:facts) { Hieraviz::Facts.new tmpdir, base, node, user }
191
+ let(:data) { { 'a' => 'b' } }
192
+ let(:factsfile) { 'spec/files/tmp/__node1.example.com__toto' }
193
+ after { File.unlink factsfile }
194
+ before do
195
+ facts.write(data)
196
+ get '/node/node1.example.com/facts'
197
+ end
198
+ it { expect(last_response).to be_ok }
199
+ it { expect(JSON.parse(last_response.body)).to eq data }
200
+ end
201
+ end
202
+
203
+ describe 'POST /v1/node/node1.example.com/facts' do
204
+ let(:data) { { 'a' => 'b' } }
205
+ let(:factsfile) { 'spec/files/tmp/__node1.example.com__toto' }
206
+ after { File.unlink factsfile }
207
+ before do
208
+ post '/node/node1.example.com/facts', data.to_json
209
+ end
210
+ it { expect(last_response).to be_ok }
211
+ it { expect(File).to exist(factsfile) }
171
212
  end
172
- describe "GET /v1/farms" do
173
- let(:expected) { { "farm1" => 0 } }
213
+
214
+ describe 'DELETE /v1/node/node1.example.com/facts' do
215
+ let(:tmpdir) { 'spec/files/tmp' }
216
+ let(:base) { '' }
217
+ let(:node) { 'node1.example.com' }
218
+ let(:user) { 'toto' }
219
+ let(:facts) { Hieraviz::Facts.new tmpdir, base, node, user }
220
+ let(:data) { { 'a' => 'b' } }
221
+ let(:factsfile) { 'spec/files/tmp/__node1.example.com__toto' }
222
+ before do
223
+ facts.write(data)
224
+ delete '/node/node1.example.com/facts'
225
+ end
226
+ it { expect(last_response).to be_ok }
227
+ it { expect(File).not_to exist(factsfile) }
228
+ end
229
+
230
+ describe 'GET /v1/farms' do
231
+ let(:expected) { { 'farm1' => 0 } }
174
232
  before do
175
233
  get '/farms'
176
234
  end
177
235
  it { expect(last_response).to be_ok }
178
- it { expect(JSON.parse last_response.body).to eq expected }
236
+ it { expect(JSON.parse(last_response.body)).to eq expected }
179
237
  end
180
- describe "GET /v1/vars" do
181
- let(:expected) { ['fqdn', 'farm'] }
238
+
239
+ describe 'GET /v1/vars' do
240
+ let(:expected) { %w(fqdn farm) }
182
241
  before do
183
242
  get '/vars'
184
243
  end
185
244
  it { expect(last_response).to be_ok }
186
- it { expect(JSON.parse last_response.body).to eq expected }
245
+ it { expect(JSON.parse(last_response.body)).to eq expected }
187
246
  end
188
- end
189
247
 
248
+ describe 'GET /v1/node/node1.example.com/hierarchy' do
249
+ let(:expected) do
250
+ {
251
+ 'hiera' => [
252
+ 'nodes/%{fqdn}',
253
+ 'farm/%{farm}',
254
+ 'common/common'
255
+ ],
256
+ 'vars' => %w(
257
+ fqdn
258
+ farm),
259
+ 'info' => {
260
+ 'fqdn' => 'node1.example.com',
261
+ 'country' => 'fr',
262
+ 'datacenter' => 'equinix',
263
+ 'farm' => 'dev',
264
+ 'classes' => [
265
+ 'farm1'
266
+ ]
267
+ },
268
+ 'files' => [
269
+ 'params/nodes/node1.example.com.yaml'
270
+ ],
271
+ 'facts' => {},
272
+ 'defaults' => nil
273
+ }
274
+ end
275
+ before do
276
+ get '/node/node1.example.com/hierarchy'
277
+ end
278
+ it { expect(last_response).to be_ok }
279
+ it { expect(JSON.parse(last_response.body)).to eq expected }
280
+ end
281
+
282
+ describe 'GET /v1/farm/dev' do
283
+ let(:expected) do
284
+ {
285
+ 'node1.example.com' => {
286
+ 'country' => 'fr',
287
+ 'datacenter' => 'equinix',
288
+ 'farm' => 'dev'
289
+ }
290
+ }
291
+ end
292
+ before do
293
+ get '/farm/dev'
294
+ end
295
+ it { expect(last_response).to be_ok }
296
+ it { expect(JSON.parse(last_response.body)).to eq expected }
297
+ end
298
+ end
190
299
  end
@@ -0,0 +1,11 @@
1
+ require 'sinatra_helper'
2
+
3
+ describe HieravizApp::Web do
4
+ before do
5
+ ENV['HIERAVIZ_CONFIG_FILE'] = File.expand_path '../../files/config_dummy.yml', __FILE__
6
+ app.set :configdata, Hieraviz::Config.load
7
+ app.set :basepaths, Hieraviz::Config.basepaths
8
+ app.set :store, Hieraviz::Store.new(app.settings.configdata['tmpdir'])
9
+ end
10
+
11
+ end
data/spec/app/web_spec.rb CHANGED
@@ -1,52 +1,50 @@
1
1
  require 'sinatra_helper'
2
2
 
3
3
  describe HieravizApp::Web do
4
-
5
- context "without creds" do
6
- describe "GET /" do
4
+ context 'without creds' do
5
+ describe 'GET /' do
7
6
  before { get '/' }
8
7
  it { expect(last_response.status).to eq 401 }
9
8
  end
10
9
  end
11
10
 
12
- context "with proper creds" do
11
+ context 'with proper creds' do
13
12
  before do
14
13
  basic_authorize 'toto', 'toto'
15
14
  end
16
- describe "GET /" do
15
+ describe 'GET /' do
17
16
  before { get '/' }
18
17
  it { expect(last_response.status).to eq 200 }
19
18
  it { expect(last_response.body).to include 'Welcome to hieraviz' }
20
19
  end
21
- describe "GET /logout" do
20
+ describe 'GET /logout' do
22
21
  before { get '/logout' }
23
22
  it { expect(last_response.body).to include 'Please login again' }
24
23
  end
25
- describe "GET /nodes" do
24
+ describe 'GET /nodes' do
26
25
  before { get '/nodes' }
27
26
  it { expect(last_response.body).to include 'node1.example.com' }
28
27
  end
29
- describe "GET /farms" do
28
+ describe 'GET /farms' do
30
29
  before { get '/farms' }
31
30
  it { expect(last_response.body).to include 'farm1' }
32
31
  end
33
- describe "GET /modules" do
32
+ describe 'GET /modules' do
34
33
  before { get '/modules' }
35
34
  it { expect(last_response.body).to include 'Work In Progress' }
36
35
  end
37
- describe "GET /resources" do
36
+ describe 'GET /resources' do
38
37
  before { get '/resources' }
39
38
  it { expect(last_response.body).to include 'Work In Progress' }
40
39
  end
41
- describe "GET /tata/toto" do
40
+ describe 'GET /tata/toto' do
42
41
  before { get '/tata/toto' }
43
42
  it { expect(last_response.status).to eq 404 }
44
43
  it { expect(last_response.body).to include 'Page not found' }
45
44
  end
46
- describe "GET /user" do
45
+ describe 'GET /user' do
47
46
  before { get '/user' }
48
47
  it { expect(last_response.body).to include 'toto' }
49
48
  end
50
49
  end
51
-
52
50
  end
@@ -12,7 +12,6 @@ puppetdb:
12
12
  usessl: false
13
13
  host: puppetdb.example.com
14
14
  port: 8080
15
-
16
15
  auth_method: dummy
17
16
  http_auth:
18
17
  username: 'toto'
@@ -2,12 +2,21 @@ require 'spec_helper'
2
2
  require 'oauth2_helper'
3
3
 
4
4
  describe Hieraviz::AuthGitlab do
5
-
6
- let(:oauth2) { Hieraviz::AuthGitlab.new({}) }
5
+ let(:settings) do
6
+ {
7
+ 'application_id' => '',
8
+ 'secret' => '',
9
+ 'host' => '',
10
+ 'resource_required' => false,
11
+ 'required_response_key' => '',
12
+ 'required_response_value' => ''
13
+ }
14
+ end
15
+ let(:oauth2) { Hieraviz::AuthGitlab.new(settings) }
7
16
  before do
8
- allow(OAuth2::Client).
9
- to receive(:new).
10
- and_return(Oauth2Mock.new)
17
+ allow(OAuth2::Client)
18
+ .to receive(:new)
19
+ .and_return(Oauth2Mock.new)
11
20
  end
12
21
 
13
22
  describe '.new' do
@@ -22,25 +31,25 @@ describe Hieraviz::AuthGitlab do
22
31
  let(:url) { 'http://example.com/something' }
23
32
  let(:token) { '123456' }
24
33
  let(:urlfail) { 'fail' }
25
- let(:expected) {
34
+ let(:expected) do
26
35
  {
27
- "somekey" => "somevalue"
36
+ 'somekey' => 'somevalue'
28
37
  }
29
- }
30
- let(:expectedfail) {
38
+ end
39
+ let(:expectedfail) do
31
40
  {
32
- "error" => "message"
41
+ 'error' => 'message'
33
42
  }
34
- }
43
+ end
35
44
  before do
36
- allow(OAuth2::AccessToken).
37
- to receive(:new).
38
- and_return(AccessTokenMock.new)
45
+ allow(OAuth2::AccessToken)
46
+ .to receive(:new)
47
+ .and_return(AccessTokenMock.new)
39
48
  end
40
- context "when there is an error" do
49
+ context 'when there is an error' do
41
50
  it { expect(oauth2.get_response(urlfail, token)).to eq expectedfail }
42
51
  end
43
- context "when everything is fine" do
52
+ context 'when everything is fine' do
44
53
  it { expect(oauth2.get_response(url, token)).to eq expected }
45
54
  end
46
55
  end
@@ -56,17 +65,60 @@ describe Hieraviz::AuthGitlab do
56
65
  end
57
66
 
58
67
  describe '.authorized?' do
68
+ context 'when authorization is required' do
69
+ let(:settings) do
70
+ {
71
+ 'application_id' => '',
72
+ 'secret' => '',
73
+ 'host' => '',
74
+ 'resource_required' => 'http://example.com/something',
75
+ 'required_response_key' => 'somekey',
76
+ 'required_response_value' => 'somevalue'
77
+ }
78
+ end
79
+ context 'with a valid authorization' do
80
+ let(:token) { '123456' }
81
+ before do
82
+ allow(OAuth2::AccessToken)
83
+ .to receive(:new)
84
+ .and_return(AccessTokenMock.new)
85
+ end
86
+ it { expect(oauth2.authorized?(token)).to eq true }
87
+ end
88
+ context 'without a valid authorization' do
89
+ let(:settings) do
90
+ {
91
+ 'application_id' => '',
92
+ 'secret' => '',
93
+ 'host' => '',
94
+ 'resource_required' => 'fail',
95
+ 'required_response_key' => 'id',
96
+ 'required_response_value' => '42'
97
+ }
98
+ end
99
+ let(:token) { '123456' }
100
+ before do
101
+ allow(OAuth2::AccessToken)
102
+ .to receive(:new)
103
+ .and_return(AccessTokenMock.new)
104
+ end
105
+ it { expect(oauth2.authorized?(token)).to eq false }
106
+ end
107
+ end
108
+ context 'when no authorization is required' do
109
+ let(:token) { '123456' }
110
+ it { expect(oauth2.authorized?(token)).to be_truthy }
111
+ end
59
112
  end
60
113
 
61
114
  describe '.user_info' do
62
115
  let(:token) { '123456' }
63
- let(:expected) { { "somekey" => "somevalue" } }
116
+ let(:expected) { { 'somekey' => 'somevalue' } }
64
117
  before do
65
- allow(OAuth2::AccessToken).
66
- to receive(:new).
67
- and_return(AccessTokenMock.new)
118
+ allow(OAuth2::AccessToken)
119
+ .to receive(:new)
120
+ .and_return(AccessTokenMock.new)
68
121
  end
69
122
  it { expect(oauth2.user_info(token)).to eq expected }
70
123
  end
71
-
72
124
  end
@@ -1,7 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Hieraviz::Config do
4
-
5
4
  context 'with a single puppet dir' do
6
5
  before do
7
6
  ENV['HIERAVIZ_CONFIG_FILE'] = File.expand_path '../../files/config.yml', __FILE__
@@ -9,7 +8,7 @@ describe Hieraviz::Config do
9
8
  end
10
9
 
11
10
  describe '.load' do
12
- let(:expected) { "spec/files/puppet" }
11
+ let(:expected) { 'spec/files/puppet' }
13
12
  it { expect(Hieraviz::Config.load['basepath']).to eq expected }
14
13
  end
15
14
 
@@ -32,12 +31,12 @@ describe Hieraviz::Config do
32
31
  context 'when path starts with a slash' do
33
32
  let(:path) { '/some/path' }
34
33
  let(:expected) { path }
35
- it { expect(Hieraviz::Config.root_path path).to eq expected }
34
+ it { expect(Hieraviz::Config.root_path(path)).to eq expected }
36
35
  end
37
36
  context 'when path don\'t start with a slash' do
38
37
  let(:path) { 'relative/path' }
39
38
  let(:expected) { File.expand_path(File.join('../../../', path), __FILE__) }
40
- it { expect(Hieraviz::Config.root_path path).to eq expected }
39
+ it { expect(Hieraviz::Config.root_path(path)).to eq expected }
41
40
  end
42
41
  end
43
42
  end
@@ -49,15 +48,13 @@ describe Hieraviz::Config do
49
48
  end
50
49
 
51
50
  describe '.basepaths' do
52
- let(:expected) {
51
+ let(:expected) do
53
52
  [
54
53
  File.expand_path('../../../spec/files/puppet', __FILE__),
55
54
  File.expand_path('../../../spec/files/puppet2', __FILE__)
56
55
  ]
57
- }
56
+ end
58
57
  it { expect(Hieraviz::Config.basepaths).to match_array(expected) }
59
58
  end
60
-
61
59
  end
62
-
63
60
  end