@@ -6,14 +6,16 @@ module PactBroker
6
6
module Api
7
7
module Contracts
8
8
describe WebhookContract do
9
- let ( :json ) { load_fixture 'webhook_valid .json' }
9
+ let ( :json ) { load_fixture 'webhook_valid_with_pacticipants .json' }
10
10
let ( :hash ) { JSON . parse ( json ) }
11
11
let ( :webhook ) { PactBroker ::Api ::Decorators ::WebhookDecorator . new ( Domain ::Webhook . new ) . from_json ( json ) }
12
12
let ( :subject ) { WebhookContract . new ( webhook ) }
13
13
let ( :matching_hosts ) { [ 'foo' ] }
14
+ let ( :consumer ) { double ( "consumer" ) }
15
+ let ( :provider ) { double ( "provider" ) }
14
16
15
17
def valid_webhook_with
16
- hash = load_json_fixture 'webhook_valid .json'
18
+ hash = load_json_fixture 'webhook_valid_with_pacticipants .json'
17
19
yield hash
18
20
hash . to_json
19
21
end
@@ -23,6 +25,8 @@ def valid_webhook_with
23
25
PactBroker . configuration . webhook_http_method_whitelist = webhook_http_method_whitelist
24
26
PactBroker . configuration . webhook_host_whitelist = webhook_host_whitelist
25
27
allow ( PactBroker ::Webhooks ::CheckHostWhitelist ) . to receive ( :call ) . and_return ( whitelist_matches )
28
+ allow ( PactBroker ::Pacticipants ::Service ) . to receive ( :find_pacticipant_by_name ) . with ( "Foo" ) . and_return ( consumer )
29
+ allow ( PactBroker ::Pacticipants ::Service ) . to receive ( :find_pacticipant_by_name ) . with ( "Bar" ) . and_return ( provider )
26
30
subject . validate ( hash )
27
31
end
28
32
@@ -36,6 +40,98 @@ def valid_webhook_with
36
40
end
37
41
end
38
42
43
+ context "with a nil consumer name" do
44
+ let ( :json ) do
45
+ valid_webhook_with do |hash |
46
+ hash [ 'consumer' ] [ 'name' ] = nil
47
+ end
48
+ end
49
+
50
+ it "contains an error" do
51
+ expect ( subject . errors [ :'consumer.name' ] ) . to eq [ "can't be blank" ]
52
+ end
53
+ end
54
+
55
+ context "with no consumer name key" do
56
+ let ( :json ) do
57
+ valid_webhook_with do |hash |
58
+ hash [ 'consumer' ] . delete ( 'name' )
59
+ end
60
+ end
61
+
62
+ # I'd prefer this to be "is missing". Isn't the whole point of dry validation
63
+ # that you can distingush between keys being missing and values being missing? FFS.
64
+ it "contains an error" do
65
+ expect ( subject . errors [ :'consumer.name' ] ) . to eq [ "can't be blank" ]
66
+ end
67
+ end
68
+
69
+ context "with no consumer" do
70
+ let ( :json ) do
71
+ valid_webhook_with do |hash |
72
+ hash . delete ( 'consumer' )
73
+ end
74
+ end
75
+
76
+ it "contains no errors" do
77
+ expect ( subject . errors ) . to be_empty
78
+ end
79
+ end
80
+
81
+ context "with a consumer name that doesn't match any existing consumer" do
82
+ let ( :consumer ) { nil }
83
+
84
+ it "contains no errors" do
85
+ expect ( subject . errors [ :'consumer.name' ] ) . to eq [ "does not match an existing pacticipant" ]
86
+ end
87
+ end
88
+
89
+ context "with a nil provider name" do
90
+ let ( :json ) do
91
+ valid_webhook_with do |hash |
92
+ hash [ 'provider' ] [ 'name' ] = nil
93
+ end
94
+ end
95
+
96
+ it "contains an error" do
97
+ expect ( subject . errors [ :'provider.name' ] ) . to eq [ "can't be blank" ]
98
+ end
99
+ end
100
+
101
+ context "with no provider name key" do
102
+ let ( :json ) do
103
+ valid_webhook_with do |hash |
104
+ hash [ 'provider' ] . delete ( 'name' )
105
+ end
106
+ end
107
+
108
+ # I'd prefer this to be "is missing". Isn't the whole point of dry validation
109
+ # that you can distingush between keys being missing and values being missing? FFS.
110
+ it "contains an error" do
111
+ expect ( subject . errors [ :'provider.name' ] ) . to eq [ "can't be blank" ]
112
+ end
113
+ end
114
+
115
+ context "with no provider" do
116
+ let ( :json ) do
117
+ valid_webhook_with do |hash |
118
+ hash . delete ( 'provider' )
119
+ end
120
+ end
121
+
122
+ it "contains no errors" do
123
+ expect ( subject . errors ) . to be_empty
124
+ end
125
+ end
126
+
127
+ context "with a provider name that doesn't match any existing provider" do
128
+ let ( :provider ) { nil }
129
+
130
+ it "contains no errors" do
131
+ expect ( subject . errors [ :'provider.name' ] ) . to eq [ "does not match an existing pacticipant" ]
132
+ end
133
+ end
134
+
39
135
context "with no request defined" do
40
136
let ( :json ) { { } . to_json }
41
137
0 commit comments