synnefo.py 3.28 KB
Newer Older
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
# Copyright (C) 2016 GRNET S.A.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License

from soi import synnefo
from mock import patch
from json import dumps


def test__stringify_json_values():
    """Make strings of all single values in a json"""
    examples = (
        (  # Simple case, trouble free
            {'a': 'a', 'b': ['a', 'b'], 'c': {'a': 'a', 'b': 'b'}},
            {'a': 'a', 'b': ['a', 'b'], 'c': {'a': 'a', 'b': 'b'}},
        ),
        (  # Numbers as values and 1 level nesting
            {'a': 1, 'b': [1, 'b'], 'c': {'a': 'a', 'b': 2}},
            {'a': '1', 'b': ['1', 'b'], 'c': {'a': 'a', 'b': '2'}},
        ),
        (  # Numbers as keys must stay unchanged
            {1: 'a', 'b': [1, 'b'], 3: {1: 'a', 'b': 2}},
            {1: 'a', 'b': ['1', 'b'], 3: {1: 'a', 'b': '2'}},
        ),
        (  # 2-level nesting
            {
                'a': 1,
                'b': [{1: 1, 2: '2'}, 'b'],
                'c': {'a': 'a', 'b': [1, '2', 'c']}},
            {
                'a': '1',
                'b': [{1: '1', 2: '2'}, 'b'],
                'c': {'a': 'a', 'b': ['1', '2', 'c']}},
        )
    )

    for input_, output_ in examples:
        r = synnefo._stringify_json_values(input_)
        assert r == output_


class DummyResponse:
    status_code = 1000
    status = 'status'
    headers = {
        'content-length': '10', 'content-type': 'application/json'}
    json = {"a": "b"}
    content = dumps(json)


def my_method(cls, *args, **kwargs):
    """dummy method"""
    return DummyResponse()


def start_response(*args, **kwargs):
    """dummy method"""
synnefo.start_response = start_response
synnefo.CycladesNetworkClient.my_method = my_method
synnefo.CycladesComputeClient.my_method = my_method


@patch('soi.synnefo.start_response')
def test_call_kamaki_compute(start_response):
    """This is the Synnefo-end of the application"""
    environ = dict(
        method_name='my_method',
        service_type='compute',
        HTTP_X_AUTH_TOKEN='my-token')
    r = synnefo.call_kamaki(environ, start_response)
    assert r == DummyResponse.content


@patch('soi.synnefo.start_response')
def test_call_kamaki_compute_no_body(start_response):
    environ = dict(
        method_name='my_method',
        service_type='compute',
        HTTP_X_AUTH_TOKEN='my-token')
    DummyResponse.json = None
    DummyResponse.content = None
    r = synnefo.call_kamaki(environ, start_response)
    assert r == ''


@patch('soi.synnefo.start_response')
def test_call_kamaki_network(start_response):
    environ = dict(
        method_name='my_method',
        service_type='network',
        HTTP_X_AUTH_TOKEN='my-token')
    DummyResponse.json = {'b': 'c'}
    DummyResponse.content = dumps(DummyResponse.json)
    r = synnefo.call_kamaki(environ, start_response)
    assert r == DummyResponse.content