pgit 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.coveralls.yml +1 -0
  3. data/README.markdown +3 -0
  4. data/bin/pgit +1 -1
  5. data/lib/pgit.rb +11 -1
  6. data/lib/pgit/configuration.rb +2 -43
  7. data/lib/pgit/configuration/layout_error.rb +9 -0
  8. data/lib/pgit/configuration/missing_attributes_error.rb +10 -0
  9. data/lib/pgit/configuration/not_found_error.rb +10 -0
  10. data/lib/pgit/configuration/project_missing_error.rb +10 -0
  11. data/lib/pgit/configuration/validator.rb +41 -0
  12. data/lib/pgit/current_project.rb +1 -7
  13. data/lib/pgit/current_project/no_paths_match_working_dir_error.rb +10 -0
  14. data/lib/pgit/current_project/validator.rb +11 -0
  15. data/lib/pgit/error.rb +4 -0
  16. data/lib/pgit/external_error.rb +9 -0
  17. data/lib/pgit/installer/configuration.rb +34 -0
  18. data/lib/pgit/pivotal_request_validator.rb +27 -0
  19. data/lib/pgit/story.rb +2 -6
  20. data/lib/pgit/version.rb +1 -1
  21. data/pgit.gemspec +2 -0
  22. data/spec/pgit/configuration/missing_attributes_error_spec.rb +30 -0
  23. data/spec/pgit/configuration/not_found_error_spec.rb +17 -0
  24. data/spec/pgit/configuration/project_missing_error_spec.rb +30 -0
  25. data/spec/pgit/configuration/validator_spec.rb +79 -0
  26. data/spec/pgit/configuration_spec.rb +22 -124
  27. data/spec/pgit/current_branch_spec.rb +1 -1
  28. data/spec/pgit/current_project/no_paths_match_working_dir_error_spec.rb +17 -0
  29. data/spec/pgit/current_project/validator_spec.rb +11 -0
  30. data/spec/pgit/current_project_spec.rb +17 -18
  31. data/spec/pgit/external_error_spec.rb +21 -0
  32. data/spec/pgit/installer/configuration_spec.rb +162 -0
  33. data/spec/pgit/pgit_spec.rb +2 -0
  34. data/spec/pgit/pivotal_request_validator_spec.rb +86 -0
  35. data/spec/pgit/story_branch/application_spec.rb +1 -1
  36. data/spec/pgit/story_branch_spec.rb +1 -1
  37. data/spec/pgit/story_spec.rb +1 -20
  38. data/spec/spec_helper.rb +101 -0
  39. metadata +52 -5
  40. data/lib/pgit/installer.rb +0 -32
  41. data/spec/pgit/application_spec.rb +0 -16
  42. data/spec/pgit/installer_spec.rb +0 -158
@@ -1,4 +1,4 @@
1
- require 'pgit'
1
+ require 'spec_helper'
2
2
 
3
3
  describe 'PGit::Configuration' do
4
4
  describe '.default_options' do
@@ -20,139 +20,37 @@ describe 'PGit::Configuration' do
20
20
  expect(default_options['projects']).to match_array(example_projects)
21
21
  end
22
22
  end
23
- describe '#new(configuration_path)' do
24
- describe 'empty' do
25
- it 'should complain that there should be at least one project' do
26
- fake_path = "~/some/config/path.yml"
27
- fake_expanded_path = "/Users/edderic/some/config/path.yml"
28
- fake_file = double('file')
29
- fake_yaml = {}
30
- error_message = <<-ERROR
31
- Error: /Users/edderic/some/config/path.yml needs at least one project.
32
- Please have the following layout:
33
- ---
34
- projects:
35
- - api_token: somepivotalatoken124
36
- id: '12345'
37
- path: "~/some/path/to/a/pivotal-git/project"
38
- - api_token: somepivotalatoken124
39
- id: '23429070'
40
- path: "~/some/other/pivotal-git/project"
41
- ERROR
42
23
 
