Skip to content

Commit

Permalink
billing: Include link to /plans and /billing in gear menu.
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerkid authored and rishig committed Sep 8, 2018
1 parent 06225d1 commit 169de2f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
14 changes: 14 additions & 0 deletions templates/zerver/app/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@
<span>{{ _('Statistics') }}</span>
</a>
</li>
{% if show_plans %}
<li role="presentation">
<a href="/plans" role="menuitem">
<i class="fa fa-rocket" aria-hidden="true"></i> {{ _('Plans and pricing') }}
</a>
</li>
{% endif %}
{% if show_billing %}
<li role="presentation">
<a href="/billing" role="menuitem">
<i class="fa fa-credit-card" aria-hidden="true"></i> {{ _('Billing') }}
</a>
</li>
{% endif %}
<li class="divider" role="presentation"></li>
{% if enable_feedback %}
<li role="presentation">
Expand Down
57 changes: 56 additions & 1 deletion zerver/tests/test_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
flush_per_request_caches, DefaultStream, Realm,
)
from zerver.views.home import home, sent_time_in_epoch_seconds
from zilencer.models import Customer

class HomeTest(ZulipTestCase):
def test_home(self) -> None:
Expand Down Expand Up @@ -276,7 +277,7 @@ def test_num_queries_for_realm_admin(self) -> None:
result = self._get_home_page()
self.assertEqual(result.status_code, 200)
self.assert_length(cache_mock.call_args_list, 6)
self.assert_length(queries, 38)
self.assert_length(queries, 39)

@slow("Creates and subscribes 10 users in a loop. Should use bulk queries.")
def test_num_queries_with_streams(self) -> None:
Expand Down Expand Up @@ -628,6 +629,60 @@ def test_show_invites_for_guest_users(self) -> None:
html = result.content.decode('utf-8')
self.assertNotIn('Invite more users', html)

def test_show_billing(self) -> None:
customer = Customer.objects.create(realm=get_realm("zulip"), stripe_customer_id="cus_id")

# realm admin, but no billing relationship -> no billing link
user = self.example_user('iago')
self.login(user.email)
result_html = self._get_home_page().content.decode('utf-8')
self.assertNotIn('Billing', result_html)

# realm admin, with billing relationship -> show billing link
customer.has_billing_relationship = True
customer.save()
result_html = self._get_home_page().content.decode('utf-8')
self.assertIn('Billing', result_html)

# billing admin, with billing relationship -> show billing link
user.is_realm_admin = False
user.is_billing_admin = True
user.save(update_fields=['is_realm_admin', 'is_billing_admin'])
result_html = self._get_home_page().content.decode('utf-8')
self.assertIn('Billing', result_html)

# billing admin, but no billing relationship -> no billing link
customer.has_billing_relationship = False
customer.save()
result_html = self._get_home_page().content.decode('utf-8')
self.assertNotIn('Billing', result_html)

# billing admin, no customer object -> make sure it doesn't crash
customer.delete()
result = self._get_home_page()
self.assertEqual(result.status_code, 200)

def test_show_plans(self) -> None:
realm = get_realm("zulip")
self.login(self.example_email('hamlet'))

# Show plans link to all users if plan_type is LIMITED
realm.plan_type = Realm.LIMITED
realm.save(update_fields=["plan_type"])
result_html = self._get_home_page().content.decode('utf-8')
self.assertIn('Plans', result_html)

# Show plans link to no one, including admins, if SELF_HOSTED or PREMIUM
realm.plan_type = Realm.SELF_HOSTED
realm.save(update_fields=["plan_type"])
result_html = self._get_home_page().content.decode('utf-8')
self.assertNotIn('Plans', result_html)

realm.plan_type = Realm.PREMIUM
realm.save(update_fields=["plan_type"])
result_html = self._get_home_page().content.decode('utf-8')
self.assertNotIn('Plans', result_html)

def test_desktop_home(self) -> None:
email = self.example_email("hamlet")
self.login(email)
Expand Down
13 changes: 13 additions & 0 deletions zerver/views/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,17 @@ def home_real(request: HttpRequest) -> HttpResponse:
if user_profile.is_guest:
show_invites = False

show_billing = False
show_plans = False
if settings.ZILENCER_ENABLED:
from zilencer.models import Customer
if user_profile.is_billing_admin or user_profile.is_realm_admin:
customer = Customer.objects.filter(realm=user_profile.realm).first()
if customer is not None and customer.has_billing_relationship:
show_billing = True
if user_profile.realm.plan_type == Realm.LIMITED:
show_plans = True

request._log_data['extra'] = "[%s]" % (register_ret["queue_id"],)

page_params['translation_data'] = {}
Expand All @@ -273,6 +284,8 @@ def home_real(request: HttpRequest) -> HttpResponse:
'pipeline': settings.PIPELINE_ENABLED,
'search_pills_enabled': settings.SEARCH_PILLS_ENABLED,
'show_invites': show_invites,
'show_billing': show_billing,
'show_plans': show_plans,
'is_admin': user_profile.is_realm_admin,
'is_guest': user_profile.is_guest,
'show_webathena': user_profile.realm.webathena_enabled,
Expand Down

0 comments on commit 169de2f

Please sign in to comment.