def generate_multiuse_invite_backend( request: HttpRequest, user_profile: UserProfile, invite_expires_in_days: Optional[int] = REQ( json_validator=check_none_or(check_int), default=settings.INVITATION_LINK_VALIDITY_DAYS), invite_as: int = REQ(json_validator=check_int, default=PreregistrationUser.INVITE_AS["MEMBER"]), stream_ids: Sequence[int] = REQ(json_validator=check_list(check_int), default=[]), ) -> HttpResponse: check_if_owner_required(invite_as, user_profile) streams = [] for stream_id in stream_ids: try: (stream, sub) = access_stream_by_id(user_profile, stream_id) except JsonableError: raise JsonableError( _("Invalid stream id {}. No invites were sent.").format( stream_id)) streams.append(stream) invite_link = do_create_multiuse_invite_link(user_profile, invite_as, invite_expires_in_days, streams) return json_success(request, data={"invite_link": invite_link})
def generate_multiuse_invite_backend(request: HttpRequest, user_profile: UserProfile, stream_ids: List[int]=REQ(validator=check_list(check_int), default=[])) -> HttpResponse: streams = [] for stream_id in stream_ids: try: (stream, recipient, sub) = access_stream_by_id(user_profile, stream_id) except JsonableError: return json_error(_("Invalid stream id {}. No invites were sent.".format(stream_id))) streams.append(stream) invite_link = do_create_multiuse_invite_link(user_profile, streams) return json_success({'invite_link': invite_link})
def handle(self, *args: Any, **options: Any) -> None: realm = self.get_realm(options) assert realm is not None # Should be ensured by parser streams = [] # type: List[Stream] if options["streams"]: stream_names = set([stream.strip() for stream in options["streams"].split(",")]) for stream_name in set(stream_names): stream = ensure_stream(realm, stream_name) streams.append(stream) referred_by = self.get_user(options['referred_by'], realm) invite_link = do_create_multiuse_invite_link(referred_by, streams) print("You can use %s to invite as many number of people to the organization." % (invite_link,))
def handle(self, *args: Any, **options: Any) -> None: realm = self.get_realm(options) assert realm is not None # Should be ensured by parser streams: List[Stream] = [] if options["streams"]: stream_names = {stream.strip() for stream in options["streams"].split(",")} for stream_name in set(stream_names): stream = ensure_stream(realm, stream_name, acting_user=None) streams.append(stream) referred_by = self.get_user(options["referred_by"], realm) invite_as = PreregistrationUser.INVITE_AS["MEMBER"] invite_link = do_create_multiuse_invite_link(referred_by, invite_as, streams) print(f"You can use {invite_link} to invite as many number of people to the organization.")
def handle(self, *args: Any, **options: Any) -> None: realm = self.get_realm(options) assert realm is not None # Should be ensured by parser streams = [] # type: List[Stream] if options["streams"]: stream_names = set([stream.strip() for stream in options["streams"].split(",")]) for stream_name in set(stream_names): stream = ensure_stream(realm, stream_name) streams.append(stream) referred_by = self.get_user(options['referred_by'], realm) invite_as = PreregistrationUser.INVITE_AS['MEMBER'] invite_link = do_create_multiuse_invite_link(referred_by, invite_as, streams) print("You can use %s to invite as many number of people to the organization." % (invite_link,))
def generate_multiuse_invite_backend( request: HttpRequest, user_profile: UserProfile, invite_as: int=REQ(validator=check_int, default=PreregistrationUser.INVITE_AS['MEMBER']), stream_ids: Sequence[int]=REQ(validator=check_list(check_int), default=[])) -> HttpResponse: check_if_owner_required(invite_as, user_profile) streams = [] for stream_id in stream_ids: try: (stream, recipient, sub) = access_stream_by_id(user_profile, stream_id) except JsonableError: return json_error(_("Invalid stream id {}. No invites were sent.").format(stream_id)) streams.append(stream) invite_link = do_create_multiuse_invite_link(user_profile, invite_as, streams) return json_success({'invite_link': invite_link})
def test_search(self) -> None: reset_emails_in_zulip_realm() def check_hamlet_user_query_result(result: HttpResponse) -> None: self.assert_in_success_response(['<span class="label">user</span>\n', '<h3>King Hamlet</h3>', '<b>Email</b>: [email protected]', '<b>Is active</b>: True<br>', '<b>Admins</b>: [email protected], [email protected]\n', 'class="copy-button" data-copytext="[email protected], [email protected]"', ], result) def check_zulip_realm_query_result(result: HttpResponse) -> None: zulip_realm = get_realm("zulip") self.assert_in_success_response([f'<input type="hidden" name="realm_id" value="{zulip_realm.id}"', 'Zulip Dev</h3>', '<option value="1" selected>Self Hosted</option>', '<option value="2" >Limited</option>', 'input type="number" name="discount" value="None"', '<option value="active" selected>Active</option>', '<option value="deactivated" >Deactivated</option>', 'scrub-realm-button">', 'data-string-id="zulip"'], result) def check_lear_realm_query_result(result: HttpResponse) -> None: lear_realm = get_realm("lear") self.assert_in_success_response([f'<input type="hidden" name="realm_id" value="{lear_realm.id}"', 'Lear & Co.</h3>', '<option value="1" selected>Self Hosted</option>', '<option value="2" >Limited</option>', 'input type="number" name="discount" value="None"', '<option value="active" selected>Active</option>', '<option value="deactivated" >Deactivated</option>', 'scrub-realm-button">', 'data-string-id="lear"', '<b>Name</b>: Zulip Standard', '<b>Status</b>: Active', '<b>Billing schedule</b>: Annual', '<b>Licenses</b>: 2/10 (Manual)', '<b>Price per license</b>: $80.0', '<b>Payment method</b>: Send invoice', '<b>Next invoice date</b>: 02 January 2017', ], result) def check_preregistration_user_query_result(result: HttpResponse, email: str, invite: bool=False) -> None: self.assert_in_success_response(['<span class="label">preregistration user</span>\n', f'<b>Email</b>: {email}', ], result) if invite: self.assert_in_success_response(['<span class="label">invite</span>'], result) self.assert_in_success_response(['<b>Expires in</b>: 1\xa0week, 3', '<b>Status</b>: Link has never been clicked'], result) self.assert_in_success_response([], result) else: self.assert_not_in_success_response(['<span class="label">invite</span>'], result) self.assert_in_success_response(['<b>Expires in</b>: 1\xa0day', '<b>Status</b>: Link has never been clicked'], result) def check_realm_creation_query_result(result: HttpResponse, email: str) -> None: self.assert_in_success_response(['<span class="label">preregistration user</span>\n', '<span class="label">realm creation</span>\n', '<b>Link</b>: http://testserver/accounts/do_confirm/', '<b>Expires in</b>: 1\xa0day<br>\n', ], result) def check_multiuse_invite_link_query_result(result: HttpResponse) -> None: self.assert_in_success_response(['<span class="label">multiuse invite</span>\n', '<b>Link</b>: http://zulip.testserver/join/', '<b>Expires in</b>: 1\xa0week, 3', ], result) def check_realm_reactivation_link_query_result(result: HttpResponse) -> None: self.assert_in_success_response(['<span class="label">realm reactivation</span>\n', '<b>Link</b>: http://zulip.testserver/reactivate/', '<b>Expires in</b>: 1\xa0day', ], result) self.login('cordelia') result = self.client_get("/activity/support") self.assertEqual(result.status_code, 302) self.assertEqual(result["Location"], "/login/") self.login('iago') customer = Customer.objects.create(realm=get_realm("lear"), stripe_customer_id='cus_123') now = datetime(2016, 1, 2, tzinfo=timezone.utc) plan = CustomerPlan.objects.create(customer=customer, billing_cycle_anchor=now, billing_schedule=CustomerPlan.ANNUAL, tier=CustomerPlan.STANDARD, price_per_license=8000, next_invoice_date=add_months(now, 12)) LicenseLedger.objects.create(licenses=10, licenses_at_next_renewal=10, event_time=timezone_now(), is_renewal=True, plan=plan) result = self.client_get("/activity/support") self.assert_in_success_response(['<input type="text" name="q" class="input-xxlarge search-query"'], result) result = self.client_get("/activity/support", {"q": "*****@*****.**"}) check_hamlet_user_query_result(result) check_zulip_realm_query_result(result) result = self.client_get("/activity/support", {"q": "lear"}) check_lear_realm_query_result(result) result = self.client_get("/activity/support", {"q": "http://lear.testserver"}) check_lear_realm_query_result(result) with self.settings(REALM_HOSTS={'zulip': 'localhost'}): result = self.client_get("/activity/support", {"q": "http://localhost"}) check_zulip_realm_query_result(result) result = self.client_get("/activity/support", {"q": "[email protected], lear"}) check_hamlet_user_query_result(result) check_zulip_realm_query_result(result) check_lear_realm_query_result(result) result = self.client_get("/activity/support", {"q": "lear, Hamlet <*****@*****.**>"}) check_hamlet_user_query_result(result) check_zulip_realm_query_result(result) check_lear_realm_query_result(result) self.client_post('/accounts/home/', {'email': self.nonreg_email("test")}) self.login('iago') result = self.client_get("/activity/support", {"q": self.nonreg_email("test")}) check_preregistration_user_query_result(result, self.nonreg_email("test")) check_zulip_realm_query_result(result) stream_ids = [self.get_stream_id("Denmark")] invitee_emails = [self.nonreg_email("test1")] self.client_post("/json/invites", {"invitee_emails": invitee_emails, "stream_ids": ujson.dumps(stream_ids), "invite_as": PreregistrationUser.INVITE_AS['MEMBER']}) result = self.client_get("/activity/support", {"q": self.nonreg_email("test1")}) check_preregistration_user_query_result(result, self.nonreg_email("test1"), invite=True) check_zulip_realm_query_result(result) email = self.nonreg_email('alice') self.client_post('/new/', {'email': email}) result = self.client_get("/activity/support", {"q": email}) check_realm_creation_query_result(result, email) do_create_multiuse_invite_link(self.example_user("hamlet"), invited_as=1) result = self.client_get("/activity/support", {"q": "zulip"}) check_multiuse_invite_link_query_result(result) check_zulip_realm_query_result(result) MultiuseInvite.objects.all().delete() do_send_realm_reactivation_email(get_realm("zulip")) result = self.client_get("/activity/support", {"q": "zulip"}) check_realm_reactivation_link_query_result(result) check_zulip_realm_query_result(result)
def test_search(self) -> None: reset_emails_in_zulip_realm() def assert_user_details_in_html_response(html_response: HttpResponse, full_name: str, email: str, role: str) -> None: self.assert_in_success_response( [ '<span class="label">user</span>\n', f"<h3>{full_name}</h3>", f"<b>Email</b>: {email}", "<b>Is active</b>: True<br />", f"<b>Role</b>: {role}<br />", ], html_response, ) def check_hamlet_user_query_result(result: HttpResponse) -> None: assert_user_details_in_html_response(result, "King Hamlet", self.example_email("hamlet"), "Member") self.assert_in_success_response( [ f"<b>Admins</b>: {self.example_email('iago')}\n", f"<b>Owners</b>: {self.example_email('desdemona')}\n", 'class="copy-button" data-copytext="{}">'.format( self.example_email("iago")), 'class="copy-button" data-copytext="{}">'.format( self.example_email("desdemona")), ], result, ) def check_othello_user_query_result(result: HttpResponse) -> None: assert_user_details_in_html_response( result, "Othello, the Moor of Venice", self.example_email("othello"), "Member") def check_polonius_user_query_result(result: HttpResponse) -> None: assert_user_details_in_html_response( result, "Polonius", self.example_email("polonius"), "Guest") def check_zulip_realm_query_result(result: HttpResponse) -> None: zulip_realm = get_realm("zulip") first_human_user = zulip_realm.get_first_human_user() assert first_human_user is not None self.assert_in_success_response( [ f"<b>First human user</b>: {first_human_user.delivery_email}\n", f'<input type="hidden" name="realm_id" value="{zulip_realm.id}"', "Zulip Dev</h3>", '<option value="1" selected>Self hosted</option>', '<option value="2" >Limited</option>', 'input type="number" name="discount" value="None"', '<option value="active" selected>Active</option>', '<option value="deactivated" >Deactivated</option>', f'<option value="{zulip_realm.org_type}" selected>', 'scrub-realm-button">', 'data-string-id="zulip"', ], result, ) def check_lear_realm_query_result(result: HttpResponse) -> None: lear_realm = get_realm("lear") self.assert_in_success_response( [ f'<input type="hidden" name="realm_id" value="{lear_realm.id}"', "Lear & Co.</h3>", '<option value="1" selected>Self hosted</option>', '<option value="2" >Limited</option>', 'input type="number" name="discount" value="None"', '<option value="active" selected>Active</option>', '<option value="deactivated" >Deactivated</option>', 'scrub-realm-button">', 'data-string-id="lear"', "<b>Name</b>: Zulip Standard", "<b>Status</b>: Active", "<b>Billing schedule</b>: Annual", "<b>Licenses</b>: 2/10 (Manual)", "<b>Price per license</b>: $80.0", "<b>Next invoice date</b>: 02 January 2017", '<option value="send_invoice" selected>', '<option value="charge_automatically" >', ], result, ) def check_preregistration_user_query_result( result: HttpResponse, email: str, invite: bool = False) -> None: self.assert_in_success_response( [ '<span class="label">preregistration user</span>\n', f"<b>Email</b>: {email}", ], result, ) if invite: self.assert_in_success_response( ['<span class="label">invite</span>'], result) self.assert_in_success_response( [ "<b>Expires in</b>: 1\xa0week, 3\xa0days", "<b>Status</b>: Link has never been clicked", ], result, ) self.assert_in_success_response([], result) else: self.assert_not_in_success_response( ['<span class="label">invite</span>'], result) self.assert_in_success_response( [ "<b>Expires in</b>: 1\xa0day", "<b>Status</b>: Link has never been clicked", ], result, ) def check_realm_creation_query_result(result: HttpResponse, email: str) -> None: self.assert_in_success_response( [ '<span class="label">preregistration user</span>\n', '<span class="label">realm creation</span>\n', "<b>Link</b>: http://testserver/accounts/do_confirm/", "<b>Expires in</b>: 1\xa0day", ], result, ) def check_multiuse_invite_link_query_result( result: HttpResponse) -> None: self.assert_in_success_response( [ '<span class="label">multiuse invite</span>\n', "<b>Link</b>: http://zulip.testserver/join/", "<b>Expires in</b>: 1\xa0week, 3\xa0days", ], result, ) def check_realm_reactivation_link_query_result( result: HttpResponse) -> None: self.assert_in_success_response( [ '<span class="label">realm reactivation</span>\n', "<b>Link</b>: http://zulip.testserver/reactivate/", "<b>Expires in</b>: 1\xa0day", ], result, ) self.login("cordelia") result = self.client_get("/activity/support") self.assertEqual(result.status_code, 302) self.assertEqual(result["Location"], "/login/") self.login("iago") do_set_realm_property( get_realm("zulip"), "email_address_visibility", Realm.EMAIL_ADDRESS_VISIBILITY_NOBODY, acting_user=None, ) customer = Customer.objects.create(realm=get_realm("lear"), stripe_customer_id="cus_123") now = datetime(2016, 1, 2, tzinfo=timezone.utc) plan = CustomerPlan.objects.create( customer=customer, billing_cycle_anchor=now, billing_schedule=CustomerPlan.ANNUAL, tier=CustomerPlan.STANDARD, price_per_license=8000, next_invoice_date=add_months(now, 12), ) LicenseLedger.objects.create( licenses=10, licenses_at_next_renewal=10, event_time=timezone_now(), is_renewal=True, plan=plan, ) result = self.client_get("/activity/support") self.assert_in_success_response( ['<input type="text" name="q" class="input-xxlarge search-query"'], result) result = self.client_get("/activity/support", {"q": self.example_email("hamlet")}) check_hamlet_user_query_result(result) check_zulip_realm_query_result(result) result = self.client_get("/activity/support", {"q": self.example_email("polonius")}) check_polonius_user_query_result(result) check_zulip_realm_query_result(result) result = self.client_get("/activity/support", {"q": "lear"}) check_lear_realm_query_result(result) result = self.client_get("/activity/support", {"q": "http://lear.testserver"}) check_lear_realm_query_result(result) with self.settings(REALM_HOSTS={"zulip": "localhost"}): result = self.client_get("/activity/support", {"q": "http://localhost"}) check_zulip_realm_query_result(result) result = self.client_get("/activity/support", {"q": "[email protected], lear"}) check_hamlet_user_query_result(result) check_zulip_realm_query_result(result) check_lear_realm_query_result(result) result = self.client_get("/activity/support", {"q": "King hamlet,lear"}) check_hamlet_user_query_result(result) check_zulip_realm_query_result(result) check_lear_realm_query_result(result) result = self.client_get("/activity/support", {"q": "Othello, the Moor of Venice"}) check_othello_user_query_result(result) check_zulip_realm_query_result(result) result = self.client_get("/activity/support", {"q": "lear, Hamlet <*****@*****.**>"}) check_hamlet_user_query_result(result) check_zulip_realm_query_result(result) check_lear_realm_query_result(result) with mock.patch( "analytics.views.support.timezone_now", return_value=timezone_now() - timedelta(minutes=50), ): self.client_post("/accounts/home/", {"email": self.nonreg_email("test")}) self.login("iago") result = self.client_get("/activity/support", {"q": self.nonreg_email("test")}) check_preregistration_user_query_result(result, self.nonreg_email("test")) check_zulip_realm_query_result(result) invite_expires_in_days = 10 stream_ids = [self.get_stream_id("Denmark")] invitee_emails = [self.nonreg_email("test1")] self.client_post( "/json/invites", { "invitee_emails": invitee_emails, "stream_ids": orjson.dumps(stream_ids).decode(), "invite_expires_in_days": invite_expires_in_days, "invite_as": PreregistrationUser.INVITE_AS["MEMBER"], }, ) result = self.client_get("/activity/support", {"q": self.nonreg_email("test1")}) check_preregistration_user_query_result(result, self.nonreg_email("test1"), invite=True) check_zulip_realm_query_result(result) email = self.nonreg_email("alice") self.client_post("/new/", {"email": email}) result = self.client_get("/activity/support", {"q": email}) check_realm_creation_query_result(result, email) do_create_multiuse_invite_link( self.example_user("hamlet"), invited_as=1, invite_expires_in_days=invite_expires_in_days, ) result = self.client_get("/activity/support", {"q": "zulip"}) check_multiuse_invite_link_query_result(result) check_zulip_realm_query_result(result) MultiuseInvite.objects.all().delete() do_send_realm_reactivation_email(get_realm("zulip"), acting_user=None) result = self.client_get("/activity/support", {"q": "zulip"}) check_realm_reactivation_link_query_result(result) check_zulip_realm_query_result(result)
def test_search(self) -> None: reset_emails_in_zulip_realm() def check_hamlet_user_query_result(result: HttpResponse) -> None: self.assert_in_success_response([ '<span class="label">user</span>\n', '<h3>King Hamlet</h3>', '<b>Email</b>: [email protected]', '<b>Is active</b>: True<br>', '<b>Admins</b>: [email protected]\n', 'class="copy-button" data-copytext="*****@*****.**"' ], result) def check_zulip_realm_query_result(result: HttpResponse) -> None: zulip_realm = get_realm("zulip") self.assert_in_success_response([ '<input type="hidden" name="realm_id" value="%s"' % (zulip_realm.id, ), 'Zulip Dev</h3>', '<option value="1" selected>Self Hosted</option>', '<option value="2" >Limited</option>', 'input type="number" name="discount" value="None"', '<option value="active" selected>Active</option>', '<option value="deactivated" >Deactivated</option>', 'scrub-realm-button">', 'data-string-id="zulip"' ], result) def check_lear_realm_query_result(result: HttpResponse) -> None: lear_realm = get_realm("lear") self.assert_in_success_response([ '<input type="hidden" name="realm_id" value="%s"' % (lear_realm.id, ), 'Lear & Co.</h3>', '<option value="1" selected>Self Hosted</option>', '<option value="2" >Limited</option>', 'input type="number" name="discount" value="None"', '<option value="active" selected>Active</option>', '<option value="deactivated" >Deactivated</option>', 'scrub-realm-button">', 'data-string-id="lear"' ], result) def check_preregistration_user_query_result( result: HttpResponse, email: str, invite: Optional[bool] = False) -> None: self.assert_in_success_response([ '<span class="label">preregistration user</span>\n', '<b>Email</b>: {}'.format(email), ], result) if invite: self.assert_in_success_response( ['<span class="label">invite</span>'], result) self.assert_in_success_response([ '<b>Expires in</b>: 1\xa0week, 3', '<b>Status</b>: Link has never been clicked' ], result) self.assert_in_success_response([], result) else: self.assert_not_in_success_response( ['<span class="label">invite</span>'], result) self.assert_in_success_response([ '<b>Expires in</b>: 1\xa0day', '<b>Status</b>: Link has never been clicked' ], result) def check_realm_creation_query_result(result: HttpResponse, email: str) -> None: self.assert_in_success_response([ '<span class="label">preregistration user</span>\n', '<span class="label">realm creation</span>\n', '<b>Link</b>: http://zulip.testserver/accounts/do_confirm/', '<b>Expires in</b>: 1\xa0day<br>\n' ], result) def check_multiuse_invite_link_query_result( result: HttpResponse) -> None: self.assert_in_success_response([ '<span class="label">multiuse invite</span>\n', '<b>Link</b>: http://zulip.testserver/join/', '<b>Expires in</b>: 1\xa0week, 3' ], result) def check_realm_reactivation_link_query_result( result: HttpResponse) -> None: self.assert_in_success_response([ '<span class="label">realm reactivation</span>\n', '<b>Link</b>: http://zulip.testserver/reactivate/', '<b>Expires in</b>: 1\xa0day' ], result) self.login('cordelia') result = self.client_get("/activity/support") self.assertEqual(result.status_code, 302) self.assertEqual(result["Location"], "/login/") self.login('iago') result = self.client_get("/activity/support") self.assert_in_success_response( ['<input type="text" name="q" class="input-xxlarge search-query"'], result) result = self.client_get("/activity/support", {"q": "*****@*****.**"}) check_hamlet_user_query_result(result) check_zulip_realm_query_result(result) result = self.client_get("/activity/support", {"q": "lear"}) check_lear_realm_query_result(result) result = self.client_get("/activity/support", {"q": "http://lear.testserver"}) check_lear_realm_query_result(result) with self.settings(REALM_HOSTS={'zulip': 'localhost'}): result = self.client_get("/activity/support", {"q": "http://localhost"}) check_zulip_realm_query_result(result) result = self.client_get("/activity/support", {"q": "[email protected], lear"}) check_hamlet_user_query_result(result) check_zulip_realm_query_result(result) check_lear_realm_query_result(result) result = self.client_get("/activity/support", {"q": "lear, Hamlet <*****@*****.**>"}) check_hamlet_user_query_result(result) check_zulip_realm_query_result(result) check_lear_realm_query_result(result) self.client_post('/accounts/home/', {'email': self.nonreg_email("test")}) self.login('iago') result = self.client_get("/activity/support", {"q": self.nonreg_email("test")}) check_preregistration_user_query_result(result, self.nonreg_email("test")) check_zulip_realm_query_result(result) stream_ids = [self.get_stream_id("Denmark")] invitee_emails = [self.nonreg_email("test1")] self.client_post( "/json/invites", { "invitee_emails": invitee_emails, "stream_ids": ujson.dumps(stream_ids), "invite_as": 1 }) result = self.client_get("/activity/support", {"q": self.nonreg_email("test1")}) check_preregistration_user_query_result(result, self.nonreg_email("test1"), invite=True) check_zulip_realm_query_result(result) email = self.nonreg_email('alice') self.client_post('/new/', {'email': email}) result = self.client_get("/activity/support", {"q": email}) check_realm_creation_query_result(result, email) do_create_multiuse_invite_link(self.example_user("hamlet"), invited_as=1) result = self.client_get("/activity/support", {"q": "zulip"}) check_multiuse_invite_link_query_result(result) check_zulip_realm_query_result(result) MultiuseInvite.objects.all().delete() do_send_realm_reactivation_email(get_realm("zulip")) result = self.client_get("/activity/support", {"q": "zulip"}) check_realm_reactivation_link_query_result(result) check_zulip_realm_query_result(result)