8
10
from canonical.testing.layers import DatabaseFunctionalLayer
9
11
from lp.testing import TestCaseWithFactory
10
12
from lp.services.features.scopes import (
12
13
BaseWebRequestScope,
23
from testtools.matchers import (
31
class MultiScopeContains(Matcher):
32
"""Matches if a MultiScopeHandler checks the given scope."""
34
def __init__(self, scope_class):
35
self.scope_class = scope_class
38
return "contains %r scope handler" % self.scope_class
40
def match(self, multi_handler):
41
for h in multi_handler.handlers:
42
if isinstance(h, self.scope_class):
46
"Scope class %r not found in %r"
47
% (self.scope_class, multi_handler))
50
class ScopeMatches(Matcher):
51
"""True if a particular scope is detected as active."""
53
def __init__(self, scope_string):
54
self.scope_string = scope_string
58
self.__class__.__name__,
61
def match(self, scope_handler):
62
if not scope_handler.lookup(self.scope_string):
64
"Handler %r didn't match scope string %r" %
65
(scope_handler, self.scope_string))
19
68
class FakeScope(BaseWebRequestScope):
67
116
script_name = self.factory.getUniqueString()
68
117
scopes = ScopesForScript(script_name)
69
118
self.assertFalse(scopes.lookup("script:other"))
121
sample_message = """\
122
From: rae@example.com
123
To: new@bugs.launchpad.net
124
Message-Id: <20110107085110.EB4A1181C2A@whatever>
125
Date: Fri, 7 Jan 2011 02:51:10 -0600 (CST)
126
Received: by other.example.com (Postfix, from userid 1000)
127
\tid DEADBEEF; Fri, 7 Jan 2011 02:51:10 -0600 (CST)
128
Received: by lithe (Postfix, from userid 1000)
129
\tid JOB_ID; Fri, 7 Jan 2011 02:51:10 -0600 (CST)
130
Subject: email scopes don't work
136
class TestMailScopes(TestCaseWithFactory):
138
layer = DatabaseFunctionalLayer
140
def make_mail_scopes(self):
141
# actual Launchpad uses an ISignedMessage which is a zinterface around
142
# an email.message, but the basic one will do
143
sample_email_message = email.message_from_string(sample_message)
144
scopes = ScopesForMail(sample_email_message)
147
def test_ScopesForMail_examines_server(self):
148
mail_scopes = self.make_mail_scopes()
149
self.assertThat(mail_scopes, MatchesAll(
150
MultiScopeContains(ServerScope),
151
MultiScopeContains(DefaultScope),
152
MultiScopeContains(MailHeaderScope),
153
MultiScopeContains(TeamScope),
156
def test_mail_header_scope(self):
157
mail_scopes = self.make_mail_scopes()
158
self.assertThat(mail_scopes, MatchesAll(
159
ScopeMatches("mail_header:From:rae@example.com")))
160
# Regexs are case-sensitive by default, like Python.
161
self.assertThat(mail_scopes, Not(
162
ScopeMatches("mail_header:From:rae@Example.com")))
163
# But header field names are case-insensitive, like rfc2822.
164
self.assertThat(mail_scopes,
165
ScopeMatches("mail_header:from:rae@example.com"))
166
# Repeated headers check all values.
167
self.assertThat(mail_scopes, MatchesAll(
168
ScopeMatches(r"mail_header:Received:other\.example\.com"),
169
ScopeMatches(r"mail_header:Received:lithe")))
170
# Long lines are not unfolded, but you can match them if you turn on
172
self.assertThat(mail_scopes,
173
ScopeMatches(r"mail_header:Received:(?s)other\.example\.com.*"