caperoma 4.0.1 → 5.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.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/Capefile +6 -6
  3. data/Capefile.template +4 -1
  4. data/Gemfile +1 -5
  5. data/Gemfile.lock +3 -45
  6. data/HELP +84 -13
  7. data/README.md +165 -29
  8. data/Rakefile +25 -22
  9. data/VERSION +1 -1
  10. data/bin/caperoma +6 -9
  11. data/caperoma.gemspec +15 -13
  12. data/config/crontab +0 -2
  13. data/config/schedule.rb +17 -19
  14. data/config/unschedule.rb +3 -0
  15. data/images/circle.png +0 -0
  16. data/images/report.png +0 -0
  17. data/lib/caperoma.rb +308 -98
  18. data/lib/caperoma/models/account.rb +1 -1
  19. data/lib/caperoma/models/project.rb +1 -2
  20. data/lib/caperoma/models/report.rb +15 -15
  21. data/lib/caperoma/models/task.rb +203 -46
  22. data/lib/caperoma/models/tasks/chore.rb +2 -0
  23. data/lib/caperoma/models/tasks/feature.rb +1 -0
  24. data/lib/caperoma/models/tasks/fix.rb +2 -0
  25. data/lib/caperoma/models/tasks/meeting.rb +2 -0
  26. data/lib/caperoma/models/tasks/modules/git.rb +94 -15
  27. data/lib/caperoma/models/tasks/task_with_commit.rb +8 -5
  28. data/lib/caperoma/models/tasks/task_with_separate_branch.rb +6 -4
  29. data/spec/caperoma_spec.rb +558 -2
  30. data/spec/factories/accounts.rb +1 -1
  31. data/spec/factories/projects.rb +1 -1
  32. data/spec/factories/report_recipients.rb +1 -1
  33. data/spec/factories/reports.rb +1 -1
  34. data/spec/factories/tasks.rb +1 -2
  35. data/spec/features/command_unknown_spec.rb +0 -1
  36. data/spec/features/feature_spec.rb +64 -44
  37. data/spec/features/init_spec.rb +20 -0
  38. data/spec/features/status_spec.rb +12 -11
  39. data/spec/models/project_spec.rb +0 -1
  40. data/spec/models/task_spec.rb +811 -27
  41. data/spec/models/task_with_commit_spec.rb +0 -4
  42. data/spec/models/task_with_separate_branch_spec.rb +4 -4
  43. data/spec/models/three_day_report_spec.rb +2 -3
  44. data/spec/spec_helper.rb +2 -0
  45. data/spec/support/capefile_generator.rb +35 -27
  46. data/spec/support/stubs.rb +7 -74
  47. metadata +47 -24
  48. data/lib/caperoma/models/branch.rb +0 -6
  49. data/lib/caperoma/services/airbrake_email_processor.rb +0 -47
  50. data/lib/caperoma/services/pivotal_fetcher.rb +0 -108
  51. data/spec/factories/branches.rb +0 -9
  52. data/spec/models/branch_spec.rb +0 -8
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- FactoryBot.define do
3
+ FactoryGirl.define do
4
4
  factory :account do
5
5
  sequence(:email) { |n| "email#{n}@example.com" }
6
6
  sequence(:password) { |n| "password#{n}" }
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- FactoryBot.define do
3
+ FactoryGirl.define do
4
4
  factory :project do
5
5
  sequence(:name) { |n| "Refactoring some thunk of code ##{n}" }
6
6
  sequence(:jira_project_id) { |n| 123_456 + n }
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- FactoryBot.define do
3
+ FactoryGirl.define do
4
4
  factory :report_recipient do
5
5
  email 'dude@example.com'
6
6
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- FactoryBot.define do
3
+ FactoryGirl.define do
4
4
  factory :report do
5
5
  content 'did this and that'
6
6
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- FactoryBot.define do
3
+ FactoryGirl.define do
4
4
  factory :task do
5
5
  project
6
6
 
@@ -20,7 +20,6 @@ FactoryBot.define do
20
20
  end
21
21
 
22
22
  factory :task_with_commit, parent: :task, class: 'TaskWithCommit' do
23
- branch
24
23
  end
25
24
 
26
25
  factory :fix, parent: :task_with_commit, class: 'Fix' do
@@ -5,7 +5,6 @@ ENV['spec_type'] = 'feature'
5
5
 
6
6
  describe 'Command unknown' do
7
7
  let!(:project) { create :project }
8
- let!(:current_branch) { create :branch, project: project }
9
8
 
10
9
  it 'submits a chore' do
11
10
  result = `CAPEROMA_INTEGRATION_TEST=true ruby -I./lib bin/caperoma boogie woogie`
@@ -6,55 +6,75 @@ ENV['spec_type'] = 'feature'
6
6
  describe 'Feature' do
7
7
  let!(:project) { create :project, jira_project_id: '123' }
8
8
 
9
- before { create_capefile('123') }
10
-
11
- context 'pivotal id blank' do
12
- it 'submits a feature' do
13
- expect do
14
- `CAPEROMA_INTEGRATION_TEST=true ruby -I./lib bin/caperoma feature -t "awesome feature" --description "some description" `
15
- end.to change {
16
- Feature.where(
17
- title: 'awesome feature',
18
- description: 'some description',
19
- project_id: project.id
20
- ).count
21
- }.by(1)
9
+ context 'capefile with only git repo' do
10
+ before { create_incomplete_capefile }
11
+
12
+ context 'creating from data' do
13
+ it 'submits a feature' do
14
+ expect do
15
+ `CAPEROMA_INTEGRATION_TEST=true ruby -I./lib bin/caperoma feature -t "awesome feature" --description "some description" `
16
+ end.to change {
17
+ Feature.where(
18
+ title: 'awesome feature',
19
+ description: 'some description',
20
+ project_id: project.id
21
+ ).count
22
+ }.by(1)
23
+ end
22
24
  end
23
25
  end
24
26
 
25
- context 'pivotal id present' do
26
- it 'submits a feature' do
27
- expect do
28
- `CAPEROMA_INTEGRATION_TEST=true ruby -I./lib bin/caperoma feature --title "awesome feature" -d "some description" --pivotal_task_id 12345678`
29
- end.to change {
30
- Feature.where(
31
- title: 'awesome feature',
32
- description: 'some description',
33
- project_id: project.id,
34
- pivotal_id: '12345678'
35
- ).count
36
- }.by(1)
27
+ context 'full capefile' do
28
+ before { create_capefile('123') }
29
+
30
+ context 'pivotal id blank' do
31
+ it 'submits a feature' do
32
+ expect do
33
+ `CAPEROMA_INTEGRATION_TEST=true ruby -I./lib bin/caperoma feature -t "awesome feature" --description "some description" `
34
+ end.to change {
35
+ Feature.where(
36
+ title: 'awesome feature',
37
+ description: 'some description',
38
+ project_id: project.id
39
+ ).count
40
+ }.by(1)
41
+ end
37
42
  end
