~bac/charmworld/bundle-redirect

« back to all changes in this revision

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

  • Committer: Tarmac
  • Author(s): Fabrice Matrat
  • Date: 2015-02-09 21:22:04 UTC
  • mfrom: (519.1.6 redirect-charm)
  • Revision ID: tarmac-20150209212204-7q0s7nrhfigu4pu5
Add a redirect for charms
R=bac, jcsackett.

Approved by Brad Crittenden, Juju Gui Bot, j.c.sackett.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
from datetime import date
5
5
from logging import getLogger
6
6
 
 
7
from pyramid.httpexceptions import HTTPMovedPermanently
 
8
 
7
9
from charmworld.jobs.ingest import (
8
10
    add_files,
9
11
)
131
133
 
132
134
    def test_charm_collection_basic(self):
133
135
        """Simple sanity check."""
134
 
        one_id, one = factory.makeCharm(self.db, promulgated=True)
135
 
        two_id, two = factory.makeCharm(self.db, promulgated=True, owner='foo')
136
 
        three_id, three = factory.makeCharm(self.db, promulgated=False)
137
 
 
138
 
        response = charm_collection(self.getRequest())
139
 
        charm_names = set(charm.name for charm in response['charms'])
140
 
        expected = set((one['name'], two['name']))
141
 
        self.assertEqual(expected, charm_names)
142
 
 
143
 
    def test_charm_collection_sub_filter(self):
144
 
        """With the subordinate filter we limit out the non-sub charms."""
145
 
        one_id, one = factory.makeCharm(
146
 
            self.db, subordinate=True, promulgated=True)
147
 
        two_id, two = factory.makeCharm(self.db, promulgated=True)
148
 
 
149
 
        request = self.getRequest()
150
 
        request.GET['sub'] = 'true'
151
 
        response = charm_collection(request)
152
 
        charms = response['charms']
153
 
        self.assertEqual(1, len(charms))
154
 
        self.assertEqual(one['name'], charms[0].name)
155
 
 
156
 
    def test_charm_collection_charms_with_errors(self):
157
 
        """Charms with errors are not returned by charm_collection()."""
158
 
        one_id, one = factory.makeCharm(self.db, promulgated=True)
159
 
        two_id, two = factory.makeCharm(
160
 
            self.db, charm_error=True, promulgated=True)
161
 
        response = charm_collection(self.getRequest())
162
 
        charms = response['charms']
163
 
        self.assertEqual(1, len(charms))
164
 
        self.assertEqual(one['name'], charms[0].name)
 
136
        with self.assertRaises(HTTPMovedPermanently) as e:
 
137
            charm_collection(self.getRequest())
 
138
        self.assertIn('/solutions', e.exception.location)
165
139
 
166
140
    def test_personal_collection(self):
167
 
        # personal_collection() returns only charms owned by the given
168
 
        # person.
169
 
        one_id, one = factory.makeCharm(self.db, owner='foo')
170
 
        two_id, two = factory.makeCharm(self.db, owner='bar')
171
 
        request = self.getRequest()
172
 
        request.matchdict = {'owner': 'foo'}
173
 
        response = personal_collection(request)
174
 
        charms = response['charms']
175
 
        self.assertEqual(1, len(charms))
176
 
        self.assertEqual('foo', charms[0].owner)
177
 
 
178
 
    def test_personal_collection_sub_filter(self):
179
 
        # If the request parameter 'sub' is set, only subordinate charms
180
 
        # are returned.
181
 
        one_id, one = factory.makeCharm(self.db, owner='foo')
182
 
        two_id, two = factory.makeCharm(self.db, owner='bar')
183
 
        three_id, three = factory.makeCharm(
184
 
            self.db, owner='foo', subordinate=True)
185
 
        request = self.getRequest()
186
 
        request.GET['sub'] = 'true'
187
 
        request.matchdict = {'owner': 'foo'}
188
 
        response = personal_collection(request)
189
 
        charms = response['charms']
190
 
        self.assertEqual(1, len(charms))
191
 
        self.assertEqual(three['name'], charms[0].name)
192
 
        # If the filter is no given in the request, all charms are returned.
193
 
        request = self.getRequest()
194
 
        request.matchdict = {'owner': 'foo'}
195
 
        response = personal_collection(request)
196
 
        charms = response['charms']
197
 
        self.assertEqual(2, len(charms))
