summaryrefslogtreecommitdiff
path: root/tests/conf/tests.py
blob: 333ea55da12436746e0ca878b338415016aa56bc (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from __future__ import with_statement

import logging
import mock

from raven.conf import setup_logging
from raven.conf.remote import RemoteConfig
from raven.exceptions import InvalidDsn
from raven.utils import get_auth_header
from raven.utils.testutils import TestCase


class RemoteConfigTest(TestCase):
    def test_path_strip(self):
        dsn = 'https://foo:bar@sentry.local/app/1\n'
        res = RemoteConfig.from_string(dsn)
        assert res.project == '1'
        assert res.base_url == 'https://sentry.local/app'
        assert res.store_endpoint == 'https://sentry.local/app/api/1/store/'
        assert res.public_key == 'foo'
        assert res.secret_key == 'bar'
        assert res.options == {}

    def test_path(self):
        dsn = 'https://foo:bar@sentry.local/app/1'
        res = RemoteConfig.from_string(dsn)
        assert res.project == '1'
        assert res.base_url == 'https://sentry.local/app'
        assert res.store_endpoint == 'https://sentry.local/app/api/1/store/'
        assert res.public_key == 'foo'
        assert res.secret_key == 'bar'
        assert res.options == {}

    def test_http(self):
        dsn = 'http://foo:bar@sentry.local/1'
        res = RemoteConfig.from_string(dsn)
        assert res.project == '1'
        assert res.base_url == 'http://sentry.local'
        assert res.store_endpoint == 'http://sentry.local/api/1/store/'
        assert res.public_key == 'foo'
        assert res.secret_key == 'bar'
        assert res.options == {}

    def test_http_with_port(self):
        dsn = 'http://foo:bar@sentry.local:9000/1'
        res = RemoteConfig.from_string(dsn)
        assert res.project == '1'
        assert res.base_url == 'http://sentry.local:9000'
        assert res.store_endpoint == 'http://sentry.local:9000/api/1/store/'
        assert res.public_key == 'foo'
        assert res.secret_key == 'bar'
        assert res.options == {}

    def test_https(self):
        dsn = 'https://foo:bar@sentry.local/1'
        res = RemoteConfig.from_string(dsn)
        assert res.project == '1'
        assert res.base_url == 'https://sentry.local'
        assert res.store_endpoint == 'https://sentry.local/api/1/store/'
        assert res.public_key == 'foo'
        assert res.secret_key == 'bar'
        assert res.options == {}

    def test_https_with_port(self):
        dsn = 'https://foo:bar@sentry.local:9000/app/1'
        res = RemoteConfig.from_string(dsn)
        assert res.project == '1'
        assert res.base_url == 'https://sentry.local:9000/app'
        assert res.store_endpoint == 'https://sentry.local:9000/app/api/1/store/'
        assert res.public_key == 'foo'
        assert res.secret_key == 'bar'
        assert res.options == {}

    def test_options(self):
        dsn = 'http://foo:bar@sentry.local/1?timeout=1'
        res = RemoteConfig.from_string(dsn)
        assert res.project == '1'
        assert res.base_url == 'http://sentry.local'
        assert res.store_endpoint == 'http://sentry.local/api/1/store/'
        assert res.public_key == 'foo'
        assert res.secret_key == 'bar'
        assert res.options == {'timeout': '1'}

    def test_no_secret_key(self):
        dsn = 'https://foo@sentry.local/1'
        res = RemoteConfig.from_string(dsn)
        assert res.project == '1'
        assert res.base_url == 'https://sentry.local'
        assert res.store_endpoint == 'https://sentry.local/api/1/store/'
        assert res.public_key == 'foo'
        assert res.secret_key is None
        assert res.options == {}
        assert res.is_active()

        assert get_auth_header(protocol=7, timestamp=42,
                               client='raven-python/1.0',
                               api_key=res.public_key) == (
            'Sentry sentry_timestamp=42, sentry_client=raven-python/1.0, '
            'sentry_version=7, sentry_key=foo')

    def test_missing_netloc(self):
        dsn = 'https://foo:bar@/1'
        self.assertRaises(InvalidDsn, RemoteConfig.from_string, dsn)

    def test_missing_project(self):
        dsn = 'https://foo:bar@example.com'
        self.assertRaises(InvalidDsn, RemoteConfig.from_string, dsn)

    def test_missing_public_key(self):
        dsn = 'https://:bar@example.com'
        self.assertRaises(InvalidDsn, RemoteConfig.from_string, dsn)

    def test_invalid_scheme(self):
        dsn = 'ftp://foo:bar@sentry.local/1'
        self.assertRaises(InvalidDsn, RemoteConfig.from_string, dsn)

    def test_get_public_dsn(self):
        res = RemoteConfig(
            base_url='http://example.com',
            project='1',
            public_key='public',
            secret_key='secret',
        )
        public_dsn = res.get_public_dsn()
        assert public_dsn == '//public@example.com/1'


class SetupLoggingTest(TestCase):
    def test_basic_not_configured(self):
        with mock.patch('logging.getLogger', spec=logging.getLogger) as getLogger:
            logger = getLogger()
            logger.handlers = []
            handler = mock.Mock()
            result = setup_logging(handler)
            self.assertTrue(result)

    def test_basic_already_configured(self):
        with mock.patch('logging.getLogger', spec=logging.getLogger) as getLogger:
            handler = mock.Mock()
            logger = getLogger()
            logger.handlers = [handler]
            result = setup_logging(handler)
            self.assertFalse(result)