38
- end
39
43
 
40
- context 'pivotal id present, additional_time present', :unstub_time_now do
41
- it 'submits a feature' do
42
- expect do
43
- `CAPEROMA_INTEGRATION_TEST=true ruby -I./lib bin/caperoma feature -t "awesome feature" --description "some description" -ptid 12345678 --additional_time 23`
44
- end.to change {
45
- Feature.where(
46
- title: 'awesome feature',
47
- description: 'some description',
48
- project_id: project.id,
49
- pivotal_id: '12345678'
50
- ).count
51
- }.by(1)
52
-
53
- time = Time.now
54
- created = Feature.first.started_at
55
- time_difference = TimeDifference.between(time, created).in_minutes.to_i
56
-
57
- expect(time_difference).to eq 23
44
+ context 'pivotal id present' do
45
+ it 'submits a feature' do
46
+ expect do
47
+ `CAPEROMA_INTEGRATION_TEST=true ruby -I./lib bin/caperoma feature --title "awesome feature" -d "some description" --pivotal_task_id 12345678`
48
+ end.to change {
49
+ Feature.where(
50
+ title: 'awesome feature',
51
+ description: 'some description',
52
+ project_id: project.id,
53
+ pivotal_id: '12345678'
54
+ ).count
55
+ }.by(1)
56
+ end
57
+ end
58
+
59
+ context 'pivotal id present, additional_time present', :unstub_time_now do
60
+ it 'submits a feature' do
61
+ expect do
62
+ `CAPEROMA_INTEGRATION_TEST=true ruby -I./lib bin/caperoma feature -t "awesome feature" --description "some description" -ptid 12345678 --additional_time 23`
63
+ end.to change {
64
+ Feature.where(
65
+ title: 'awesome feature',
66
+ description: 'some description',
67
+ project_id: project.id,
68
+ pivotal_id: '12345678'
69
+ ).count
70
+ }.by(1)
71
+
72
+ time = Time.now
73
+ created = Feature.first.started_at
74
+ time_difference = TimeDifference.between(time, created).in_minutes.to_i
75
+
76
+ expect(time_difference).to eq 23
77
+ end
58
78
  end
59
79
  end
60
80
  end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
4
+ ENV['spec_type'] = 'feature'
5
+
6
+ describe 'Init' do
7
+ context 'project not set up' do
8
+ before { remove_capefile }
9
+
10
+ it 'should say Capefile is created' do
11
+ result = `CAPEROMA_INTEGRATION_TEST=true ruby -I./lib bin/caperoma init`
12
+ expect(result).to match /Capefile successfully created/
13
+ end
14
+
15
+ it 'should create Capefile' do
16
+ result = `CAPEROMA_INTEGRATION_TEST=true ruby -I./lib bin/caperoma init`
17
+ expect(File).to exist 'Capefile.test'
18
+ end
19
+ end
20
+ end
@@ -4,30 +4,31 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
4
4
  ENV['spec_type'] = 'feature'
5
5
 
6
6
  describe 'Status' do
7
-
8
- context "not working on anything" do
9
- it "should say I am not working" do
7
+ context 'not working on anything' do
8
+ it 'should say I am not working' do
10
9
  result = `CAPEROMA_INTEGRATION_TEST=true ruby -I./lib bin/caperoma status`
11
10
  expect(result).to eq "You are not working on anything now.\n"
12
11
  end
13
12
  end
14
13
 
15
- context "working on a feature", :unstub_time_now do
14
+ context 'working on a feature', :unstub_time_now do
16
15
  let!(:account) { create :account }
17
- let!(:project) { create :project }
18
- let!(:task) { create :feature, project: project, title: 'my title', jira_key: 'PBO-2', pivotal_id: 12345678, finished_at: nil }
16
+ let!(:project) { create :project, jira_url: 'https://test.atlassian.com/' }
17
+ let!(:task) { create :feature, project: project, title: 'my title', jira_key: 'PBO-2', pivotal_id: 12_345_678, finished_at: nil }
19
18
 
20
19
  before { task.update_column :started_at, 2.hours.ago }
21
- before { task.update_column :parent_branch, 'master' }
20
+ before { task.update_column :branch, 'pbo-2-my-title' }
21
+ before { task.update_column :parent_branch, 'develop' }
22
22
 
23
- it "should say I am not working" do
23
+ it 'should say I am not working' do
24
24
  result = `CAPEROMA_INTEGRATION_TEST=true ruby -I./lib bin/caperoma status`
25
25
  expect(result).to match /Type: Feature/
26
26
  expect(result).to match /Title: my title/