43
- allow(File).to receive(:expand_path).with(fake_path).and_return(fake_expanded_path)
44
- allow(File).to receive(:expand_path).with('.')
45
- allow(File).to receive(:exists?).with(fake_expanded_path).and_return(true)
46
- allow(File).to receive(:open).with(fake_expanded_path, 'r').and_return(fake_file)
47
- allow(YAML).to receive(:load).with(fake_file).and_return(fake_yaml)
48
- error_message.gsub!(/^\s{10}/,'')
49
-
50
- expect{ PGit::Configuration.new(fake_path) }.to raise_error(error_message)
51
- end
52
- end
53
-
54
- describe 'has projects but is missing a token' do
55
- it 'should raise an error' do
56
- fake_path = "~/some/config/path.yml"
57
- fake_expanded_path = "/Users/edderic/some/config/path.yml"
58
- fake_file = double('file')
59
- fake_projects = [ { path: 'fake_path',
60
- api_token: 'fake_token'
61
- }]
62
- fake_yaml = { 'projects' => fake_projects }
63
-
64
- allow(File).to receive(:expand_path).with(fake_path).and_return(fake_expanded_path)
65
- allow(File).to receive(:expand_path).with('.')
66
- allow(File).to receive(:exists?).with(fake_expanded_path).and_return(true)
67
- allow(File).to receive(:open).with(fake_expanded_path, 'r').and_return(fake_file)
68
- allow(YAML).to receive(:load).with(fake_file).and_return(fake_yaml)
69
- error_message = <<-ERROR
70
- Error: Must have a path, id, and api_token for each project.
71
- Please have the following layout:
72
- ---
73
- projects:
74
- - api_token: somepivotalatoken124
75
- id: '12345'
76
- path: "~/some/path/to/a/pivotal-git/project"
77
- - api_token: somepivotalatoken124
78
- id: '23429070'
79
- path: "~/some/other/pivotal-git/project"
80
- ERROR
81
- error_message.gsub!(/^\s{10}/, '')
24
+ describe '#new (without any arguments)' do
25
+ it 'should delegate the default path to PGit::Configuration::Validator instance' do
26
+ fake_validator = instance_double('PGit::Configuration::Validator')
27
+ allow(PGit::Configuration::Validator).to receive(:new).with("~/.pgit.rc.yml").and_return fake_validator
28
+ PGit::Configuration.new
82
29
 
83
- expect{ PGit::Configuration.new(fake_path) }.to raise_error(error_message)
84
- end
30
+ expect(PGit::Configuration::Validator).to have_received(:new).with("~/.pgit.rc.yml")
85
31
  end
86
32
  end
87
33
 
88
- describe '#new(configuration_path)' do
89
- describe 'configuration_path exists' do
90
- it '#to_yaml should return the yaml file' do
91
- fake_path = "~/some/config/path.yml"
92
- fake_expanded_path = "/Users/edderic/some/config/path.yml"
93
- fake_file = double('file')
94
- fake_projects = [ { "path" => 'fake_path',
95
- "id" => 'fake_id',
96
- "api_token" => 'fake_token'
97
- }]
34
+ describe '#new ("~/some/path")' do
35
+ it 'should delegate the path to PGit::Configuration::Validator instance' do
36
+ fake_validator = instance_double('PGit::Configuration::Validator')
37
+ fake_path = "~/some/path"
38
+ allow(PGit::Configuration::Validator).to receive(:new).with(fake_path).and_return fake_validator
39
+ PGit::Configuration.new(fake_path)
98
40
 
