@@ -7,6 +7,116 @@ module Resources
7
7
8
8
describe AllWebhooks do
9
9
10
+ let ( :webhook_service ) { PactBroker ::Webhooks ::Service }
11
+ let ( :uuid ) { '1483234k24DKFGJ45K' }
12
+ let ( :path ) { "/webhooks" }
13
+ let ( :headers ) { { 'CONTENT_TYPE' => 'application/json' } }
14
+ let ( :webhook ) { double ( 'webhook' , consumer : parsed_consumer , provider : parsed_provider ) }
15
+ let ( :parsed_provider ) { instance_double ( PactBroker ::Domain ::Pacticipant , name : "Some Provider" ) }
16
+ let ( :parsed_consumer ) { instance_double ( PactBroker ::Domain ::Pacticipant , name : "Some Consumer" ) }
17
+ let ( :consumer ) { double ( 'consumer' , name : "Some Consumer" ) }
18
+ let ( :provider ) { double ( 'provider' , name : "Some Provider" ) }
19
+ let ( :saved_webhook ) { double ( 'saved_webhook' ) }
20
+ let ( :webhook_decorator ) { instance_double ( Decorators ::WebhookDecorator , from_json : webhook ) }
21
+
22
+ before do
23
+ allow ( PactBroker ::Pacticipants ::Service ) . to receive ( :find_pacticipant_by_name ) . with ( "Some Provider" ) . and_return ( provider )
24
+ allow ( PactBroker ::Pacticipants ::Service ) . to receive ( :find_pacticipant_by_name ) . with ( "Some Consumer" ) . and_return ( consumer )
25
+ allow ( Decorators ::WebhookDecorator ) . to receive ( :new ) . and_return ( webhook_decorator )
26
+ end
27
+
28
+ describe "POST" do
29
+ let ( :webhook_json ) do
30
+ {
31
+ some : 'json'
32
+ } . to_json
33
+ end
34
+
35
+ let ( :next_uuid ) { '123k2nvkkwjrwk34' }
36
+ let ( :valid ) { true }
37
+ let ( :errors ) { double ( "errors" , empty? : valid , messages : [ 'messages' ] ) }
38
+
39
+ before do
40
+ allow ( webhook_service ) . to receive ( :create ) . and_return ( saved_webhook )
41
+ allow ( webhook_service ) . to receive ( :next_uuid ) . and_return ( next_uuid )
42
+ allow ( webhook_service ) . to receive ( :errors ) . and_return ( errors )
43
+ allow ( PactBroker ::Domain ::Webhook ) . to receive ( :new ) . and_return ( webhook )
44
+ end
45
+
46
+ subject { post path , webhook_json , headers }
47
+
48
+ context "with malformed JSON" do
49
+ let ( :webhook_json ) { "{" }
50
+
51
+ it "returns a 400 error" do
52
+ subject
53
+ expect ( last_response . status ) . to eq 400
54
+ end
55
+ end
56
+
57
+ context "with invalid attributes" do
58
+
59
+ let ( :valid ) { false }
60
+
61
+ it "returns a 400" do
62
+ subject
63
+ expect ( last_response . status ) . to be 400
64
+ end
65
+
66
+ it "returns a HAL JSON content type" do
67
+ subject
68
+ expect ( last_response . headers [ 'Content-Type' ] ) . to eq 'application/hal+json;charset=utf-8'
69
+ end
70
+
71
+ it "returns the validation errors" do
72
+ subject
73
+ expect ( JSON . parse ( last_response . body , symbolize_names : true ) ) . to eq errors : [ 'messages' ]
74
+ end
75
+
76
+ end
77
+
78
+ context "with valid attributes" do
79
+
80
+ let ( :webhook_response_json ) { { some : 'webhook' } . to_json }
81
+
82
+ before do
83
+ allow_any_instance_of ( Decorators ::WebhookDecorator ) . to receive ( :to_json ) . and_return ( webhook_response_json )
84
+ allow ( webhook_decorator ) . to receive ( :to_json ) . and_return ( webhook_response_json )
85
+ end
86
+
87
+ it "saves the webhook" do
88
+ expect ( webhook_service ) . to receive ( :create ) . with ( next_uuid , webhook , consumer , provider )
89
+ subject
90
+ end
91
+
92
+ it "returns a 201 response" do
93
+ subject
94
+ expect ( last_response . status ) . to be 201
95
+ end
96
+
97
+ it "returns the Location header" do
98
+ subject
99
+ expect ( last_response . headers [ 'Location' ] ) . to include ( next_uuid )
100
+ end
101
+
102
+ it "returns a HAL JSON content type" do
103
+ subject
104
+ expect ( last_response . headers [ 'Content-Type' ] ) . to eq 'application/hal+json;charset=utf-8'
105
+ end
106
+
107
+ it "generates the JSON response body" do
108
+ expect ( Decorators ::WebhookDecorator ) . to receive ( :new ) . with ( saved_webhook ) . and_return ( webhook_decorator )
109
+ expect ( webhook_decorator ) . to receive ( :to_json ) . with ( user_options : { base_url : 'http://example.org' } )
110
+ subject
111
+ end
112
+
113
+ it "returns the JSON representation of the webhook" do
114
+ subject
115
+ expect ( last_response . body ) . to eq webhook_response_json
116
+ end
117
+ end
118
+ end
119
+
10
120
describe "GET" do
11
121
12
122
subject { get "/webhooks" }
0 commit comments