27
- expect(result).to match /Jira ID: PBO-2/
28
- expect(result).to match /Pivotal ID: 12345678/
27
+ expect(result).to match %r{Jira ID: PBO-2 \(https://test.atlassian.com/browse/PBO-2\)}
28
+ expect(result).to match %r{Pivotal ID: 12345678 \(https://www.pivotaltracker.com/story/show/12345678\)}
29
+ expect(result).to match /Branch with the task: pbo-2-my-title/
29
30
  expect(result).to match /Time spent at the moment: 2h/
30
- expect(result).to match /Pull request will be sent to this branch: master/
31
+ expect(result).to match /Pull request will be sent to this branch: develop/
31
32
  end
32
33
  end
33
34
  end
@@ -3,7 +3,6 @@
3
3
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
4
4
 
5
5
  RSpec.describe Project, type: :model do
6
- it { expect(subject).to have_many :branches }
7
6
  it { expect(subject).to have_many :chores }
8
7
  it { expect(subject).to have_many :bugs }
9
8
  it { expect(subject).to have_many :features }
@@ -13,7 +13,7 @@ RSpec.describe Task, type: :model do
13
13
  describe 'validations' do
14
14
  it { expect(subject).to validate_presence_of(:title) }
15
15
 
16
- describe 'length' do
16
+ describe 'pivotal_id' do
17
17
  it { expect(subject).to allow_value(nil).for(:pivotal_id) }
18
18
  it { expect(subject).to allow_value('123456').for(:pivotal_id) }
19
19
  it { expect(subject).to allow_value('1234567').for(:pivotal_id) }
@@ -23,6 +23,14 @@ RSpec.describe Task, type: :model do
23
23
  it { expect(subject).not_to allow_value('12').for(:pivotal_id) }
24
24
  it { expect(subject).not_to allow_value('123.45678').for(:pivotal_id) }
25
25
  end
26
+
27
+ describe 'additional_time' do
28
+ it { expect(subject).to allow_value(nil).for(:additional_time) }
29
+ it { expect(subject).to allow_value('12').for(:additional_time) }
30
+ it { expect(subject).to allow_value('123456').for(:additional_time) }
31
+ it { expect(subject).not_to allow_value('#12345678').for(:additional_time) }
32
+ it { expect(subject).not_to allow_value('123.45678').for(:additional_time) }
33
+ end
26
34
  end
27
35
 
28
36
  describe 'scopes' do
@@ -46,6 +54,12 @@ RSpec.describe Task, type: :model do
46
54
  end
47
55
 
48
56
  describe 'class_methods' do
57
+ let!(:account) { create :account, type: '--jira' }
58
+ let(:faraday) { double('Faraday', post: response) }
59
+ let(:response) { double('Faraday', body: JIRA_ISSUE_CREATION_RESPONSE, status: 200) }
60
+
61
+ before { expect(Faraday).to receive(:new).and_return faraday }
62
+
49
63
  describe '::finish_started' do
50
64
  let!(:started) { create :task, finished_at: nil }
51
65
 
@@ -88,8 +102,11 @@ RSpec.describe Task, type: :model do
88
102
  end
89
103
 
90
104
  describe 'methods' do
105
+ let!(:jira_account) { create :account, type: '--jira' }
106
+ let!(:pivotal_account) { create :account, type: '--pivotal' }
107
+
91
108
  context 'pivotal_id present' do
92
- let!(:task) { create :task, finished_at: nil, pivotal_id: '12345678' }
109
+ let!(:task) { create :task, finished_at: nil, pivotal_id: '12345678', additional_time: 5 }
93
110
  describe '#finish' do
94
111
  it 'should finish it and log time' do
95
112
  expect(task).to receive :close_issue_on_jira
@@ -132,7 +149,7 @@ RSpec.describe Task, type: :model do
132
149
  end
133
150
 
134
151
  context 'pivotal_id blank' do
135
- let!(:task) { create :task, finished_at: nil, pivotal_id: nil }
152
+ let!(:task) { create :task, finished_at: nil, pivotal_id: nil, additional_time: 5 }
136
153
  describe '#finish' do
137
154
  it 'should finish it and log time' do
138
155
  expect(task).to receive :close_issue_on_jira
@@ -216,7 +233,7 @@ RSpec.describe Task, type: :model do
216
233
  let(:task) { create :task }
217
234
  let!(:account) { create :account, type: '--jira' }
218
235
  let(:faraday) { double('Faraday', post: response) }
219
- let(:response) { double('Faraday', body: JIRA_ISSUE_CREATION_RESPONSE) }
236
+ let(:response) { double('Faraday', body: JIRA_ISSUE_CREATION_RESPONSE, status: 200) }
220
237
 
221
238
  it 'should create task in Jira after create' do
222
239
  expect(Faraday).to receive(:new).and_return faraday
@@ -231,10 +248,168 @@ RSpec.describe Task, type: :model do
231
248
  end
232
249
  end
233
250
 
251
+ context 'jira account present but no internet connection' do
252
+ let(:task) { create :task }
253
+ let!(:account) { create :account, type: '--jira' }
254
+ let(:faraday) { double('Faraday', post: response) }
255
+ let(:response) { double('Faraday', body: JIRA_ISSUE_CREATION_RESPONSE, status: 200) }
256
+
257
+ before do
258
+ expect(Faraday).to receive(:new).and_return faraday
259
+ allow(faraday).to receive(:post).and_raise Faraday::ConnectionFailed, [404]
260
+ allow(faraday).to receive(:get).and_raise Faraday::ConnectionFailed, [404]
261
+ allow(faraday).to receive(:put).and_raise Faraday::ConnectionFailed, [404]
262
+ end
263
+
264
+ it 'give an error that there is no connection' do
265
+ expect(STDOUT).to receive(:puts).with /Connection failed. Performing the task without requests to Jira./
266
+ task.save
267
+ end
268
+
269
+ it 'should still create the task' do
270
+ expect do
271
+ task.save
272
+ end.to change {
273
+ Task.count
274
+ }.by(1)
275
+ end
276
+ end
277
+
278
+ context 'jira account present but the returned status was "forbidden"' do
279
+ let(:task) { create :task, jira_key: nil }
280
+ let!(:account) { create :account, type: '--jira' }
281
+ let(:faraday) { double('Faraday', post: response) }
282
+ let(:response) { double('Faraday', body: JIRA_ISSUE_CREATION_RESPONSE, status: 401, reason_phrase: 'not authorized') }
283
+
284
+ before do
285
+ expect(Faraday).to receive(:new).and_return faraday
286
+ allow(faraday).to receive(:post).and_return response
287
+ allow(faraday).to receive(:get).and_return response
288
+ allow(faraday).to receive(:put).and_return response
289
+ end
290
+
291
+ it 'give an error that there is no connection' do
292
+ expect(STDOUT).to receive(:puts).with /Forbidden access/
293
+ task.save
294
+ end
295
+
296
+ it 'should still create the task' do
297
+ expect do
298
+ task.save
299
+ end.to change {
300
+ Task.count
301
+ }.by(1)
302
+ end
303
+
304
+ it 'should keep jira_key blank' do
305
+ task.save
306
+ expect(task.reload.jira_key).to be_nil
307
+ end
308
+ end
309
+
310
+ context 'jira account present but the returned status was "forbidden"' do
311
+ let(:task) { create :task, jira_key: nil }
312
+ let!(:account) { create :account, type: '--jira' }
313
+ let(:faraday) { double('Faraday', post: response) }
314
+ let(:response) { double('Faraday', body: JIRA_ISSUE_CREATION_RESPONSE, status: 403, reason_phrase: 'not authorized') }
315
+
316
+ before do
317
+ expect(Faraday).to receive(:new).and_return faraday
318
+ allow(faraday).to receive(:post).and_return response
319
+ allow(faraday).to receive(:get).and_return response
320
+ allow(faraday).to receive(:put).and_return response
321
+ end
322
+
323
+ it 'give an error that there is no connection' do
324
+ expect(STDOUT).to receive(:puts).with /Forbidden access/
325
+ task.save
326
+ end
327
+
328
+ it 'should still create the task' do
329
+ expect do
330
+ task.save
331
+ end.to change {
332
+ Task.count
333
+ }.by(1)
334
+ end
335
+
336
+ it 'should keep jira_key blank' do
337
+ task.save
338
+ expect(task.reload.jira_key).to be_nil
339
+ end
340
+ end
341
+
342
+ context 'jira account present but the returned status was "not found"' do
343
+ let(:task) { create :task, jira_key: nil }
344
+ let!(:account) { create :account, type: '--jira' }
345
+ let(:faraday) { double('Faraday', post: response) }
346
+ let(:response) { double('Faraday', body: JIRA_ISSUE_CREATION_RESPONSE, status: 404, reason_phrase: 'not found') }
347
+
348
+ before do
349
+ expect(Faraday).to receive(:new).and_return faraday
350
+ allow(faraday).to receive(:post).and_return response
351
+ allow(faraday).to receive(:get).and_return response
352
+ allow(faraday).to receive(:put).and_return response
353
+ end
354
+
355
+ it 'give an error that there is no connection' do
356
+ expect(STDOUT).to receive(:puts).with /Not found/
357
+ task.save
358
+ end
359
+
360
+ it 'should still create the task' do
361
+ expect do
362
+ task.save
363
+ end.to change {
364
+ Task.count
365
+ }.by(1)
366
+ end
367
+
368
+ it 'should keep jira_key blank' do
369
+ task.save
370
+ expect(task.reload.jira_key).to be_nil
371
+ end
372
+ end
373
+
374
+ context 'jira account present but the returned status was unknown error' do
375
+ let(:task) { create :task, jira_key: nil }
376
+ let!(:account) { create :account, type: '--jira' }
377
+ let(:faraday) { double('Faraday', post: response) }
378
+ let(:response) { double('Faraday', body: JIRA_ISSUE_CREATION_RESPONSE, status: 500, reason_phrase: 'server error') }
379
+
380
+ before do
381
+ expect(Faraday).to receive(:new).and_return faraday
382
+ allow(faraday).to receive(:post).and_return response
383
+ allow(faraday).to receive(:get).and_return response
384
+ allow(faraday).to receive(:put).and_return response
385
+ end
386
+
387
+ it 'give an error that there is no connection' do
388
+ expect(STDOUT).to receive(:puts).with /Could not/
389
+ expect(STDOUT).to receive(:puts).with /500/
390
+ expect(STDOUT).to receive(:puts).with /server error/
391
+
392
+ task.save
393
+ end
394
+
395
+ it 'should still create the task' do
396
+ expect do
397
+ task.save
398
+ end.to change {
399
+ Task.count
400
+ }.by(1)
401
+ end
402
+
403
+ it 'should keep jira_key blank' do
404
+ task.save
405
+ expect(task.reload.jira_key).to be_nil
406
+ end
407
+ end
408
+
234
409
  context 'jira account not present' do
235
410
  let(:task) { create :task }
236
411
  let(:faraday) { double('Faraday', post: response) }
237
- let(:response) { double('Faraday', body: JIRA_ISSUE_CREATION_RESPONSE) }
412
+ let(:response) { double('Faraday', body: JIRA_ISSUE_CREATION_RESPONSE, status: 200) }
238
413
 
239
414
  it 'should not create task in Jira after create' do
240
415
  allow(Faraday).to receive(:new).and_return faraday
@@ -259,6 +434,83 @@ RSpec.describe Task, type: :model do
259
434
  end
260
435
  end
261
436
 
437
+ context 'jira id present, account present, no internet connection' do
438
+ let!(:account) { create :account, type: '--jira' }
439
+ let!(:jira_key) { 'OK-1' }
440
+ let(:faraday) { double('Faraday', post: response) }
441
+ let(:response) { double('Faraday', body: JIRA_ISSUE_CREATION_RESPONSE, status: 200) }
442
+
443
+ before do
444
+ allow(Faraday).to receive(:new).and_return faraday
445
+ allow(faraday).to receive(:post).and_raise Faraday::ConnectionFailed, [404]
446
+ allow(faraday).to receive(:get).and_raise Faraday::ConnectionFailed, [404]
447
+ allow(faraday).to receive(:put).and_raise Faraday::ConnectionFailed, [404]
448
+ end
449
+
450
+ it 'should give the no connection error', :unstub_jira_starting do
451
+ expect(STDOUT).to receive(:puts).with /Connection failed. Performing the task without requests to Jira./
452
+ task.save
453
+ end
454
+ end
455
+
456
+ describe 'error codes', :unstub_jira_starting do
457
+ let!(:account) { create :account, type: '--jira' }
458
+ let!(:jira_key) { 'OK-1' }
459
+ let(:faraday) { double('Faraday', post: response) }
460
+ let(:response) { double('Faraday', body: JIRA_ISSUE_CREATION_RESPONSE, status: status, reason_phrase: reason_phrase) }
461
+ let(:status) { 200 }
462
+ let(:reason_phrase) { 'OK' }
463
+
464
+ before do
465
+ allow(Faraday).to receive(:new).and_return faraday
466
+ allow(faraday).to receive(:post).and_return response
467
+ allow(faraday).to receive(:get).and_return response
468
+ allow(faraday).to receive(:put).and_return response
469
+ end
470
+
471
+ context 'jira id present, account present, but server gave "unouthorized" error' do
472
+ let(:status) { 401 }
473
+ let(:reason_phrase) { 'unauthorized' }
474
+
475
+ it 'should say it could not start' do
476
+ expect(STDOUT).to receive(:puts).with /No access/
477
+ task.save
478
+ end
479
+ end
480
+
481
+ context 'jira id present, account present, but server gave "unouthorized" error' do
482
+ let(:status) { 403 }
483
+ let(:reason_phrase) { 'unauthorized' }
484
+
485
+ it 'should say it could not start' do
486
+ expect(STDOUT).to receive(:puts).with /No access/
487
+ task.save
488
+ end
489
+ end
490
+
491
+ context 'jira id present, account present, but server gave "not found" error' do
492
+ let(:status) { 404 }
493
+ let(:reason_phrase) { 'not found' }
494
+
495
+ it 'should say it could not start' do
496
+ expect(STDOUT).to receive(:puts).with /not found/
497
+ task.save
498
+ end
499
+ end
500
+
501
+ context 'jira id present, account present, but server gave "unknown" error' do
502
+ let(:status) { 500 }
503
+ let(:reason_phrase) { 'server error' }
504
+
505
+ it 'should say it could not start' do
506
+ expect(STDOUT).to receive(:puts).with /Could not/
507
+ expect(STDOUT).to receive(:puts).with /500/
508
+ expect(STDOUT).to receive(:puts).with /server error/
509
+ task.save
510
+ end
511
+ end
512
+ end
513
+
262
514
  context 'account not present, jira id not present' do
263
515
  let!(:jira_key) { 'OK-1' }
264
516
 
@@ -296,7 +548,9 @@ RSpec.describe Task, type: :model do
296
548
  let(:task) { build :task, pivotal_id: pt_id }
297
549
  let!(:account) { create :account, type: '--pivotal' }
298
550
  let(:faraday) { double('Faraday', post: response) }
299
- let(:response) { double('Faraday', body: PIVOTAL_ISSUE_CREATION_RESPONSE) }
551
+ let(:response) { double('Faraday', body: PIVOTAL_ISSUE_CREATION_RESPONSE, status: status, reason_phrase: reason_phrase) }
552
+ let(:status) { 200 }
553
+ let(:reason_phrase) { 'OK' }
300
554
 
301
555
  before { allow(task).to receive(:this_is_a_type_a_user_wants_to_create?).and_return(should_create) }
302
556
 
@@ -313,7 +567,7 @@ RSpec.describe Task, type: :model do
313
567
  end
314
568
  end
315
569
 
316
- context 'PT id present but should create' do
570
+ context 'PT id present and should create' do
317
571
  let(:pt_id) { '567890123' }
318
572
  let(:should_create) { true }
319
573
 
@@ -326,6 +580,39 @@ RSpec.describe Task, type: :model do
326
580
  end
327
581
  end
328
582
 
583
+ context 'PT id present and should create but there is no internet connection' do
584
+ let(:pt_id) { nil }
585
+ let(:should_create) { true }
586
+
587
+ before do
588
+ allow(Faraday).to receive(:new).and_return faraday
589
+ allow(faraday).to receive(:post).and_raise Faraday::ConnectionFailed, [404]
590
+ allow(faraday).to receive(:get).and_raise Faraday::ConnectionFailed, [404]
591
+ allow(faraday).to receive(:put).and_raise Faraday::ConnectionFailed, [404]
592
+ end
593
+
594
+ it 'should still be nil' do
595
+ task.save
596
+
597
+ task.reload.tap do |task|
598
+ expect(task.pivotal_id).to be_blank
599
+ end
600
+ end
601
+
602
+ it 'should say there is no connection' do
603
+ expect(STDOUT).to receive(:puts).with /Connection failed. Performing the task without requests to Pivotal./
604
+ task.save
605
+ end
606
+
607
+ it 'should still create the task' do
608
+ expect do
609
+ task.save
610
+ end.to change {
611
+ Task.count
612
+ }.by(1)
613
+ end
614
+ end
615
+
329
616
  context 'PT id blank but should not create' do
330
617
  let(:pt_id) { nil }
331
618
  let(:should_create) { false }
@@ -352,12 +639,154 @@ RSpec.describe Task, type: :model do
352
639
  end
353
640
  end
354
641
  end
642
+
643
+ context 'PT id blank and should create but got "unauthorized error' do
644
+ let(:pt_id) { nil }
645
+ let(:should_create) { true }
646
+ let(:status) { 401 }
647
+ let(:reason_phrase) { 'unauthorized' }
648
+
649
+ before do
650
+ allow(Faraday).to receive(:new).and_return faraday
651
+ allow(faraday).to receive(:post).and_return response
652
+ allow(faraday).to receive(:get).and_return response
653
+ allow(faraday).to receive(:put).and_return response
654
+ end
655
+
656
+ it 'should create task in Pivotal' do
657
+ expect do
658
+ task.save
659
+ end.to change {
660
+ Task.count
661
+ }.by(1)
662
+ end
663
+
664
+ it 'should keep pivotal id blank' do
665
+ task.save
666
+ task.reload.tap do |task|
667
+ expect(task.pivotal_id).to eq nil
668
+ end
669
+ end
670
+
671
+ it 'should say it got an error' do
672
+ expect(STDOUT).to receive(:puts).with /No access/
673
+
674
+ task.save
675
+ end
676
+ end
677
+
678
+ context 'PT id blank and should create but got "unauthorized error' do
679
+ let(:pt_id) { nil }
680
+ let(:should_create) { true }
681
+ let(:status) { 403 }
682
+ let(:reason_phrase) { 'unauthorized' }
683
+
684
+ before do
685
+ allow(Faraday).to receive(:new).and_return faraday
686
+ allow(faraday).to receive(:post).and_return response
687
+ allow(faraday).to receive(:get).and_return response
688
+ allow(faraday).to receive(:put).and_return response
689
+ end
690
+
691
+ it 'should create task in Pivotal' do
692
+ expect do
693
+ task.save
694
+ end.to change {
695
+ Task.count
696
+ }.by(1)
697
+ end
698
+
699
+ it 'should keep pivotal id blank' do
700
+ task.save
701
+ task.reload.tap do |task|
702
+ expect(task.pivotal_id).to eq nil
703
+ end
704
+ end
705
+
706
+ it 'should say it got an error' do
707
+ expect(STDOUT).to receive(:puts).with /No access/
708
+
709
+ task.save
710
+ end
711
+ end
712
+
713
+ context 'PT id blank and should create but got "not found" error' do
714
+ let(:pt_id) { nil }
715
+ let(:should_create) { true }
716
+ let(:status) { 404 }
717
+ let(:reason_phrase) { 'unauthorized' }
718
+
719
+ before do
720
+ allow(Faraday).to receive(:new).and_return faraday
721
+ allow(faraday).to receive(:post).and_return response
722
+ allow(faraday).to receive(:get).and_return response
723
+ allow(faraday).to receive(:put).and_return response
724
+ end
725
+
726
+ it 'should create task in Pivotal' do
727
+ expect do
728
+ task.save
729
+ end.to change {
730
+ Task.count
731
+ }.by(1)
732
+ end
733
+
734
+ it 'should keep pivotal id blank' do
735
+ task.save
736
+ task.reload.tap do |task|
737
+ expect(task.pivotal_id).to eq nil
738
+ end
739
+ end
740
+
741
+ it 'should say it got an error' do
742
+ expect(STDOUT).to receive(:puts).with /not found/
743
+
744
+ task.save
745
+ end
746
+ end
747
+
748
+ context 'PT id blank and should create but got "some other" error' do
749
+ let(:pt_id) { nil }
750
+ let(:should_create) { true }
751
+ let(:status) { 500 }
752
+ let(:reason_phrase) { 'server error' }
753
+
754
+ before do
755
+ allow(Faraday).to receive(:new).and_return faraday
756
+ allow(faraday).to receive(:post).and_return response
757
+ allow(faraday).to receive(:get).and_return response
758
+ allow(faraday).to receive(:put).and_return response
759
+ end
760
+
761
+ it 'should create task in Pivotal' do
762
+ expect do
763
+ task.save
764
+ end.to change {
765
+ Task.count
766
+ }.by(1)
767
+ end
768
+
769
+ it 'should keep pivotal id blank' do
770
+ task.save
771
+ task.reload.tap do |task|
772
+ expect(task.pivotal_id).to eq nil
773
+ end
774
+ end
775
+
776
+ it 'should say it got an error' do
777
+ expect(STDOUT).to receive(:puts).with /Could not/
778
+ expect(STDOUT).to receive(:puts).with /500/
779
+ expect(STDOUT).to receive(:puts).with /server error/
780
+
781
+ task.save
782
+ end
783
+ end
355
784
  end
356
785
 
357
786
  context 'pivotal account blank' do
358
787
  let(:task) { build :task, pivotal_id: pt_id }
359
788
  let(:faraday) { double('Faraday', post: response) }
360
- let(:response) { double('Faraday', body: PIVOTAL_ISSUE_CREATION_RESPONSE) }
789
+ let(:response) { double('Faraday', body: PIVOTAL_ISSUE_CREATION_RESPONSE, status: 200) }
361
790
 
362
791
  before { allow(task).to receive(:this_is_a_type_a_user_wants_to_create?).and_return(should_create) }
363
792
 
@@ -418,7 +847,6 @@ RSpec.describe Task, type: :model do
418
847
 
419
848
  describe '::start_issue_on_pivotal' do
420
849
  let(:task) { build :task, pivotal_id: pivotal_id }
421
- # before { allow(task).to receive(:this_is_a_type_a_user_wants_to_create?).and_return should_create_this_type }
422
850
 
423
851
  context 'pt id present, pt account present' do
424
852
  let(:pivotal_id) { '12345678' }
@@ -431,6 +859,83 @@ RSpec.describe Task, type: :model do
431
859
  end
432
860
  end
433
861
 
862
+ context 'pt id present, pt account present, no internet connection' do
863
+ let(:pivotal_id) { '12345678' }
864
+ let!(:account) { create :account, type: '--pivotal' }
865
+ let(:faraday) { double('Faraday', post: response) }
866
+ let(:response) { double('Faraday', body: PIVOTAL_ISSUE_CREATION_RESPONSE, status: 200) }
867
+
868
+ before do
869
+ allow(Faraday).to receive(:new).and_return faraday
870
+ allow(faraday).to receive(:post).and_raise Faraday::ConnectionFailed, [404]
871
+ allow(faraday).to receive(:get).and_raise Faraday::ConnectionFailed, [404]
872
+ allow(faraday).to receive(:put).and_raise Faraday::ConnectionFailed, [404]
873
+ end
874
+
875
+ it 'should give the no connection error', :unstub_pivotal_starting do
876
+ expect(STDOUT).to receive(:puts).with /Connection failed. Performing the task without requests to Pivotal./
877
+ task.save
878
+ end
879
+ end
880
+
881
+ describe 'error codes', :unstub_pivotal_starting do
882
+ let(:pivotal_id) { '12345678' }
883
+ let!(:account) { create :account, type: '--pivotal' }
884
+ let(:faraday) { double('Faraday', post: response) }
885
+ let(:response) { double('Faraday', body: PIVOTAL_ISSUE_CREATION_RESPONSE, status: status, reason_phrase: reason_phrase) }
886
+ let(:status) { 200 }
887
+ let(:reason_phrase) { 'OK' }
888
+
889
+ before do
890
+ allow(Faraday).to receive(:new).and_return faraday
891
+ allow(faraday).to receive(:post).and_return response
892
+ allow(faraday).to receive(:get).and_return response
893
+ allow(faraday).to receive(:put).and_return response
894
+ end
895
+
896
+ context 'pt id present, pt account present, "unauthorized" server error' do
897
+ let(:status) { 401 }
898
+ let(:reason_phrase) { 'unauth' }
899
+
900
+ it 'should give the no auth error' do
901
+ expect(STDOUT).to receive(:puts).with /No access/
902
+ task.save
903
+ end
904
+ end
905
+
906
+ context 'pt id present, pt account present, "unauthorized" server error' do
907
+ let(:status) { 403 }
908
+ let(:reason_phrase) { 'unauth' }
909
+
910
+ it 'should give the no auth error' do
911
+ expect(STDOUT).to receive(:puts).with /No access/
912
+ task.save
913
+ end
914
+ end
915
+
916
+ context 'pt id present, pt account present, "not found" server error' do
917
+ let(:status) { 404 }
918
+ let(:reason_phrase) { 'not found' }
919
+
920
+ it 'should give the not found error' do
921
+ expect(STDOUT).to receive(:puts).with /not found/
922
+ task.save
923
+ end
924
+ end
925
+
926
+ context 'pt id present, pt account present, "unknown" server error' do
927
+ let(:status) { 500 }
928
+ let(:reason_phrase) { 'error' }
929
+
930
+ it 'should give the error' do
931
+ expect(STDOUT).to receive(:puts).with /Could not/
932
+ expect(STDOUT).to receive(:puts).with /500/
933
+ expect(STDOUT).to receive(:puts).with /error/
934
+ task.save
935
+ end
936
+ end
937
+ end
938
+
434
939
  context 'pt id blank, pt account present' do
435
940
  let(:pivotal_id) { nil }
436
941
  let!(:account) { create :account, type: '--pivotal' }
@@ -491,16 +996,38 @@ RSpec.describe Task, type: :model do
491
996
  let!(:account) { create :account, type: '--jira', username: 'someuser' }
492
997
 
493
998
  describe '#create_issue_on_jira_data' do
494
- let!(:task) { create :task, title: 'dupis', description: 'bamis', project: project }
999
+ let!(:task) { create :task, title: 'dupis', description: description, project: project }
1000
+
1001
+ context 'description present' do
1002
+ let(:description) { 'bamis' }
1003
+
1004
+ it 'should format hash' do
1005
+ allow(task).to receive(:issue_type).and_return '492'
1006
+ result = task.send(:create_issue_on_jira_data)
1007
+ JSON.parse(result).tap do |format|
1008
+ expect(format['fields']['project']['id']).to eq '123'
1009
+ expect(format['fields']['issuetype']['id']).to eq '492'
1010
+ expect(format['fields']['summary']).to eq 'dupis'
1011
+ expect(format['fields']['description']['content'][0]['content'][0]['text']).to eq 'bamis'
1012
+ expect(format['fields']['assignee']['name']).to eq 'someuser'
1013
+ end
1014
+ end
1015
+ end
495
1016
 
496
- it 'should format hash' do
497
- allow(task).to receive(:issue_type).and_return '492'
498
- result = task.send(:create_issue_on_jira_data)
499
- JSON.parse(result).tap do |format|
500
- expect(format['fields']['project']['id']).to eq '123'
501
- expect(format['fields']['issuetype']['id']).to eq '492'
502
- expect(format['fields']['summary']).to eq 'dupis'
503
- expect(format['fields']['assignee']['name']).to eq 'someuser'
1017
+ context 'description not' do
1018
+ let(:description) { nil }
1019
+
1020
+ it 'should format hash' do
1021
+ allow(task).to receive(:issue_type).and_return '492'
1022
+ result = task.send(:create_issue_on_jira_data)
1023
+ JSON.parse(result).tap do |format|
1024
+ expect(format['fields']['project']['id']).to eq '123'
1025
+ expect(format['fields']['issuetype']['id']).to eq '492'
1026
+ expect(format['fields']['summary']).to eq 'dupis'
1027
+ expect(format['fields']['assignee']['name']).to eq 'someuser'
1028
+
1029
+ expect(format['fields']['description']).to be_nil
1030
+ end
504
1031
  end
505
1032
  end
506
1033
  end
@@ -531,6 +1058,82 @@ RSpec.describe Task, type: :model do
531
1058
  end
532
1059
  end
533
1060
 
1061
+ describe '::close_issue_on_jira' do
1062
+ let(:task) { build :task, jira_key: jira_key }
1063
+
1064
+ let!(:account) { create :account, type: '--jira' }
1065
+ let!(:jira_key) { 'OK-1' }
1066
+ let(:faraday) { double('Faraday', post: response) }
1067
+ let(:response) { double('Faraday', body: JIRA_ISSUE_CREATION_RESPONSE, status: status, reason_phrase: reason_phrase) }
1068
+ let(:status) { 200 }
1069
+ let(:reason_phrase) { 'OK' }
1070
+
1071
+ before { allow(Faraday).to receive(:new).and_return faraday }
1072
+
1073
+ context 'no internet connection' do
1074
+ before do
1075
+ allow(faraday).to receive(:post).and_raise Faraday::ConnectionFailed, [404]
1076
+ allow(faraday).to receive(:get).and_raise Faraday::ConnectionFailed, [404]
1077
+ allow(faraday).to receive(:put).and_raise Faraday::ConnectionFailed, [404]
1078
+ end
1079
+
1080
+ it 'should give the no connection error' do
1081
+ expect(STDOUT).to receive(:puts).with /Connection failed. Performing the task without requests to Jira./
1082
+ task.send(:close_issue_on_jira)
1083
+ end
1084
+ end
1085
+
1086
+ describe 'error codes', :unstub_jira_starting do
1087
+ before do
1088
+ allow(faraday).to receive(:post).and_return response
1089
+ allow(faraday).to receive(:get).and_return response
1090
+ allow(faraday).to receive(:put).and_return response
1091
+ end
1092
+
1093
+ context 'server gave "unouthorized" error' do
1094
+ let(:status) { 401 }
1095
+ let(:reason_phrase) { 'unauthorized' }
1096
+
1097
+ it 'should say it could not start' do
1098
+ expect(STDOUT).to receive(:puts).with /No access/
1099
+ task.send(:close_issue_on_jira)
1100
+ end
1101
+ end
1102
+
1103
+ context 'server gave "unouthorized" error' do
1104
+ let(:status) { 403 }
1105
+ let(:reason_phrase) { 'unauthorized' }
1106
+
1107
+ it 'should say it could not start' do
1108
+ expect(STDOUT).to receive(:puts).with /No access/
1109
+ task.send(:close_issue_on_jira)
1110
+ end
1111
+ end
1112
+
1113
+ context 'server gave "not found" error' do
1114
+ let(:status) { 404 }
1115
+ let(:reason_phrase) { 'not found' }
1116
+
1117
+ it 'should say it could not start' do
1118
+ expect(STDOUT).to receive(:puts).with /not found/
1119
+ task.send(:close_issue_on_jira)
1120
+ end
1121
+ end
1122
+
1123
+ context 'server gave "unknown" error' do
1124
+ let(:status) { 500 }
1125
+ let(:reason_phrase) { 'server error' }
1126
+
1127
+ it 'should say it could not finish' do
1128
+ expect(STDOUT).to receive(:puts).with /Could not/
1129
+ expect(STDOUT).to receive(:puts).with /500/
1130
+ expect(STDOUT).to receive(:puts).with /server error/
1131
+ task.send(:close_issue_on_jira)
1132
+ end
1133
+ end
1134
+ end
1135
+ end
1136
+
534
1137
  describe '#log_work_to_jira_data' do
535
1138
  let!(:task) { create :task }
536
1139
 
@@ -547,17 +1150,123 @@ RSpec.describe Task, type: :model do
547
1150
  end
548
1151
  end
549
1152
 
1153
+ describe '#log_work_to_jira' do
1154
+ let(:task) { build :task, jira_key: jira_key }
1155
+
1156
+ context 'no internet connection' do
1157
+ let!(:account) { create :account, type: '--jira' }
1158
+ let(:jira_key) { 'OK-1' }
1159
+ let(:faraday) { double('Faraday', post: response) }
1160
+ let(:response) { double('Faraday', body: JIRA_ISSUE_CREATION_RESPONSE, status: 200) }
1161
+
1162
+ before do
1163
+ allow(Faraday).to receive(:new).and_return faraday
1164
+ allow(faraday).to receive(:post).and_raise Faraday::ConnectionFailed, [404]
1165
+ allow(faraday).to receive(:get).and_raise Faraday::ConnectionFailed, [404]
1166
+ allow(faraday).to receive(:put).and_raise Faraday::ConnectionFailed, [404]
1167
+ end
1168
+
1169
+ it 'should give the no connection error', :unstub_jira_starting do
1170
+ task.save
1171
+ expect(STDOUT).to receive(:puts).with /Connection failed. Performing the task without requests to Jira./
1172
+ task.send(:log_work_to_jira)
1173
+ end
1174
+ end
1175
+
1176
+ describe 'errors', :unstub_jira_starting do
1177
+ let!(:account) { create :account, type: '--jira' }
1178
+ let(:jira_key) { 'OK-1' }
1179
+ let(:faraday) { double('Faraday', post: response) }
1180
+ let(:response) { double('Faraday', body: JIRA_ISSUE_CREATION_RESPONSE, status: status, reason_phrase: reason_phrase) }
1181
+ let(:status) { 200 }
1182
+ let(:reason_phrase) { 'OK' }
1183
+
1184
+ before do
1185
+ allow(Faraday).to receive(:new).and_return faraday
1186
+ allow(faraday).to receive(:post).and_return response
1187
+ allow(faraday).to receive(:get).and_return response
1188
+ allow(faraday).to receive(:put).and_return response
1189
+ end
1190
+
1191
+ context 'server gave the "unauth" error' do
1192
+ let(:status) { 401 }
1193
+ let(:reason_phrase) { 'unauth' }
1194
+
1195
+ it 'should give the "unauth" error' do
1196
+ task.save
1197
+ expect(STDOUT).to receive(:puts).with /No access/
1198
+ task.send(:log_work_to_jira)
1199
+ end
1200
+ end
1201
+
1202
+ context 'server gave the "unauth" error' do
1203
+ let(:status) { 403 }
1204
+ let(:reason_phrase) { 'unauth' }
1205
+
1206
+ it 'should give the "unauth" error' do
1207
+ task.save
1208
+ expect(STDOUT).to receive(:puts).with /No access/
1209
+ task.send(:log_work_to_jira)
1210
+ end
1211
+ end
1212
+
1213
+ context 'server gave the "not found" error' do
1214
+ let(:status) { 404 }
1215
+ let(:reason_phrase) { 'not found' }
1216
+
1217
+ it 'should give the "unauth" error' do
1218
+ task.save
1219
+ expect(STDOUT).to receive(:puts).with /not found/
1220
+ task.send(:log_work_to_jira)
1221
+ end
1222
+ end
1223
+
1224
+ context 'server gave the "unknown" error' do
1225
+ let(:status) { 500 }
1226
+ let(:reason_phrase) { 'unknown' }
1227
+
1228
+ it 'should give the "unknown" error' do
1229
+ task.save
1230
+ expect(STDOUT).to receive(:puts).with /Could not/
1231
+ expect(STDOUT).to receive(:puts).with /500/
1232
+ expect(STDOUT).to receive(:puts).with /unknown/
1233
+ task.send(:log_work_to_jira)
1234
+ end
1235
+ end
1236
+ end
1237
+ end
1238
+
550
1239
  describe '#create_issue_on_pivotal_data' do
551
- let!(:task) { create :task, title: 'dupis', description: 'bamis', project: project }
1240
+ let!(:task) { create :task, title: 'dupis', description: 'bamis', project: project, pivotal_estimate: estimate }
1241
+
1242
+ context 'estimate is present' do
1243
+ let(:estimate) { 5 }
1244
+
1245
+ it 'should format hash' do
1246
+ allow(task).to receive(:story_type).and_return 'chore'
1247
+ result = task.send(:create_issue_on_pivotal_data)
1248
+ JSON.parse(result).tap do |format|
1249
+ expect(format['current_state']).to eq 'unstarted'
1250
+ expect(format['estimate']).to eq 5
1251
+ expect(format['name']).to eq 'dupis'
1252
+ expect(format['description']).to eq 'bamis'
1253
+ expect(format['story_type']).to eq 'chore'
1254
+ end
1255
+ end
1256
+ end
552
1257
 
553
- it 'should format hash' do
554
- allow(task).to receive(:story_type).and_return 'chore'
555
- result = task.send(:create_issue_on_pivotal_data)
556
- JSON.parse(result).tap do |format|
557
- expect(format['current_state']).to eq 'unstarted'
558
- expect(format['estimate']).to eq 1
559
- expect(format['name']).to eq 'dupis'
560
- expect(format['story_type']).to eq 'chore'
1258
+ context 'estimate is not present' do
1259
+ let(:estimate) { 0 }
1260
+
1261
+ it 'should format hash' do
1262
+ allow(task).to receive(:story_type).and_return 'chore'
1263
+ result = task.send(:create_issue_on_pivotal_data)
1264
+ JSON.parse(result).tap do |format|
1265
+ expect(format['current_state']).to eq 'unstarted'
1266
+ expect(format['estimate']).to eq 1
1267
+ expect(format['name']).to eq 'dupis'
1268
+ expect(format['story_type']).to eq 'chore'
1269
+ end
561
1270
  end
562
1271
  end
563
1272
  end
@@ -574,6 +1283,81 @@ RSpec.describe Task, type: :model do
574
1283
  end
575
1284
  end
576
1285
 
1286
+ describe '#finish_on_pivotal' do
1287
+ let(:task) { build :task, pivotal_id: pivotal_id }
1288
+ let(:pivotal_id) { '12345678' }
1289
+ let!(:account) { create :account, type: '--pivotal' }
1290
+ let(:faraday) { double('Faraday', post: response) }
1291
+ let(:response) { double('Faraday', body: PIVOTAL_ISSUE_CREATION_RESPONSE, status: status, reason_phrase: reason_phrase) }
1292
+ let(:status) { 200 }
1293
+ let(:reason_phrase) { 'OK' }
1294
+
1295
+ before { allow(Faraday).to receive(:new).and_return faraday }
1296
+
1297
+ context 'pt id present, pt account present, no internet connection' do
1298
+ before do
1299
+ allow(faraday).to receive(:post).and_raise Faraday::ConnectionFailed, [404]
1300
+ allow(faraday).to receive(:get).and_raise Faraday::ConnectionFailed, [404]
1301
+ allow(faraday).to receive(:put).and_raise Faraday::ConnectionFailed, [404]
1302
+ end
1303
+
1304
+ it 'should give the no connection error', :unstub_pivotal_starting do
1305
+ expect(STDOUT).to receive(:puts).with /Connection failed. Performing the task without requests to Pivotal./
1306
+ task.send(:finish_on_pivotal)
1307
+ end
1308
+ end
1309
+
1310
+ describe 'error codes', :unstub_pivotal_starting do
1311
+ before do
1312
+ allow(faraday).to receive(:post).and_return response
1313
+ allow(faraday).to receive(:get).and_return response
1314
+ allow(faraday).to receive(:put).and_return response
1315
+ end
1316
+
1317
+ context 'pt id present, pt account present, "unauthorized" server error' do
1318
+ let(:status) { 401 }
1319
+ let(:reason_phrase) { 'unauth' }
1320
+
1321
+ it 'should give the no auth error' do
1322
+ expect(STDOUT).to receive(:puts).with /No access/
1323
+ task.send(:finish_on_pivotal)
1324
+ end
1325
+ end
1326
+
1327
+ context 'pt id present, pt account present, "unauthorized" server error' do
1328
+ let(:status) { 403 }
1329
+ let(:reason_phrase) { 'unauth' }
1330
+
1331
+ it 'should give the no auth error' do
1332
+ expect(STDOUT).to receive(:puts).with /No access/
1333
+ task.send(:finish_on_pivotal)
1334
+ end
1335
+ end
1336
+
1337
+ context 'pt id present, pt account present, "not found" server error' do
1338
+ let(:status) { 404 }
1339
+ let(:reason_phrase) { 'not found' }
1340
+
1341
+ it 'should give the not found error' do
1342
+ expect(STDOUT).to receive(:puts).with /not found/
1343
+ task.send(:finish_on_pivotal)
1344
+ end
1345
+ end
1346
+
1347
+ context 'pt id present, pt account present, "unknown" server error' do
1348
+ let(:status) { 500 }
1349
+ let(:reason_phrase) { 'error' }
1350
+
1351
+ it 'should give the error' do
1352
+ expect(STDOUT).to receive(:puts).with /Could not/
1353
+ expect(STDOUT).to receive(:puts).with /500/
1354
+ expect(STDOUT).to receive(:puts).with /error/
1355
+ task.send(:finish_on_pivotal)
1356
+ end
1357
+ end
1358
+ end
1359
+ end
1360
+
577
1361
  describe '#start_issue_on_pivotal_data' do
578
1362
  let!(:task) { create :task }
579
1363