myer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +4 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +9 -0
  5. data/Gemfile +2 -0
  6. data/LICENSE +21 -0
  7. data/README.md +5 -0
  8. data/Rakefile +44 -0
  9. data/bin/myer +198 -0
  10. data/features/myer.feature +8 -0
  11. data/features/step_definitions/myer_steps.rb +6 -0
  12. data/features/support/env.rb +15 -0
  13. data/lib/myer.rb +25 -0
  14. data/lib/myer/admin_cli_controller.rb +70 -0
  15. data/lib/myer/api.rb +104 -0
  16. data/lib/myer/cli_controller.rb +204 -0
  17. data/lib/myer/config.rb +86 -0
  18. data/lib/myer/content.rb +92 -0
  19. data/lib/myer/crypto.rb +40 -0
  20. data/lib/myer/exceptions.rb +7 -0
  21. data/lib/myer/plot.rb +11 -0
  22. data/lib/myer/proc_net_parser.rb +11 -0
  23. data/lib/myer/test_cli_controller.rb +52 -0
  24. data/lib/myer/ticket.rb +3 -0
  25. data/lib/myer/ticket_store.rb +61 -0
  26. data/lib/myer/version.rb +3 -0
  27. data/myer.gemspec +26 -0
  28. data/myer.rdoc +5 -0
  29. data/scripts/plot-helper.py +32 -0
  30. data/spec/admin_cli_controller_spec.rb +128 -0
  31. data/spec/api_spec.rb +136 -0
  32. data/spec/cli_controller_spec.rb +368 -0
  33. data/spec/config_spec.rb +116 -0
  34. data/spec/content_spec.rb +103 -0
  35. data/spec/crypto_spec.rb +37 -0
  36. data/spec/data/myer-full.config +9 -0
  37. data/spec/data/myer-multiple.config +14 -0
  38. data/spec/data/myer.config +6 -0
  39. data/spec/data/plot-data.csv +31 -0
  40. data/spec/data/secret-ticket-12345678.json +1 -0
  41. data/spec/data/secret-ticket-987654321.json +1 -0
  42. data/spec/plot_spec.rb +9 -0
  43. data/spec/proc_net_parser_spec.rb +15 -0
  44. data/spec/shared_examples_for_config.rb +47 -0
  45. data/spec/spec_helper.rb +14 -0
  46. data/spec/test_cli_controller_spec.rb +72 -0
  47. data/spec/ticket_store_spec.rb +86 -0
  48. data/test/default_test.rb +14 -0
  49. data/test/test_helper.rb +9 -0
  50. metadata +220 -0
