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
|
# Copyright 2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Test feature-flag scopes."""
__metaclass__ = type
from canonical.testing.layers import DatabaseFunctionalLayer
from lp.testing import (
person_logged_in,
TestCase,
TestCaseWithFactory,
)
from lp.services.features import (
getFeatureFlag,
)
from lp.services.features.scopes import (
BaseScope,
MultiScopeHandler,
ScopesForScript,
ScriptScope,
UserSliceScope,
)
from lp.services.features.testing import (
FeatureFixture,
)
class FakeScope(BaseScope):
pattern = r'fake:'
def __init__(self, name):
self.name = name
def lookup(self, scope_name):
return scope_name == (self.pattern + self.name)
class TestScopes(TestCaseWithFactory):
layer = DatabaseFunctionalLayer
def test_ScriptScope_lookup_matches_script_scope(self):
script_name = self.factory.getUniqueString()
scope = ScriptScope(script_name)
self.assertTrue(scope.lookup("script:" + script_name))
def test_ScriptScope_lookup_does_not_match_other_script_scope(self):
script_name = self.factory.getUniqueString()
scope = ScriptScope(script_name)
self.assertFalse(scope.lookup("script:other"))
def test_MultiScopeHandler_lookup_ignores_unmatched_scope(self):
scope_name = self.factory.getUniqueString()
fake_scope = FakeScope(scope_name)
handler = MultiScopeHandler([fake_scope])
self.assertFalse(handler.lookup("other:other"))
def test_MultiScopeHandler_lookup_ignores_inapplicable_scope(self):
scope_name = self.factory.getUniqueString()
handler = MultiScopeHandler([FakeScope(scope_name)])
self.assertFalse(handler.lookup("fake:other"))
def test_MultiScopeHandler_lookup_finds_matching_scope(self):
scope_name = self.factory.getUniqueString()
handler = MultiScopeHandler([FakeScope(scope_name)])
self.assertTrue(handler.lookup("fake:" + scope_name))
def test_ScopesForScript_includes_default_scope(self):
script_name = self.factory.getUniqueString()
scopes = ScopesForScript(script_name)
self.assertTrue(scopes.lookup("default"))
def test_ScopesForScript_lookup_finds_script(self):
script_name = self.factory.getUniqueString()
scopes = ScopesForScript(script_name)
self.assertTrue(scopes.lookup("script:" + script_name))
def test_ScopesForScript_lookup_does_not_find_other_script(self):
script_name = self.factory.getUniqueString()
scopes = ScopesForScript(script_name)
self.assertFalse(scopes.lookup("script:other"))
class FakePerson(object):
id = 7
class TestUserSliceScope(TestCase):
def test_user_slice(self):
person = FakePerson()
# NB: scopes take a callable that returns the person, that in
# production comes from the request.
scope = UserSliceScope(lambda: person)
# Effectively selects everyone; should always be true.
self.assertTrue(scope.lookup('userslice:0,1'))
# Exactly one of these should be true.
checks = 7
matches = []
for i in range(checks):
name = 'userslice:%d,%d' % (i, checks)
if scope.lookup(name):
matches.append(name)
self.assertEquals(len(matches), 1, matches)
class TestUserSliceScopeIntegration(TestCaseWithFactory):
layer = DatabaseFunctionalLayer
def test_user_slice_from_rules(self):
"""Userslice matches against the real request user"""
person = self.factory.makePerson()
with FeatureFixture({}, full_feature_rules=[
dict(
flag='test_feature',
scope='userslice:0,1',
priority=999,
value=u'on'),
dict(
flag='test_not',
scope='userslice:1,1',
priority=999,
value=u'not_value'),
]):
with person_logged_in(person):
self.assertEquals(getFeatureFlag('test_feature'), 'on')
self.assertEquals(getFeatureFlag('test_not'), None)
|