summaryrefslogtreecommitdiff
path: root/troveclient/tests/test_accounts.py
blob: 8d8be1e8739c6b69f78bb3a4169aefe55faf5333 (plain)
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
# Copyright 2011 OpenStack Foundation
# Copyright 2013 Rackspace Hosting
# Copyright 2013 Hewlett-Packard Development Company, L.P.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import mock
import testtools

from troveclient import base
from troveclient.v1 import accounts

"""
Unit tests for accounts.py
"""


class AccountTest(testtools.TestCase):

    def setUp(self):
        super(AccountTest, self).setUp()
        self.orig__init = accounts.Account.__init__
        accounts.Account.__init__ = mock.Mock(return_value=None)
        self.account = accounts.Account()

    def tearDown(self):
        super(AccountTest, self).tearDown()
        accounts.Account.__init__ = self.orig__init

    def test___repr__(self):
        self.account.name = "account-1"
        self.assertEqual('<Account: account-1>', self.account.__repr__())


class AccountsTest(testtools.TestCase):

    def setUp(self):
        super(AccountsTest, self).setUp()
        self.orig__init = accounts.Accounts.__init__
        accounts.Accounts.__init__ = mock.Mock(return_value=None)
        self.accounts = accounts.Accounts()
        self.accounts.api = mock.Mock()
        self.accounts.api.client = mock.Mock()

    def tearDown(self):
        super(AccountsTest, self).tearDown()
        accounts.Accounts.__init__ = self.orig__init

    def test__list(self):
        def side_effect_func(self, val):
            return val

        self.accounts.resource_class = mock.Mock(side_effect=side_effect_func)
        key_ = 'key'
        body_ = {key_: "test-value"}
        self.accounts.api.client.get = mock.Mock(return_value=('resp', body_))
        self.assertEqual("test-value", self.accounts._list('url', key_))

        self.accounts.api.client.get = mock.Mock(return_value=('resp', None))
        self.assertRaises(Exception, self.accounts._list, 'url', None)

    def test_index(self):
        resp = mock.Mock()
        resp.status_code = 400
        body = {"Accounts": {}}
        self.accounts.api.client.get = mock.Mock(return_value=(resp, body))
        self.assertRaises(Exception, self.accounts.index)
        resp.status_code = 200
        self.assertIsInstance(self.accounts.index(), base.Resource)
        self.accounts.api.client.get = mock.Mock(return_value=(resp, None))
        self.assertRaises(Exception, self.accounts.index)

    def test_show(self):
        def side_effect_func(acct_name, acct):
            return acct_name, acct

        account_ = mock.Mock()
        account_.name = "test-account"
        self.accounts._list = mock.Mock(side_effect=side_effect_func)
        self.assertEqual(('/mgmt/accounts/test-account', 'account'),
                         self.accounts.show(account_))

    def test__get_account_name(self):
        account_ = 'account with no name'
        self.assertEqual(account_,
                         accounts.Accounts._get_account_name(account_))
        account_ = mock.Mock()
        account_.name = "account-name"
        self.assertEqual("account-name",
                         accounts.Accounts._get_account_name(account_))