~fabricematrat/charmworld/redirect-charm

« back to all changes in this revision

Viewing changes to charmworld/views/tests/test_charms.py

  • Committer: Fabrice Matrat
  • Date: 2015-02-09 11:37:32 UTC
  • Revision ID: fabricematrat@gmail.com-20150209113732-pocsisg9z3lsw2ua
Redirect for series added

Show diffs side-by-side

added added

removed removed

Lines of Context:
166
166
        self.assertEqual(one['name'], charms[0].name)
167
167
 
168
168
    def test_personal_collection(self):
169
 
        # personal_collection() returns only charms owned by the given
170
 
        # person.
171
 
        one_id, one = factory.makeCharm(self.db, owner='foo')
172
 
        two_id, two = factory.makeCharm(self.db, owner='bar')
173
 
        request = self.getRequest()
174
 
        request.matchdict = {'owner': 'foo'}
175
 
        response = personal_collection(request)
176
 
        charms = response['charms']
177
 
        self.assertEqual(1, len(charms))
178
 
        self.assertEqual('foo', charms[0].owner)
179
 
 
180
 
    def test_personal_collection_sub_filter(self):
181
 
        # If the request parameter 'sub' is set, only subordinate charms
182
 
        # are returned.
183
 
        one_id, one = factory.makeCharm(self.db, owner='foo')
184
 
        two_id, two = factory.makeCharm(self.db, owner='bar')
185
 
        three_id, three = factory.makeCharm(
186
 
            self.db, owner='foo', subordinate=True)
187
 
        request = self.getRequest()
188
 
        request.GET['sub'] = 'true'
189
 
        request.matchdict = {'owner': 'foo'}
190
 
        response = personal_collection(request)
191
 
        charms = response['charms']
192
 
        self.assertEqual(1, len(charms))
193
 
        self.assertEqual(three['name'], charms[0].name)
194
 
        # If the filter is no given in the request, all charms are returned.
195
 
        request = self.getRequest()
196
 
        request.matchdict = {'owner': 'foo'}
197
 
        response = personal_collection(request)
198
 
        charms = response['charms']
199
 
        self.assertEqual(2, len(charms))
200
 
        self.assertEqual('foo', charms[0].owner)
201
 
        self.assertEqual('foo', charms[1].owner)
202
 
 
203
 
    def test_personal_collection_charms_with_errors(self):
204
 
        # Charms with errors are not returned by personal_collection().
205
 
        one_id, one = factory.makeCharm(self.db, owner='foo')
206
 
        two_id, two = factory.makeCharm(self.db, owner='bar')
207
 
        three_id, three = factory.makeCharm(
208
 
            self.db, owner='foo', charm_error=True)
209
 
        request = self.getRequest()
210
 
        request.matchdict = {'owner': 'foo'}
211
 
        response = personal_collection(request)
212
 
        charms = response['charms']
213
 
        self.assertEqual(1, len(charms))
214
 
        self.assertEqual(one['name'], charms[0].name)
 
169
        request = self.getRequest()
 
170
        request.matchdict = {'owner': 'foo'}
 
171
        with self.assertRaises(HTTPMovedPermanently):
 
172
            personal_collection(request)
215
173
 
216
174
    def test_series_collection(self):
217
 
        # series_collection() returns only promulgated charms for a
218
 
        # given series.
219
 
        one_id, one = factory.makeCharm(
220
 
            self.db, series='one', promulgated=True, name='charm-a')
221
 
        two_id, two = factory.makeCharm(
222
 
            self.db, series='two', promulgated=True)
223
 
        three_id, three = factory.makeCharm(
224
 
            self.db, series='one')
225
 
        four_id, four = factory.makeCharm(
226
 
            self.db, series='one', promulgated=True, owner='foo',
227
 
            name='charm-b')
228
 
        request = self.getRequest()
229
 
        request.matchdict = {'series': 'one'}
230
 
        response = series_collection(request)
231
 
        charms = response['charms']
232
 
        self.assertEqual(
233
 
            ['charm-a', 'charm-b'], [charm.name for charm in charms])
234
 
        self.assertEqual('one', charms[0].series)
235
 
 
236
 
    def test_series_collection_sub_filter(self):
237
 
        # If the request parameter 'sub' is set, only subordinate charms
238
 
        # are returned.
239
 
        one_id, one = factory.makeCharm(
240
 
            self.db, series='one', promulgated=True)
241
 
        two_id, two = factory.makeCharm(
242
 
            self.db, series='two', promulgated=True)
243
 
        three_is, three = factory.makeCharm(
244
 
            self.db, series='one', subordinate=True, promulgated=True)
245
 
        request = self.getRequest()
246
 
        request.matchdict = {'series': 'one'}