198
 
        self.assertEqual('foo', charms[0].owner)
199
 
        self.assertEqual('foo', charms[1].owner)
200
 
 
201
 
    def test_personal_collection_charms_with_errors(self):
202
 
        # Charms with errors are not returned by personal_collection().
203
 
        one_id, one = factory.makeCharm(self.db, owner='foo')
204
 
        two_id, two = factory.makeCharm(self.db, owner='bar')
205
 
        three_id, three = factory.makeCharm(
206
 
            self.db, owner='foo', charm_error=True)
207
 
        request = self.getRequest()
208
 
        request.matchdict = {'owner': 'foo'}
209
 
        response = personal_collection(request)
210
 
        charms = response['charms']
211
 
        self.assertEqual(1, len(charms))
212
 
        self.assertEqual(one['name'], charms[0].name)
 
141
        request = self.getRequest()
 
142
        request.matchdict = {'owner': 'foo'}
 
143
        with self.assertRaises(HTTPMovedPermanently) as e:
 
144
            personal_collection(request)
 
145
        self.assertIn('/q/foo', e.exception.location)
213
146
 
214
147
    def test_series_collection(self):
215
 
        # series_collection() returns only promulgated charms for a
216
 
        # given series.
217
 
        one_id, one = factory.makeCharm(
218
 
            self.db, series='one', promulgated=True, name='charm-a')
219
 
        two_id, two = factory.makeCharm(
220
 
            self.db, series='two', promulgated=True)
221
 
        three_id, three = factory.makeCharm(
222
 
            self.db, series='one')
223
 
        four_id, four = factory.makeCharm(
224
 
            self.db, series='one', promulgated=True, owner='foo',
225
 
            name='charm-b')
226
 
        request = self.getRequest()
227
 
        request.matchdict = {'series': 'one'}
228
 
        response = series_collection(request)
229
 
        charms = response['charms']
230
 
        self.assertEqual(
231
 
            ['charm-a', 'charm-b'], [charm.name for charm in charms])
232
 
        self.assertEqual('one', charms[0].series)
233
 
 
234
 
    def test_series_collection_sub_filter(self):
235
 
        # If the request parameter 'sub' is set, only subordinate charms
236
 
        # are returned.
237
 
        one_id, one = factory.makeCharm(
238
 
            self.db, series='one', promulgated=True)
239
 
        two_id, two = factory.makeCharm(
240
 
            self.db, series='two', promulgated=True)
241
 
        three_is, three = factory.makeCharm(
242
 
            self.db, series='one', subordinate=True, promulgated=True)
243
 
        request = self.getRequest()
244
 
        request.matchdict = {'series': 'one'}
245
 
        request.GET['sub'] = 'true'
246
 
        response = series_collection(request)
247
 
        charms = response['charms']
248
 
        self.assertEqual(1, len(charms))
249
 
        self.assertEqual(three['name'], charms[0].name)
250
 
 
251
 
    def test_series_collection_charms_with_errors(self):
252
 
        # Charms with errors are not returned by series_collection().
253
 
        one_id, one = factory.makeCharm(
254
 
            self.db, series='one', promulgated=True)
255
 
        two_id, two = factory.makeCharm(
256
 
            self.db, series='two', promulgated=True)
257
 
        three_is, three = factory.makeCharm(
258
 
            self.db, series='one', charm_error=True, promulgated=True)
259
 
        request = self.getRequest()
260
 
        request.matchdict = {'series': 'one'}
261
 
        response = series_collection(request)
262
 
        charms = response['charms']
263
 
        self.assertEqual(1, len(charms))
264
 
        self.assertEqual(one['name'], charms[0].name)
 
148
        request = self.getRequest()
 
149
        request.matchdict = {'series': 'one'}
 
150
        with self.assertRaises(HTTPMovedPermanently) as e:
 
151
            series_collection(request)
 
152
        self.assertIn('/q/one', e.exception.location)
265
153
 
266
154
    def test_personal_series_collection(self):
267
 
        # personal_series_collection() returns only charms for a given
268
 
        # series and person.
269
 
        one_id, one = factory.makeCharm(self.db, series='one', owner='foo')
270
 
        two_id, two = factory.makeCharm(self.db, series='two', owner='foo')
271
 
        three_id, three = factory.makeCharm(self.db, series='one', owner='bar')
272
 
        request = self.getRequest()