99
- fake_yaml = { 'projects' => fake_projects }
100
-
101
- allow(File).to receive(:expand_path).with(fake_path).and_return(fake_expanded_path)
102
- allow(File).to receive(:exists?).with(fake_expanded_path).and_return(true)
103
- allow(File).to receive(:open).with(fake_expanded_path, 'r').and_return(fake_file)
104
- allow(YAML).to receive(:load).with(fake_file).and_return(fake_yaml)
105
-
106
- configuration = PGit::Configuration.new(fake_path)
107
-
108
- expect(configuration.to_yaml).to eq fake_yaml
109
- end
110
- end
111
-
112
- describe 'configuration path does not exist' do
113
- it 'should throw an error' do
114
- file_path = '~/.edderic-dotfiles/config/project.yml'
115
- fake_expanded_path = "/home/edderic/.edderic-dotfiles/config/project.yml"
116
- error_message = "No such file or directory. Please give a valid path to the project.yml"
117
- allow(File).to receive(:exists?).and_return(false)
118
-
119
- expect{ PGit::Configuration.new(file_path) }.to raise_error
120
- end
41
+ expect(PGit::Configuration::Validator).to have_received(:new).with(fake_path)
121
42
  end
122
43
  end
123
44
 
124
- describe '#new (without any arguments)' do
125
- describe 'default configuration_path does exist' do
126
- it 'should load the default configuration file' do
127
- default_path = "~/.pgit.rc.yml"
128
- default_expanded_path = "/Users/edderic/.pgit.rc.yml"
129
- fake_file = double('file')
130
-
131
- fake_projects = [ { "path" => 'fake_path',
132
- "id" => 'fake_id',
133
- "api_token" => 'fake_token'
134
- }]
135
-
136
- fake_yaml = { 'projects' => fake_projects }
137
-
138
- allow(File).to receive(:exists?).with(default_expanded_path).and_return(true)
139
- allow(File).to receive(:expand_path).with(default_path).and_return(default_expanded_path)
140
- allow(File).to receive(:open).with(default_expanded_path, 'r').and_return(fake_file)
141
- allow(YAML).to receive(:load).with(fake_file).and_return(fake_yaml)
142
-
143
- configuration = PGit::Configuration.new
144
-
145
- expect(File).to have_received(:expand_path).with(default_path)
146
- end
147
- end
148
-
149
- describe 'default configuration file does not exist' do
150
- it 'should throw an error' do
151
- allow(File).to receive(:exists?).and_return(false)
45
+ describe '#to_yaml' do
46
+ it 'should delegate #yaml to validator' do
47
+ fake_validator = instance_double('PGit::Configuration::Validator')
48
+ allow(fake_validator).to receive(:yaml)
49
+ allow(PGit::Configuration::Validator).to receive(:new).with("~/.pgit.rc.yml").and_return fake_validator
50
+ configuration = PGit::Configuration.new
51
+ configuration.to_yaml
152
52
 
153
- error_message = "Default configuration file does not exist. Please run `pgit install`"
154
- expect{ PGit::Configuration.new }.to raise_error(error_message)
155
- end
53
+ expect(fake_validator).to have_received(:yaml)
156
54
  end
157
55
  end
158
56
  end
@@ -1,4 +1,4 @@
1
- require 'pgit'
1
+ require 'spec_helper'
2
2
 
3
3
  describe 'PGit::CurrentBranch' do
4
4
  describe '#name' do
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'PGit::CurrentProject::NoPathsMatchWorkingDirError' do
4
+ it 'should have PGit::Error as an ancestor' do
5
+ ancestors = PGit::CurrentProject::NoPathsMatchWorkingDirError.ancestors
6
+
7
+ expect(ancestors).to include PGit::Error
8
+ end
9
+
10
+ it 'should have the appropriate message' do
11
+ matches = []
12
+ error = PGit::CurrentProject::NoPathsMatchWorkingDirError.new
13
+ message = error.instance_eval { @message }
14
+
15
+ expect(message).to eq "None of the project paths matches the working directory"
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'PGit::CurrentProject::Validator' do
4
+ it 'should raise an error if there are no matching projects' do
5
+ matching_projects = []
6
+
7
+ expect do
8
+ PGit::CurrentProject::Validator.new(matching_projects)
9
+ end.to raise_error PGit::CurrentProject::NoPathsMatchWorkingDirError
10
+ end
11
+ end
@@ -1,4 +1,4 @@
1
- require 'pgit'
1
+ require 'spec_helper'
2
2
  # .edderic-dotfiles/config.yml
3
3
  # projects
4
4
  # - path: ~/Therapy-Exercises-Online