247
 
        request.GET['sub'] = 'true'
248
 
        response = series_collection(request)
249
 
        charms = response['charms']
250
 
        self.assertEqual(1, len(charms))
251
 
        self.assertEqual(three['name'], charms[0].name)
252
 
 
253
 
    def test_series_collection_charms_with_errors(self):
254
 
        # Charms with errors are not returned by series_collection().
255
 
        one_id, one = factory.makeCharm(
256
 
            self.db, series='one', promulgated=True)
257
 
        two_id, two = factory.makeCharm(
258
 
            self.db, series='two', promulgated=True)
259
 
        three_is, three = factory.makeCharm(
260
 
            self.db, series='one', charm_error=True, promulgated=True)
261
 
        request = self.getRequest()
262
 
        request.matchdict = {'series': 'one'}
263
 
        response = series_collection(request)
264
 
        charms = response['charms']
265
 
        self.assertEqual(1, len(charms))
266
 
        self.assertEqual(one['name'], charms[0].name)
 
175
        request = self.getRequest()
 
176
        request.matchdict = {'series': 'one'}
 
177
        with self.assertRaises(HTTPMovedPermanently):
 
178
            series_collection(request)
267
179
 
268
180
    def test_personal_series_collection(self):
269
 
        # personal_series_collection() returns only charms for a given
270
 
        # series and person.
271
 
        one_id, one = factory.makeCharm(self.db, series='one', owner='foo')
272
 
        two_id, two = factory.makeCharm(self.db, series='two', owner='foo')
273
 
        three_id, three = factory.makeCharm(self.db, series='one', owner='bar')
274
 
        request = self.getRequest()
275
 
        request.matchdict = {
276
 
            'owner': 'foo',
277
 
            'series': 'one',
278
 
        }
279
 
        response = personal_series_collection(request)
280
 
        charms = response['charms']
281
 
        self.assertEqual(1, len(charms))
282
 
        self.assertEqual(one['name'], charms[0].name)
283
 
 
284
 
    def test_personal_series_collection_sub_filter(self):
285
 
        # If the parameter 'sub' is set, personal_series_collection()
286
 
        # returns only subordinate charms for the given person ans series.
287
 
        one_id, one = factory.makeCharm(self.db, series='one', owner='foo')
288
 
        two_id, two = factory.makeCharm(self.db, series='two', owner='foo')
289
 
        three_id, three = factory.makeCharm(self.db, series='one', owner='bar')
290
 
        four_id, four = factory.makeCharm(
291
 
            self.db, series='one', owner='foo', subordinate=True)
292
 
        request = self.getRequest()
293
 
        request.matchdict = {
294
 
            'owner': 'foo',
295
 
            'series': 'one',
296
 
        }
297
 
        request.GET['sub'] = 'true'
298
 
        response = personal_series_collection(request)
299
 
        charms = response['charms']
300
 
        self.assertEqual(1, len(charms))
301
 
        self.assertEqual(four['name'], charms[0].name)
302
 
 
303
 
    def test_personal_series_collection_charms_with_errors(self):
304
 
        # Charms with errors are not returned by series_collection().
305
 
        one_id, one = factory.makeCharm(self.db, series='one', owner='foo')
306
 
        two_id, two = factory.makeCharm(self.db, series='two', owner='foo')
307
 
        three_id, three = factory.makeCharm(self.db, series='one', owner='bar')
308
 
        four_id, four = factory.makeCharm(
309
 
            self.db, series='one', owner='foo', charm_error=True)
310
 
        request = self.getRequest()
311
 
        request.matchdict = {
312
 
            'owner': 'foo',
313
 
            'series': 'one',
314
 
        }
315
 
        response = personal_series_collection(request)
316
 
        charms = response['charms']
317
 
        self.assertEqual(1, len(charms))
318
 
        self.assertEqual(one['name'], charms[0].name)
 
181
        request = self.getRequest()
 
182
        request.matchdict = {'series': 'one', 'owner': 'me'}
 
183
        with self.assertRaises(HTTPMovedPermanently):
 
184
            personal_series_collection(request)
319
185
 
320
186
    def test_interface(self):
321
187
        # interface() returns all charms requiring or providing a given
540
406
        two_id, two = factory.makeCharm(self.db, promulgated=True)
541
407
        del two['summary']
542
408
        self.db.charms.save(two)
543
 
        # Test that the route works.
544
 
        resp = self.app.get('/charms/precise', status=200)
545
 
        self.assertIn(two['short_url'], resp.body)
 
409
        response = self.app.get('/charms/precise', status=301)
 
410
        self.assertIn('/q/precise', response.body)
546
411
 
547
412
    def test_route_personal_charm_with_revision(self):
548
413
        # The route /~owner/series/charm-1 finds charms using owner,