@@ -0,0 +1,368 @@
1
+ require_relative "spec_helper"
2
+
3
+ require "shared_examples_for_config"
4
+
5
+ include GivenFilesystemSpecHelpers
6
+
7
+ describe CliController do
8
+ use_given_filesystem
9
+
10
+ before(:each) do
11
+ @controller = CliController.new
12
+ end
13
+
14
+ it_behaves_like "config"
15
+
16
+ describe "#api" do
17
+ it "provides user API" do
18
+ @controller.config_dir = given_directory do
19
+ given_file("myer.config", from: "myer-full.config")
20
+ end
21
+ @controller.read_config
22
+ api = @controller.api
23
+
24
+ expect(api.user).to eq "ddd"
25
+ expect(api.password).to eq "ggg"
26
+ end
27
+ end
28
+
29
+ describe "#create_bucket" do
30
+ it "creates new bucket" do
31
+ expected_bucket_id = "150479372"
32
+ expect_any_instance_of(MySelf::Api).to receive(:create_bucket)
33
+ .and_return(expected_bucket_id)
34
+
35
+ config_file_path = nil
36
+ @controller.config_dir = given_directory do
37
+ config_file_path = given_file("myer.config", from: "myer-full.config")
38
+ end
39
+
40
+ ticket_name = "secret-ticket-#{expected_bucket_id}.json"
41
+ ticket_file_path = File.join(@controller.config_dir, ticket_name)
42
+
43
+ out = double
44
+ expect(out).to receive(:puts).with(/#{ticket_name}/).at_least(:once)
45
+ expect(out).to receive(:puts).at_least(:once)
46
+ @controller.out = out
47
+
48
+ bucket_id = @controller.create_bucket("Test Data")
49
+ expect(bucket_id).to eq expected_bucket_id
50
+
51
+ expect(@controller.default_bucket_id).to eq bucket_id
52
+
53
+ config = YAML.load_file(config_file_path)
54
+ expect(config["servers"]["example.org"]["default_bucket_id"]).to eq bucket_id
55
+
56
+ json = JSON.parse(File.read(ticket_file_path))
57
+ expect(json["server"]).to eq "example.org"
58
+ expect(json["name"]).to eq "Test Data"
59
+ expect(json["bucket_id"]).to eq expected_bucket_id
60
+ end
61
+ end
62
+
63
+ describe "#write_item" do
64
+ it "writes raw item" do
65
+ expect_any_instance_of(MySelf::Api).to receive(:create_item)
66
+ .with("309029630", "my data")
67
+ .and_return("504885608")
68
+
69
+ @controller.config_dir = given_directory do
70
+ given_file("myer.config", from: "myer-full.config")
71
+ end
72
+
73
+ item_id = @controller.write_item("309029630", "my data")
74
+
75
+ expect(item_id).to eq "504885608"
76
+ end
77
+ end
78
+
79
+ describe "#write" do
80
+ it "writes encrypted item" do
81
+ @controller.config_dir = given_directory do
82
+ given_file("myer.config")
83
+ given_file("secret-ticket-987654321.json")
84
+ end
85
+
86
+ @controller.read_config
87
+ bucket_id = @controller.default_bucket_id
88
+
89
+ allow_any_instance_of(Crypto).to receive(:encrypt).and_return("encrypted")
90
+
91
+ expect(@controller).to receive(:write_item).with(bucket_id, "encrypted")
92
+
93
+ @controller.write("some data")
94
+ end
95
+ end
96
+
97
+ describe "#read_items" do
98
+ it "reads raw items" do
99
+ bucket_id = "987654321"
100
+ items = [
101
+ OpenStruct.new(id: "271086077", content: "my data"),
102
+ OpenStruct.new(id: "263800370", content: "more data")
103
+ ]
104
+
105
+ expect_any_instance_of(MySelf::Api).to receive(:get_items).with(bucket_id)
106
+ .and_return(items)
107
+
108
+ @controller.config_dir = given_directory do
109
+ given_file("myer.config", from: "myer-full.config")
110
+ given_file("secret-ticket-#{bucket_id}.json")
111
+ end
112
+
113
+ class TestCrypto < Crypto
114
+ def decrypt(cipher)
115
+ "A#{cipher}O"
116
+ end
117
+ end
118
+
119
+ @controller.crypto = TestCrypto.new
120
+
121
+ out = double
122
+ expect(out).to receive(:puts).with("263800370: Amore dataO")
123
+ expect(out).to receive(:puts).with("271086077: Amy dataO")
124
+ @controller.out = out
125
+
126
+ inner_items = @controller.read_items(bucket_id)
127
+
128
+ expect(@controller.crypto.passphrase).to eq "secret key"
129
+ expect(inner_items.count).to eq 2
130
+ expect(inner_items[0]).to eq "Amy dataO"
131
+ expect(inner_items[1]).to eq "Amore dataO"
132
+ end
133
+ end
134
+
135
+ describe "#read" do
136
+ it "gives an error when bucket is not set" do
137
+ @controller.config_dir = given_directory
138
+
139
+ expect {
140
+ @controller.read
141
+ }.to raise_error(Myer::Error)
142
+ end
143
+
144
+ it "writes read data to local file" do
145
+ @controller.data_dir = given_directory
146
+ @controller.config_dir = given_directory do
147
+ given_file("myer.config")
148
+ end
149
+
150
+ expect(@controller).to receive(:read_items).
151
+ with("987654321").
152
+ and_return(['{"data":"2014-06-03,37"}','{"data":"2014-06-04,39"}'])
153
+
154
+ @controller.read
155
+
156
+ expect(File.read(File.join(@controller.data_dir,
157
+ @controller.default_bucket_id + ".csv"))).
158
+ to eq <<EOT
159
+ 2014-06-03,37
160
+ 2014-06-04,39
161
+ EOT
162
+ end
163
+ end
164
+
165
+ describe "#write_value" do
166
+ it "writes value" do
167
+ @controller.config_dir = given_directory do
168
+ given_file("myer.config")
169
+ given_file("secret-ticket-987654321.json")
170
+ end
171
+
172
+ value = 42
173
+
174
+ expect(@controller).to receive(:write_item)
175
+ @controller.write_value(value)
176
+ end
177
+ end
178
+
179
+ describe "#write_pair" do
180
+ it "writes value pair" do
181
+ @controller.config_dir = given_directory do
182
+ given_file("myer.config")
183
+ given_file("secret-ticket-987654321.json")
184
+ end
185
+
186
+ expect(@controller).to receive(:write_value).with('["2014-10-24","42"]')
187
+
188
+ @controller.write_pair("2014-10-24", "42")
189
+ end
190
+ end
191
+
192
+ describe "#create_payload" do
193
+ it "creates payload" do
194
+ value = "some data"
195
+ payload_string = @controller.create_payload(value)
196
+ payload = JSON.parse(payload_string)
197
+ expect(payload["id"].length).to be > 6
198
+ expect(payload["written_at"]).to match /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\dZ$/
199
+ expect(payload.has_key?("tag")).to be false
200
+ expect(payload["data"]).to eq "some data"
201
+ end
202
+
203
+ it "creates payload with tag" do
204
+ value = "some data"
205
+ payload_string = @controller.create_payload(value, "title")
206
+ payload = JSON.parse(payload_string)
207
+ expect(payload["tag"]).to eq "title"
208
+ expect(payload["data"]).to eq "some data"
209
+ end
210
+ end
211
+
212
+ describe "#plot" do
213
+ it "reads data from server and plots pairs of date and value" do
214
+ @controller.data_dir = given_directory
215
+ @controller.config_dir = given_directory do
216
+ given_file("myer.config")
217
+ given_file("secret-ticket-987654321.json")
218
+ end
219
+
220
+ expect(@controller).to receive(:read_items).and_return(['{"data":"[\"2014-06-03\",\"37\"]"}','{"data":"[\"2014-06-04\",\"39\"]"}'])
221
+
222
+ expect_any_instance_of(Plot).to receive(:show)
223
+
224
+ @controller.plot
225
+ end
226
+
227
+ it "reads data from local file and plots it" do
228
+ expect(@controller).to_not receive(:read_items)
229
+
230
+ expect_any_instance_of(Plot).to receive(:show)
231
+
232
+ @controller.plot(dont_sync: true)
233
+ end
234
+ end
235
+
236
+ describe "#export" do
237
+ it "exports read data as JSON" do
238
+ @controller.data_dir = given_directory
239
+ @controller.config_dir = given_directory do
240
+ given_file("myer.config")
241
+ given_file("secret-ticket-987654321.json")
242
+ end
243
+
244
+ output_path = File.join(given_directory, "export.json")
245
+
246
+ expect(@controller).to receive(:read_items).and_return(['{"tag":"type","data":"json"}','{"data":"[\"2014-06-03\",\"37\"]"}','{"data":"[\"2014-06-04\",\"39\"]"}','{"data":"My Data","tag":"title"}'])
247
+
248
+ @controller.export(output_path)
249
+
250
+ expected_data = <<EOT
251
+ {"title":"My Data","data":[{"date":"2014-06-03","value":"37"},{"date":"2014-06-04","value":"39"}]}
252
+ EOT
253
+
254
+ expect(File.read(output_path)).to eq(expected_data.chomp)
255
+ end
256
+ end
257
+
258
+ describe "#create_token" do
259
+ it "creates token" do
260
+ @controller.config_dir = given_directory do
261
+ given_file("myer.config", from: "myer-full.config")
262
+ end
263
+
264
+ expected_token = "12345677890"
265
+ expect_any_instance_of(MySelf::Api).to receive(:create_token)
266
+ .and_return(expected_token)
267
+
268
+ out = double
269
+ expect(out).to receive(:puts).with(/#{expected_token}/).at_least(:once)
270
+ @controller.out = out
271
+
272
+ token = @controller.create_token
273
+
274
+ expect(token).to eq expected_token
275
+ end
276
+ end
277
+
278
+ describe "#register" do
279
+ it "registers user client" do
280
+ @controller.config_dir = given_directory
281
+
282
+ token = "12342"
283
+ server = "example.org"
284
+ expected_user = "xxx"
285
+ expected_password = "yyy"
286
+ expect_any_instance_of(MySelf::Api).to receive(:register)
287
+ .with(token)
288
+ .and_return([expected_user, expected_password])
289
+
290
+ @controller.register(server, token)
291
+
292
+ expect(@controller.default_server).to eq server
293
+ expect(@controller.user_id).to eq expected_user
294
+ expect(@controller.user_password).to eq expected_password
295
+
296
+ config = YAML.load_file(File.join(@controller.config_dir, "myer.config"))
297
+ expect(config["servers"]["example.org"]["user_id"]).to eq expected_user
298
+ expect(config["servers"]["example.org"]["user_password"]).to eq expected_password
299
+ end
300
+ end
301
+
302
+ describe "#consume_ticket" do
303
+ it "moves ticket to correct place" do
304
+ ticket_name = "secret-ticket-12345678.json"
305
+ ticket_source_path = given_file(ticket_name)
306
+ @controller.config_dir = given_directory
307
+ ticket_target_path = File.join(@controller.config_dir, ticket_name)
308
+ ticket_content = File.read(ticket_source_path)
309
+
310
+ expect(File.exist?(ticket_target_path)).to be(false)
311
+
312
+ @controller.consume_ticket(ticket_source_path)
313
+
314
+ expect(File.exist?(ticket_source_path)).to be(false)
315
+ expect(File.read(ticket_target_path)).to eq ticket_content
316
+
317
+ expect(@controller.default_bucket_id).to eq(YAML.load(ticket_content)["bucket_id"])
318
+ end
319
+ end
320
+
321
+ describe "#list_tickets" do
322
+ it "lists tickets" do
323
+ @controller.config_dir = given_directory do
324
+ given_file("secret-ticket-12345678.json")
325
+ given_file("secret-ticket-987654321.json")
326
+ end
327
+
328
+ @controller.out = StringIO.new
329
+
330
+ @controller.list_tickets
331
+
332
+ expect(@controller.out.string).to eq <<EOT
333
+ Available Tickets:
334
+ Server 'mycroft.example.org':
335
+ Bucket 'Test Data' (12345678)
336
+ Server 'localhost':
337
+ Bucket 'Test Data' (987654321)
338
+ EOT
339
+ end
340
+
341
+ it "lists tickets with status" do
342
+ stub_request(:get, "http://mycroft.example.org:4735/ping").
343
+ with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
344
+ to_return(:status => 200, :body => "{\"ping\":\"pong\"}", :headers => {})
345
+ stub_request(:get, "http://localhost:4735/ping").
346
+ with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
347
+ to_return(:status => 200, :body => "broken", :headers => {})
348
+
349
+ @controller.config_dir = given_directory do
350
+ given_file("secret-ticket-12345678.json")
351
+ given_file("secret-ticket-987654321.json")
352
+ given_file("myer.config", from: "myer-full.config")
353
+ end
354
+
355
+ @controller.out = StringIO.new
356
+
357
+ @controller.list_tickets(show_status: true)
358
+
359
+ expect(@controller.out.string).to eq <<EOT
360
+ Available Tickets:
361
+ Server 'mycroft.example.org' [pings]:
362
+ Bucket 'Test Data' (12345678)
363
+ Server 'localhost' [ping error: 757: unexpected token at 'broken']:
364
+ Bucket 'Test Data' (987654321)
365
+ EOT
366
+ end
367
+ end
368
+ end
@@ -0,0 +1,116 @@
1
+ require_relative "spec_helper.rb"
2
+
3
+ include GivenFilesystemSpecHelpers
4
+
5
+ class MyConfig
6
+ include Myer::Config
7
+ end
8
+
9
+ describe Myer::Config do
10
+ use_given_filesystem
11
+
12
+ describe "#local_csv_path" do
13
+ before(:each) do
14
+ @config = MyConfig.new
15
+ @config.config_dir = given_directory
16
+ end
17
+
18
+ it "constructs path from bucket id" do
19
+ @config.data_dir = given_directory
20
+
21
+ expect(@config.local_csv_path("x7326")).
22
+ to eq(File.join(@config.data_dir, "x7326.csv"))
23
+ end
24
+ end
25
+
26
+ describe "#write_config" do
27
+ before(:each) do
28
+ @config = MyConfig.new
29
+ @config.config_dir = given_directory
30
+ end
31
+
32
+ describe "with one server" do
33
+ it "writes config" do
34
+ @config.default_server = "example.org"
35
+
36
+ @config.admin_id = "abc"
37
+ @config.admin_password = "def"
38
+ @config.user_id = "ddd"
39
+ @config.user_password = "ggg"
40
+ @config.default_bucket_id = "987654321"
41
+
42
+ @config.write_config
43
+
44
+ expect(File.read(File.join(@config.config_dir, "myer.config"))).
45
+ to eq(File.read(test_data_path("myer-full.config")))
46
+ end
47
+ end
48
+ end
49
+
50
+ describe "#read_config" do
51
+ describe "with one server" do
52
+ before(:each) do
53
+ @config = MyConfig.new
54
+ @config.config_dir = given_directory do
55
+ given_file("myer.config", from: "myer-full.config")
56
+ end
57
+
58
+ @config.read_config
59
+ end
60
+
61
+ it "reads config" do
62
+ server = @config.server(@config.default_server)
63
+ expect(server.admin_id).to eq "abc"
64
+ expect(server.admin_password).to eq "def"
65
+ expect(server.user_id).to eq "ddd"
66
+ expect(server.user_password).to eq "ggg"
67
+ expect(server.default_bucket_id).to eq "987654321"
68
+ end
69
+
70
+ it "reads default server" do
71
+ expect(@config.default_server).to eq "example.org"
72
+ end
73
+ end
74
+
75
+ describe "with multiple servers" do
76
+ before(:each) do
77
+ @config = MyConfig.new
78
+ @config.config_dir = given_directory do
79
+ given_file("myer.config", from: "myer-multiple.config")
80
+ end
81
+
82
+ @config.read_config
83
+ end
84
+
85
+ it "reads list of servers" do
86
+ expect(@config.servers).to eq ["example.org", "localhost"]
87
+ end
88
+
89
+ it "returns nil, when server does not exist" do
90
+ expect(@config.server("whatever")).to be_nil
91
+ end
92
+
93
+ it "reads config of one" do
94
+ server = @config.server("example.org")
95
+ expect(server.admin_id).to eq "one_admin_id"
96
+ expect(server.admin_password).to eq "one_admin_password"
97
+ expect(server.user_id).to eq "one_user_id"
98
+ expect(server.user_password).to eq "one_user_password"
99
+ expect(server.default_bucket_id).to eq "one_default_bucket"
100
+ end
101
+
102
+ it "reads config of two" do
103
+ server = @config.server("localhost")
104
+ expect(server.admin_id).to eq "two_admin_id"
105
+ expect(server.admin_password).to eq "two_admin_password"
106
+ expect(server.user_id).to eq "two_user_id"
107
+ expect(server.user_password).to eq "two_user_password"
108
+ expect(server.default_bucket_id).to eq "two_default_bucket"
109
+ end
110
+
111
+ it "reads default server" do
112
+ expect(@config.default_server).to eq "example.org"
113
+ end
114
+ end
115
+ end
116
+ end