@@ -22,25 +22,24 @@ end
22
22
 
23
23
  describe 'PGit::CurrentProject' do
24
24
  describe '#new(config_yaml)' do
25
- describe 'none of the projects listed matches the working directory' do
26
- it 'should throw an error' do
27
- error_message = "None of the project paths matches the working directory"
28
- fake_project_1 = { "path" => "~/some-non-matching-path",
29
- "id" => 12345,
30
- "api_token" => "astoeuh" }
31
- fake_project_2 = { "path" => "~/some-other-non-matching-path",
32
- "id" => 19191,
33
- "api_token" => "astoeuh" }
34
- fake_project_list = [ fake_project_1, fake_project_2 ]
35
- fake_yaml = { "projects" => fake_project_list }
36
- fake_pwd = "/Therapy-Exercises-Online/some_other_project/some_subdirectory"
37
- fake_configuration = double('configuration', to_yaml: fake_yaml)
38
- allow(Dir).to receive(:pwd).and_return(fake_pwd)
25
+ it 'should delegate to PGit::CurrentProject::Validator' do
26
+ fake_project_1 = { "path" => "~/some-non-matching-path",
27
+ "id" => 12345,
28
+ "api_token" => "astoeuh" }
29
+ fake_project_2 = { "path" => "~/some-other-non-matching-path",
30
+ "id" => 19191,
31
+ "api_token" => "astoeuh" }
32
+ fake_project_list = [ fake_project_1, fake_project_2 ]
33
+ fake_yaml = { "projects" => fake_project_list }
34
+ fake_pwd = "/Therapy-Exercises-Online/some_other_project/some_subdirectory"
35
+ fake_configuration = double('configuration', to_yaml: fake_yaml)
36
+ allow(Dir).to receive(:pwd).and_return(fake_pwd)
37
+ allow(PGit::CurrentProject::Validator).to receive(:new).with []
39
38
 
40
- expect{ PGit::CurrentProject.new(fake_configuration.to_yaml) }.to raise_error(error_message)
41
- end
42
- end
39
+ PGit::CurrentProject.new(fake_configuration.to_yaml)
43
40
 
41
+ expect(PGit::CurrentProject::Validator).to have_received(:new).with []
42
+ end
44
43
  end
45
44
 
