-
Notifications
You must be signed in to change notification settings - Fork 255
/
form_spec.rb
189 lines (151 loc) · 5.07 KB
/
form_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
describe Hubspot::Form do
let(:guid) { '78c2891f-ebdd-44c0-bd94-15c012bbbfbf' } # '561d9ce9-bb4c-45b4-8e32-21cdeaa3a7f0'
let(:example_form_hash) do
VCR.use_cassette('form_example') do
HTTParty.get("https://api.hubapi.com#{Hubspot::Form::FORMS_PATH}/#{guid}/?hapikey=demo").parsed_response
end
end
describe '#initialize' do
subject { Hubspot::Form.new(example_form_hash) }
it { should be_an_instance_of Hubspot::Form }
its(:guid) { should be_a(String) }
its(:properties) { should be_a(Hash) }
end
before { Hubspot.configure(hapikey: 'demo', portal_id: '62515') }
describe '.all' do
cassette 'find_all_forms'
it 'returns all forms' do
forms = Hubspot::Form.all
expect(forms.count).to be > 20
form = forms.first
expect(form).to be_a(Hubspot::Form)
end
end
describe '.find' do
cassette 'form_find'
subject { Hubspot::Form.find(guid) }
context 'when the form is found' do
it { should be_an_instance_of Hubspot::Form }
its(:fields) { should_not be_empty }
end
context 'when the form is not found' do
it 'raises an error' do
expect { Hubspot::Form.find(-1) }.to raise_error(Hubspot::RequestError)
end
end
end
describe '.create' do
subject { Hubspot::Form.create!(params) }
context 'with all required parameters' do
cassette 'create_form'
let(:params) do
{
name: "Demo Form #{Time.now.to_i}",
action: '',
method: 'POST',
cssClass: 'hs-form stacked',
redirect: '',
submitText: 'Sign Up',
followUpId: '',
leadNurturingCampaignId: '',
notifyRecipients: '',
embeddedCode: ''
}
end
it { should be_an_instance_of Hubspot::Form }
its(:guid) { should be_a(String) }
end
context 'without all required parameters' do
cassette 'fail_to_create_form'
it 'raises an error' do
expect { Hubspot::Form.create!({}) }.to raise_error(Hubspot::RequestError)
end
end
end
describe '#fields' do
context 'returning all the fields' do
cassette 'fields_among_form'
let(:form) { Hubspot::Form.new(example_form_hash) }
it 'returns by default the fields property if present' do
fields = form.fields
fields.should_not be_empty
end
it 'updates the fields property and returns it' do
fields = form.fields(bypass_cache: true)
fields.should_not be_empty
end
end
context 'returning an uniq field' do
cassette 'field_among_form'
let(:form) { Hubspot::Form.new(example_form_hash) }
let(:field_name) { form.fields.first['name'] }
it 'returns by default the field if present as a property' do
field = form.fields(only: field_name)
expect(field).to be_a(Hash)
expect(field['name']).to be == field_name
end
it 'makes an API request if specified' do
field = form.fields(only: field_name, bypass_cache: true)
expect(field).to be_a(Hash)
expect(field['name']).to be == field_name
end
end
end
describe '#submit' do
cassette 'form_submit_data'
let(:form) { Hubspot::Form.find('561d9ce9-bb4c-45b4-8e32-21cdeaa3a7f0') }
context 'with a valid portal id' do
before do
Hubspot.configure(hapikey: 'demo', portal_id: '62515')
end
it 'returns true if the form submission is successful' do
params = {}
result = form.submit(params)
result.should be true
end
end
context 'with an invalid portal id' do
before do
Hubspot.configure(hapikey: 'demo', portal_id: 'xxxx')
end
it 'returns false in case of errors' do
params = { unknown_field: :bogus_value }
result = form.submit(params)
result.should be false
end
end
context 'when initializing Hubspot::Form directly' do
let(:form) { Hubspot::Form.new('guid' => '561d9ce9-bb4c-45b4-8e32-21cdeaa3a7f0') }
before { Hubspot.configure(hapikey: 'demo', portal_id: '62515') }
it 'returns true if the form submission is successful' do
params = {}
result = form.submit(params)
result.should be true
end
end
end
describe '#update!' do
cassette 'form_update'
new_name = 'updated form name 1424709912'
redirect = 'http://hubspot.com'
let(:form) { Hubspot::Form.find('561d9ce9-bb4c-45b4-8e32-21cdeaa3a7f0') }
let(:params) { { name: new_name, redirect: redirect } }
subject { form.update!(params) }
it { should be_an_instance_of Hubspot::Form }
it 'updates properties' do
subject.properties['name'].should be == new_name
subject.properties['redirect'].should be == redirect
end
end
describe '#destroy!' do
cassette 'form_destroy'
# NOTE: form previous created via the create! method
let(:form) { Hubspot::Form.find('beb92950-ca65-4daf-87ae-a42c054e429f') }
subject { form.destroy! }
it { should be true }
it 'should be destroyed' do
subject
form.destroyed?.should be true
end
end
end