notifications-ruby-client 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +12 -0
- data/.gitignore +17 -0
- data/.rspec +3 -0
- data/.rubocop.yml +2 -0
- data/CHANGELOG.md +100 -0
- data/CONTRIBUTING.md +39 -0
- data/DOCUMENTATION.md +949 -0
- data/Gemfile +3 -0
- data/LICENSE +21 -0
- data/Makefile +87 -0
- data/README.md +10 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/generate_docker_env.sh +29 -0
- data/bin/setup +8 -0
- data/bin/test_client.rb +454 -0
- data/docker/Dockerfile +26 -0
- data/docker/Makefile +16 -0
- data/lib/notifications/client.rb +153 -0
- data/lib/notifications/client/helper_methods.rb +9 -0
- data/lib/notifications/client/notification.rb +54 -0
- data/lib/notifications/client/notifications_collection.rb +19 -0
- data/lib/notifications/client/received_text.rb +31 -0
- data/lib/notifications/client/received_text_collection.rb +18 -0
- data/lib/notifications/client/request_error.rb +59 -0
- data/lib/notifications/client/response_notification.rb +21 -0
- data/lib/notifications/client/response_precompiled_letter.rb +19 -0
- data/lib/notifications/client/response_template.rb +41 -0
- data/lib/notifications/client/speaker.rb +171 -0
- data/lib/notifications/client/template_collection.rb +16 -0
- data/lib/notifications/client/template_preview.rb +22 -0
- data/lib/notifications/client/uuid_validator.rb +25 -0
- data/lib/notifications/client/version.rb +14 -0
- data/notifications-ruby-client.gemspec +31 -0
- metadata +181 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Government Digital Service
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/Makefile
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
.DEFAULT_GOAL := help
|
2
|
+
SHELL := /bin/bash
|
3
|
+
|
4
|
+
DOCKER_BUILDER_IMAGE_NAME = govuk/notify-ruby-client-builder
|
5
|
+
|
6
|
+
BUILD_TAG ?= notifications-ruby-client-manual
|
7
|
+
|
8
|
+
DOCKER_CONTAINER_PREFIX = ${USER}-${BUILD_TAG}
|
9
|
+
|
10
|
+
.PHONY: help
|
11
|
+
help:
|
12
|
+
@cat $(MAKEFILE_LIST) | grep -E '^[a-zA-Z_-]+:.*?## .*$$' | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
|
13
|
+
|
14
|
+
.PHONY: dependencies
|
15
|
+
dependencies: ## Install build dependencies
|
16
|
+
bundle install --path=vendor/bundle --binstubs=vendor/bin
|
17
|
+
|
18
|
+
.PHONY: build
|
19
|
+
build: dependencies ## Build project
|
20
|
+
|
21
|
+
.PHONY: test
|
22
|
+
test: ## Run tests
|
23
|
+
bundle exec rake spec
|
24
|
+
|
25
|
+
.PHONY: integration-test
|
26
|
+
integration-test: ## Run integration tests
|
27
|
+
bundle exec bin/test_client.rb
|
28
|
+
|
29
|
+
.PHONY: generate-env-file
|
30
|
+
generate-env-file: ## Generate the environment file for running the tests inside a Docker container
|
31
|
+
bin/generate_docker_env.sh
|
32
|
+
|
33
|
+
.PHONY: prepare-docker-runner-image
|
34
|
+
prepare-docker-runner-image: ## Prepare the Docker builder image
|
35
|
+
make -C docker build
|
36
|
+
|
37
|
+
.PHONY: build-with-docker
|
38
|
+
build-with-docker: prepare-docker-runner-image ## Build inside a Docker container
|
39
|
+
docker run -i --rm \
|
40
|
+
--name "${DOCKER_CONTAINER_PREFIX}-build" \
|
41
|
+
-v "`pwd`:/var/project" \
|
42
|
+
-e http_proxy="${HTTP_PROXY}" \
|
43
|
+
-e HTTP_PROXY="${HTTP_PROXY}" \
|
44
|
+
-e https_proxy="${HTTPS_PROXY}" \
|
45
|
+
-e HTTPS_PROXY="${HTTPS_PROXY}" \
|
46
|
+
-e NO_PROXY="${NO_PROXY}" \
|
47
|
+
${DOCKER_BUILDER_IMAGE_NAME} \
|
48
|
+
make build
|
49
|
+
|
50
|
+
.PHONY: test-with-docker
|
51
|
+
test-with-docker: prepare-docker-runner-image generate-env-file ## Run tests inside a Docker container
|
52
|
+
docker run -i --rm \
|
53
|
+
--name "${DOCKER_CONTAINER_PREFIX}-test" \
|
54
|
+
-v "`pwd`:/var/project" \
|
55
|
+
-e http_proxy="${HTTP_PROXY}" \
|
56
|
+
-e HTTP_PROXY="${HTTP_PROXY}" \
|
57
|
+
-e https_proxy="${HTTPS_PROXY}" \
|
58
|
+
-e HTTPS_PROXY="${HTTPS_PROXY}" \
|
59
|
+
-e NO_PROXY="${NO_PROXY}" \
|
60
|
+
--env-file docker.env \
|
61
|
+
${DOCKER_BUILDER_IMAGE_NAME} \
|
62
|
+
make test
|
63
|
+
|
64
|
+
.PHONY: integration-test-with-docker
|
65
|
+
integration-test-with-docker: prepare-docker-runner-image generate-env-file ## Run integration tests inside a Docker container
|
66
|
+
docker run -i --rm \
|
67
|
+
--name "${DOCKER_CONTAINER_PREFIX}-integration-test" \
|
68
|
+
-v "`pwd`:/var/project" \
|
69
|
+
-e http_proxy="${HTTP_PROXY}" \
|
70
|
+
-e HTTP_PROXY="${HTTP_PROXY}" \
|
71
|
+
-e https_proxy="${HTTPS_PROXY}" \
|
72
|
+
-e HTTPS_PROXY="${HTTPS_PROXY}" \
|
73
|
+
-e NO_PROXY="${NO_PROXY}" \
|
74
|
+
--env-file docker.env \
|
75
|
+
${DOCKER_BUILDER_IMAGE_NAME} \
|
76
|
+
make integration-test
|
77
|
+
|
78
|
+
.PHONY: clean-docker-containers
|
79
|
+
clean-docker-containers: ## Clean up any remaining docker containers
|
80
|
+
docker rm -f $(shell docker ps -q -f "name=${DOCKER_CONTAINER_PREFIX}") 2> /dev/null || true
|
81
|
+
|
82
|
+
.PHONY: run-govuk-lint
|
83
|
+
run-govuk-lint: ## Runs GOVUK-lint for Ruby
|
84
|
+
bundle exec govuk-lint-ruby lib spec bin/test_client
|
85
|
+
|
86
|
+
clean:
|
87
|
+
rm -rf vendor
|
data/README.md
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
# GOV.UK Notify Ruby client
|
2
|
+
|
3
|
+
Use this client to send emails, text messages and letters using the [GOV.UK Notify](https://www.notifications.service.gov.uk) API.
|
4
|
+
|
5
|
+
Useful links:
|
6
|
+
|
7
|
+
- [Documentation](https://docs.notifications.service.gov.uk/ruby.html)
|
8
|
+
- [Ruby gem](https://rubygems.org/gems/notifications-ruby-client)
|
9
|
+
- [Changelog](https://github.com/alphagov/notifications-ruby-client/blob/master/CHANGELOG.md)
|
10
|
+
- [Contributing to this client](https://github.com/alphagov/notifications-ruby-client/blob/master/CONTRIBUTING.md)
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "notifications/client"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
set -eo pipefail
|
4
|
+
|
5
|
+
function exit_with_msg {
|
6
|
+
echo $1
|
7
|
+
exit $2
|
8
|
+
}
|
9
|
+
|
10
|
+
echo -n "" > docker.env
|
11
|
+
|
12
|
+
env_vars=(
|
13
|
+
NOTIFY_API_URL
|
14
|
+
SERVICE_ID
|
15
|
+
API_KEY
|
16
|
+
FUNCTIONAL_TEST_EMAIL
|
17
|
+
FUNCTIONAL_TEST_NUMBER
|
18
|
+
EMAIL_TEMPLATE_ID
|
19
|
+
SMS_TEMPLATE_ID
|
20
|
+
LETTER_TEMPLATE_ID
|
21
|
+
EMAIL_REPLY_TO_ID
|
22
|
+
SMS_SENDER_ID
|
23
|
+
API_SENDING_KEY
|
24
|
+
INBOUND_SMS_QUERY_KEY
|
25
|
+
)
|
26
|
+
|
27
|
+
for env_var in "${env_vars[@]}"; do
|
28
|
+
echo "${env_var}=${!env_var}" >> docker.env
|
29
|
+
done
|
data/bin/setup
ADDED
data/bin/test_client.rb
ADDED
@@ -0,0 +1,454 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require './lib/notifications/client'
|
3
|
+
|
4
|
+
def main
|
5
|
+
client = Notifications::Client.new(ENV['API_KEY'], ENV['NOTIFY_API_URL'])
|
6
|
+
test_get_template_by_id(client, ENV['EMAIL_TEMPLATE_ID'])
|
7
|
+
test_get_template_version(client, ENV['SMS_TEMPLATE_ID'], 1)
|
8
|
+
test_get_all_templates(client)
|
9
|
+
test_get_all_templates_filter_by_type(client)
|
10
|
+
test_generate_template_preview(client, ENV['EMAIL_TEMPLATE_ID'])
|
11
|
+
email_notification = test_send_email_endpoint(client)
|
12
|
+
email_notification_with_document = test_send_email_endpoint_with_document(client)
|
13
|
+
sms_notification = test_send_sms_endpoint(client)
|
14
|
+
letter_notification = test_send_letter_endpoint(client)
|
15
|
+
precompiled_letter_notification = test_send_precompiled_letter_endpoint(client)
|
16
|
+
test_get_notification_by_id_endpoint(client, email_notification.id, 'email')
|
17
|
+
test_get_notification_by_id_endpoint(client, email_notification_with_document.id, 'email')
|
18
|
+
test_get_notification_by_id_endpoint(client, sms_notification.id, 'sms')
|
19
|
+
test_get_notification_by_id_endpoint(client, letter_notification.id, 'letter')
|
20
|
+
test_get_notification_by_id_endpoint(client, precompiled_letter_notification.id, 'precompiled_letter')
|
21
|
+
test_get_all_notifications(client)
|
22
|
+
test_get_received_texts
|
23
|
+
p 'ruby client integration tests pass'
|
24
|
+
exit 0
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_get_template_by_id(client, id)
|
28
|
+
response = client.get_template_by_id(id)
|
29
|
+
test_template_response(response, 'test_get_template_by_id')
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_get_template_version(client, id, version)
|
33
|
+
response = client.get_template_version(id, version)
|
34
|
+
test_template_response(response, 'test_get_template_version')
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_get_all_templates(client)
|
38
|
+
response = client.get_all_templates
|
39
|
+
unless response.is_a?(Notifications::Client::TemplateCollection)
|
40
|
+
p 'failed test_get_all_templates response is not a Notifications::Client::TemplateCollection'
|
41
|
+
exit 1
|
42
|
+
end
|
43
|
+
unless response.collection.length >= 3
|
44
|
+
p 'failed test_get_all_templates, expected at least 3 templates returned.'
|
45
|
+
exit 1
|
46
|
+
end
|
47
|
+
test_template_response(response.collection[0], 'test_get_all_templates')
|
48
|
+
test_template_response(response.collection[1], 'test_get_all_templates')
|
49
|
+
test_template_response(response.collection[2], 'test_get_all_templates')
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_get_all_templates_filter_by_type(client)
|
53
|
+
response = client.get_all_templates('type' => 'sms')
|
54
|
+
unless response.is_a?(Notifications::Client::TemplateCollection)
|
55
|
+
p 'failed test_get_all_templates response is not a Notifications::Client::TemplateCollection'
|
56
|
+
exit 1
|
57
|
+
end
|
58
|
+
unless response.collection.length >= 1
|
59
|
+
p 'failed test_get_all_templates, expected at least 2 templates returned.'
|
60
|
+
exit 1
|
61
|
+
end
|
62
|
+
test_template_response(response.collection[0], 'test_get_all_templates')
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_generate_template_preview(client, id)
|
66
|
+
response = client.generate_template_preview(id, personalisation: { "name" => "some name" })
|
67
|
+
test_template_preview(response)
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_template_response(response, test_method)
|
71
|
+
unless response.is_a?(Notifications::Client::Template)
|
72
|
+
p 'failed test_get_template_by_id response is not a Notifications::Client::Template'
|
73
|
+
exit 1
|
74
|
+
end
|
75
|
+
unless response.id.is_a?(String)
|
76
|
+
p 'failed template id is not a String'
|
77
|
+
exit 1
|
78
|
+
end
|
79
|
+
field_should_not_be_nil(expected_fields_in_template_response, response, test_method)
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_template_preview(response)
|
83
|
+
unless response.is_a?(Notifications::Client::TemplatePreview)
|
84
|
+
p 'failed test_generate_template_preview response is not a Notifications::Client::TemplatePreview'
|
85
|
+
exit 1
|
86
|
+
end
|
87
|
+
unless response.id.is_a?(String)
|
88
|
+
p 'failed template id is not a String'
|
89
|
+
exit 1
|
90
|
+
end
|
91
|
+
field_should_not_be_nil(expected_fields_in_template_preview, response, 'generate_template_preview')
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_send_email_endpoint(client)
|
95
|
+
email_resp = client.send_email(email_address: ENV['FUNCTIONAL_TEST_EMAIL'],
|
96
|
+
template_id: ENV['EMAIL_TEMPLATE_ID'],
|
97
|
+
personalisation: { "name" => "some name" },
|
98
|
+
reference: "some reference",
|
99
|
+
email_reply_to_id: ENV['EMAIL_REPLY_TO_ID'])
|
100
|
+
test_notification_response_data_type(email_resp, 'email')
|
101
|
+
email_resp
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_send_email_endpoint_with_document(client)
|
105
|
+
email_resp = File.open('spec/test_files/test_pdf.pdf', 'rb') do |f|
|
106
|
+
client.send_email(email_address: ENV['FUNCTIONAL_TEST_EMAIL'],
|
107
|
+
template_id: ENV['EMAIL_TEMPLATE_ID'],
|
108
|
+
personalisation: { name: Notifications.prepare_upload(f) },
|
109
|
+
reference: "some reference",
|
110
|
+
email_reply_to_id: ENV['EMAIL_REPLY_TO_ID'])
|
111
|
+
end
|
112
|
+
|
113
|
+
test_notification_response_data_type(email_resp, 'email')
|
114
|
+
email_resp
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_send_sms_endpoint(client)
|
118
|
+
sms_resp = client.send_sms(phone_number: ENV['FUNCTIONAL_TEST_NUMBER'], template_id: ENV['SMS_TEMPLATE_ID'],
|
119
|
+
personalisation: { "name" => "some name" },
|
120
|
+
reference: "some reference",
|
121
|
+
sms_sender_id: ENV['SMS_SENDER_ID'])
|
122
|
+
test_notification_response_data_type(sms_resp, 'sms')
|
123
|
+
sms_resp
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_send_letter_endpoint(client)
|
127
|
+
letter_resp = client.send_letter(
|
128
|
+
template_id: ENV['LETTER_TEMPLATE_ID'],
|
129
|
+
personalisation: {
|
130
|
+
address_line_1: "Her Majesty The Queen",
|
131
|
+
address_line_2: "Buckingham Palace",
|
132
|
+
postcode: "SW1 1AA"
|
133
|
+
},
|
134
|
+
reference: "some reference"
|
135
|
+
)
|
136
|
+
test_notification_response_data_type(letter_resp, 'letter')
|
137
|
+
letter_resp
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_send_precompiled_letter_endpoint(client)
|
141
|
+
precompiled_letter_resp = File.open('spec/test_files/test_pdf.pdf', 'rb') do |file|
|
142
|
+
client.send_precompiled_letter("some reference", file, "first")
|
143
|
+
end
|
144
|
+
|
145
|
+
test_notification_response_data_type(precompiled_letter_resp, 'precompiled_letter')
|
146
|
+
|
147
|
+
precompiled_letter_resp
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_notification_response_data_type(notification, message_type)
|
151
|
+
unless notification.is_a?(Notifications::Client::ResponseNotification) || (notification.is_a?(Notifications::Client::ResponsePrecompiledLetter) && message_type == "precompiled_letter")
|
152
|
+
p 'failed ' + message_type + ' response is not a Notifications::Client::ResponseNotification'
|
153
|
+
exit 1
|
154
|
+
end
|
155
|
+
unless notification.id.is_a?(String)
|
156
|
+
p 'failed ' + message_type + 'id is not a String'
|
157
|
+
exit 1
|
158
|
+
end
|
159
|
+
|
160
|
+
if message_type == 'precompiled_letter'
|
161
|
+
field_should_not_be_nil(expected_fields_in_precompiled_letter_response, notification, 'send_precompiled_letter')
|
162
|
+
if notification.postage != "first"
|
163
|
+
p "Postage should be set to 'first' for precompiled letter sending test. Right now it is set to #{notification.postage}"
|
164
|
+
exit 1
|
165
|
+
end
|
166
|
+
return
|
167
|
+
end
|
168
|
+
|
169
|
+
field_should_not_be_nil(expected_fields_in_notification_response, notification, 'send_' + message_type)
|
170
|
+
hash_key_should_not_be_nil(expected_fields_in_template, notification.send('template'), 'send_' + message_type + '.template')
|
171
|
+
|
172
|
+
if message_type == 'email'
|
173
|
+
hash_key_should_not_be_nil(expected_fields_in_email_content, notification.send('content'), 'send_' + message_type + '.content')
|
174
|
+
elsif message_type == 'sms'
|
175
|
+
hash_key_should_not_be_nil(expected_fields_in_sms_content, notification.send('content'), 'send_' + message_type + '.content')
|
176
|
+
elsif message_type == 'letter'
|
177
|
+
hash_key_should_not_be_nil(expected_fields_in_letter_content, notification.send('content'), 'send_' + message_type + '.content')
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
def test_get_notification_by_id_endpoint(client, id, message_type)
|
182
|
+
get_notification_response = client.get_notification(id)
|
183
|
+
|
184
|
+
unless get_notification_response.is_a?(Notifications::Client::Notification)
|
185
|
+
p 'get notification is not a Notifications::Client::Notification for id ' + id
|
186
|
+
exit 1
|
187
|
+
end
|
188
|
+
|
189
|
+
if message_type == 'email'
|
190
|
+
field_should_not_be_nil(expected_fields_in_email_notification, get_notification_response, 'Notifications::Client::Notification for type email')
|
191
|
+
field_should_be_nil(expected_fields_in_email_notification_that_are_nil, get_notification_response, 'Notifications::Client::Notification for type email')
|
192
|
+
hash_key_should_not_be_nil(expected_fields_in_template, get_notification_response.send('template'), 'Notifications::Client::Notification.template for type email')
|
193
|
+
elsif message_type == 'sms'
|
194
|
+
field_should_not_be_nil(expected_fields_in_sms_notification, get_notification_response, 'Notifications::Client::Notification for type sms')
|
195
|
+
field_should_be_nil(expected_fields_in_sms_notification_that_are_nil, get_notification_response, 'Notifications::Client::Notification for type sms')
|
196
|
+
hash_key_should_not_be_nil(expected_fields_in_template, get_notification_response.send('template'), 'Notifications::Client::Notification.template for type sms')
|
197
|
+
elsif message_type == 'letter'
|
198
|
+
field_should_not_be_nil(expected_fields_in_letter_notification, get_notification_response, 'Notifications::Client::Notification for type letter')
|
199
|
+
field_should_be_nil(expected_fields_in_letter_notification_that_are_nil, get_notification_response, 'Notifications::Client::Notification for type letter')
|
200
|
+
hash_key_should_not_be_nil(expected_fields_in_template, get_notification_response.send('template'), 'Notifications::Client::Notification.template for type letter')
|
201
|
+
elsif message_type == 'precompiled_letter'
|
202
|
+
field_should_not_be_nil(expected_fields_in_precompiled_letter_notification, get_notification_response, 'Notifications::Client::Notification for type precompiled letter')
|
203
|
+
field_should_be_nil(expected_fields_in_precompiled_letter_notification_that_are_nil, get_notification_response, 'Notifications::Client::Notification for type precompiled letter')
|
204
|
+
hash_key_should_not_be_nil(expected_fields_in_template, get_notification_response.send('template'), 'Notifications::Client::Notification.template for type precompiled letter')
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
def hash_key_should_not_be_nil(fields, obj, method_name)
|
209
|
+
fields.each do |field|
|
210
|
+
if obj.has_value?(:"#{field}")
|
211
|
+
p 'contract test failed because ' + field + ' should not be nil for ' + method_name + ' response'
|
212
|
+
exit 1
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
def field_should_not_be_nil(fields, obj, method_name)
|
218
|
+
fields.each do |field|
|
219
|
+
if obj.send(:"#{field}") == nil
|
220
|
+
p 'contract test failed because ' + field + ' should not be nil for ' + method_name + ' response'
|
221
|
+
exit 1
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
def field_should_be_nil(fields, obj, method_name)
|
227
|
+
fields.each do |field|
|
228
|
+
if obj.send(:"#{field}") != nil
|
229
|
+
p 'contract test failed because ' + field + ' should be nil for ' + method_name + ' response'
|
230
|
+
exit 1
|
231
|
+
end
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
def expected_fields_in_template_response
|
236
|
+
%w(id
|
237
|
+
name
|
238
|
+
type
|
239
|
+
created_at
|
240
|
+
created_by
|
241
|
+
body
|
242
|
+
version)
|
243
|
+
end
|
244
|
+
|
245
|
+
def expected_fields_in_template_preview
|
246
|
+
%w(id
|
247
|
+
body
|
248
|
+
version
|
249
|
+
type
|
250
|
+
html)
|
251
|
+
end
|
252
|
+
|
253
|
+
def expected_fields_in_notification_response
|
254
|
+
%w(id
|
255
|
+
reference
|
256
|
+
content
|
257
|
+
template
|
258
|
+
uri)
|
259
|
+
end
|
260
|
+
|
261
|
+
def expected_fields_in_precompiled_letter_response
|
262
|
+
%w(id
|
263
|
+
reference
|
264
|
+
postage)
|
265
|
+
end
|
266
|
+
|
267
|
+
def expected_fields_in_email_content
|
268
|
+
%w(from_email
|
269
|
+
body
|
270
|
+
subject)
|
271
|
+
end
|
272
|
+
|
273
|
+
def expected_fields_in_sms_content
|
274
|
+
%w(body
|
275
|
+
from_number)
|
276
|
+
end
|
277
|
+
|
278
|
+
def expected_fields_in_letter_content
|
279
|
+
%w(
|
280
|
+
body
|
281
|
+
subject
|
282
|
+
)
|
283
|
+
end
|
284
|
+
|
285
|
+
def expected_fields_in_email_notification
|
286
|
+
%w(id
|
287
|
+
email_address
|
288
|
+
type
|
289
|
+
status
|
290
|
+
template
|
291
|
+
body
|
292
|
+
subject
|
293
|
+
created_at)
|
294
|
+
end
|
295
|
+
|
296
|
+
def expected_fields_in_email_notification_that_are_nil
|
297
|
+
%w(phone_number
|
298
|
+
line_1
|
299
|
+
line_2
|
300
|
+
line_3
|
301
|
+
line_4
|
302
|
+
line_5
|
303
|
+
line_5
|
304
|
+
line_6
|
305
|
+
postcode
|
306
|
+
created_by_name
|
307
|
+
postage)
|
308
|
+
end
|
309
|
+
|
310
|
+
def expected_fields_in_sms_notification
|
311
|
+
%w(id
|
312
|
+
phone_number
|
313
|
+
type
|
314
|
+
status
|
315
|
+
template
|
316
|
+
body
|
317
|
+
created_at)
|
318
|
+
end
|
319
|
+
|
320
|
+
def expected_fields_in_sms_notification_that_are_nil
|
321
|
+
%w(email_address
|
322
|
+
line_1
|
323
|
+
line_2
|
324
|
+
line_3
|
325
|
+
line_4
|
326
|
+
line_5
|
327
|
+
line_5
|
328
|
+
line_6
|
329
|
+
postcode
|
330
|
+
subject
|
331
|
+
created_by_name
|
332
|
+
postage)
|
333
|
+
end
|
334
|
+
|
335
|
+
def expected_fields_in_letter_notification
|
336
|
+
%w(
|
337
|
+
id
|
338
|
+
type
|
339
|
+
status
|
340
|
+
template
|
341
|
+
body
|
342
|
+
subject
|
343
|
+
line_1
|
344
|
+
line_2
|
345
|
+
postcode
|
346
|
+
created_at
|
347
|
+
postage
|
348
|
+
)
|
349
|
+
end
|
350
|
+
|
351
|
+
def expected_fields_in_letter_notification_that_are_nil
|
352
|
+
%w(
|
353
|
+
phone_number
|
354
|
+
email_address
|
355
|
+
line_3
|
356
|
+
line_4
|
357
|
+
line_5
|
358
|
+
line_5
|
359
|
+
line_6
|
360
|
+
created_by_name
|
361
|
+
)
|
362
|
+
end
|
363
|
+
|
364
|
+
def expected_fields_in_precompiled_letter_notification
|
365
|
+
%w(
|
366
|
+
body
|
367
|
+
created_at
|
368
|
+
id
|
369
|
+
line_1
|
370
|
+
reference
|
371
|
+
status
|
372
|
+
subject
|
373
|
+
template
|
374
|
+
type
|
375
|
+
postage
|
376
|
+
)
|
377
|
+
end
|
378
|
+
|
379
|
+
def expected_fields_in_precompiled_letter_notification_that_are_nil
|
380
|
+
%w(
|
381
|
+
completed_at
|
382
|
+
created_by_name
|
383
|
+
email_address
|
384
|
+
line_2
|
385
|
+
line_3
|
386
|
+
line_4
|
387
|
+
line_5
|
388
|
+
line_6
|
389
|
+
phone_number
|
390
|
+
postcode
|
391
|
+
sent_at
|
392
|
+
)
|
393
|
+
end
|
394
|
+
|
395
|
+
def expected_fields_in_template
|
396
|
+
%w(id
|
397
|
+
version
|
398
|
+
uri)
|
399
|
+
end
|
400
|
+
|
401
|
+
def expected_fields_in_received_text_response
|
402
|
+
%w(id
|
403
|
+
created_at
|
404
|
+
content
|
405
|
+
notify_number
|
406
|
+
service_id
|
407
|
+
user_number)
|
408
|
+
end
|
409
|
+
|
410
|
+
def test_get_all_notifications(client)
|
411
|
+
notifications = client.get_notifications
|
412
|
+
unless notifications.is_a?(Notifications::Client::NotificationsCollection)
|
413
|
+
p 'get all notifications is not Notifications::Client::NotificationsCollection'
|
414
|
+
exit 1
|
415
|
+
end
|
416
|
+
field_should_not_be_nil(expected_fields_for_get_all_notifications, notifications, 'get_notifications')
|
417
|
+
end
|
418
|
+
|
419
|
+
def test_get_received_texts
|
420
|
+
client = Notifications::Client.new(ENV['INBOUND_SMS_QUERY_KEY'], ENV['NOTIFY_API_URL'])
|
421
|
+
response = client.get_received_texts
|
422
|
+
unless response.is_a?(Notifications::Client::ReceivedTextCollection)
|
423
|
+
p 'failed test_get_received_texts response is not a Notifications::Client::ReceivedTextCollection'
|
424
|
+
exit 1
|
425
|
+
end
|
426
|
+
unless response.collection.length >= 0
|
427
|
+
p 'failed test_get_received_texts, expected at least 1 received text returned.'
|
428
|
+
exit 1
|
429
|
+
end
|
430
|
+
test_received_text_response(response.collection[0], 'test_received_text_response')
|
431
|
+
test_received_text_response(response.collection[1], 'test_received_text_response')
|
432
|
+
test_received_text_response(response.collection[2], 'test_received_text_response')
|
433
|
+
end
|
434
|
+
|
435
|
+
def test_received_text_response(response, test_method)
|
436
|
+
unless response.is_a?(Notifications::Client::ReceivedText)
|
437
|
+
p 'failed test_get_received_texts response is not a Notifications::Client::ReceivedText'
|
438
|
+
exit 1
|
439
|
+
end
|
440
|
+
unless response.id.is_a?(String)
|
441
|
+
p 'failed received text id is not a String'
|
442
|
+
exit 1
|
443
|
+
end
|
444
|
+
field_should_not_be_nil(expected_fields_in_received_text_response, response, test_method)
|
445
|
+
end
|
446
|
+
|
447
|
+
def expected_fields_for_get_all_notifications
|
448
|
+
%W(links
|
449
|
+
collection)
|
450
|
+
end
|
451
|
+
|
452
|
+
if __FILE__ == $PROGRAM_NAME
|
453
|
+
main
|
454
|
+
end
|