bitbuckets 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +7 -0
  3. data/README.md +169 -0
  4. data/Rakefile +1 -0
  5. data/lib/bitbucket_rest_api.rb +86 -0
  6. data/lib/bitbucket_rest_api/api.rb +104 -0
  7. data/lib/bitbucket_rest_api/api/actions.rb +32 -0
  8. data/lib/bitbucket_rest_api/api_factory.rb +29 -0
  9. data/lib/bitbucket_rest_api/authorization.rb +31 -0
  10. data/lib/bitbucket_rest_api/client.rb +53 -0
  11. data/lib/bitbucket_rest_api/configuration.rb +103 -0
  12. data/lib/bitbucket_rest_api/connection.rb +97 -0
  13. data/lib/bitbucket_rest_api/constants.rb +57 -0
  14. data/lib/bitbucket_rest_api/core_ext/array.rb +6 -0
  15. data/lib/bitbucket_rest_api/core_ext/hash.rb +58 -0
  16. data/lib/bitbucket_rest_api/deprecation.rb +36 -0
  17. data/lib/bitbucket_rest_api/error.rb +37 -0
  18. data/lib/bitbucket_rest_api/error/bad_events.rb +10 -0
  19. data/lib/bitbucket_rest_api/error/bad_request.rb +11 -0
  20. data/lib/bitbucket_rest_api/error/blank_value.rb +10 -0
  21. data/lib/bitbucket_rest_api/error/client_error.rb +19 -0
  22. data/lib/bitbucket_rest_api/error/forbidden.rb +11 -0
  23. data/lib/bitbucket_rest_api/error/internal_server_error.rb +11 -0
  24. data/lib/bitbucket_rest_api/error/invalid_options.rb +17 -0
  25. data/lib/bitbucket_rest_api/error/no_events.rb +10 -0
  26. data/lib/bitbucket_rest_api/error/not_found.rb +11 -0
  27. data/lib/bitbucket_rest_api/error/required_params.rb +17 -0
  28. data/lib/bitbucket_rest_api/error/service_error.rb +18 -0
  29. data/lib/bitbucket_rest_api/error/service_unavailable.rb +11 -0
  30. data/lib/bitbucket_rest_api/error/unauthorized.rb +11 -0
  31. data/lib/bitbucket_rest_api/error/unknown_value.rb +17 -0
  32. data/lib/bitbucket_rest_api/error/unprocessable_entity.rb +11 -0
  33. data/lib/bitbucket_rest_api/error/validations.rb +17 -0
  34. data/lib/bitbucket_rest_api/invitations.rb +14 -0
  35. data/lib/bitbucket_rest_api/issues.rb +229 -0
  36. data/lib/bitbucket_rest_api/issues/comments.rb +116 -0
  37. data/lib/bitbucket_rest_api/issues/components.rb +105 -0
  38. data/lib/bitbucket_rest_api/issues/milestones.rb +105 -0
  39. data/lib/bitbucket_rest_api/normalizer.rb +24 -0
  40. data/lib/bitbucket_rest_api/parameter_filter.rb +29 -0
  41. data/lib/bitbucket_rest_api/repos.rb +276 -0
  42. data/lib/bitbucket_rest_api/repos/changesets.rb +52 -0
  43. data/lib/bitbucket_rest_api/repos/commits.rb +38 -0
  44. data/lib/bitbucket_rest_api/repos/components.rb +35 -0
  45. data/lib/bitbucket_rest_api/repos/default_reviewers.rb +60 -0
  46. data/lib/bitbucket_rest_api/repos/download.rb +15 -0
  47. data/lib/bitbucket_rest_api/repos/following.rb +38 -0
  48. data/lib/bitbucket_rest_api/repos/forks.rb +66 -0
  49. data/lib/bitbucket_rest_api/repos/keys.rb +86 -0
  50. data/lib/bitbucket_rest_api/repos/pull_request.rb +158 -0
  51. data/lib/bitbucket_rest_api/repos/services.rb +101 -0
  52. data/lib/bitbucket_rest_api/repos/sources.rb +36 -0
  53. data/lib/bitbucket_rest_api/repos/webhooks.rb +99 -0
  54. data/lib/bitbucket_rest_api/request.rb +71 -0
  55. data/lib/bitbucket_rest_api/request/basic_auth.rb +30 -0
  56. data/lib/bitbucket_rest_api/request/jsonize.rb +39 -0
  57. data/lib/bitbucket_rest_api/request/oauth.rb +50 -0
  58. data/lib/bitbucket_rest_api/response.rb +26 -0
  59. data/lib/bitbucket_rest_api/response/helpers.rb +18 -0
  60. data/lib/bitbucket_rest_api/response/jsonize.rb +25 -0
  61. data/lib/bitbucket_rest_api/response/mashify.rb +23 -0
  62. data/lib/bitbucket_rest_api/response/raise_error.rb +28 -0
  63. data/lib/bitbucket_rest_api/response/xmlize.rb +25 -0
  64. data/lib/bitbucket_rest_api/result.rb +136 -0
  65. data/lib/bitbucket_rest_api/teams.rb +91 -0
  66. data/lib/bitbucket_rest_api/user.rb +87 -0
  67. data/lib/bitbucket_rest_api/users.rb +20 -0
  68. data/lib/bitbucket_rest_api/users/account.rb +50 -0
  69. data/lib/bitbucket_rest_api/utils/url.rb +61 -0
  70. data/lib/bitbucket_rest_api/validations.rb +23 -0
  71. data/lib/bitbucket_rest_api/validations/format.rb +21 -0
  72. data/lib/bitbucket_rest_api/validations/presence.rb +21 -0
  73. data/lib/bitbucket_rest_api/validations/required.rb +37 -0
  74. data/lib/bitbucket_rest_api/validations/token.rb +38 -0
  75. data/lib/bitbucket_rest_api/version.rb +10 -0
  76. data/lib/bitbuckets.rb +2 -0
  77. data/spec/bitbucket_rest_api/api/actions_spec.rb +18 -0
  78. data/spec/bitbucket_rest_api/api_factory_spec.rb +28 -0
  79. data/spec/bitbucket_rest_api/api_spec.rb +87 -0
  80. data/spec/bitbucket_rest_api/authorization_spec.rb +74 -0
  81. data/spec/bitbucket_rest_api/client_spec.rb +17 -0
  82. data/spec/bitbucket_rest_api/core_ext/array_spec.rb +13 -0
  83. data/spec/bitbucket_rest_api/core_ext/hash_spec.rb +47 -0
  84. data/spec/bitbucket_rest_api/deprecation_spec.rb +31 -0
  85. data/spec/bitbucket_rest_api/error/bad_events_spec.rb +11 -0
  86. data/spec/bitbucket_rest_api/error/blank_value_spec.rb +14 -0
  87. data/spec/bitbucket_rest_api/error/no_events_spec.rb +11 -0
  88. data/spec/bitbucket_rest_api/invitations_spec.rb +21 -0
  89. data/spec/bitbucket_rest_api/issues/comments_spec.rb +89 -0
  90. data/spec/bitbucket_rest_api/issues/components_spec.rb +89 -0
  91. data/spec/bitbucket_rest_api/issues/milestones_spec.rb +89 -0
  92. data/spec/bitbucket_rest_api/issues_spec.rb +91 -0
  93. data/spec/bitbucket_rest_api/normalizer_spec.rb +29 -0
  94. data/spec/bitbucket_rest_api/parameter_filter_spec.rb +42 -0
  95. data/spec/bitbucket_rest_api/repos/changesets_spec.rb +44 -0
  96. data/spec/bitbucket_rest_api/repos/commits_spec.rb +21 -0
  97. data/spec/bitbucket_rest_api/repos/components_spec.rb +43 -0
  98. data/spec/bitbucket_rest_api/repos/default_reviewers_spec.rb +65 -0
  99. data/spec/bitbucket_rest_api/repos/download_spec.rb +10 -0
  100. data/spec/bitbucket_rest_api/repos/following_spec.rb +53 -0
  101. data/spec/bitbucket_rest_api/repos/forks_spec.rb +46 -0
  102. data/spec/bitbucket_rest_api/repos/keys_spec.rb +73 -0
  103. data/spec/bitbucket_rest_api/repos/pull_request_spec.rb +283 -0
  104. data/spec/bitbucket_rest_api/repos/sources_spec.rb +78 -0
  105. data/spec/bitbucket_rest_api/repos/webhooks_spec.rb +245 -0
  106. data/spec/bitbucket_rest_api/repos_spec.rb +158 -0
  107. data/spec/bitbucket_rest_api/request/jsonize_spec.rb +19 -0
  108. data/spec/bitbucket_rest_api/request/oauth_spec.rb +26 -0
  109. data/spec/bitbucket_rest_api/request_spec.rb +88 -0
  110. data/spec/bitbucket_rest_api/response/jsonize_spec.rb +13 -0
  111. data/spec/bitbucket_rest_api/response/mashify_spec.rb +33 -0
  112. data/spec/bitbucket_rest_api/response/raise_error_spec.rb +42 -0
  113. data/spec/bitbucket_rest_api/teams_spec.rb +136 -0
  114. data/spec/bitbucket_rest_api/user_spec.rb +78 -0
  115. data/spec/bitbucket_rest_api/utils/url_spec.rb +34 -0
  116. data/spec/bitbucket_rest_api/validations/format_spec.rb +30 -0
  117. data/spec/bitbucket_rest_api/validations/presence_spec.rb +13 -0
  118. data/spec/bitbucket_rest_api/validations/required_spec.rb +44 -0
  119. data/spec/bitbucket_rest_api/validations/token_spec.rb +17 -0
  120. data/spec/bitbucket_rest_api_spec.rb +17 -0
  121. data/spec/spec_helper.rb +24 -0
  122. metadata +358 -0
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+ require 'spec_helper'
3
+
4
+ describe BitBucket::Repos::Keys do
5
+ let(:deploy_keys) { described_class.new }
6
+ describe '.list' do
7
+ before do
8
+ expect(deploy_keys).to receive(:request).with(
9
+ :get,
10
+ '/1.0/repositories/mock_username/mock_repo/deploy-keys/',
11
+ {},
12
+ {}
13
+ ).and_return(%w[key1 key2 key3])
14
+ end
15
+
16
+ context 'without a block' do
17
+ it 'should make a GET request for the deploy keys belonging to the given repo' do
18
+ deploy_keys.list('mock_username', 'mock_repo')
19
+ end
20
+ end
21
+
22
+ context 'with a block' do
23
+ it 'should make a GET request for the deploy keys belonging to the given repo' do
24
+ deploy_keys.list('mock_username', 'mock_repo') { |key| key }
25
+ end
26
+ end
27
+ end
28
+
29
+ describe '.create' do
30
+ before do
31
+ expect(deploy_keys).to receive(:request).with(
32
+ :post,
33
+ '/1.0/repositories/mock_username/mock_repo/deploy-keys/',
34
+ { 'key' => 'mock_ssh_key', 'label' => 'mock_label' },
35
+ headers: { 'Content-Type' => 'application/json' }
36
+ )
37
+ end
38
+
39
+ it 'should make a POST request for the deploy keys belonging to the given repo' do
40
+ deploy_keys.create('mock_username', 'mock_repo', key: 'mock_ssh_key', label: 'mock_label')
41
+ end
42
+ end
43
+
44
+ describe '.edit' do
45
+ before do
46
+ expect(deploy_keys).to receive(:request).with(
47
+ :put,
48
+ '/1.0/repositories/mock_username/mock_repo/deploy-keys/1',
49
+ { 'key' => 'mock_ssh_key', 'label' => 'mock_label' },
50
+ {}
51
+ )
52
+ end
53
+
54
+ it 'should make a PUT request for the deploy keys belonging to the given repo' do
55
+ deploy_keys.edit('mock_username', 'mock_repo', 1, key: 'mock_ssh_key', label: 'mock_label')
56
+ end
57
+ end
58
+
59
+ describe '.delete' do
60
+ before do
61
+ expect(deploy_keys).to receive(:request).with(
62
+ :delete,
63
+ '/1.0/repositories/mock_username/mock_repo/deploy-keys/mock_id',
64
+ { 'key' => 'mock_ssh_key', 'label' => 'mock_label' },
65
+ {}
66
+ )
67
+ end
68
+
69
+ it 'should make a DELETE request for the deploy keys belonging to the given repo' do
70
+ deploy_keys.delete('mock_username', 'mock_repo', 'mock_id', key: 'mock_ssh_key', label: 'mock_label')
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,283 @@
1
+ # frozen_string_literal: true
2
+ require 'spec_helper'
3
+
4
+ describe BitBucket::Repos::PullRequest do
5
+ subject { described_class.new }
6
+ describe '#list' do
7
+ before do
8
+ expect(subject).to receive(:request).with(
9
+ :get,
10
+ '/2.0/repositories/mock_user/mock_repo/pullrequests',
11
+ {},
12
+ {}
13
+ ).and_return(%w[pr1 pr2 pr3])
14
+ end
15
+
16
+ context 'without a block' do
17
+ it 'makes a GET request for all pull requests belonging to the repo' do
18
+ subject.list('mock_user', 'mock_repo')
19
+ end
20
+ end
21
+
22
+ context 'with a block' do
23
+ it 'makes a GET request for all pull requests belonging to the repo' do
24
+ subject.list('mock_user', 'mock_repo') { |pr| pr }
25
+ end
26
+ end
27
+ end
28
+
29
+ describe '#participants' do
30
+ before do
31
+ expect(subject).to receive(:request).with(
32
+ :get,
33
+ '/1.0/repositories/mock_user/mock_repo/pullrequests/mock_pull_request_id/participants',
34
+ {},
35
+ {}
36
+ ).and_return(%w[participant1 participant2 participant3])
37
+ end
38
+
39
+ context 'without a block' do
40
+ it 'makes a GET request for all participants belonging to the repo' do
41
+ subject.participants('mock_user', 'mock_repo', 'mock_pull_request_id')
42
+ end
43
+ end
44
+
45
+ context 'with a block' do
46
+ it 'makes a GET request for all participants belonging to the repo' do
47
+ subject.participants('mock_user', 'mock_repo', 'mock_pull_request_id') { |p| p }
48
+ end
49
+ end
50
+ end
51
+
52
+ describe '#get' do
53
+ before do
54
+ expect(subject).to receive(:request).with(
55
+ :get,
56
+ '/2.0/repositories/mock_user/mock_repo/pullrequests/mock_pull_request_id',
57
+ {}
58
+ )
59
+ end
60
+
61
+ it 'makes a GET request for the pull request belonging to the repo' do
62
+ subject.get('mock_user', 'mock_repo', 'mock_pull_request_id')
63
+ end
64
+ end
65
+
66
+ describe '#create' do
67
+ before do
68
+ @params = {
69
+ title: 'mock_pr_title',
70
+ description: 'mock_pull_request_description',
71
+ source: {
72
+ branch: {
73
+ name: 'mock_source_branch_name'
74
+ },
75
+ repository: {
76
+ full_name: 'mock_owner/mock_repo'
77
+ }
78
+ },
79
+ destination: {
80
+ branch: {
81
+ name: 'mock_destination_branch_name'
82
+ },
83
+ commit: {
84
+ hash: 'mock_uuid'
85
+ }
86
+ },
87
+ close_source_branch: true
88
+ }
89
+ end
90
+
91
+ it 'makes a POST request to create a new pull request' do
92
+ expect(subject).to receive(:request).with(
93
+ :post,
94
+ '/2.0/repositories/mock_user/mock_repo/pullrequests',
95
+ @params
96
+ )
97
+
98
+ subject.create('mock_user', 'mock_repo', @params)
99
+ end
100
+
101
+ it 'validates presence of required params' do
102
+ expect do
103
+ subject.create(
104
+ 'mock_user',
105
+ 'mock_repo',
106
+ title: '',
107
+ description: 'mock_pull_request_description',
108
+ source: {
109
+ branch: {
110
+ name: 'mock_source_branch_name'
111
+ },
112
+ repository: {
113
+ full_name: 'mock_owner/mock_repo'
114
+ }
115
+ },
116
+ destination: {
117
+ branch: {
118
+ name: 'mock_destination_branch_name'
119
+ },
120
+ commit: {
121
+ hash: 'mock_uuid'
122
+ }
123
+ },
124
+ close_source_branch: true
125
+ )
126
+ end.to raise_error
127
+ end
128
+ end
129
+
130
+ describe '.put' do
131
+ before do
132
+ expect(subject).to receive(:request).with(
133
+ :put,
134
+ '/2.0/repositories/mock_user/mock_repo/pullrequests/mock_id',
135
+ {}
136
+ )
137
+ end
138
+
139
+ it 'makes a PUT request for the given pull request' do
140
+ subject.update('mock_user', 'mock_repo', 'mock_id')
141
+ end
142
+ end
143
+
144
+ describe '.commits' do
145
+ before do
146
+ expect(subject).to receive(:request).with(
147
+ :get,
148
+ '/2.0/repositories/mock_user/mock_repo/pullrequests/mock_id/commits',
149
+ {}
150
+ )
151
+ end
152
+
153
+ it 'makes a GET request for the commits' do
154
+ subject.commits('mock_user', 'mock_repo', 'mock_id')
155
+ end
156
+ end
157
+
158
+ describe '.commits' do
159
+ before do
160
+ expect(subject).to receive(:request).with(
161
+ :post,
162
+ '/2.0/repositories/mock_user/mock_repo/pullrequests/mock_id/approve',
163
+ {}
164
+ )
165
+ end
166
+
167
+ it 'makes a POST request' do
168
+ subject.approve('mock_user', 'mock_repo', 'mock_id')
169
+ end
170
+ end
171
+
172
+ describe '.delete_approval' do
173
+ before do
174
+ expect(subject).to receive(:request).with(
175
+ :delete,
176
+ '/2.0/repositories/mock_user/mock_repo/pullrequests/mock_id/approve',
177
+ {}
178
+ )
179
+ end
180
+
181
+ it 'makes a DELTE request' do
182
+ subject.delete_approval('mock_user', 'mock_repo', 'mock_id')
183
+ end
184
+ end
185
+
186
+ describe '.diff' do
187
+ before do
188
+ expect(subject).to receive(:request).with(
189
+ :get,
190
+ '/2.0/repositories/mock_user/mock_repo/pullrequests/mock_id/diff',
191
+ {}
192
+ )
193
+ end
194
+
195
+ it 'makes a GET request for the diff of the pull request' do
196
+ subject.diff('mock_user', 'mock_repo', 'mock_id')
197
+ end
198
+ end
199
+
200
+ describe '.all_activity' do
201
+ before do
202
+ expect(subject).to receive(:request).with(
203
+ :get,
204
+ '/2.0/repositories/mock_user/mock_repo/pullrequests/activity',
205
+ {}
206
+ )
207
+ end
208
+
209
+ it 'makes a GET request' do
210
+ subject.all_activity('mock_user', 'mock_repo')
211
+ end
212
+ end
213
+
214
+ describe '.activity' do
215
+ before do
216
+ expect(subject).to receive(:request).with(
217
+ :get,
218
+ '/2.0/repositories/mock_user/mock_repo/pullrequests/mock_id/activity',
219
+ {}
220
+ )
221
+ end
222
+
223
+ it 'makes a GET request' do
224
+ subject.activity('mock_user', 'mock_repo', 'mock_id')
225
+ end
226
+ end
227
+
228
+ describe '.accept_and_merge' do
229
+ before do
230
+ expect(subject).to receive(:request).with(
231
+ :post,
232
+ '/2.0/repositories/mock_user/mock_repo/pullrequests/mock_id/merge',
233
+ {}
234
+ )
235
+ end
236
+
237
+ it 'makes a POST request' do
238
+ subject.merge('mock_user', 'mock_repo', 'mock_id')
239
+ end
240
+ end
241
+
242
+ describe '.decline' do
243
+ before do
244
+ expect(subject).to receive(:request).with(
245
+ :post,
246
+ '/2.0/repositories/mock_user/mock_repo/pullrequests/mock_id/decline',
247
+ {}
248
+ )
249
+ end
250
+
251
+ it 'makes a POST request' do
252
+ subject.decline('mock_user', 'mock_repo', 'mock_id')
253
+ end
254
+ end
255
+
256
+ describe '.comments' do
257
+ before do
258
+ expect(subject).to receive(:request).with(
259
+ :get,
260
+ '/2.0/repositories/mock_user/mock_repo/pullrequests/mock_id/comments',
261
+ {}
262
+ )
263
+ end
264
+
265
+ it 'makes a GET request' do
266
+ subject.comments('mock_user', 'mock_repo', 'mock_id')
267
+ end
268
+ end
269
+
270
+ describe '.comment' do
271
+ before do
272
+ expect(subject).to receive(:request).with(
273
+ :get,
274
+ '/2.0/repositories/mock_user/mock_repo/pullrequests/mock_id/comments/comment_id',
275
+ {}
276
+ )
277
+ end
278
+
279
+ it 'makes a GET request' do
280
+ subject.comment('mock_user', 'mock_repo', 'mock_id', 'comment_id')
281
+ end
282
+ end
283
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+ require 'spec_helper'
3
+
4
+ describe BitBucket::Repos::Sources do
5
+ let(:subject) { BitBucket::Repos::Sources.new }
6
+
7
+ describe '#list' do
8
+ context 'when some parameters are missing' do
9
+ it 'raises an error' do
10
+ expect do
11
+ subject.list(
12
+ 'mock_username',
13
+ 'mock_repo'
14
+ )
15
+ end.to raise_error(ArgumentError)
16
+ end
17
+ end
18
+
19
+ context 'when path parameter is empty' do
20
+ before do
21
+ expect(subject).to receive(:request).with(
22
+ :get,
23
+ '/1.0/repositories/mock_username/mock_repo/src/moch_sha/',
24
+ {},
25
+ {}
26
+ )
27
+ end
28
+
29
+ it 'sends a GET request for a list of all source files' do
30
+ subject.list('mock_username', 'mock_repo', 'moch_sha', '')
31
+ end
32
+ end
33
+
34
+ context 'when path parameter is defined' do
35
+ before do
36
+ expect(subject).to receive(:request).with(
37
+ :get,
38
+ '/1.0/repositories/mock_username/mock_repo/src/moch_sha/app/controller',
39
+ {},
40
+ {}
41
+ )
42
+ end
43
+
44
+ it 'send a GET request for a list of the source files under the specified path' do
45
+ subject.list('mock_username', 'mock_repo', 'moch_sha', 'app/controller')
46
+ end
47
+ end
48
+ end
49
+
50
+ describe '#get' do
51
+ context 'when some parameters are missing' do
52
+ it 'raises an error' do
53
+ expect do
54
+ subject.get(
55
+ 'mock_username',
56
+ 'mock_repo',
57
+ 'moch_sha'
58
+ )
59
+ end.to raise_error(ArgumentError)
60
+ end
61
+ end
62
+
63
+ context 'when path parameter is defined' do
64
+ before do
65
+ expect(subject).to receive(:request).with(
66
+ :get,
67
+ '/1.0/repositories/mock_username/mock_repo/raw/moch_sha/app/assets/images/logo.jpg',
68
+ {},
69
+ {}
70
+ )
71
+ end
72
+
73
+ it "send a GET request for a source file's size and contents" do
74
+ subject.get('mock_username', 'mock_repo', 'moch_sha', 'app/assets/images/logo.jpg')
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,245 @@
1
+ # frozen_string_literal: true
2
+ require 'spec_helper'
3
+
4
+ describe BitBucket::Repos::Webhooks, wip: true do
5
+ subject { described_class.new }
6
+ let(:post_put_params) do
7
+ {
8
+ 'description' => 'mock_description',
9
+ 'url' => 'mock_url',
10
+ 'active' => true,
11
+ 'events' => ['repo:push']
12
+ }
13
+ end
14
+
15
+ let(:missing_key_params) do
16
+ {
17
+ 'description' => 'mock_description',
18
+ 'active' => true,
19
+ 'events' => ['repo:push']
20
+ }
21
+ end
22
+
23
+ let(:blank_value_params) do
24
+ {
25
+ 'description' => '',
26
+ 'url' => 'mock_url',
27
+ 'active' => true,
28
+ 'events' => ['repo:push']
29
+ }
30
+ end
31
+
32
+ let(:bad_event_params) do
33
+ {
34
+ 'description' => 'mock_description',
35
+ 'url' => 'mock_url',
36
+ 'active' => true,
37
+ 'events' => ['bad:event']
38
+ }
39
+ end
40
+
41
+ let(:missing_events_params) do
42
+ {
43
+ 'description' => 'mock_description',
44
+ 'url' => 'mock_url',
45
+ 'active' => true,
46
+ 'events' => []
47
+ }
48
+ end
49
+
50
+ describe '#create' do
51
+ context 'when required fields are missing from params' do
52
+ context 'when a required key is missing' do
53
+ it 'raises an instance of BitBucket::Error::RequiredParams' do
54
+ expect do
55
+ subject.create(
56
+ 'mock_username',
57
+ 'mock_repo',
58
+ missing_key_params
59
+ )
60
+ end.to raise_error(BitBucket::Error::RequiredParams)
61
+ end
62
+ end
63
+
64
+ context 'when values of required keys are blank' do
65
+ it 'raises an instance of BitBucket::Error::RequiredParams' do
66
+ expect do
67
+ subject.create(
68
+ 'mock_username',
69
+ 'mock_repo',
70
+ blank_value_params
71
+ )
72
+ end.to raise_error(
73
+ BitBucket::Error::BlankValue,
74
+ "The value for: 'description', cannot be blank :("
75
+ )
76
+ end
77
+ end
78
+ end
79
+
80
+ it 'validates the given events' do
81
+ allow(subject).to(receive(:request))
82
+
83
+ expect do
84
+ subject.create(
85
+ 'mock_username',
86
+ 'mock_repo',
87
+ bad_event_params
88
+ )
89
+ end.to raise_error(
90
+ BitBucket::Error::BadEvents,
91
+ "The event: 'bad:event', does not exist :("
92
+ )
93
+ end
94
+
95
+ it 'checks that at least one event is given' do
96
+ allow(subject).to(receive(:request))
97
+
98
+ expect do
99
+ subject.create(
100
+ 'mock_username',
101
+ 'mock_repo',
102
+ missing_events_params
103
+ )
104
+ end.to raise_error(
105
+ BitBucket::Error::NoEvents,
106
+ 'At least one event is required, none given :('
107
+ )
108
+ end
109
+
110
+ it 'makes a POST request to create a specific webhook' do
111
+ expect(subject).to receive(:request).with(
112
+ :post,
113
+ '/2.0/repositories/mock_username/mock_repo/hooks',
114
+ post_put_params,
115
+ headers: { 'Content-Type' => 'application/json' }
116
+ )
117
+
118
+ subject.create(
119
+ 'mock_username',
120
+ 'mock_repo',
121
+ post_put_params
122
+ )
123
+ end
124
+ end
125
+
126
+ describe '#list' do
127
+ it 'makes a GET request for all the webhooks beloning to the given repo' do
128
+ expect(subject).to receive(:request).with(
129
+ :get,
130
+ '/2.0/repositories/mock_username/mock_repo/hooks',
131
+ {},
132
+ {}
133
+ )
134
+
135
+ subject.list('mock_username', 'mock_repo')
136
+ end
137
+ end
138
+
139
+ describe '#get' do
140
+ it 'makes a GET request for a specific webook' do
141
+ expect(subject).to receive(:request).with(
142
+ :get,
143
+ '/2.0/repositories/mock_username/mock_repo/hooks/mock_uuid',
144
+ {},
145
+ {}
146
+ )
147
+
148
+ subject.get('mock_username', 'mock_repo', 'mock_uuid')
149
+ end
150
+ end
151
+
152
+ describe '#edit' do
153
+ context 'when required fields are missing from params' do
154
+ context 'when a required key is missing' do
155
+ it 'raises an instance of BitBucket::Error::RequiredParams' do
156
+ expect do
157
+ subject.edit(
158
+ 'mock_username',
159
+ 'mock_repo',
160
+ 'mock_uuid',
161
+ missing_key_params
162
+ )
163
+ end.to raise_error(BitBucket::Error::RequiredParams)
164
+ end
165
+ end
166
+
167
+ context 'when values of required keys are blank' do
168
+ it 'raises an instance of BitBucket::Error::RequiredParams' do
169
+ expect do
170
+ subject.edit(
171
+ 'mock_username',
172
+ 'mock_repo',
173
+ 'mock_uuid',
174
+ blank_value_params
175
+ )
176
+ end.to raise_error(
177
+ BitBucket::Error::BlankValue,
178
+ "The value for: 'description', cannot be blank :("
179
+ )
180
+ end
181
+ end
182
+ end
183
+
184
+ it 'validates the existence of the given events' do
185
+ allow(subject).to(receive(:request))
186
+
187
+ expect do
188
+ subject.edit(
189
+ 'mock_username',
190
+ 'mock_repo',
191
+ 'mock_uuid',
192
+ bad_event_params
193
+ )
194
+ end.to raise_error(
195
+ BitBucket::Error::BadEvents,
196
+ "The event: 'bad:event', does not exist :("
197
+ )
198
+ end
199
+
200
+ it 'validates that at least one event is given' do
201
+ allow(subject).to(receive(:request))
202
+
203
+ expect do
204
+ subject.edit(
205
+ 'mock_username',
206
+ 'mock_repo',
207
+ 'mock_uuid',
208
+ missing_events_params
209
+ )
210
+ end.to raise_error(
211
+ BitBucket::Error::NoEvents,
212
+ 'At least one event is required, none given :('
213
+ )
214
+ end
215
+
216
+ it 'makes a PUT request for the given webhook' do
217
+ expect(subject).to receive(:request).with(
218
+ :put,
219
+ '/2.0/repositories/mock_username/mock_repo/hooks/mock_uuid',
220
+ post_put_params,
221
+ headers: { 'Content-Type' => 'application/json' }
222
+ )
223
+
224
+ subject.edit(
225
+ 'mock_username',
226
+ 'mock_repo',
227
+ 'mock_uuid',
228
+ post_put_params
229
+ )
230
+ end
231
+ end
232
+
233
+ describe '#delete' do
234
+ it 'sends a DELETE request for the given webhook' do
235
+ expect(subject).to receive(:request).with(
236
+ :delete,
237
+ '/2.0/repositories/mock_username/mock_repo/hooks/mock_uuid',
238
+ {},
239
+ {}
240
+ )
241
+
242
+ subject.delete('mock_username', 'mock_repo', 'mock_uuid')
243
+ end
244
+ end
245
+ end