46
45
  describe '#pwd' do
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'PGit::ExternalError' do
4
+ it 'should take in the response' do
5
+ response = '{
6
+ "code": "unfound_resource",
7
+ "kind": "error",
8
+ "error": "The object you tried to access could not be found. It may have been removed by another user, you may be using the ID of another object type, or you may be trying to access a sub-resource at the wrong point in a tree."
9
+ }'
10
+ external_error = PGit::ExternalError.new(response)
11
+ message = external_error.instance_eval { @message }
12
+
13
+ expect(message).to eq(response)
14
+ end
15
+
16
+ it 'should inherit from PGit::Error' do
17
+ ancestors = PGit::ExternalError.ancestors
18
+
19
+ expect(ancestors).to include PGit::Error
20
+ end
21
+ end
@@ -0,0 +1,162 @@
1
+ require 'pgit'
2
+
3
+ describe 'PGit::Installer::Configuration' do
4
+ describe '::FILEPATH' do
5
+ it 'should eq ~/.pgit.rc.yml' do
6
+ expect(PGit::Installer::Configuration::FILEPATH).to eq '~/.pgit.rc.yml'
7
+ end
8
+ end
9
+
10
+ describe "#{PGit::Installer::Configuration::FILEPATH} exists" do
11
+ it 'should say that the file already exists' do
12
+ global_opts = {}
13
+ opts = { }
14
+ args = {}
15
+ message = "#{PGit::Installer::Configuration::FILEPATH} already exists"
16
+ default_file_path = "#{PGit::Installer::Configuration::FILEPATH}"
17
+ file_expanded_path = "/home/edderic/.pgit.rc.yml"
18
+
19
+ allow(File).to receive(:expand_path).
20
+ with(".")
21
+ allow(File).to receive(:expand_path).
22
+ with(default_file_path).and_return(file_expanded_path)
23
+ allow(File).to receive(:exists?).
24
+ with(file_expanded_path).and_return(true)
25
+ allow_any_instance_of(PGit::Installer::Configuration).to receive(:warn).with(message)
26
+
27
+ installer = PGit::Installer::Configuration.new(global_opts, opts, args)
28
+
29
+ expect(installer).to have_received(:warn).with(message)
30
+ end
31
+ end
32
+
33
+ describe '~/pgit.rc.yml does not exist' do
34
+ it 'should ask to continue or not' do
35
+ global_opts = {}
36
+ opts = { }
37
+ args = {}
38
+
39
+ message = "*** Installing example pgit configuration file under #{PGit::Installer::Configuration::FILEPATH}. " +
40
+ "Continue? [Y/n]"
41
+ confirmation_message = "Saving example pgit config in #{PGit::Installer::Configuration::FILEPATH}..."
42
+ edit_message = "Saved! Please edit #{PGit::Installer::Configuration::FILEPATH} and add the proper Pivotal Tracker API tokens, id, and file paths for each project"
43
+ answer = instance_double("String", chomp: 'y')
44
+ expanded_path = "/home/edderic/.pgit.rc.yml"
45
+ fake_writable_file = double('File')
46
+ allow(File).to receive(:expand_path).with("#{PGit::Installer::Configuration::FILEPATH}").and_return(expanded_path)
47
+ allow(File).to receive(:exists?).with(expanded_path).and_return(false)
48
+ allow(File).to receive(:open).with(expanded_path, 'w').and_return(fake_writable_file)
49
+ allow(STDIN).to receive(:gets).and_return(answer)
50
+ allow_any_instance_of(PGit::Installer::Configuration).to receive(:puts).with(message)
51
+ allow_any_instance_of(PGit::Installer::Configuration).to receive(:puts).with(confirmation_message)
52
+ allow_any_instance_of(PGit::Installer::Configuration).to receive(:puts).with(edit_message)
53
+ installer = PGit::Installer::Configuration.new(global_opts, opts, args)
54
+
55
+ expect(installer).to have_received(:puts).with(message)
56
+ end
57
+
58
+ describe 'user answers "y"' do
59
+ it "should show the save message" do
60
+ global_opts = {}
61
+ opts = { }
62
+ args = {}
63
+
64
+ first_message = "*** Installing example pgit configuration file under #{PGit::Installer::Configuration::FILEPATH}. " +
65
+ "Continue? [Y/n]"
66
+ save_message = "Saving example pgit config in #{PGit::Installer::Configuration::FILEPATH}..."
67
+ expanded_path = "/home/edderic/.pgit.rc.yml"
68
+ edit_message = "Saved! Please edit #{PGit::Installer::Configuration::FILEPATH} and add the proper Pivotal Tracker API tokens, id, and file paths for each project"
69
+ fake_writable_file = double('File')
70
+ allow(File).to receive(:expand_path).with("#{PGit::Installer::Configuration::FILEPATH}").and_return(expanded_path)
71
+ allow(File).to receive(:exists?).with(expanded_path).and_return(false)
72
+ allow(File).to receive(:open).with(expanded_path, 'w').and_return(fake_writable_file)
73
+ allow_any_instance_of(PGit::Installer::Configuration).to receive(:puts).with(edit_message)
74
+ allow_any_instance_of(PGit::Installer::Configuration).to receive(:puts).with(first_message)
75
+ allow_any_instance_of(PGit::Installer::Configuration).to receive(:puts).with(save_message)
76
+
77
+ answer = instance_double("String", chomp: 'Y')
78
+ allow(STDIN).to receive(:gets).and_return(answer)
79
+
80
+ installer = PGit::Installer::Configuration.new(global_opts, opts, args)
81
+
82
+ expect(installer).to have_received(:puts).with(save_message)
83
+ end
84
+
85
+ it "should save the file under #{PGit::Installer::Configuration::FILEPATH}" do
86
+ global_opts = {}
87
+ opts = { }
88
+ args = {}
89
+
90
+ first_message = "*** Installing example pgit configuration file under #{PGit::Installer::Configuration::FILEPATH}. " +
91
+ "Continue? [Y/n]"
92
+ save_message = "Saving example pgit config in #{PGit::Installer::Configuration::FILEPATH}..."
93
+ expanded_path = "/home/edderic/.pgit.rc.yml"
94
+ fake_writable_file = double('File')
95
+ edit_message = "Saved! Please edit #{PGit::Installer::Configuration::FILEPATH} and add the proper Pivotal Tracker API tokens, id, and file paths for each project"
96
+ allow(File).to receive(:expand_path).with(PGit::Installer::Configuration::FILEPATH).and_return(expanded_path)
97
+ allow(File).to receive(:exists?).with(expanded_path).and_return(false)
98
+ allow(File).to receive(:open).with(expanded_path, 'w').and_return(fake_writable_file)
99
+ allow_any_instance_of(PGit::Installer::Configuration).to receive(:puts).with(first_message)
100
+ allow_any_instance_of(PGit::Installer::Configuration).to receive(:puts).with(save_message)
101
+ allow_any_instance_of(PGit::Installer::Configuration).to receive(:puts).with(edit_message)
102
+
103
+ answer = instance_double("String", chomp: 'Y')
104
+ allow(STDIN).to receive(:gets).and_return(answer)
105
+
106
+ installer = PGit::Installer::Configuration.new(global_opts, opts, args)
107
+
108
+ expect(File).to have_received(:open).with(expanded_path, 'w')
109
+ end
110
+
111
+ it 'should ask the user to edit the config file' do
112
+ global_opts = {}
113
+ opts = { }
114
+ args = {}
115
+
116
+ first_message = "*** Installing example pgit configuration file under #{PGit::Installer::Configuration::FILEPATH}. " +
117
+ "Continue? [Y/n]"
118
+ save_message = "Saving example pgit config in #{PGit::Installer::Configuration::FILEPATH}..."
119
+ edit_message = "Saved! Please edit #{PGit::Installer::Configuration::FILEPATH} and add the proper Pivotal Tracker API tokens, id, and file paths for each project"
120
+ expanded_path = "/home/edderic/.pgit.rc.yml"
121
+ fake_writable_file = double('File')
122
+ allow(File).to receive(:expand_path).with(PGit::Installer::Configuration::FILEPATH).and_return(expanded_path)
123
+ allow(File).to receive(:exists?).with(expanded_path).and_return(false)
124
+ allow(File).to receive(:open).with(expanded_path, 'w').and_return(fake_writable_file)
125
+ allow_any_instance_of(PGit::Installer::Configuration).to receive(:puts).with(first_message)
126
+ allow_any_instance_of(PGit::Installer::Configuration).to receive(:puts).with(edit_message)
127
+ allow_any_instance_of(PGit::Installer::Configuration).to receive(:puts).with(save_message)
128
+
129
+ answer = instance_double("String", chomp: 'Y')
130
+ allow(STDIN).to receive(:gets).and_return(answer)
131
+
132
+ installer = PGit::Installer::Configuration.new(global_opts, opts, args)
133
+
134
+ expect(installer).to have_received(:puts).with(edit_message)
135
+ end
136
+ end
137
+
138
+ describe 'user answers with "n"' do
139
+ it "should show the abort message" do
140
+ message = "Aborting installation..."
141
+ global_opts = {}
142
+ opts = { }
143
+ args = {}
144
+
145
+ first_message = "*** Installing example pgit configuration file under #{PGit::Installer::Configuration::FILEPATH}. " +
146
+ "Continue? [Y/n]"
147
+ expanded_path = "/home/edderic/.pgit.rc.yml"
148
+ allow(File).to receive(:expand_path).with("#{PGit::Installer::Configuration::FILEPATH}").and_return(expanded_path)
149
+ allow(File).to receive(:exists?).with(expanded_path).and_return(false)
150
+ allow_any_instance_of(PGit::Installer::Configuration).to receive(:puts).with(first_message)
151
+ allow_any_instance_of(PGit::Installer::Configuration).to receive(:puts).with(message)
152
+
153
+ answer = instance_double("String", chomp: 'n')
154
+ allow(STDIN).to receive(:gets).and_return(answer)
155
+
156
+ installer = PGit::Installer::Configuration.new(global_opts, opts, args)
157
+
158
+ expect(installer).to have_received(:puts).with(message)
159
+ end
160
+ end
161
+ end
162
+ end
@@ -1,3 +1,5 @@
1
+ require 'spec_helper'
2
+
1
3
  describe 'pgit' do
