~ericsnowcurrently/charms/trusty/reviewboard-oauth/trunk

« back to all changes in this revision

Viewing changes to hooks/tests/test_charm.py

  • Committer: Eric Snow
  • Date: 2014-09-02 15:10:09 UTC
  • Revision ID: ericsnowcurrently@gmail.com-20140902151009-g7attz6qmmpvozd4
Publish the charm in launchpad (for the sake of a private namespace in the charm store).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import unittest
 
2
 
 
3
from charm import RBOAuthCharm
 
4
 
 
5
 
 
6
class TestCharm(RBOAuthCharm):
 
7
 
 
8
    MSG = 'updating settings for the ReviewBoard reviewboard-oauth extension'
 
9
 
 
10
    def __init__(self):
 
11
        self.name = self.EXT_TYPE.NAME
 
12
        self.creds = ""  # the default
 
13
 
 
14
        self._ext = self
 
15
        self._rb = self
 
16
 
 
17
        self.logs = []
 
18
        self.calls = []
 
19
        self.settings = []
 
20
 
 
21
    def assertResult(self, test, settings=None, logs=None, calls=None):
 
22
        if not settings:
 
23
            settings = []
 
24
        if not logs:
 
25
            logs = [self.MSG]
 
26
        elif isinstance(logs, str):
 
27
            logs = [self.MSG, logs]
 
28
        else:
 
29
            logs.insert(0, self.MSG)
 
30
        if not calls:
 
31
            calls = []
 
32
        elif isinstance(calls, str):
 
33
            calls = [calls]
 
34
 
 
35
        test.assertEqual(self.settings, settings)
 
36
        test.assertEqual(self.calls, calls)
 
37
        test.assertEqual(self.logs, logs)
 
38
 
 
39
    # charm methods
 
40
 
 
41
    def config(self):
 
42
        return {'provider-credentials': self.creds}
 
43
 
 
44
    def log(self, msg):
 
45
        self.logs.append(msg)
 
46
 
 
47
    def cmd(self, cmd, *args):
 
48
        call = cmd
 
49
        if args:
 
50
            call += ' ' + ' '.join(args)
 
51
        self.calls.append(call)
 
52
 
 
53
    # extension methods
 
54
 
 
55
    def set(self, **kwargs):
 
56
        self.settings.append(kwargs)
 
57
 
 
58
 
 
59
class CharmTests(unittest.TestCase):
 
60
 
 
61
    def setUp(self):
 
62
        super(CharmTests, self).setUp()
 
63
        self.charm = TestCharm()
 
64
 
 
65
    def assertResult(self, **kwargs):
 
66
        self.charm.assertResult(self, **kwargs)
 
67
 
 
68
    def test_handle_config_no_credentials(self):
 
69
        self.charm.handle_config()
 
70
 
 
71
        self.assertResult()
 
72
 
 
73
    def test_handle_config_single_credentials(self):
 
74
        self.charm.creds = 'github:spam:eggs'
 
75
        self.charm.handle_config()
 
76
 
 
77
        self.assertResult(settings=[{'GITHUB_APP_ID': 'spam',
 
78
                                     'GITHUB_APP_SECRET': 'eggs',
 
79
                                     },
 
80
                                    ],
 
81
                          logs="found OAuth credentials for 'github'",
 
82
                          )
 
83
 
 
84
    def test_handle_config_multiple_credentials(self):
 
85
        self.charm.creds = 'github:spam:eggs foo:bar:baz'
 
86
        providers = self.charm.PROVIDERS.copy()
 
87
        providers['foo'] = ('FOO_USER', 'FOO_KEY')
 
88
 
 
89
        orig = self.charm.PROVIDERS
 
90
        self.charm.PROVIDERS = providers
 
91
        try:
 
92
            self.charm.handle_config()
 
93
        finally:
 
94
            self.charm.PROVIDERS = orig
 
95
 
 
96
        self.assertResult(settings=[{'GITHUB_APP_ID': 'spam',
 
97
                                     'GITHUB_APP_SECRET': 'eggs',
 
98
                                     'FOO_USER': 'bar',
 
99
                                     'FOO_KEY': 'baz'
 
100
                                     },
 
101
                                    ],
 
102
                          logs=["found OAuth credentials for 'github'",
 
103
                                "found OAuth credentials for 'foo'",
 
104
                                ],
 
105
                          )
 
106
 
 
107
    def test_handle_config_bad_provider(self):
 
108
        self.charm.creds = 'bogus:spam:eggs'
 
109
        with self.assertRaises(RuntimeError):
 
110
            self.charm.handle_config()
 
111
 
 
112
    def test_handle_config_bad_credentials(self):
 
113
        self.charm.creds = 'github:spam:eggs:ham'
 
114
        with self.assertRaises(RuntimeError):
 
115
            self.charm.handle_config()