Skip to content

Commit

Permalink
test: Integration test for create configuration
Browse files Browse the repository at this point in the history
It integrates the client with the API, saves the response and use it in
the next tests. It includes the betamax library to record and replay
tests so we don't rely on hand written mocks.
  • Loading branch information
mauricioabreu committed Jun 6, 2018
1 parent 8576fbe commit 9d00945
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 18 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pytest = "*"
sphinx = "*"
coverage = "*"
proselint = "*"
betamax = "*"

[packages]
requests = "*"
Expand Down
44 changes: 26 additions & 18 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Configuration for all test cases."""
import json
import os

import betamax

from betamax.serializers import JSONSerializer


class PrettyJSONSerializer(JSONSerializer):
"""Serializer that saves all cassettes
in a human readable format, indenting and sorting keys."""
name = 'prettyjson'

def serialize(self, cassette_data):
return json.dumps(
cassette_data,
sort_keys=True,
indent=2,
separators=(',', ': '),
)


token = os.environ.get('AZ_TOKEN', 'foobar')

# Register pretty JSON serializer
betamax.Betamax.register_serializer(PrettyJSONSerializer)

with betamax.Betamax.configure() as config:
# Directory to save/load requests and responses
# executed by betamax
config.cassette_library_dir = 'tests/integration/cassettes'
# Change real token by this placeholder
config.define_cassette_placeholder('<AUTH_TOKEN>', token)
# Save formatted JSON instead of one line files
config.default_cassette_options['serialize_with'] = 'prettyjson'
Empty file added tests/integration/__init__.py
Empty file.
97 changes: 97 additions & 0 deletions tests/integration/cassettes/Configuration_create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"http_interactions": [
{
"recorded_at": "2018-06-06T02:20:05",
"request": {
"body": {
"encoding": "utf-8",
"string": "{\"name\": \"Dummy configuration\", \"origin_address\": \"www.example.com\", \"origin_host_header\": \"ww2.example.com\", \"cname\": [\"www.example-cname.com\"], \"cname_access_only\": false, \"delivery_protocol\": \"http\", \"origin_protocol_policy\": \"preserve\", \"browser_cache_settings\": false, \"browser_cache_settings_maximum_ttl\": 0, \"cdn_cache_settings\": \"honor\", \"cdn_cache_settings_maximum_ttl\": 0}"
},
"headers": {
"Accept": [
"application/json; version=1"
],
"Accept-Charset": [
"utf-8"
],
"Accept-Encoding": [
"gzip, deflate"
],
"Authorization": [
"token <AUTH_TOKEN>"
],
"Connection": [
"keep-alive"
],
"Content-Length": [
"382"
],
"Content-Type": [
"application/json"
],
"User-Agent": [
"python-requests/2.18.4"
]
},
"method": "POST",
"uri": "https://api.azion.net/content_delivery/configurations"
},
"response": {
"body": {
"encoding": null,
"string": "{\"id\":1528252734,\"name\":\"Dummy configuration\",\"domain_name\":\"109445k.ha.azioncdn.net\",\"active\":true,\"delivery_protocol\":\"http\",\"digital_certificate\":null,\"cname_access_only\":false,\"rawlogs\":false,\"cname\":[\"www.example-cname.com\"]}"
},
"headers": {
"Allow": [
"GET, POST, OPTIONS"
],
"Connection": [
"keep-alive"
],
"Content-Language": [
"en"
],
"Content-Length": [
"230"
],
"Content-Type": [
"application/json"
],
"Date": [
"Wed, 06 Jun 2018 02:20:05 GMT"
],
"Server": [
"azion webserver"
],
"Vary": [
"Accept-Language, Cookie"
],
"X-Frame-Options": [
"SAMEORIGIN"
],
"X-RateLimit-Limit": [
"20"
],
"X-RateLimit-Remaining": [
"18"
],
"X-RateLimit-Reset": [
"2018-06-06T02:21:04.340405"
],
"x-content-type-options": [
"nosniff"
],
"x-xss-protection": [
"1; mode=block"
]
},
"status": {
"code": 201,
"message": "CREATED"
},
"url": "https://api.azion.net/content_delivery/configurations"
}
}
],
"recorded_with": "betamax/0.8.1"
}
30 changes: 30 additions & 0 deletions tests/integration/test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os

from azion.client import Azion
from azion.models import Configuration

import betamax

token = os.environ.get('AZ_TOKEN', 'foobar')


class TestConfiguration(object):

def test_create_configuration(self):
client = Azion(token)
recorder = betamax.Betamax(client.session)

with recorder.use_cassette('Configuration_create'):
configuration = client.create_configuration(
'Dummy configuration', 'www.example.com', 'ww2.example.com',
cname=['www.example-cname.com'], delivery_protocol='http')
assert isinstance(configuration, Configuration)
assert configuration.active is True
assert configuration.id
assert configuration.cname == ['www.example-cname.com']
assert configuration.digital_certificate is None
assert configuration.rawlogs is False
assert configuration.delivery_protocol == 'http'
assert configuration.cname_access_only is False
assert configuration.name == 'Dummy configuration'
assert configuration.domain_name

0 comments on commit 9d00945

Please sign in to comment.