2
4
  describe 'story_branch' do
3
5
  it 'should output the help info for story_branch command' do
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'PGit::PivotalRequestValidator' do
4
+ describe 'when the request has an "error" but "kind" is not "error"' do
5
+ it 'should not raise an error' do
6
+ request = <<-REQUEST
7
+ {
8
+ "kind": "story",
9
+ "id": 84538682,
10
+ "project_id": 1228944,
11
+ "name": "Don't just use regex to match the whole JSON string response for the word \"error\".",
12
+ "description": " If \"kind\" of the get response is \"error\", throw an error.",
13
+ "story_type": "bug",
14
+ "current_state": "started",
15
+ "requested_by_id": 1121520,
16
+ "owned_by_id": 1121520,
17
+ "owner_ids": [ 1121520 ],
18
+ "labels": [ ],
19
+ "created_at": "2014-12-14T15:11:46Z",
20
+ "updated_at": "2014-12-15T12:04:13Z",
21
+ "url": "https://www.pivotaltracker.com/story/show/84538682"
22
+ }
23
+ REQUEST
24
+
25
+ expect do
26
+ PGit::PivotalRequestValidator.new(request)
27
+ end.not_to raise_error
28
+ end
29
+ end
30
+
31
+ describe 'when the request has "kind": "error"' do
32
+ it 'should raise an error' do
33
+ request = <<-REQUEST
34
+ {
35
+ "code": "unfound_resource",
36
+ "kind": "error",
37
+ "error": "The object you tried to access could not be found. It may have been removed by another user, you may be using the ID of another object type, or you may be trying to access a sub-resource at the wrong point in a tree."
38
+ }
39
+ REQUEST
40
+
41
+ expect do
42
+ PGit::PivotalRequestValidator.new(request)
43
+ end.to raise_error(PGit::ExternalError)
44
+ end
45
+ end
46
+
47
+ describe 'when the request has no "kind"' do
48
+ it 'should raise a PGit::ExternalError' do
49
+ request = <<-REQUEST
50
+ some unrecognizable request
51
+ REQUEST
52
+
53
+ expect do
54
+ PGit::PivotalRequestValidator.new(request)
55
+ end.to raise_error(PGit::ExternalError)
56
+ end
57
+ end
58
+
59
+ describe '#request' do
60
+ it 'should return the passed request when there is no error' do
61
+ request = <<-REQUEST
62
+ {
63
+ "kind": "story",
64
+ "id": 84538682,
65
+ "project_id": 1228944,
66
+ "name": "Don't just use regex to match the whole JSON string response for the word \"error\".",
67
+ "description": " If \"kind\" of the get response is \"error\", throw an error.",
68
+ "story_type": "bug",
69
+ "current_state": "started",
70
+ "requested_by_id": 1121520,
71
+ "owned_by_id": 1121520,
72
+ "owner_ids": [ 1121520 ],
73
+ "labels": [ ],
74
+ "created_at": "2014-12-14T15:11:46Z",
75
+ "updated_at": "2014-12-15T12:04:13Z",
76
+ "url": "https://www.pivotaltracker.com/story/show/84538682"
77
+ }
78
+ REQUEST
79
+
80
+ validator = PGit::PivotalRequestValidator.new(request)
81
+ filtered_request = validator.request
82
+
83
+ expect(filtered_request).to eq request
84
+ end
85
+ end
86
+ end