273
 
        request.matchdict = {
274
 
            'owner': 'foo',
275
 
            'series': 'one',
276
 
        }
277
 
        response = personal_series_collection(request)
278
 
        charms = response['charms']
279
 
        self.assertEqual(1, len(charms))
280
 
        self.assertEqual(one['name'], charms[0].name)
281
 
 
282
 
    def test_personal_series_collection_sub_filter(self):
283
 
        # If the parameter 'sub' is set, personal_series_collection()
284
 
        # returns only subordinate charms for the given person ans series.
285
 
        one_id, one = factory.makeCharm(self.db, series='one', owner='foo')
286
 
        two_id, two = factory.makeCharm(self.db, series='two', owner='foo')
287
 
        three_id, three = factory.makeCharm(self.db, series='one', owner='bar')
288
 
        four_id, four = factory.makeCharm(
289
 
            self.db, series='one', owner='foo', subordinate=True)
290
 
        request = self.getRequest()
291
 
        request.matchdict = {
292
 
            'owner': 'foo',
293
 
            'series': 'one',
294
 
        }
295
 
        request.GET['sub'] = 'true'
296
 
        response = personal_series_collection(request)
297
 
        charms = response['charms']
298
 
        self.assertEqual(1, len(charms))
299
 
        self.assertEqual(four['name'], charms[0].name)
300
 
 
301
 
    def test_personal_series_collection_charms_with_errors(self):
302
 
        # Charms with errors are not returned by series_collection().
303
 
        one_id, one = factory.makeCharm(self.db, series='one', owner='foo')
304
 
        two_id, two = factory.makeCharm(self.db, series='two', owner='foo')
305
 
        three_id, three = factory.makeCharm(self.db, series='one', owner='bar')
306
 
        four_id, four = factory.makeCharm(
307
 
            self.db, series='one', owner='foo', charm_error=True)
308
 
        request = self.getRequest()
309
 
        request.matchdict = {
310
 
            'owner': 'foo',
311
 
            'series': 'one',
312
 
        }
313
 
        response = personal_series_collection(request)
314
 
        charms = response['charms']
315
 
        self.assertEqual(1, len(charms))
316
 
        self.assertEqual(one['name'], charms[0].name)
 
155
        request = self.getRequest()
 
156
        request.matchdict = {'series': 'one', 'owner': 'me'}
 
157
        with self.assertRaises(HTTPMovedPermanently) as e:
 
158
            personal_series_collection(request)
 
159
        self.assertIn('/q/me/one', e.exception.location)
317
160
 
318
161
    def test_interface(self):
319
162
        # interface() returns all charms requiring or providing a given
429
272
            'charm': 'sample_charm',
430
273
            'series': 'precise',
431
274
        }
432
 
        response = distro_charm(request)
433
 
        expected_keys = set((
434
 
            'charm', 'is_featured', 'format_change', 'format_proof', 'name',
435
 
            'others', 'project', 'qadata', 'readme', 'readme_format',
436
 
            'icon_path'))
437
 
        self.assertEqual(expected_keys, set(response))
 
275
        with self.assertRaises(HTTPMovedPermanently):
 
276
            distro_charm(request)
438
277
 
439
278
    def test_distro_charm_json(self):
