allow authors to download premium addons, watermarked from the details page (bug 694183)

This commit is contained in:
Andy McKay 2011-10-28 16:20:57 -07:00
Родитель 363b45a952
Коммит 1abccf5399
5 изменённых файлов: 68 добавлений и 29 удалений

Просмотреть файл

@ -45,7 +45,10 @@
{{ sharing_widget(addon) }}
</div>
{% if request.check_ownership(addon, require_owner=False) %}
<a href="{{ url('devhub.addons.edit', addon.slug) }}" class="button developer prominent"><span>{{ _('Manage') }}</span></a>
<p><a href="{{ url('devhub.addons.edit', addon.slug) }}" class="button developer prominent"><span>{{ _('Manage') }}</span></a></p>
{% if waffle.switch('marketplace') and addon.is_premium() and not addon.is_webapp() %}
<p><a href="{{ url('downloads.latest', addon.slug) }}" class="button developer prominent"><span>{{ _('Download') }}</span></a></p>
{% endif %}
{% endif %}
</aside>

Просмотреть файл

@ -427,6 +427,13 @@ class TestPurchaseEmbedded(amo.tests.TestCase):
assert 'chains' in get_paykey.call_args_list[0][0][0].keys()
def setup_premium(addon):
price = Price.objects.create(price='0.99')
AddonPremium.objects.create(addon=addon, price=price)
addon.update(premium_type=amo.ADDON_PREMIUM)
return addon, price
# TODO: remove when the marketplace is live.
@patch.object(waffle, 'switch_is_active', lambda x: True)
# TODO: figure out why this is being set
@ -442,12 +449,8 @@ class TestPaypalStart(amo.tests.TestCase):
self.data = {'username': 'jbalogh@mozilla.com',
'password': 'foo'}
self.addon = Addon.objects.all()[0]
self.url = addon_url('addons.purchase.start', self.addon)
self.price = Price.objects.create(price='0.99')
AddonPremium.objects.create(addon=self.addon, price=self.price)
self.addon.update(premium_type=amo.ADDON_PREMIUM)
self.addon, self.price = setup_premium(self.addon)
def test_loggedout_purchased(self):
# "Buy" the add-on
@ -1160,6 +1163,26 @@ class TestImpalaDetailPage(amo.tests.TestCase):
def test_other_addons_none(self):
eq_(self.get_more_pq()('#author-addons').length, 0)
# TODO: remove when the marketplace is live.
@patch.object(waffle, 'switch_is_active', lambda x: True)
def test_author_watermarked(self):
# Test that an author can get a watermarked addon.
self.addon, self.price = setup_premium(self.addon)
assert self.client.login(username=self.addon.authors.all()[0].email,
password='password')
res = self.client.get(self.url)
eq_(pq(res.content)('.prominent').eq(1).attr('href'),
reverse('downloads.latest', args=[self.addon.slug]))
@patch.object(waffle, 'switch_is_active', lambda x: True)
def test_not_author(self):
# A non-author should not see the download link.
self.addon, self.price = setup_premium(self.addon)
assert self.client.login(username='regular@mozilla.com',
password='password')
res = self.client.get(self.url)
eq_(len(pq(res.content)('.prominent')), 1)
class TestStatus(amo.tests.TestCase):
fixtures = ['base/apps', 'base/addon_3615']

Просмотреть файл

@ -489,6 +489,7 @@ class TestWatermarkedFile(amo.tests.TestCase, amo.tests.AMOPaths):
self.xpi_copy_over(self.file, 'firefm')
self.url = reverse('downloads.watermarked', args=[self.file.pk])
self.user = UserProfile.objects.get(pk=999)
self.author = self.addon.authors.all()[0]
self.purchase = AddonPurchase.objects.create(addon=self.addon,
user=self.user)
self.client.login(username='regular@mozilla.com', password='password')
@ -550,3 +551,14 @@ class TestWatermarkedFile(amo.tests.TestCase, amo.tests.AMOPaths):
self.purchase.delete()
res = self.client.get(self.url)
eq_(res.status_code, 403)
def test_watermark_latest_redirects(self):
url = reverse('downloads.latest', args=[self.addon.slug])
res = self.client.get(url, follow=False)
self.assertRedirects(res, '%s/%s' % (self.url, self.file.filename))
def test_author_can_get(self):
self.client.logout()
self.client.login(username=self.author.email, password='password')
res = self.client.get(self.url)
assert os.path.exists(res['X-SENDFILE'])

Просмотреть файл

@ -24,6 +24,6 @@ download_patterns = patterns('',
'(?:platform:(?P<platform>\d+)/)?.*' % ADDON_ID,
views.download_latest, name='downloads.latest'),
url('^watermarked/(?P<file_id>\d+)',
url('^watermarked/(?P<file_id>\d+)?(?:/.*)?',
views.download_watermarked, name='downloads.watermarked'),
)

Просмотреть файл

@ -80,28 +80,30 @@ def download_watermarked(request, file_id):
file = get_object_or_404(File.objects, pk=file_id)
addon = get_object_or_404(Addon.objects, pk=file.version.addon_id)
if (not addon.is_premium() or addon.is_disabled
or file.status == amo.STATUS_DISABLED):
raise http.Http404()
author = request.check_ownership(addon, require_owner=False)
user = request.amo_user
if request.user.is_anonymous():
log.debug('Anonymous user, checking hash: %s' % file_id)
email = request.GET.get(amo.WATERMARK_KEY, None)
hsh = request.GET.get(amo.WATERMARK_KEY_HASH, None)
user = addon.get_user_from_hash(email, hsh)
if not user:
log.debug('Watermarking denied, no user: %s, %s, %s'
% (file_id, email, hsh))
if not author:
if (not addon.is_premium() or addon.is_disabled
or file.status == amo.STATUS_DISABLED):
raise http.Http404()
if request.user.is_anonymous():
log.debug('Anonymous user, checking hash: %s' % file_id)
email = request.GET.get(amo.WATERMARK_KEY, None)
hsh = request.GET.get(amo.WATERMARK_KEY_HASH, None)
user = addon.get_user_from_hash(email, hsh)
if not user:
log.debug('Watermarking denied, no user: %s, %s, %s'
% (file_id, email, hsh))
return http.HttpResponseForbidden()
if not addon.has_purchased(user):
log.debug('Watermarking denied, not purchased: %s, %s'
% (file_id, user.id))
return http.HttpResponseForbidden()
if not addon.has_purchased(user):
log.debug('Watermarking denied, not purchased: %s, %s'
% (file_id, user.id))
return http.HttpResponseForbidden()
dest = file.watermark(user)
if not dest:
# TODO(andym): the watermarking is already in progress and we've
@ -144,9 +146,6 @@ guard = lambda: Addon.objects.filter(_current_version__isnull=False)
@addon_view_factory(guard)
def download_latest(request, addon, type='xpi', platform=None):
if addon.is_premium():
return http.HttpResponseForbidden()
platforms = [amo.PLATFORM_ALL.id]
if platform is not None and int(platform) in amo.PLATFORMS:
platforms.append(int(platform))
@ -158,7 +157,9 @@ def download_latest(request, addon, type='xpi', platform=None):
except IndexError:
raise http.Http404()
args = [file.id, type] if type else [file.id]
url = posixpath.join(reverse('downloads.file', args=args), file.filename)
pattern = ('downloads.watermarked' if addon.is_premium()
else 'downloads.file')
url = posixpath.join(reverse(pattern, args=args), file.filename)
if request.GET:
url += '?' + request.GET.urlencode()
return redirect(url)