diff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.py index 7b46340cee..e043c8e69a 100644 --- a/zerver/tests/test_auth_backends.py +++ b/zerver/tests/test_auth_backends.py @@ -231,3 +231,17 @@ class DevFetchAPIKeyTest(AuthedTestCase): result = self.client.post("/api/v1/dev_fetch_api_key", dict(username=self.email)) self.assert_json_error_contains(result, "Dev environment not enabled.", 400) + +class DevGetEmailsTest(AuthedTestCase): + def test_success(self): + # type: () -> None + result = self.client.get("/api/v1/dev_get_emails") + self.assert_json_success(result) + self.assertIn("direct_admins", result.content) + self.assertIn("direct_users", result.content) + + def test_dev_auth_disabled(self): + # type: () -> None + with mock.patch('zerver.views.dev_auth_enabled', return_value=False): + result = self.client.get("/api/v1/dev_get_emails") + self.assert_json_error_contains(result, "Dev environment not enabled.", 400) diff --git a/zerver/views/__init__.py b/zerver/views/__init__.py index 67c0f7fc00..f01f4c065e 100644 --- a/zerver/views/__init__.py +++ b/zerver/views/__init__.py @@ -612,6 +612,17 @@ def api_dev_fetch_api_key(request, username=REQ()): login(request, user_profile) return json_success({"api_key": user_profile.api_key, "email": user_profile.email}) +@csrf_exempt +def api_dev_get_emails(request): + # type: (HttpRequest) -> HttpResponse + if not dev_auth_enabled() or settings.PRODUCTION: + return json_error(_("Dev environment not enabled.")) + MAX_DEV_BACKEND_USERS = 100 # type: int + users_query = UserProfile.objects.select_related().filter(is_bot=False, is_active=True) + users = users_query.order_by('email')[0:MAX_DEV_BACKEND_USERS] + return json_success(dict(direct_admins=[u.email for u in users if u.is_realm_admin], + direct_users=[u.email for u in users if not u.is_realm_admin])) + @authenticated_json_post_view @has_request_variables def json_bulk_invite_users(request, user_profile, diff --git a/zproject/urls.py b/zproject/urls.py index 2523eb9a07..901904b272 100644 --- a/zproject/urls.py +++ b/zproject/urls.py @@ -142,6 +142,9 @@ urlpatterns += patterns('zerver.views', # This is for the signing in through the devAuthBackEnd on mobile apps. url(r'^api/v1/dev_fetch_api_key$', 'api_dev_fetch_api_key'), + # This is for fetching the emails of the admins and the users. + url(r'^api/v1/dev_get_emails$', 'api_dev_get_emails'), + # Used to present the GOOGLE_CLIENT_ID to mobile apps url(r'^api/v1/fetch_google_client_id$', 'api_fetch_google_client_id'),