440
279
        ignore, charm = factory.makeCharm(
542
381
        two_id, two = factory.makeCharm(self.db, promulgated=True)
543
382
        del two['summary']
544
383
        self.db.charms.save(two)
545
 
        # Test that the route works.
546
 
        resp = self.app.get('/charms/precise', status=200)
547
 
        self.assertIn(two['short_url'], resp.body)
 
384
        response = self.app.get('/charms/precise', status=301)
 
385
        self.assertIn('/q/precise', response.body)
548
386
 
549
387
    def test_route_personal_charm_with_revision(self):
550
388
        # The route /~owner/series/charm-1 finds charms using owner,
552
390
        # does not include the revision.
553
391
        ignore, charm = factory.makeCharm(
554
392
            self.db, owner='owner', series='series', name='charm', files={})
555
 
        response = self.app.get('/~owner/series/charm-1', status=200)
556
 
        self.assertIn('juju deploy cs:~owner/series/charm', response.body)
 
393
        response = self.app.get('/~owner/series/charm-1', status=301)
 
394
        self.assertIn('/u/owner/charm/series/1', response.body)
557
395
 
558
396
    def test_route_personal_charm_with_dash_and_revision(self):
559
397
        # /~owner/series/charm-name-1 route find the charm.
562
400
        ignore, charm = factory.makeCharm(
563
401
            self.db, owner='owner', series='series', name='charm-name',
564
402
            files={})
565
 
        response = self.app.get('/~owner/series/charm-name-1', status=200)
566
 
        self.assertIn('juju deploy cs:~owner/series/charm-name', response.body)
 
403
        response = self.app.get('/~owner/series/charm-name-1', status=301)
 
404
        self.assertIn('/u/owner/charm-name/series/1', response.body)
567
405
 
568
406
    def test_route_personal_charm_with_head_revision(self):
569
407
        # the /~owner/series/charm-name-HEAD route finds the charm.
570
408
        # The juju-gui apache rewrite versionless charm urls to help the GUI.
571
409
        ignore, charm = factory.makeCharm(
572
410
            self.db, owner='owner', series='series', name='charm', files={})
573
 
        response = self.app.get('/~owner/series/charm-HEAD', status=200)
574
 
        self.assertIn('juju deploy cs:~owner/series/charm', response.body)
 
411
        response = self.app.get('/~owner/series/charm-HEAD', status=301)
 
412
        self.assertIn('/u/owner/charm/series', response.body)
 
413
        self.assertNotIn('HEAD', response.body)
575
414
 
576
415
    def test_route_personal_charm_without_revision(self):
577
416
        # The route /~owner/series/charm route finds the charm using owner
579
418
        # matches the route.
580
419
        ignore, charm = factory.makeCharm(
581
420
            self.db, owner='owner', series='series', name='charm', files={})
582
 
        response = self.app.get('/~owner/series/charm', status=200)
583
 
        self.assertIn('juju deploy cs:~owner/series/charm', response.body)
 
421
        response = self.app.get('/~owner/series/charm', status=301)
 
422
        self.assertIn('/u/owner/charm/series', response.body)
584
423
 
585
424
    def test_route_personal_charm_json_with_revision(self):
586
425
        # The route /~owner/series/charm-1/json finds charms using owner,
604
443
        # The route /series/charm-1 route finds promulgated charms using
605
444
        # The series, charm name and version. The deploy instruction does not
606
445
        # include the version.
607
 
        ignore, charm = factory.makeCharm(
 
446
        factory.makeCharm(
608
447
            self.db, promulgated=True, series='series', name='charm', files={})
609
 
        response = self.app.get('/series/charm-1', status=200)
610
 
        self.assertIn('juju deploy cs:series/charm', response.body)
 
448
        response = self.app.get('/series/charm-1', status=301)
 
449
        self.assertIn('/charm/series/1', response.body)
611
450
 
612
451
    def test_route_promulgated_charm_with_head_revision(self):
613
452
        # The route /series/charm-HEAD finds promulgated charms using
615
454
        # in the deploy instruction.
616
455
        ignore, charm = factory.makeCharm(
617
456
            self.db, promulgated=True, series='series', name='charm', files={})
618
 
        response = self.app.get('/series/charm-HEAD', status=200)
619
 
        self.assertIn('juju deploy cs:series/charm', response.body)
 
457
        response = self.app.get('/series/charm-HEAD', status=301)
 
458
        self.assertNotIn('HEAD', response.body)
 
459
        self.assertIn('/charm/series', response.body)
620
460
 
621
461
    def test_route_promulgated_charm_without_revision(self):
622
462
        # The route /series/charm finds the promulgated charms using just
623
463
        # the series and charm name. The deploy instruction matches the route.
624
 
        ignore, charm = factory.makeCharm(
 
464
        factory.makeCharm(
625
465
            self.db, promulgated=True, series='series', name='charm', files={})
626
 
        response = self.app.get('/series/charm', status=200)
627
 
        self.assertIn('juju deploy cs:series/charm', response.body)
 
466
        self.app.get('/series/charm', status=301)
628
467
 
629
468
    def test_charm_short_url_route(self):
630
469
        ignore, charm_data = factory.makeCharm(
631
470
            self.db, promulgated=True, series='series', name='charm', files={})
632
471
        charm = Charm(charm_data)
633
 
        self.app.get(charm.short_url, status=200)
 
472
        self.app.get(charm.short_url, status=301)
634
473
 
635
474
 
636
475
class TestQAForm(ViewTestBase):