~allanlesage/qakit/i18ncheck-sample-test

« back to all changes in this revision

Viewing changes to qakit/tests/test_lkkb.py

  • Committer: Thomi Richards
  • Date: 2014-11-12 02:33:48 UTC
  • mfrom: (4.1.1 trunk-add-project-backlog)
  • Revision ID: thomi.richards@canonical.com-20141112023348-nzw8bny0fb8c146b
Merge code to retrieve project backlog.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 
 
3
from testtools import TestCase
 
4
from testtools.matchers import raises, Equals
 
5
 
 
6
import tempfile
 
7
from qakit import lkkb
 
8
from contextlib import contextmanager
 
9
 
 
10
 
 
11
@contextmanager
 
12
def temp_config_file(content):
 
13
    with tempfile.NamedTemporaryFile() as f:
 
14
        f.write(content)
 
15
        f.flush()
 
16
        f.seek(0)
 
17
        yield f.name
 
18
 
 
19
 
 
20
class ConfigFileLoadTests(TestCase):
 
21
 
 
22
    def test_raises_IOError_if_file_not_found(self):
 
23
        name = tempfile.mktemp()
 
24
        self.assertThat(
 
25
            lambda: lkkb.get_lkkb_auth_pair(name),
 
26
            raises(IOError)
 
27
        )
 
28
 
 
29
    def assert_content_raises_exception(self, content, exception):
 
30
        with temp_config_file(content) as name:
 
31
            self.assertThat(
 
32
                lambda: lkkb.get_lkkb_auth_pair(name),
 
33
                raises(exception)
 
34
            )
 
35
 
 
36
    def test_empty_file_raises_ValueError(self):
 
37
        self.assert_content_raises_exception(
 
38
            "",
 
39
            ValueError("Could not find 'LeanKit' section in config file.")
 
40
        )
 
41
 
 
42
    def test_empty_section_raises_ValueError(self):
 
43
        self.assert_content_raises_exception(
 
44
            "[LeanKit]\n",
 
45
            ValueError("No username set in config file.")
 
46
        )
 
47
 
 
48
    def test_empty_user_option_raises_ValueError(self):
 
49
        self.assert_content_raises_exception(
 
50
            (
 
51
                "[LeanKit]\n"
 
52
                "Username=\n"
 
53
            ),
 
54
            ValueError("No username set in config file.")
 
55
        )
 
56
 
 
57
    def test_missing_password_raises_ValueError(self):
 
58
        self.assert_content_raises_exception(
 
59
            (
 
60
                "[LeanKit]\n"
 
61
                "Username=foo\n"
 
62
            ),
 
63
            ValueError("No password set in config file.")
 
64
        )
 
65
 
 
66
    def test_empty_password_raises_ValueError(self):
 
67
        self.assert_content_raises_exception(
 
68
            (
 
69
                "[LeanKit]\n"
 
70
                "Username=foo\n"
 
71
                "Password=\n"
 
72
            ),
 
73
            ValueError("No password set in config file.")
 
74
        )
 
75
 
 
76
    def test_can_retrieve_username_and_password(self):
 
77
        with temp_config_file(
 
78
            "[LeanKit]\n"
 
79
            "Username=foo\n"
 
80
            "Password=bar\n"
 
81
        ) as name:
 
82
            self.assertThat(
 
83
                lkkb.get_lkkb_auth_pair(name),
 
84
                Equals(('foo', 'bar'))
 
85
            )
 
86
 
 
87
 
 
88
def test_suite():
 
89
    from unittest import TestLoader
 
90
    return TestLoader().loadTestsFromName(__name__)