15
16
return fn(self, *args, **kwargs)
18
20
class FakeReviewSettings(object):
19
'''An object that simply holds settings which are used by RatingsAndReviewsAPI
20
in the rnrclient_fake module. Using this module allows a developer to test
21
the reviews functionality without any interaction with a reviews server.
22
Each setting here provides complete control over how the 'server' will
23
respond. Changes to these settings should be made to the class attributes
24
directly without creating an instance of this class.
25
The intended usage is for unit tests where a predictable response is
26
required and where the application should THINK it has spoken to a
28
The unit test would make changes to settings in this class before
21
'''An object that simply holds settings which are used by
22
RatingsAndReviewsAPI in the rnrclient_fake module. Using this module
23
allows a developer to test the reviews functionality without any
24
interaction with a reviews server. Each setting here provides complete
25
control over how the 'server' will respond. Changes to these settings
26
should be made to the class attributes directly without creating an
27
instance of this class.
28
The intended usage is for unit tests where a predictable response is
29
required and where the application should THINK it has spoken to a
31
The unit test would make changes to settings in this class before
29
32
running the unit test.
32
35
_FAKE_SETTINGS = {}
35
38
#*****************************
36
39
#delay (in seconds) before returning from any of the fake rnr methods
41
44
#*****************************
42
45
#raises APIError if True
43
46
_FAKE_SETTINGS['server_response_error'] = False
46
49
#*****************************
47
50
#raises APIError if True
48
_FAKE_SETTINGS['review_stats_error'] = False
51
_FAKE_SETTINGS['review_stats_error'] = False
50
53
#the following has no effect if review_stats_error = True
51
#determines the number of package stats (i.e. ReviewStats list size) to return
52
#max 15 packages (any number higher than 15 will still return 15)
54
#determines the number of package stats (i.e. ReviewStats list size) to
55
#return max 15 packages (any number higher than 15 will still return 15)
53
56
_FAKE_SETTINGS['packages_returned'] = 10
56
59
#*****************************
57
60
#raises APIError if True
58
61
_FAKE_SETTINGS['get_reviews_error'] = False
60
#number of pages of 10 reviews to return before returning the number specified
61
#in the reviews_returned value below
63
#number of pages of 10 reviews to return before returning the number
64
# specified in the reviews_returned value below
62
65
_FAKE_SETTINGS['review_pages'] = 1
64
67
#the following has no effect if get_reviews_error = True
65
#determines number of reviews to return
68
#determines number of reviews to return
66
69
# (Accepts 0 to n but should really be between 1 and 10)
67
70
_FAKE_SETTINGS['reviews_returned'] = 3
70
73
#*****************************
71
74
#raises APIError if True
72
75
_FAKE_SETTINGS['get_review_error'] = False
75
78
#*****************************
76
79
#raises APIError if True
77
80
_FAKE_SETTINGS['submit_review_error'] = False
78
#fake username(str) and review_id(int) to give back with a successful review
81
#fake username(str) and review_id(int) to give back with a successful
79
83
#leave as None to generate a random username and review_id
80
84
_FAKE_SETTINGS['reviewer_username'] = None
81
85
_FAKE_SETTINGS['submit_review_id'] = None
84
88
#*****************************
85
89
#raises APIError if True
88
92
_FAKE_SETTINGS['flagger_username'] = None
89
93
#fake package name (str) to give back as flagged app
90
94
_FAKE_SETTINGS['flag_package_name'] = None
93
97
#*****************************
94
98
#raises APIError if True
95
99
_FAKE_SETTINGS['submit_usefulness_error'] = False
97
101
#the following has no effect if submit_usefulness_error = True
98
102
#which string to pretend the server returned
99
103
#choices are "Created", "Updated", "Not modified"
100
104
_FAKE_SETTINGS['usefulness_response_string'] = "Created"
103
107
#*****************************
104
108
#raises APIError if True
105
109
_FAKE_SETTINGS['get_usefulness_error'] = False
107
111
#the following has no effect if get_usefulness_error = True
108
112
#how many usefulness votes to return
109
113
_FAKE_SETTINGS['votes_returned'] = 5
111
#pre-configured review ids to return in the result
115
#pre-configured review ids to return in the result
112
116
#if you don't complete this or enter less review ids than votes_returned
113
117
#above, it will be random
114
_FAKE_SETTINGS['required_review_ids'] = [3,6,15]
118
_FAKE_SETTINGS['required_review_ids'] = [3, 6, 15]
116
120
#THE FOLLOWING SETTINGS RELATE TO LOGIN SSO FUNCTIONALITY
117
121
# LoginBackendDbusSSO
119
123
#***********************
120
# what to fake the login response as
124
# what to fake the login response as
121
125
# choices (strings): "successful", "failed", "denied"
122
126
_FAKE_SETTINGS['login_response'] = "successful"
126
130
#***********************
127
# what to fake whoami response as
131
# what to fake whoami response as
128
132
# choices (strings): "whoami", "error"
129
133
_FAKE_SETTINGS['whoami_response'] = "whoami"
130
134
#this only has effect if whoami_response = 'whoami'
132
136
#expects a string or None (for a random username)
133
137
_FAKE_SETTINGS['whoami_username'] = None
136
139
def __init__(self, defaults=False):
137
'''Initialises the object and loads the settings into the _FAKE_SETTINGS
138
dict.. If defaults is passed as True any existing settings in the cache
139
file are ignored and the cache file is overwritten with the defaults
140
set in the class. This is useful if you don't want previously used
141
settings from the cache file being used again'''
140
'''Initialises the object and loads the settings into the
141
_FAKE_SETTINGS dict.. If defaults is passed as True any existing
142
settings in the cache file are ignored and the cache file is
143
overwritten with the defaults set in the class. This is useful if
144
you don't want previously used settings from the cache file being
142
146
fname = 'fake_review_settings.p'
143
147
self.LOCATION = os.path.join(SOFTWARE_CENTER_CACHE_DIR, fname)
145
149
self._save_settings()
147
151
self._update_from_file()
150
153
def update_setting(self, key_name, new_value):
151
'''Takes a string (key_name) which corresponds to a setting in this object
152
and updates it with the value passed in (new_value).
154
'''Takes a string (key_name) which corresponds to a setting in this
155
object and updates it with the value passed in (new_value).
153
156
Raises a NameError if the setting name doesn't exist'''
155
158
if not key_name in self._FAKE_SETTINGS:
156
raise NameError ('Setting key name %s does not exist' % key_name)
159
raise NameError('Setting key name %s does not exist' % key_name)
158
161
self._FAKE_SETTINGS[key_name] = new_value
159
162
self._save_settings()
162
165
def update_multiple(self, settings):
163
'''Takes a dict (settings) of key,value pairs to perform multiple updates
164
in one action, then saves. Dict being passed should contain only keys that
165
match settings in this object or a NameError will be raised'''
166
'''Takes a dict (settings) of key,value pairs to perform multiple
167
updates in one action, then saves. Dict being passed should contain
168
only keys that match settings in this object or a NameError will be
166
170
for key, value in settings.items():
167
171
if not key in self._FAKE_SETTINGS:
168
raise NameError ('Setting key name %s does not exist' % key)
172
raise NameError('Setting key name %s does not exist' % key)
170
174
for key, value in settings.items():
171
175
self._FAKE_SETTINGS[key] = value
172
176
self._save_settings()
175
179
def get_setting(self, key_name):
176
'''Takes a string (key_name) which corresponds to a setting in this object,
177
gets the latest copy of it from the file and returns the setting.
178
Raises a NameError if the setting name doesn't exist'''
180
'''Takes a string (key_name) which corresponds to a setting in this
181
object, gets the latest copy of it from the file and returns the
182
setting. Raises a NameError if the setting name doesn't exist'''
179
183
if not key_name in self._FAKE_SETTINGS:
180
raise NameError ('Setting %s does not exist' % key_name)
184
raise NameError('Setting %s does not exist' % key_name)
182
186
self._update_from_file()
183
187
return self._FAKE_SETTINGS[key_name]
185
189
def _update_from_file(self):
186
190
'''Loads existing settings from cache file into _FAKE_SETTINGS dict'''
187
191
if os.path.exists(self.LOCATION):
189
193
self._FAKE_SETTINGS = pickle.load(open(self.LOCATION))
191
os.rename(self.LOCATION, self.LOCATION+".fail")
195
os.rename(self.LOCATION, self.LOCATION + ".fail")
194
198
def _save_settings(self):
195
199
"""write the dict out to cache file"""