~launchpad-pqm/launchpad/devel

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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Tests of the oopsreferences core."""

__metaclass__ = type

from datetime import (
    datetime,
    timedelta,
    )

from pytz import utc

from lp.registry.model.oopsreferences import referenced_oops
from lp.services.database.lpstorm import IStore
from lp.services.messages.model.message import (
    Message,
    MessageSet,
    )
from lp.testing import (
    person_logged_in,
    TestCaseWithFactory,
    )
from lp.testing.layers import DatabaseFunctionalLayer


class TestOopsReferences(TestCaseWithFactory):

    layer = DatabaseFunctionalLayer

    def setUp(self):
        super(TestOopsReferences, self).setUp()
        self.store = IStore(Message)

    def test_oops_in_messagechunk(self):
        oopsid = "OOPS-abcdef1234"
        MessageSet().fromText('foo', "foo %s bar" % oopsid)
        self.store.flush()
        now = datetime.now(tz=utc)
        day = timedelta(days=1)
        self.failUnlessEqual(
            set([oopsid.upper()]),
            referenced_oops(now - day, now, "product=1", {}))
        self.failUnlessEqual(
            set(),
            referenced_oops(now + day, now + day, "product=1", {}))

    def test_oops_in_messagesubject(self):
        oopsid = "OOPS-abcdef1234"
        self.factory.makeEmailMessage()
        MessageSet().fromText("Crash with %s" % oopsid, "body")
        self.store.flush()
        now = datetime.now(tz=utc)
        day = timedelta(days=1)
        self.failUnlessEqual(
            set([oopsid.upper()]),
            referenced_oops(now - day, now, "product=1", {}))
        self.failUnlessEqual(
            set(),
            referenced_oops(now + day, now + day, "product=1", {}))

    def test_oops_in_bug_title(self):
        oopsid = "OOPS-abcdef1234"
        bug = self.factory.makeBug()
        with person_logged_in(bug.owner):
            bug.title = "Crash with %s" % oopsid
        self.store.flush()
        now = datetime.now(tz=utc)
        day = timedelta(days=1)
        self.failUnlessEqual(
            set([oopsid.upper()]),
            referenced_oops(now - day, now, "product=1", {}))
        self.failUnlessEqual(
            set(),
            referenced_oops(now + day, now + day, "product=1", {}))

    def test_oops_in_bug_description(self):
        oopsid = "OOPS-abcdef1234"
        bug = self.factory.makeBug()
        with person_logged_in(bug.owner):
            bug.description = "Crash with %s" % oopsid
        self.store.flush()
        now = datetime.now(tz=utc)
        day = timedelta(days=1)
        self.failUnlessEqual(
            set([oopsid.upper()]),
            referenced_oops(now - day, now, "product=1", {}))
        self.failUnlessEqual(
            set(),
            referenced_oops(now + day, now + day, "product=1", {}))

    def test_oops_in_question_title(self):
        oopsid = "OOPS-abcdef1234"
        question = self.factory.makeQuestion(title="Crash with %s" % oopsid)
        self.store.flush()
        now = datetime.now(tz=utc)
        day = timedelta(days=1)
        self.failUnlessEqual(
            set([oopsid.upper()]),
            referenced_oops(now - day, now, "product=%(product)s",
            {'product': question.product.id}))
        self.failUnlessEqual(
            set([]),
            referenced_oops(now + day, now + day, "product=%(product)s",
            {'product': question.product.id}))

    def test_oops_in_question_wrong_context(self):
        oopsid = "OOPS-abcdef1234"
        question = self.factory.makeQuestion(title="Crash with %s" % oopsid)
        self.store.flush()
        now = datetime.now(tz=utc)
        day = timedelta(days=1)
        self.store.flush()
        self.failUnlessEqual(
            set(),
            referenced_oops(now - day, now, "product=%(product)s",
            {'product': question.product.id + 1}))

    def test_oops_in_question_description(self):
        oopsid = "OOPS-abcdef1234"
        question = self.factory.makeQuestion(
            description="Crash with %s" % oopsid)
        self.store.flush()
        now = datetime.now(tz=utc)
        day = timedelta(days=1)
        self.failUnlessEqual(
            set([oopsid.upper()]),
            referenced_oops(now - day, now, "product=%(product)s",
            {'product': question.product.id}))
        self.failUnlessEqual(
            set([]),
            referenced_oops(now + day, now + day, "product=%(product)s",
            {'product': question.product.id}))

    def test_oops_in_question_whiteboard(self):
        oopsid = "OOPS-abcdef1234"
        question = self.factory.makeQuestion()
        with person_logged_in(question.owner):
            question.whiteboard = "Crash with %s" % oopsid
            self.store.flush()
        now = datetime.now(tz=utc)
        day = timedelta(days=1)
        self.failUnlessEqual(
            set([oopsid.upper()]),
            referenced_oops(now - day, now, "product=%(product)s",
            {'product': question.product.id}))
        self.failUnlessEqual(
            set([]),
            referenced_oops(now + day, now + day, "product=%(product)s",
            {'product': question.product.id}))

    def test_oops_in_question_distribution(self):
        oopsid = "OOPS-abcdef1234"
        distro = self.factory.makeDistribution()
        question = self.factory.makeQuestion(target=distro)
        with person_logged_in(question.owner):
            question.whiteboard = "Crash with %s" % oopsid
            self.store.flush()
        now = datetime.now(tz=utc)
        day = timedelta(days=1)
        self.failUnlessEqual(
            set([oopsid.upper()]),
            referenced_oops(now - day, now, "distribution=%(distribution)s",
            {'distribution': distro.id}))
        self.failUnlessEqual(
            set([]),
            referenced_oops(now + day, now + day,
            "distribution=%(distribution)s", {'distribution': distro.id}))

    def test_referenced_oops_in_urls_bug_663249(self):
        # Sometimes OOPS ids appears as part of an URL. These should could as
        # a reference even though they are not formatted specially - this
        # requires somewhat special handling in the reference calculation
        # function.
        oopsid = "OOPS-abcdef1234"
        bug = self.factory.makeBug()
        with person_logged_in(bug.owner):
            bug.description = (
                "foo https://lp-oops.canonical.com/oops.py?oopsid=%s bar"
                % oopsid)
            self.store.flush()
        now = datetime.now(tz=utc)
        day = timedelta(days=1)
        self.failUnlessEqual(
            set([oopsid.upper()]),
            referenced_oops(now - day, now, "product=1", {}))
        self.failUnlessEqual(
            set([]),
            referenced_oops(now + day, now + day, "product=1", {}))